TINKERPOP-1784 Minor refactoring of python gherkin steps
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/2881ace3 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/2881ace3 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/2881ace3 Branch: refs/heads/TINKERPOP-1784 Commit: 2881ace3a6003e87926839696879168063427d84 Parents: 1b9d992 Author: Stephen Mallette <[email protected]> Authored: Fri Sep 22 13:49:50 2017 -0400 Committer: Stephen Mallette <[email protected]> Committed: Mon Oct 16 11:19:29 2017 -0400 ---------------------------------------------------------------------- .../src/main/jython/radish/feature_steps.py | 130 ++++++++++--------- 1 file changed, 69 insertions(+), 61 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/2881ace3/gremlin-python/src/main/jython/radish/feature_steps.py ---------------------------------------------------------------------- diff --git a/gremlin-python/src/main/jython/radish/feature_steps.py b/gremlin-python/src/main/jython/radish/feature_steps.py index f67a4c2..19da3ec 100644 --- a/gremlin-python/src/main/jython/radish/feature_steps.py +++ b/gremlin-python/src/main/jython/radish/feature_steps.py @@ -28,17 +28,6 @@ from hamcrest import * out = __.out -def convert(m, ctx): - n = {} - for key, value in m.items(): - if isinstance(key, str) and re.match("v\[.*\]", key): - n[ctx.lookup["modern"][key[2:-1]]] = value - else: - n[key] = value - - return n - - @given("the {graph_name:w} graph") def choose_graph(step, graph_name): # only have modern atm but graphName would be used to select the right one @@ -59,62 +48,81 @@ def iterate_the_traversal(step): step.context.result = step.context.traversal.toList() +def __convert(m, ctx): + n = {} + for key, value in m.items(): + if isinstance(key, str) and re.match("v\[.*\]", key): + n[ctx.lookup["modern"][key[2:-1]]] = value + else: + n[key] = value + + return n + + +def __ordered_assertion(step): + data = step.table + + # results from traversal should have the same number of entries as the feature data table + assert_that(len(step.context.result), equal_to(len(data))) + + # assert the results by type where the first column will hold the type and the second column + # the data to assert. the contents of the second column will be dependent on the type specified + # in the first column + for ix, line in enumerate(data): + if line[0] == "numeric": + assert_that(long(step.context.result[ix]), equal_to(long(line[1]))) + elif line[0] == "string": + assert_that(str(step.context.result[ix]), equal_to(str(line[1]))) + elif line[0] == "vertex": + assert_that(step.context.result[ix].label, equal_to(line[1])) + elif line[0] == "map": + assert_that(__convert(step.context.result[ix], step.context), json.loads(line[1])) + else: + raise ValueError("unknown type of " + line[0]) + + +def __unordered_assertion(step): + data = step.table + + # results from traversal should have the same number of entries as the feature data table + assert_that(len(step.context.result), equal_to(len(data))) + + results_to_test = list(step.context.result) + + # finds a match in the results for each line of data to assert and then removes that item + # from the list - in the end there should be no items left over and each will have been asserted + for line in data: + if line[0] == "numeric": + val = long(line[1]) + assert_that(val, is_in(list(map(long, results_to_test)))) + results_to_test.remove(val) + elif line[0] == "string": + val = str(line[1]) + assert_that(val, is_in(list(map(str, results_to_test)))) + results_to_test.remove(val) + elif line[0] == "vertex": + val = str(line[1]) + v = step.context.lookup["modern"][val] + assert_that(v, is_in(results_to_test)) + results_to_test.remove(v) + elif line[0] == "map": + val = __convert(json.load(line[1]), step.context) + assert_that(val, is_in(results_to_test)) + results_to_test.remove(val) + else: + raise ValueError("unknown type of " + line[0]) + + assert_that(len(results_to_test), is_(0)) + + @then("the result should be {characterized_as:w}") def assert_result(step, characterized_as): if characterized_as == "empty": assert_that(len(step.context.result), equal_to(0)) elif characterized_as == "ordered": - data = step.table - - # results from traversal should have the same number of entries as the feature data table - assert_that(len(step.context.result), equal_to(len(data))) - - # assert the results by type where the first column will hold the type and the second column - # the data to assert. the contents of the second column will be dependent on the type specified - # in the first column - for ix, line in enumerate(data): - if line[0] == "numeric": - assert_that(long(step.context.result[ix]), equal_to(long(line[1]))) - elif line[0] == "string": - assert_that(str(step.context.result[ix]), equal_to(str(line[1]))) - elif line[0] == "vertex": - assert_that(step.context.result[ix].label, equal_to(line[1])) - elif line[0] == "map": - assert_that(convert(step.context.result[ix], step.context), json.loads(line[1])) - else: - raise ValueError("unknown type of " + line[0]) + __ordered_assertion(step) elif characterized_as == "unordered": - data = step.table - - # results from traversal should have the same number of entries as the feature data table - assert_that(len(step.context.result), equal_to(len(data))) - - results_to_test = list(step.context.result) - - # finds a match in the results for each line of data to assert and then removes that item - # from the list - in the end there should be no items left over and each will have been asserted - for line in data: - if line[0] == "numeric": - val = long(line[1]) - assert_that(val, is_in(list(map(long, results_to_test)))) - results_to_test.remove(val) - elif line[0] == "string": - val = str(line[1]) - assert_that(val, is_in(list(map(str, results_to_test)))) - results_to_test.remove(val) - elif line[0] == "vertex": - val = str(line[1]) - v = step.context.lookup["modern"][val] - assert_that(v, is_in(results_to_test)) - results_to_test.remove(v) - elif line[0] == "map": - val = convert(json.load(line[1]), step.context) - assert_that(val, is_in(results_to_test)) - results_to_test.remove(val) - else: - raise ValueError("unknown type of " + line[0]) - - assert_that(len(results_to_test), is_(0)) + __unordered_assertion(step) else: raise ValueError("unknown data characterization of " + characterized_as)
