diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2013-05-26 14:29:42 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2013-05-26 14:29:42 (GMT) |
commit | 3d4c5f19ef5cd178e00b62be659af20c6c9fafe8 (patch) | |
tree | 9ac4f4e7c307d25dd4570727064695280c2a7c3c | |
parent | 8086d90feb944cf3022e9970a54bf3fd6c5ad3d3 (diff) |
Fix indexing author property
-rwxr-xr-x | misc/aslo-sync | 2 | ||||
-rw-r--r-- | sugar_network/db/metadata.py | 16 | ||||
-rw-r--r-- | sugar_network/resources/volume.py | 12 | ||||
-rwxr-xr-x | tests/units/db/index.py | 24 | ||||
-rwxr-xr-x | tests/units/resources/volume.py | 37 |
5 files changed, 78 insertions, 13 deletions
diff --git a/misc/aslo-sync b/misc/aslo-sync index 51f9509..2faff7c 100755 --- a/misc/aslo-sync +++ b/misc/aslo-sync @@ -176,7 +176,7 @@ class Application(application.Application): self.volume['context'].create( guid=SUGAR_GUID, implement=SUGAR_GUID, - type='package', title='Sugar', + type='package', title='sugar', summary='Constructionist learning platform', description= 'Sugar provides simple yet powerful means of engaging ' diff --git a/sugar_network/db/metadata.py b/sugar_network/db/metadata.py index 87d412f..b32cc54 100644 --- a/sugar_network/db/metadata.py +++ b/sugar_network/db/metadata.py @@ -26,7 +26,7 @@ from sugar_network.toolkit import http, enforce #: Xapian term prefix for GUID value GUID_PREFIX = 'I' -_LIST_TYPES = (list, tuple, frozenset) +_LIST_TYPES = (list, tuple, frozenset, types.GeneratorType) def indexed_property(property_class=None, *args, **kwargs): @@ -203,14 +203,14 @@ class Property(object): if self._reprcast is not None: value = self._reprcast(value) - for value in (value if type(value) in _LIST_TYPES else [value]): - if type(value) is bool: - value = int(value) - if type(value) is unicode: - value = unicode(value).encode('utf8') + for subvalue in (value if type(value) in _LIST_TYPES else [value]): + if type(subvalue) is bool: + subvalue = int(subvalue) + if type(subvalue) is unicode: + subvalue = unicode(subvalue).encode('utf8') else: - value = str(value) - result.append(value) + subvalue = str(subvalue) + result.append(subvalue) return result diff --git a/sugar_network/resources/volume.py b/sugar_network/resources/volume.py index caba5f1..0600d33 100644 --- a/sugar_network/resources/volume.py +++ b/sugar_network/resources/volume.py @@ -29,10 +29,14 @@ _logger = logging.getLogger('resources.volume') def _reprcast_authors(value): - for guid, props in value.items(): - if 'name' in props: - yield props['name'] - yield guid + if isinstance(value, dict): + for guid, props in value.items(): + if 'name' in props: + yield props['name'] + else: + yield guid + else: + yield value class Resource(db.Document): diff --git a/tests/units/db/index.py b/tests/units/db/index.py index 1b5eed0..c51b436 100755 --- a/tests/units/db/index.py +++ b/tests/units/db/index.py @@ -87,6 +87,30 @@ class IndexTest(tests.Test): [], db._find(key='fake', reply=['key'])[0]) + def test_IndexByReprcastGenerator(self): + + def iterate(value): + if value != 'fake': + yield 'foo' + yield 'bar' + yield value + + db = Index({'key': IndexedProperty('key', 1, 'K', reprcast=iterate)}) + db.store('1', {'key': 'value'}, True) + + self.assertEqual( + [{'guid': '1'}], + db._find(key='foo')[0]) + self.assertEqual( + [{'guid': '1'}], + db._find(key='bar')[0]) + self.assertEqual( + [{'guid': '1'}], + db._find(key='value')[0]) + self.assertEqual( + [], + db._find(key='fake')[0]) + def test_find(self): db = Index({ 'var_1': IndexedProperty('var_1', 1, 'A', full_text=True), diff --git a/tests/units/resources/volume.py b/tests/units/resources/volume.py index 8fe1141..404f1ed 100755 --- a/tests/units/resources/volume.py +++ b/tests/units/resources/volume.py @@ -177,6 +177,43 @@ class VolumeTest(tests.Test): {'user': {'name': 'User', 'role': 3, 'order': 0}}, volume['document'].get(guid)['author']) + def test_FindByAuthor(self): + + class Document(Resource): + pass + + volume = Volume('db', [User, Document]) + cp = TestCommands(volume) + + volume['user'].create(guid='user1', color='', pubkey='', name='UserName1') + volume['user'].create(guid='user2', color='', pubkey='', name='User Name2') + volume['user'].create(guid='user3', color='', pubkey='', name='User Name 3') + + guid1 = call(cp, method='POST', document='document', content={}, principal='user1') + guid2 = call(cp, method='POST', document='document', content={}, principal='user2') + guid3 = call(cp, method='POST', document='document', content={}, principal='user3') + + self.assertEqual(sorted([ + {'guid': guid1}, + ]), + call(cp, method='GET', document='document', author='UserName1')['result']) + + self.assertEqual(sorted([ + {'guid': guid1}, + ]), + sorted(call(cp, method='GET', document='document', query='author:UserName')['result'])) + self.assertEqual(sorted([ + {'guid': guid1}, + {'guid': guid2}, + {'guid': guid3}, + ]), + sorted(call(cp, method='GET', document='document', query='author:User')['result'])) + self.assertEqual(sorted([ + {'guid': guid2}, + {'guid': guid3}, + ]), + sorted(call(cp, method='GET', document='document', query='author:Name')['result'])) + def test_PreserveAuthorsOrder(self): class Document(Resource): |