Skip to content

Commit

Permalink
fix bug in doubly nested results getting overwriting eachother
Browse files Browse the repository at this point in the history
  • Loading branch information
dwiel committed Dec 3, 2015
1 parent 3f5f78c commit ce88f82
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
24 changes: 24 additions & 0 deletions graphcore/graphcore_test.py
Expand Up @@ -479,6 +479,30 @@ def test_search_outputs(self):
'xyz.id'
]

def test_nested_results(self):
gc = graphcore.Graphcore()

gc.register_rule(
[], 'x.id', function=lambda: [1, 2, 3], cardinality='many'
)
gc.register_rule(
['x.id'], ['x.ys.z'], function=lambda id:id
)

ret = gc.query({
'x.ys': [{
'z?': None,
}]
})

assert ret == [{
'x.ys': [{'z': 1}],
}, {
'x.ys': [{'z': 2}],
}, {
'x.ys': [{'z': 3}],
}]


class TestQuerySearch(unittest.TestCase):

Expand Down
24 changes: 21 additions & 3 deletions graphcore/result_set.py
Expand Up @@ -83,8 +83,15 @@ def extract_json(self, paths):

return ret

def copy(self):
return Result(self)
def deepcopy(self):
new_result = {}
for k, v in self.result.items():
if isinstance(v, ResultSet):
v = v.deepcopy()

new_result[k] = v

return Result(new_result)

def __getitem__(self, k):
return self.result[str(k)]
Expand Down Expand Up @@ -162,6 +169,16 @@ def filter(self, path, relation):
path[1:], relation
)

def deepcopy(self):
new_results = []
for result in self.results:
if isinstance(result, Result):
result = result.deepcopy()

new_results.append(result)

return ResultSet(new_results)

def __repr__(self):
return '<ResultSet {str}>'.format(str=str(self))

Expand Down Expand Up @@ -269,7 +286,8 @@ def apply_rule(data, fn, outputs, cardinality, scope):

new_datas = []
for values in values_set:
new_data = data.copy()
# deepcopy: recursively copy Result and ResultSet objects only
new_data = data.deepcopy()
for output, value in zip(outputs, values):
new_data[output[0]] = value
new_datas.append(new_data)
Expand Down

0 comments on commit ce88f82

Please sign in to comment.