Download tasks/data/forward/accenture__cymple.f15a7b9/tests/test_patch.diff from Lego-X/ProgramGen: direct link, hf CLI and curl.
- Browser
- Download file 38.6 kB
-
https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/accenture__cymple.f15a7b9/tests/test_patch.diff
- Command line
-
hf download hf://datasets/Lego-X/ProgramGen/tasks/data/forward/accenture__cymple.f15a7b9/tests/test_patch.diff
-
curl -L -o test_patch.diff https://huggingface.co/datasets/Lego-X/ProgramGen/resolve/main/tasks/data/forward/accenture__cymple.f15a7b9/tests/test_patch.diff
38.6 kB
| diff --git a/tests/__init__.py b/tests/__init__.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/__init__.py | |
| +"""Test suite for the cymple package.""" | |
| diff --git a/tests/conftest.py b/tests/conftest.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/conftest.py | |
| +from pytest import fixture | |
| + | |
| + | |
| +def pytest_addoption(parser): | |
| + try: | |
| + parser.addoption('--e2e', action='store_true', default=False) | |
| + parser.addoption('--neo4j_user', action='store', default=None) | |
| + parser.addoption('--neo4j_password', action='store', default=None) | |
| + except Exception as ex: | |
| + print(ex) | |
| + | |
| +@fixture(scope='session') | |
| +def is_run_e2e(pytestconfig) -> bool: | |
| + return pytestconfig.getoption('e2e') | |
| + | |
| +@fixture(scope='session') | |
| +def neo4j_user(pytestconfig) -> bool: | |
| + return pytestconfig.getoption('neo4j_user') | |
| + | |
| +@fixture(scope='session') | |
| +def neo4j_password(pytestconfig) -> bool: | |
| + return pytestconfig.getoption('neo4j_password') | |
| diff --git a/tests/data/onto_types/labels.py b/tests/data/onto_types/labels.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/data/onto_types/labels.py | |
| +from dataclasses import dataclass | |
| + | |
| + | |
| +@dataclass | |
| +class Labels: | |
| + Application: str = "Application" | |
| + CVEType: str = "CVEType" | |
| + CloudObject: str = "CloudObject" | |
| + Finding: str = "Finding" | |
| + FindingScan: str = "FindingScan" | |
| + FindingSourceType: str = "FindingSourceType" | |
| + FindingType: str = "FindingType" | |
| + ReferenceType: str = "ReferenceType" | |
| + ServiceType: str = "ServiceType" | |
| diff --git a/tests/data/onto_types/properties.py b/tests/data/onto_types/properties.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/data/onto_types/properties.py | |
| +from dataclasses import dataclass | |
| + | |
| + | |
| +@dataclass | |
| +class Properties: | |
| + has_id: str = "has_id" | |
| + has_name: str = "has_name" | |
| + has_probability: str = "has_probability" | |
| + has_severity: str = "has_severity" | |
| + has_description: str = "has_description" | |
| + has_attack_scenario: str = "has_attack_scenario" | |
| diff --git a/tests/data/onto_types/relations.py b/tests/data/onto_types/relations.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/data/onto_types/relations.py | |
| +from dataclasses import dataclass | |
| + | |
| + | |
| +@dataclass | |
| +class Relations: | |
| + has_cloud_object: str = "has_cloud_object" | |
| + has_service: str = "has_service" | |
| + has_finding_type: str = "has_finding_type" | |
| + has_diagnosed_cloud_object: str = "has_diagnosed_cloud_object" | |
| + has_finding: str = "has_finding" | |
| + has_cve: str = "has_cve" | |
| + has_reference: str = "has_reference" | |
| diff --git a/tests/e2e/test_e2e_samples.py b/tests/e2e/test_e2e_samples.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/e2e/test_e2e_samples.py | |
| +import pytest | |
| +from samples import neo4j_e2e | |
| + | |
| + | |
| +def test_neo4j_sample(is_run_e2e, neo4j_user, neo4j_password): | |
| + if not is_run_e2e: | |
| + pytest.skip('Use "pytest --e2e --neo4j_user <username> --neo4j_password <password>" to run this!') | |
| + | |
| + movie_name = 'The Matrix' | |
| + | |
| + neo4j_e2e.init(neo4j_user=neo4j_user, neo4j_password=neo4j_password) | |
| + assert neo4j_e2e.write_movie_node(movie_name) == movie_name | |
| + assert neo4j_e2e.read_movie_node(movie_name) == movie_name | |
| diff --git a/tests/functional/__init__.py b/tests/functional/__init__.py | |
| new file mode 100644 | |
| index 0000000..e69de29 | |
| diff --git a/tests/functional/test_functionality.py b/tests/functional/test_functionality.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/functional/test_functionality.py | |
| +import inspect | |
| +import pytest | |
| +from cymple import QueryBuilder, builder | |
| + | |
| +any_available_clauses = [ | |
| + ("cypher", {"cypher_query_str": ""}) | |
| +] | |
| + | |
| + | |
| +@pytest.mark.parametrize("clause, params", any_available_clauses) | |
| +def test_any_available(clause, params): | |
| + qb = QueryBuilder() | |
| + callables = [_callable for _callable in dir(getattr(qb.match(), clause)(*params)) | |
| + if not _callable.startswith("__")] | |
| + builder_classes = [_class for _class in inspect.getmembers( | |
| + builder, | |
| + lambda member: inspect.isclass(member) and member.__module__ == builder.__name__ | |
| + ) if not _class[0].lower().endswith("available") and _class[0].lower() != "querybuilder"] | |
| + | |
| + missing_callables = set() | |
| + for _class in builder_classes: | |
| + missing_callables.update([ | |
| + func for func in dir(_class[1]) if not func.startswith("__") and func not in callables | |
| + ]) | |
| + assert not missing_callables | |
| diff --git a/tests/unit/__init__.py b/tests/unit/__init__.py | |
| new file mode 100644 | |
| index 0000000..e69de29 | |
| diff --git a/tests/unit/test_clauses.py b/tests/unit/test_clauses.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/unit/test_clauses.py | |
| +import pytest | |
| +from cymple import QueryBuilder | |
| + | |
| +qb = QueryBuilder() | |
| + | |
| +rendered = { | |
| + '_RESET_': qb.reset(), | |
| + 'CALL': qb.reset().call().procedure("db.labels()"), | |
| + 'CASE WHEN': qb.reset().match().node(ref_name='n').with_('n').case_when({'n.name': 'Bob'}, 'true', 'false', 'my_boolean'), | |
| + 'DELETE': qb.reset().match().node(ref_name='n').delete('n'), | |
| + 'DETACH DELETE': qb.reset().match().node(ref_name='n').detach_delete('n'), | |
| + 'WHERE (single)': qb.reset().match().node(ref_name='n').where('n.name', '=', 'value'), | |
| + 'WHERE (multiple)': qb.reset().match().node(ref_name='n').where_multiple({'n.name': 'value', 'n.age': 20}), | |
| + 'WHERE (literal)': qb.reset().match().node(ref_name='n').where_literal('NOT exists(n)'), | |
| + 'MATCH': qb.reset().match(), | |
| + 'MATCH OPTIONAL': qb.reset().match_optional(), | |
| + 'MERGE': qb.reset().merge(), | |
| + 'NODE': qb.reset().match().node(['label1', 'label2'], 'node', {'name': 'Bob'}), | |
| + 'NODE MERGE': qb.reset().merge().node(labels=['label1', 'label2'], ref_name='n', properties={'name': 'Bob'}).related_to().node(ref_name='m'), | |
| + 'NODE (not escaping)': qb.reset().match().node(['label1', 'label2'], 'node', {'name': 'Bob'}, escape=False), | |
| + 'OPERATOR': qb.reset().match().operator_start('SHORTESTPATH', 'p', '(:A)-[*]-(:B)').operator_end(), | |
| + 'RELATION (forward)': qb.reset().match().node().related_to().node(), | |
| + 'RELATION (backward)': qb.reset().match().node().related_from().node(), | |
| + 'RELATION (unidirectional)': qb.reset().match().node().related().node(), | |
| + 'RELATION (variable length)': qb.reset().match().node().related(min_hops=1, max_hops=2).node(), | |
| + 'RELATION (variable length, empty)': qb.reset().match().node().related(min_hops=-1, max_hops=-1).node(), | |
| + 'RELATION (variable length, with label)': qb.reset().match().node().related(label='Relation', ref_name='rel', min_hops=1, max_hops=2).node(), | |
| + 'RETURN (literal)': qb.reset().match().node(ref_name='n').return_literal('n'), | |
| + 'RETURN (mapping)': qb.reset().match().node(ref_name='n').return_mapping(('n.name', 'name')), | |
| + 'RETURN (mapping, list)': qb.reset().match().node(ref_name='n').return_mapping([('n.name', 'name'), ('n.age', 'age')]), | |
| + 'SET': qb.reset().merge().node(ref_name='n').set({'n.name': 'Alice'}), | |
| + 'SET (not escaping)': qb.reset().merge().node(ref_name='n').set({'n.name': 'n.name + "!"'}, escape_values=False), | |
| + 'SET (string)': qb.reset().merge().node(ref_name='n').set('n.name = n.name + "!"'), | |
| + 'ON CREATE': qb.reset().merge().node(ref_name='n').on_create().set({'n.name': 'Bob'}), | |
| + 'ON MATCH': qb.reset().merge().node(ref_name='n').on_match().set({'n.name': 'Bob'}), | |
| + 'ON CREATE ON MATCH': qb.reset().merge().node(ref_name='n').on_create().set({'n.name': 'Bob'}).on_match().set({'n.name': 'Alice'}), | |
| + 'ON MATCH ON CREATE': qb.reset().merge().node(ref_name='n').on_match().set({'n.name': 'Bob'}).on_create().set({'n.name': 'Alice'}), | |
| + 'UNWIND': qb.reset().match().node(ref_name='n').with_('n').unwind('n'), | |
| + 'WITH': qb.reset().match().node(ref_name='a').with_('a,b'), | |
| + 'WITH (start)': qb.reset().with_('a').match().node(ref_name='a').with_('a,b'), | |
| + 'YIELD': qb.reset().call().procedure("db.labels()").yield_(("label", "label")).where("label", "CONTAINS", "User").return_literal("count(label) AS numLabels"), | |
| + 'YIELD (list)': qb.reset().call().procedure("db.labels()").yield_([('length(labels)', 'len'), ('count(label)', 'cnt')]), | |
| + 'LIMIT': qb.reset().match().node(ref_name='n').return_literal('n').limit(1), | |
| + 'LIMIT (expression)': qb.reset().match().node(ref_name='n').return_literal('n').limit("1 + toInteger(3 * rand())"), | |
| + 'LIMIT (with)': qb.reset().match().node(ref_name='n').with_('n').limit(1), | |
| + 'LIMIT (with set)': qb.reset().match().node(ref_name='n').with_('n').limit(1).set({'n.name': 'Bob'}), | |
| + 'CYPHER': qb.reset().match().node(ref_name='n').cypher("my cypher").limit(1), | |
| + 'SKIP': qb.reset().match().node(ref_name='n').return_literal('n').skip(1), | |
| + 'SKIP (expression)': qb.reset().match().node(ref_name='n').return_literal('n').skip("1 + toInteger(3 * rand())"), | |
| + 'SKIP (with)': qb.reset().match().node(ref_name='n').with_('n').skip(1), | |
| + 'SKIP (with set)': qb.reset().match().node(ref_name='n').with_('n').skip(1).set({'n.name': 'Bob'}), | |
| + 'ORDER BY': qb.match().node(ref_name='n').return_literal('n.name, n.age').order_by("elementId(n)"), | |
| + 'ORDER BY (List)': qb.match().node(ref_name='n').return_literal('n.name, n.age').order_by( | |
| + ["n.name", "keys(n)"]), | |
| + 'ORDER BY (Desc)': qb.match().node(ref_name='n').return_literal('n.name, n.age').order_by("n.name", False), | |
| + 'CREATE': qb.reset().create().node(ref_name='n').return_literal('n'), | |
| + 'REMOVE': qb.reset().match().node(ref_name='n').remove('n.name').return_literal('n.age, n.name'), | |
| + 'REMOVE (list)': qb.reset().match().node(ref_name='n').remove(['n.age', 'n.name']).return_literal('n.age, n.name'), | |
| + 'UNION': qb.match().node(ref_name='n', labels="Actor").return_mapping([('n.name', 'name')]).union().match().node( | |
| + ref_name='n', labels="Movie").return_mapping([('n.title', 'name')]), | |
| + 'UNION (all)': qb.match().node(ref_name='n', labels="Actor").return_mapping( | |
| + [('n.name', 'name')]).union_all().match().node(ref_name='n', labels="Movie").return_mapping([('n.title', 'name')]), | |
| + 'CASE (simple)': qb.match().node(ref_name="n", labels="Person").return_literal().case(when_then_mapping={"1": "'blue'", "2": ["'brown'", "'hazel'"]}, default_result="3", results_ref="result, n.eyes", test_expression="n.eyes"), | |
| + 'CASE (simple extended)': qb.match().node(ref_name="n", labels="Person").return_literal("n.name,").case(when_then_mapping={"'Unknown'": ["IS NULL", "IS NOT TYPED INTEGER | FLOAT"], "'Baby'": ["= 0", "= 1", "= 2"], "'Child'": "<= 13", "'Teenager'": "< 20", "'Young Adult'": "< 30", "'Immortal'": "> 1000"}, default_result="'Adult'", test_expression="n.age", results_ref="result"), | |
| + 'CASE (generic)': qb.match().node(ref_name="n", labels="Person").return_literal().case(when_then_mapping={"1": "n.eyes = 'blue'", "2": "n.age < 40"}, default_result="3", results_ref="result, n.eyes, n.age"), | |
| + 'CASE (null values)': qb.match().node(ref_name="n", labels="Person").return_literal("n.name,").case(when_then_mapping={"-1": "n.age IS NULL"}, default_result="n.age - 10", results_ref="age_10_years_ago"), | |
| + 'CASE (with set)': qb.match().node(ref_name="n", labels="Person").with_("n,").case(when_then_mapping={"1": "'blue'", "2": "'brown'"}, default_result="3", results_ref="colorCode", test_expression="n.eyes").set({"n.colorCode": "colorCode"}, escape_values=False).return_literal("n.name, n.colorCode") | |
| +} | |
| + | |
| +expected = { | |
| + '_RESET_': '', | |
| + 'CALL': 'CALL db.labels()', | |
| + 'CASE WHEN': 'MATCH (n) WITH n CASE WHEN n.name = "Bob" THEN true ELSE false END AS my_boolean', | |
| + 'DELETE': 'MATCH (n) DELETE n', | |
| + 'DETACH DELETE': 'MATCH (n) DETACH DELETE n', | |
| + 'WHERE (single)': 'MATCH (n) WHERE n.name = "value"', | |
| + 'WHERE (multiple)': 'MATCH (n) WHERE n.name = "value" AND n.age = 20', | |
| + 'WHERE (literal)': 'MATCH (n) WHERE NOT exists(n)', | |
| + 'MATCH': 'MATCH', | |
| + 'MATCH OPTIONAL': 'OPTIONAL MATCH', | |
| + 'MERGE': 'MERGE', | |
| + 'NODE': 'MATCH (node: label1: label2 {name : "Bob"})', | |
| + 'NODE MERGE': 'MERGE (n: label1: label2 {name : "Bob"})-->(m)', | |
| + 'NODE (not escaping)': 'MATCH (node: label1: label2 {name : Bob})', | |
| + 'OPERATOR': 'MATCH p = SHORTESTPATH( (:A)-[*]-(:B) )', | |
| + 'RELATION (forward)': 'MATCH ()-->()', | |
| + 'RELATION (backward)': 'MATCH ()<--()', | |
| + 'RELATION (unidirectional)': 'MATCH ()--()', | |
| + 'RELATION (variable length)': 'MATCH ()-[*1..2]-()', | |
| + 'RELATION (variable length, empty)': 'MATCH ()-[*]-()', | |
| + 'RELATION (variable length, with label)': 'MATCH ()-[rel: Relation*1..2]-()', | |
| + 'RETURN (literal)': 'MATCH (n) RETURN n', | |
| + 'RETURN (mapping)': 'MATCH (n) RETURN n.name AS name', | |
| + 'RETURN (mapping, list)': 'MATCH (n) RETURN n.name AS name, n.age AS age', | |
| + 'SET': 'MERGE (n) SET n.name = "Alice"', | |
| + 'SET (not escaping)': 'MERGE (n) SET n.name = n.name + "!"', | |
| + 'SET (string)': 'MERGE (n) SET n.name = n.name + "!"', | |
| + 'ON CREATE': 'MERGE (n) ON CREATE SET n.name = "Bob"', | |
| + 'ON MATCH': 'MERGE (n) ON MATCH SET n.name = "Bob"', | |
| + 'ON CREATE ON MATCH': 'MERGE (n) ON CREATE SET n.name = "Bob" ON MATCH SET n.name = "Alice"', | |
| + 'ON MATCH ON CREATE': 'MERGE (n) ON MATCH SET n.name = "Bob" ON CREATE SET n.name = "Alice"', | |
| + 'UNWIND': 'MATCH (n) WITH n UNWIND n', | |
| + 'WITH': 'MATCH (a) WITH a,b', | |
| + 'WITH (start)': 'WITH a MATCH (a) WITH a,b', | |
| + 'YIELD': 'CALL db.labels() YIELD label AS label WHERE label CONTAINS "User" RETURN count(label) AS numLabels', | |
| + 'YIELD (list)': 'CALL db.labels() YIELD length(labels) AS len, count(label) AS cnt', | |
| + 'LIMIT': 'MATCH (n) RETURN n LIMIT 1', | |
| + 'LIMIT (expression)': 'MATCH (n) RETURN n LIMIT 1 + toInteger(3 * rand())', | |
| + 'LIMIT (with)': 'MATCH (n) WITH n LIMIT 1', | |
| + 'LIMIT (with set)': 'MATCH (n) WITH n LIMIT 1 SET n.name = "Bob"', | |
| + 'CYPHER': 'MATCH (n) my cypher LIMIT 1', | |
| + 'SKIP': 'MATCH (n) RETURN n SKIP 1', | |
| + 'SKIP (expression)': 'MATCH (n) RETURN n SKIP 1 + toInteger(3 * rand())', | |
| + 'SKIP (with)': 'MATCH (n) WITH n SKIP 1', | |
| + 'SKIP (with set)': 'MATCH (n) WITH n SKIP 1 SET n.name = "Bob"', | |
| + 'ORDER BY': 'MATCH (n) RETURN n.name, n.age ORDER BY elementId(n) ASC', | |
| + 'ORDER BY (List)': 'MATCH (n) RETURN n.name, n.age ORDER BY n.name, keys(n) ASC', | |
| + 'ORDER BY (Desc)': 'MATCH (n) RETURN n.name, n.age ORDER BY n.name DESC', | |
| + 'CREATE': 'CREATE (n) RETURN n', | |
| + 'REMOVE': 'MATCH (n) REMOVE n.name RETURN n.age, n.name', | |
| + 'REMOVE (list)': 'MATCH (n) REMOVE n.age, n.name RETURN n.age, n.name', | |
| + 'UNION': 'MATCH (n: Actor) RETURN n.name AS name UNION MATCH (n: Movie) RETURN n.title AS name', | |
| + 'UNION (all)': 'MATCH (n: Actor) RETURN n.name AS name UNION ALL MATCH (n: Movie) RETURN n.title AS name', | |
| + 'CASE (simple)': "MATCH (n: Person) RETURN CASE n.eyes WHEN 'blue' THEN 1 WHEN 'brown', 'hazel' THEN 2 ELSE 3 END" | |
| + " AS result, n.eyes", | |
| + 'CASE (simple extended)': "MATCH (n: Person) RETURN n.name, CASE n.age WHEN IS NULL, IS NOT TYPED INTEGER | FLOAT" | |
| + " THEN 'Unknown' WHEN = 0, = 1, = 2 THEN 'Baby' WHEN <= 13 THEN 'Child' WHEN < 20 THEN" | |
| + " 'Teenager' WHEN < 30 THEN 'Young Adult' WHEN > 1000 THEN 'Immortal' ELSE 'Adult' END" | |
| + " AS result", | |
| + 'CASE (generic)': "MATCH (n: Person) RETURN CASE WHEN n.eyes = 'blue' THEN 1 WHEN n.age < 40 THEN 2 ELSE 3 END" | |
| + " AS result, n.eyes, n.age", | |
| + 'CASE (null values)': "MATCH (n: Person) RETURN n.name, CASE WHEN n.age IS NULL THEN -1 ELSE n.age - 10 END" | |
| + " AS age_10_years_ago", | |
| + 'CASE (with set)': "MATCH (n: Person) WITH n, CASE n.eyes WHEN 'blue' THEN 1 WHEN 'brown' THEN 2 ELSE 3 END " | |
| + "AS colorCode SET n.colorCode = colorCode RETURN n.name, n.colorCode" | |
| +} | |
| + | |
| + | |
| +@pytest.mark.parametrize('clause', expected) | |
| +def test_case(clause: str): | |
| + assert str(rendered[clause]) == expected[clause] | |
| diff --git a/tests/unit/test_generated.py b/tests/unit/test_generated.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/unit/test_generated.py | |
| +import pytest | |
| +from cymple import QueryBuilder | |
| + | |
| +qb = QueryBuilder() | |
| + | |
| +rendered = { | |
| + 'CASE_1': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('p.name', 'name'), ('CASE p.age WHEN 18 THEN "Young Adult" WHEN 65 THEN "Senior" ELSE "Adult" END', 'ageCategory')]), | |
| + 'CASE_2': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('p.name', 'name'), ('CASE WHEN p.age < 18 THEN "Minor" WHEN p.age >= 18 AND p.age < 65 THEN "Adult" ELSE "Senior" END', 'ageGroup')]), | |
| + 'CASE_3': qb.reset().match().node(ref_name='p', labels='Person').set({'p.category': 'CASE WHEN p.age < 18 THEN "Minor" WHEN p.age >= 18 AND p.age < 65 THEN "Adult" ELSE "Senior" END'}, escape_values=False).return_literal('p'), | |
| + 'CASE_4': qb.reset().match().node(ref_name='p', labels='Person').with_('p,').case(when_then_mapping={"\"America\"": "\"US\"", "\"North America\"": "\"Canada\""}, default_result="\"Other\"", results_ref='region', test_expression='p.country').return_literal('p.name, region'), | |
| + 'CASE_5': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('p.name', 'name'), ('CASE WHEN p.age < 18 THEN CASE p.age WHEN 0 THEN "Newborn" ELSE "Child" END WHEN p.age < 65 THEN "Adult" ELSE "Senior" END', 'lifeStage')]), | |
| + 'CASE_6': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('COUNT(CASE WHEN p.age < 18 THEN 1 END)', 'minors'), ('COUNT(CASE WHEN p.age >= 18 AND p.age < 65 THEN 1 END)', 'adults'), ('COUNT(CASE WHEN p.age >= 65 THEN 1 END)', 'seniors')]), | |
| + 'CASE_7': qb.reset().match().node(ref_name='p', labels='Person').where_literal('CASE WHEN p.age < 18 THEN false ELSE true END').return_literal('p'), | |
| + 'CASE_8': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('p.name', 'name'), ('p.age', 'age')]).order_by('CASE WHEN p.age < 18 THEN 1 WHEN p.age >= 18 AND p.age < 65 THEN 2 ELSE 3 END'), | |
| + 'CASE_9': qb.reset().match().node(ref_name='p', labels='Person').return_mapping([('p.name', 'name'), ('p.age', 'age'), ('CASE WHEN p.age < 18 THEN "Young " + p.name WHEN p.age >= 65 THEN "Senior " + p.name ELSE "Adult " + p.name END', 'titledName')]) | |
| +} | |
| + | |
| +expected = { | |
| + 'CASE_1': 'MATCH (p: Person) RETURN p.name AS name, CASE p.age WHEN 18 THEN "Young Adult" WHEN 65 THEN "Senior" ELSE "Adult" END AS ageCategory', | |
| + 'CASE_2': 'MATCH (p: Person) RETURN p.name AS name, CASE WHEN p.age < 18 THEN "Minor" WHEN p.age >= 18 AND p.age < 65 THEN "Adult" ELSE "Senior" END AS ageGroup', | |
| + 'CASE_3': 'MATCH (p: Person) SET p.category = CASE WHEN p.age < 18 THEN "Minor" WHEN p.age >= 18 AND p.age < 65 THEN "Adult" ELSE "Senior" END RETURN p', | |
| + 'CASE_4': 'MATCH (p: Person) WITH p, CASE p.country WHEN "US" THEN "America" WHEN "Canada" THEN "North America" ELSE "Other" END AS region RETURN p.name, region', | |
| + 'CASE_5': 'MATCH (p: Person) RETURN p.name AS name, CASE WHEN p.age < 18 THEN CASE p.age WHEN 0 THEN "Newborn" ELSE "Child" END WHEN p.age < 65 THEN "Adult" ELSE "Senior" END AS lifeStage', | |
| + 'CASE_6': 'MATCH (p: Person) RETURN COUNT(CASE WHEN p.age < 18 THEN 1 END) AS minors, COUNT(CASE WHEN p.age >= 18 AND p.age < 65 THEN 1 END) AS adults, COUNT(CASE WHEN p.age >= 65 THEN 1 END) AS seniors', | |
| + 'CASE_7': 'MATCH (p: Person) WHERE CASE WHEN p.age < 18 THEN false ELSE true END RETURN p', | |
| + 'CASE_8': 'MATCH (p: Person) RETURN p.name AS name, p.age AS age ORDER BY CASE WHEN p.age < 18 THEN 1 WHEN p.age >= 18 AND p.age < 65 THEN 2 ELSE 3 END ASC', | |
| + 'CASE_9': 'MATCH (p: Person) RETURN p.name AS name, p.age AS age, CASE WHEN p.age < 18 THEN "Young " + p.name WHEN p.age >= 65 THEN "Senior " + p.name ELSE "Adult " + p.name END AS titledName' | |
| +} | |
| + | |
| + | |
| +@pytest.mark.parametrize('clause', expected) | |
| +def test_samples(clause: str): | |
| + assert str(rendered[clause]) == expected[clause] | |
| diff --git a/tests/unit/test_real_use_cases.py b/tests/unit/test_real_use_cases.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/unit/test_real_use_cases.py | |
| +from cymple import QueryBuilder | |
| + | |
| +from ..data.onto_types.labels import Labels | |
| +from ..data.onto_types.properties import Properties | |
| +from ..data.onto_types.relations import Relations | |
| + | |
| + | |
| +def cypher_get_all_findings(): | |
| + output_mapping = [('n', 'n')] | |
| + query = QueryBuilder().match().node(Labels.Finding, 'n').return_mapping(output_mapping).get() | |
| + return query | |
| + | |
| + | |
| +def cypher_get_findings(): | |
| + output_mapping = [(f'f.{Properties.has_id}', 'id'), | |
| + (f't.{Properties.has_probability}', 'probability'), | |
| + (f't.{Properties.has_severity}', 'severity'), | |
| + (f'ct.{Properties.has_id}', 'cve')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Finding, 'f') | |
| + .related_to(Relations.has_finding_type) | |
| + .node(Labels.FindingType, 't') | |
| + .related_to(Relations.has_cve) | |
| + .node(Labels.CVEType, 'ct') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_services(): | |
| + output_mapping = [(f's.{Properties.has_id}', 'id'), | |
| + (f's.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.ServiceType, 's') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_identities(): | |
| + output_mapping = [(f'i.{Properties.has_id}', 'id'), | |
| + (f'i.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Identity, 'i') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_business_applications(): | |
| + output_mapping = [(f'a.{Properties.has_id}', 'id'), | |
| + (f'a.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Application, 'a') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_finding_detailed(finding_id): | |
| + output_mapping = [(f'f.{Properties.has_id}', 'id'), | |
| + (f't.{Properties.has_probability}', 'probability'), | |
| + (f't.{Properties.has_severity}', 'severity'), | |
| + (f'ct.{Properties.has_id}', 'cve'), | |
| + (f't.{Properties.has_description}', 'description'), | |
| + (f't.{Properties.has_attack_scenario}', 'attack_scenario'), | |
| + (f't.recommendation', 'recommendations')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Finding, 'f', {Properties.has_id: finding_id}) | |
| + .related_to(Relations.has_finding_type) | |
| + .node(Labels.FindingType, 't') | |
| + .related_to(Relations.has_cve) | |
| + .node(Labels.CVEType, 'ct') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_business_application(application_id): | |
| + output_mapping = [(f'a.{Properties.has_id}', 'id'), | |
| + (f'a.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Application, 'a', {Properties.has_id: application_id}) | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_references_for_finding(finding_id): | |
| + output_mapping = [(f'r.{Properties.has_description}', 'reference')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Finding, None, {Properties.has_id: finding_id}) | |
| + .related_to(Relations.has_reference) | |
| + .node(Labels.ReferenceType, 'r') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_applications_for_finding(finding_id): | |
| + output_mapping = [(f'a.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Finding, None, {Properties.has_id: finding_id}) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.Application, 'a') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_finding_type_details_per_id(id): | |
| + output_mapping = [(f'collect(t.{Properties.has_severity})', 'severities'), | |
| + (f'collect(t.{Properties.has_probability})', 'probabilities')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.FindingType, 't') | |
| + .related_from(Relations.has_finding_type) | |
| + .node(Labels.Finding) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.CloudObject) | |
| + .related_to(Relations.has_service) | |
| + .node(None, None, {Properties.has_id: id}) | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_finding_details_for_application(application_id): | |
| + output_mapping = [(f'f.{Properties.has_id}', 'id'), | |
| + (f't.{Properties.has_probability}', 'probability'), | |
| + (f't.{Properties.has_severity}', 'severity'), | |
| + (f'ct.{Properties.has_id}', 'cve')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Application, None, {Properties.has_id: application_id}) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.CloudObject) | |
| + .related_from(Relations.has_cloud_object) | |
| + .node(Labels.Finding, 'f') | |
| + .related_to(Relations.has_finding_type) | |
| + .node(Labels.FindingType, 't') | |
| + .related_to(Relations.has_cve) | |
| + .node(Labels.CVEType, 'ct') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_get_service_details_for_application(application_id): | |
| + output_mapping = [(f's.{Properties.has_id}', 'id'), | |
| + (f's.{Properties.has_name}', 'name')] | |
| + | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Application, None, {Properties.has_id: application_id}) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.CloudObject) | |
| + .related_to(Relations.has_service) | |
| + .node(Labels.ServiceType, 's') | |
| + .return_mapping(output_mapping) | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_match_set_id(service_id): | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.ServiceType, 's') | |
| + .set({Properties.has_id: service_id}) | |
| + .return_literal('s') | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_merge_on_match(service_id): | |
| + query = (QueryBuilder().merge() | |
| + .node(Labels.ServiceType, 's') | |
| + .on_match() | |
| + .set({Properties.has_id: service_id}) | |
| + .return_literal('s') | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def cypher_relationship_properties(service_id): | |
| + query = (QueryBuilder().match() | |
| + .node(Labels.Application) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.CloudObject) | |
| + .related_to(Relations.has_service, properties={Properties.has_id: service_id}) | |
| + .node(Labels.ServiceType, 's', {Properties.has_id: service_id}) | |
| + .return_literal('s') | |
| + .get()) | |
| + | |
| + return query | |
| + | |
| + | |
| +def test_cypher_get_all_findings(): | |
| + expected_query = f'MATCH (n: {Labels.Finding}) RETURN n AS n' | |
| + actual_query = cypher_get_all_findings() | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_findings(): | |
| + expected_query = ( | |
| + f'MATCH (f: {Labels.Finding})-[: {Relations.has_finding_type}]->' | |
| + f'(t: {Labels.FindingType})-[: {Relations.has_cve}]->(ct: {Labels.CVEType}) ' | |
| + f'RETURN f.{Properties.has_id} AS id, t.{Properties.has_probability} AS probability, ' | |
| + f't.{Properties.has_severity} AS severity, ct.{Properties.has_id} AS cve') | |
| + actual_query = cypher_get_findings() | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_services(): | |
| + expected_query = (f'MATCH (s: {Labels.ServiceType}) ' | |
| + f'RETURN s.{Properties.has_id} AS id, s.{Properties.has_name} AS name') | |
| + actual_query = cypher_get_services() | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_business_applications(): | |
| + expected_query = (f'MATCH (a: {Labels.Application}) ' | |
| + f'RETURN a.{Properties.has_id} AS id, a.{Properties.has_name} AS name') | |
| + actual_query = cypher_get_business_applications() | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_finding_detailed(): | |
| + finding_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = ( | |
| + f'MATCH (f: {Labels.Finding} {{{Properties.has_id} : "{finding_id}"}})-' | |
| + f'[: {Relations.has_finding_type}]->(t: {Labels.FindingType})' | |
| + f'-[: {Relations.has_cve}]->(ct: {Labels.CVEType}) ' | |
| + f'RETURN f.{Properties.has_id} AS id, ' | |
| + f't.{Properties.has_probability} AS probability, ' | |
| + f't.{Properties.has_severity} AS severity, ct.{Properties.has_id} AS cve, ' | |
| + f't.{Properties.has_description} AS description, ' | |
| + f't.{Properties.has_attack_scenario} AS attack_scenario, t.recommendation AS recommendations') | |
| + | |
| + actual_query = cypher_get_finding_detailed(finding_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_business_application(): | |
| + application_id = 'THAT_is_MOCK_id' | |
| + expected_query = (f'MATCH (a: {Labels.Application} {{{Properties.has_id} : "{application_id}"}}) ' | |
| + f'RETURN a.{Properties.has_id} AS id, a.{Properties.has_name} AS name') | |
| + actual_query = cypher_get_business_application(application_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_references_for_finding(): | |
| + finding_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = (f'MATCH (: {Labels.Finding} {{{Properties.has_id} : "{finding_id}"}})' | |
| + f'-[: {Relations.has_reference}]->(r: {Labels.ReferenceType}) ' | |
| + f'RETURN r.{Properties.has_description} AS reference') | |
| + | |
| + actual_query = cypher_get_references_for_finding(finding_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_applications_for_finding(): | |
| + finding_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = ( | |
| + f'MATCH (: {Labels.Finding} {{{Properties.has_id} : "{finding_id}"}})' | |
| + f'-[: {Relations.has_cloud_object}]->(a: {Labels.Application}) ' | |
| + f'RETURN a.{Properties.has_name} AS name') | |
| + | |
| + actual_query = cypher_get_applications_for_finding(finding_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_get_finding_type_details_for_service(): | |
| + service_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = ( | |
| + f'MATCH (t: {Labels.FindingType})<-[: {Relations.has_finding_type}]-' | |
| + f'(: {Labels.Finding})-[: {Relations.has_cloud_object}]->' | |
| + f'(: {Labels.CloudObject})-[: {Relations.has_service}]->( {{{Properties.has_id} : "{service_id}"}}) ' | |
| + f'RETURN collect(t.{Properties.has_severity}) AS severities, ' | |
| + f'collect(t.{Properties.has_probability}) AS probabilities') | |
| + | |
| + actual_query = cypher_get_finding_type_details_per_id(service_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_match_set_id(): | |
| + service_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = f'MATCH (s: {Labels.ServiceType}) SET {Properties.has_id} = "{service_id}" RETURN s' | |
| + | |
| + actual_query = cypher_match_set_id(service_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_merge_on_match(): | |
| + service_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = f'MERGE (s: {Labels.ServiceType}) ON MATCH SET {Properties.has_id} = "{service_id}" RETURN s' | |
| + | |
| + actual_query = cypher_merge_on_match(service_id) | |
| + assert actual_query == expected_query | |
| + | |
| + | |
| +def test_cypher_relationship_properties(): | |
| + service_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = ( | |
| + f'MATCH (: {Labels.Application})-[: {Relations.has_cloud_object}]->' | |
| + f'(: {Labels.CloudObject})-[: {Relations.has_service} {{{Properties.has_id} : "{service_id}"}}]->' | |
| + f'(s: {Labels.ServiceType} {{{Properties.has_id} : "{service_id}"}}) RETURN s') | |
| + | |
| + actual_query = cypher_relationship_properties(service_id) | |
| + assert actual_query == expected_query | |
| + | |
| +def test_cypher_query_add(): | |
| + service_id = 'THAT_is_MOCK_id' | |
| + | |
| + expected_query = ( | |
| + f'MATCH (: {Labels.Application})-[: {Relations.has_cloud_object}]->' | |
| + f'(: {Labels.CloudObject})-[: {Relations.has_service} {{{Properties.has_id} : "{service_id}"}}]->' | |
| + f'(s: {Labels.ServiceType} {{{Properties.has_id} : "{service_id}"}}) WITH s MATCH (s) RETURN s') | |
| + | |
| + actual_query1 = (QueryBuilder().match() | |
| + .node(Labels.Application) | |
| + .related_to(Relations.has_cloud_object) | |
| + .node(Labels.CloudObject) | |
| + .related_to(Relations.has_service, properties={Properties.has_id: service_id}) | |
| + .node(Labels.ServiceType, 's', {Properties.has_id: service_id}) | |
| + .with_('s')) | |
| + actual_query2 = (QueryBuilder().match() | |
| + .node(ref_name='s') | |
| + .return_literal('s')) | |
| + | |
| + actual_query = str(actual_query1 + actual_query2) | |
| + assert actual_query == expected_query | |
| + | |
| + actual_query1 += actual_query2 | |
| + actual_query = str(actual_query1) | |
| + assert actual_query == expected_query | |
| + | |
| +def test_cypher_set_unescaped_after_merge(): | |
| + node_id = '1' | |
| + | |
| + expected_query = ( | |
| + f'MERGE (n: Node {{name : "test"}}) ' | |
| + f'ON CREATE SET n.id = "{node_id}" ' | |
| + f'ON MATCH SET n.id = n.id + "1" ' | |
| + f'RETURN n') | |
| + | |
| + actual_query = (QueryBuilder() | |
| + .merge() | |
| + .node('Node', 'n', {'name': 'test'}) | |
| + .on_create().set({f'n.id': node_id}) | |
| + .on_match().set({f'n.id': f'n.id + "1"'}, escape_values=False) | |
| + .return_literal('n') | |
| + .get()) | |
| + | |
| + assert actual_query == expected_query | |
| + | |
| +def test_multiple_where_1(): | |
| + expected_query = 'MATCH (p) WHERE p.lock IS NULL OR p.value IS NULL' | |
| + actual_query = str(QueryBuilder().match().node(ref_name='p').where_multiple({'p.lock': 'NULL', 'p.value': 'NULL'}, 'IS', ' OR ', escape=False)) | |
| + assert actual_query == expected_query | |
| + | |
| +def test_multiple_where_2(): | |
| + expected_query = 'MATCH (n: Person) WHERE n.age=32 AND NOT n:Teacher' | |
| + actual_query = str(QueryBuilder().match().node('Person', ref_name='n').where_literal('n.age=32 AND NOT n:Teacher')) | |
| + assert actual_query == expected_query | |
| + | |
| +def test_with_list(): | |
| + expected_query = 'MATCH (n) WITH n AS n_test WITH [n_test] AS n_list RETURN n_list AS n_test' | |
| + actual_query = str(QueryBuilder().match().node(ref_name='n').with_('n AS n_test').with_('[n_test] AS n_list').return_mapping(('n_list', 'n_test'))) | |
| + assert actual_query == expected_query | |
| diff --git a/tests/unit/test_samples.py b/tests/unit/test_samples.py | |
| new file mode 100644 | |
| --- /dev/null | |
| +++ b/tests/unit/test_samples.py | |
| +import pytest | |
| +from src.samples import basic | |
| + | |
| + | |
| +rendered = { | |
| + # Read Operations # | |
| + basic.get_all_nodes_by_label.__name__: | |
| + basic.get_all_nodes_by_label(labels='Movie', node_name='n'), | |
| + basic.get_all_nodes_by_label_and_properties.__name__: | |
| + basic.get_all_nodes_by_label_and_properties(labels='Movie', properties={'name': 'The Matrix'}, node_name='n'), | |
| + basic.get_all_paths.__name__: | |
| + basic.get_all_paths(src_node_labels='Movie', dst_node_labels='Director', | |
| + relationship_type='has_directed', path_name='path'), | |
| + basic.get_all_nodes_related_to_nodes.__name__: | |
| + basic.get_all_nodes_related_to_nodes( | |
| + src_node_labels='Movie', dst_node_labels='Director', relationship_type='has_directed', dst_node_name='director'), | |
| + basic.get_all_nodes_related_by_fixed_num_of_hops.__name__: | |
| + basic.get_all_nodes_related_by_fixed_num_of_hops( | |
| + src_node_labels='Movie', dst_node_labels='Director', num_hops=2, dst_node_name='director'), | |
| + basic.get_all_nodes_related_by_varying_num_of_hops.__name__: | |
| + basic.get_all_nodes_related_by_varying_num_of_hops( | |
| + src_node_labels='Movie', dst_node_labels='Director', min_hops=1, max_hops=3, dst_node_name='director'), | |
| + # Write Operations # | |
| + basic.merge_node.__name__: | |
| + basic.merge_node(node_labels='Movie', properties={'name': 'The Matrix Revolutions'}), | |
| + basic.merge_relationship.__name__: | |
| + basic.merge_relationship(relationship_type='has_sequel', src_node_labels='Movie', src_node_properties={'name': 'The Matrix'}, | |
| + dst_node_labels='Movie', dst_node_properties={'name': 'The Matrix Revolutions'}), | |
| +} | |
| + | |
| +expected = { | |
| + basic.get_all_nodes_by_label.__name__: | |
| + 'MATCH (n: Movie) RETURN n', | |
| + basic.get_all_nodes_by_label_and_properties.__name__: | |
| + 'MATCH (n: Movie {name : "The Matrix"}) RETURN n', | |
| + basic.get_all_paths.__name__: | |
| + 'MATCH path = ( (: Movie)-[: has_directed]-(: Director) ) RETURN path', | |
| + basic.get_all_nodes_related_to_nodes.__name__: | |
| + 'MATCH (: Movie)-[: has_directed]-(director: Director) RETURN director', | |
| + basic.get_all_nodes_related_by_fixed_num_of_hops.__name__: | |
| + 'MATCH (: Movie)-[*2]-(director: Director) RETURN director', | |
| + basic.get_all_nodes_related_by_varying_num_of_hops.__name__: | |
| + 'MATCH (: Movie)-[*1..3]-(director: Director) RETURN director', | |
| + basic.merge_node.__name__: | |
| + 'MERGE (: Movie {name : "The Matrix Revolutions"})', | |
| + basic.merge_relationship.__name__: | |
| + 'MATCH (src: Movie {name : "The Matrix"}) MATCH (dst: Movie {name : "The Matrix Revolutions"}) MERGE (src)-[: has_sequel]->(dst)', | |
| +} | |
| + | |
| + | |
| +@pytest.mark.parametrize('clause', expected) | |
| +def test_samples(clause: str): | |
| + assert str(rendered[clause]) == expected[clause] | |
| diff --git a/tests/data/.gitkeep b/tests/data/.gitkeep | |
| new file mode 100644 | |
| index 0000000..e69de29 | |
| diff --git a/tests/e2e/.gitkeep b/tests/e2e/.gitkeep | |
| new file mode 100644 | |
| index 0000000..e69de29 | |
| diff --git a/tests/integration/.gitkeep b/tests/integration/.gitkeep | |
| new file mode 100644 | |
| index 0000000..e69de29 | |