Skip to content

Commit

Permalink
add tests on filter with datetime variables
Browse files Browse the repository at this point in the history
  • Loading branch information
aless10 committed Jun 11, 2024
1 parent e5a9491 commit 6b5f97f
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
56 changes: 56 additions & 0 deletions integration/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,62 @@ def test_replace_values(self):
assert r[variable.body["id"]] == [1, 2, 3, 4, 5]
ds.delete()

def test_append_dataset(self):
ds = site.datasets.create(as_entity({"name": "test_scrunch_append_dataset"})).refresh()
ds.variables.create(
as_entity(
{
"name": "my_var",
"alias": "my_var",
"type": "numeric",
"values": [1, 2, 3, None, 5],
}
)
).refresh()
ds_to_append = site.datasets.create(as_entity({"name": "test_scrunch_dataset_to_append"})).refresh()
datetime_var = ds_to_append.variables.create(
as_entity(
{
"name": "my_datetime_var",
"alias": "my_datetime_var",
"type": "datetime",
"resolution": "ms",
"values": [
"2024-01-03T20:00:52.333",
"2024-02-03T20:00:52.234",
"2024-03-03T20:00:52.456",
"2024-04-03T20:00:52.999",
"2024-06-03T20:00:52.123",
],
}
)
).refresh()
scrunch_dataset = get_mutable_dataset(ds.body.id, site)
scrunch_dataset_to_append = get_mutable_dataset(ds_to_append.body.id, site)
# This is intended to leave only two records. Changing the variable `datetime_var`
# above also changes the test's results
filter_value = "2024-03-15T00:00:00.393"
resp = scrunch_dataset.append_dataset(
scrunch_dataset_to_append,
filter=f"my_datetime_var > '{filter_value}'"
)
if resp is not None:
resp['body']['filter'] = {
'args': [
{
'variable': datetime_var['self']
}, {
'value': filter_value
}
],
'function': '>'
}
else:
pass

ds.delete()
ds_to_append.delete()


class TestCategories(TestCase):
def test_edit_category(self):
Expand Down
54 changes: 52 additions & 2 deletions scrunch/tests/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -6506,7 +6506,6 @@ class TestMutableMixin(TestDatasetBase):
'is_subvar': False
}
}

variables_b = {
'var_a': {
'id': '003',
Expand All @@ -6524,8 +6523,32 @@ class TestMutableMixin(TestDatasetBase):
}
}

variables_with_datetime = {
'var_a': {
'id': '003',
'alias': 'var_a',
'name': 'Variable A',
'type': 'numeric',
'is_subvar': False
},
'var_b': {
'id': '004',
'alias': 'var_b',
'name': 'Variable B',
'type': 'categorical',
'is_subvar': False
},
'var_d': {
'id': '005',
'alias': 'endtime',
'name': 'Endtime',
'type': 'datetime',
'is_subvar': False
}
}

def test_compare_datasets(self):
ds_a_mock = self._dataset_mock(variables=self.variables)
ds_a_mock = self._dataset_mock(variables=self.variables_with_datetime)
ds_a = MutableDataset(ds_a_mock)
ds_b_mock = self._dataset_mock(variables=self.variables_b)
ds_b = MutableDataset(ds_b_mock)
Expand Down Expand Up @@ -6582,6 +6605,33 @@ def test_append_with_variables(self):
ds_b.append_dataset(ds_a, variables=["var_a", "var_b"])
ds_b.resource.batches.create.assert_called_with(expected_payload)

def test_append_with_filter(self):
ds_a_mock = self._dataset_mock(variables=self.variables_with_datetime)
ds_a = MutableDataset(ds_a_mock)
ds_b_mock = self._dataset_mock(variables=self.variables_b)
ds_b = MutableDataset(ds_b_mock)
ds_a.url = 'http://test.crunch.io/api/datasets/123/'
expected_payload = {
"element": "shoji:entity",
"autorollback": True,
"body": {
"dataset": ds_a.url,
"filter": {
"function": ">",
"args": [
{
'variable': 'https://test.crunch.io/api/datasets/123456/variables/var_d/'
},
{
"value": "2024-06-03T22:53:52.393"
}
]
},
},
}
ds_b.append_dataset(ds_a, filter='endtime > "2024-06-03T22:53:52.393"')
ds_b.resource.batches.create.assert_called_with(expected_payload)


class TestHeadingSubtotals(TestDatasetBase):
variables = {
Expand Down

0 comments on commit 6b5f97f

Please sign in to comment.