storage: change get_with_indexed_value into a generator (#23180)

This commit is contained in:
Thomas NOËL 2018-04-16 16:09:17 +02:00 committed by Frédéric Péters
parent 8c968c4685
commit 4e596155ac
2 changed files with 12 additions and 9 deletions

View File

@ -90,7 +90,7 @@ def test_get_with_indexed_value():
test.unique_value = 'unique-value3'
test.store()
tests = Foobar.get_with_indexed_value('value', 'value')
tests = list(Foobar.get_with_indexed_value('value', 'value'))
assert len(tests) == 2
assert 'unique-value' in [x.unique_value for x in tests]
assert 'unique-value2' in [x.unique_value for x in tests]
@ -114,7 +114,7 @@ def test_get_with_indexed_value_changes():
test.unique_value = 'unique-value3'
test.store()
tests = Foobar.get_with_indexed_value('value', 'value')
tests = list(Foobar.get_with_indexed_value('value', 'value'))
assert len(tests) == 2
assert 'unique-value' in [x.unique_value for x in tests]
assert 'unique-value2' in [x.unique_value for x in tests]
@ -123,7 +123,7 @@ def test_get_with_indexed_value_changes():
test = Foobar.get_on_index('unique-value3', 'unique_value')
test.value = 'value'
test.store()
tests = Foobar.get_with_indexed_value('value', 'value')
tests = list(Foobar.get_with_indexed_value('value', 'value'))
assert len(tests) == 3
assert 'unique-value3' in [x.unique_value for x in tests]
@ -146,7 +146,7 @@ def test_get_with_indexed_value_dict():
test.dict_value = {'plop': '2'}
test.store()
tests = Foobar.get_with_indexed_value('dict_value', '2')
tests = list(Foobar.get_with_indexed_value('dict_value', '2'))
assert len(tests) == 2
assert 'unique-value' not in [x.unique_value for x in tests]
assert 'unique-value2' in [x.unique_value for x in tests]
@ -172,7 +172,7 @@ def test_get_with_indexed_value_dict_changes():
test3.dict_value = {'plop': '2'}
test3.store()
tests = Foobar.get_with_indexed_value('dict_value', '2')
tests = list(Foobar.get_with_indexed_value('dict_value', '2'))
assert len(tests) == 2
assert 'unique-value' not in [x.unique_value for x in tests]
assert 'unique-value2' in [x.unique_value for x in tests]
@ -181,13 +181,13 @@ def test_get_with_indexed_value_dict_changes():
test1.dict_value = {'plop': '2'}
test1.store()
tests = Foobar.get_with_indexed_value('dict_value', '2')
tests = list(Foobar.get_with_indexed_value('dict_value', '2'))
assert len(tests) == 3
test1.dict_value = None
test1.store()
tests = Foobar.get_with_indexed_value('dict_value', '2')
tests = list(Foobar.get_with_indexed_value('dict_value', '2'))
assert len(tests) == 2
@ -384,7 +384,7 @@ def test_destroy_rebuild_index():
Foobar.destroy_indexes()
assert not os.path.exists(os.path.join(Foobar.get_objects_dir(), '.indexes'))
tests = Foobar.get_with_indexed_value('value', 'value')
tests = list(Foobar.get_with_indexed_value('value', 'value'))
assert len(tests) == 2
assert 'unique-value' in [x.unique_value for x in tests]
assert 'unique-value2' in [x.unique_value for x in tests]

View File

@ -349,7 +349,10 @@ class StorableObject(object):
@classmethod
def get_with_indexed_value(cls, index, value, ignore_errors = False):
ids = cls.get_ids_with_indexed_value(str(index), str(value))
return cls.get_ids(ids)
for x in ids:
obj = cls.get(x, ignore_errors=ignore_errors)
if obj is not None:
yield obj
@classmethod
def storage_load(cls, fd):