Skip to content

Commit

Permalink
more grouping unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasht86 committed Dec 5, 2024
1 parent a82b411 commit d4997ee
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion tests/unit/test_q.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ def test_condition_annotation(self):
return q

def test_grouping_with_condition(self):
self.maxDiff = None
grouping = G.all(G.group("customer"), G.each(G.output(G.sum("price"))))
q = (
Query(select_fields="*")
Expand All @@ -202,6 +201,69 @@ def test_grouping_with_condition(self):
self.assertEqual(q, expected)
return q

def test_grouping_with_ordering_and_limiting(self):
self.maxDiff = None
grouping = G.all(
G.group("customer"),
G.max(2),
G.precision(12),
G.order(-G.count()),
G.each(G.output(G.sum("price"))),
)
q = Query(select_fields="*").from_("purchase").where(True).groupby(grouping)
expected = "select * from purchase where true | all(group(customer) max(2) precision(12) order(-count()) each(output(sum(price))))"
self.assertEqual(q, expected)
return q

def test_grouping_with_map_keys(self):
grouping = G.all(
G.group("mymap.key"),
G.each(G.group("mymap.value"), G.each(G.output(G.count()))),
)
q = Query(select_fields="*").from_("purchase").where(True).groupby(grouping)
expected = "select * from purchase where true | all(group(mymap.key) each(group(mymap.value) each(output(count()))))"
self.assertEqual(q, expected)
return q

def test_group_by_year(self):
grouping = G.all(G.group("time.year(a)"), G.each(G.output(G.count())))
q = Query(select_fields="*").from_("purchase").where(True).groupby(grouping)
expected = "select * from purchase where true | all(group(time.year(a)) each(output(count())))"
self.assertEqual(q, expected)
return q

def test_grouping_with_date_agg(self):
grouping = G.all(
G.group("time.year(a)"),
G.each(
G.output(G.count()),
G.all(
G.group("time.monthofyear(a)"),
G.each(
G.output(G.count()),
G.all(
G.group("time.dayofmonth(a)"),
G.each(
G.output(G.count()),
G.all(
G.group("time.hourofday(a)"),
G.each(G.output(G.count())),
),
),
),
),
),
),
)
q = Query(select_fields="*").from_("purchase").where(True).groupby(grouping)
expected = "select * from purchase where true | all(group(time.year(a)) each(output(count()) all(group(time.monthofyear(a)) each(output(count()) all(group(time.dayofmonth(a)) each(output(count()) all(group(time.hourofday(a)) each(output(count())))))))))"
# q select * from purchase where true | all(group(time.year(a)) each(output(count()) all(group(time.monthofyear(a)) each(output(count()) all(group(time.dayofmonth(a)) each(output(count()) all(group(time.hourofday(a)) each(output(count())))))))))
# e select * from purchase where true | all(group(time.year(a)) each(output(count() all(group(time.monthofyear(a)) each(output(count()) all(group(time.dayofmonth(a)) each(output(count()) all(group(time.hourofday(a)) each(output(count())))))))))
print(q)
print(expected)
self.assertEqual(q, expected)
return q

def test_add_parameter(self):
f1 = Queryfield("title")
condition = f1.contains("Python")
Expand Down

0 comments on commit d4997ee

Please sign in to comment.