Skip to content

Commit

Permalink
test: fix clauses tests and add generated test cases using the CASE W…
Browse files Browse the repository at this point in the history
…HEN clause
  • Loading branch information
Roei-Levi committed Nov 6, 2024
1 parent e6a762a commit 4dc6315
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
14 changes: 7 additions & 7 deletions tests/unit/test_clauses.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
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',
'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"',
Expand All @@ -90,8 +90,8 @@
'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',
'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 + "!"',
Expand All @@ -102,8 +102,8 @@
'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',
'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',
Expand All @@ -119,8 +119,8 @@
'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',
'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"
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/test_generated.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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]

0 comments on commit 4dc6315

Please sign in to comment.