def test_and_logic(self): filter = Filter() res = filter.selector(dimension='agent_gender', value='M') & filter.selector(dimension='agent_home_state', value='1') & filter.bound(dimension='agent_year_of_birth', lower=1969, ordering='numeric') expected = { "filter": { "type": "and", "fields": [{ "type": "selector", "dimension": "agent_gender", "value": "M" }, { "type": "selector", "dimension": "agent_home_state", "value": "1" }, { "type": "bound", "dimension": "agent_year_of_birth", "lower": 1969, "ordering": "numeric" }] } } self.assertEqual(res.to_dict(), expected)
def test_mix_operation(self): filter = Filter() listOfSA2 = ['305021115', '303021053', '303021055', '303021058'] and_filter_list = list(map(lambda x: (filter.selector(dimension="origin_sa2", value=x) & filter.selector(dimension="destination_sa2", value=x)), listOfSA2)) combined_filters = reduce(lambda a, b: a | b, and_filter_list) expected = {'filter': {'fields': [{'fields': [{'dimension': 'origin_sa2', 'value': '305021115', 'type': 'selector'}, {'dimension': 'destination_sa2', 'value': '305021115', 'type': 'selector'}], 'type': 'and'}, {'fields': [{'dimension': 'origin_sa2', 'value': '303021053', 'type': 'selector'}, {'dimension': 'destination_sa2', 'value': '303021053', 'type': 'selector'}], 'type': 'and'}, {'fields': [{'dimension': 'origin_sa2', 'value': '303021055', 'type': 'selector'}, {'dimension': 'destination_sa2', 'value': '303021055', 'type': 'selector'}], 'type': 'and'}, {'fields': [{'dimension': 'origin_sa2', 'value': '303021058', 'type': 'selector'}, {'dimension': 'destination_sa2', 'value': '303021058', 'type': 'selector'}], 'type': 'and'}], 'type': 'or'}} self.assertEqual(combined_filters.to_dict(), expected)
def test_filter_auto_renew3(self): filter = Filter() filter1 = filter.selector(dimension="country_name", value='AUSTRALIA') filter2 = filter.selector(dimension="country_name", value='SINGAPORE') res1 = (filter1 & filter2).to_dict() res2 = (filter1 | filter2).to_dict() expected1 = {'filter': {'type': 'and', 'fields': [{ "type": "selector", "dimension": "country_name", "value": "AUSTRALIA"},{ "type": "selector", "dimension": "country_name", "value": "SINGAPORE" } ]}} expected2 = {'filter': {'type': 'or', 'fields': [{ "type": "selector", "dimension": "country_name", "value": "AUSTRALIA"},{ "type": "selector", "dimension": "country_name", "value": "SINGAPORE" } ]}} self.assertEqual(res1, expected1) self.assertEqual(res2, expected2)
def test_filter_auto_renew2(self): filter = Filter() res = ~filter.selector(dimension='country_name', value='AUSTRALIA') res.to_dict() res2 = filter.in_filter("D", "E", dimension='links') expected_logic = None self.assertEqual(res2._logic, expected_logic)
def test_selector(self): selector = Filter() res = selector.selector(dimension='agent_gender', value='M') expected = { 'filter':{ 'type': 'selector', 'dimension': 'agent_gender', 'value': 'M' } } self.assertEqual(res.to_dict(), expected)
def test_or_logic(self): filter = Filter() res = filter.selector(dimension='sa2', value='117011325') \ | filter.selector(dimension='sa2', value='210051248') expected = { "filter": { "type": "or", "fields": [{ "type": "selector", "dimension": "sa2", "value": "117011325" }, { "type": "selector", "dimension": "sa2", "value": "210051248" }] } } self.assertEqual(res.to_dict(), expected)
def test_not_logic(self): filter1 = Filter() res = ~filter1.selector(dimension='country_name', value='AUSTRALIA') expected = { "filter":{ "type": "not", "field": { "type": "selector", "dimension": "country_name", "value": "AUSTRALIA" } } } self.assertEqual(res.to_dict(), expected)
def test_extraction_fn_in_filter(self): filter1 = Filter() extraction = ExtractionFn(typ='timeFormat') res = filter1.selector(dimension='__time', value='AUSTRALIA', extraction_fn=extraction(format="EEEE", timezone="Australia/Sydney")) expected = { 'filter': { 'dimension': '__time', 'value': 'AUSTRALIA', 'type': 'selector', 'extractionFn': { 'format': 'EEEE', 'timeZone': 'Australia/Sydney', 'locale': 'en', 'type': 'timeFormat'} } } self.assertEqual(res.to_dict(), expected)
def test_query4(self): extraction = ExtractionFn(typ='timeFormat') extract = extraction(format='EEEE', timezone='Australia/Sydney') filter = Filter() filter = filter.selector(dimension='__time', extraction_fn=extract, value="Monday") \ | filter.selector(dimension='__time', extraction_fn=extract, value="Tuesday") \ | filter.selector(dimension='__time', extraction_fn=extract, value="Wednesday") \ | filter.selector(dimension='__time', extraction_fn=extract, value="Thursday") \ | filter.selector(dimension='__time', extraction_fn=extract, value="Friday") query = BasicQuery('linkmeta') query.dates(begin_date="2019-07-07", end_date="2019-08-03") query.location(location_type="locationHierarchyLevel", level_type="sa2", id="117031337") query.filter(filt=filter) query.aggregate(metric="unique_agents", described_as="unique_agents") query.granularity(period="P7D") query.dumps() expected = { "dates": { "type": "range", "beginDate": "2019-07-07", "endDate": "2019-08-03" }, "location": { "locationType": "locationHierarchyLevel", "levelType": "sa2", "id": "117031337" }, "filter": { "type": "or", "fields": [{ "type": "selector", "dimension": "__time", "value": "Monday", "extractionFn": { "type": "timeFormat", "format": "EEEE", "timeZone": "Australia/Sydney", "locale": "en" } }, { "type": "selector", "dimension": "__time", "value": "Tuesday", "extractionFn": { "type": "timeFormat", "format": "EEEE", "timeZone": "Australia/Sydney", "locale": "en" } }, { "type": "selector", "dimension": "__time", "value": "Wednesday", "extractionFn": { "type": "timeFormat", "format": "EEEE", "timeZone": "Australia/Sydney", "locale": "en" } }, { "type": "selector", "dimension": "__time", "value": "Thursday", "extractionFn": { "type": "timeFormat", "format": "EEEE", "timeZone": "Australia/Sydney", "locale": "en" } }, { "type": "selector", "dimension": "__time", "value": "Friday", "extractionFn": { "type": "timeFormat", "format": "EEEE", "timeZone": "Australia/Sydney", "locale": "en" } }] }, "aggregations": [{ "metric": "unique_agents", "type": "hyperUnique", "describedAs": "unique_agents" }], "queryGranularity": { "type": "period", "period": "P7D", "timeZone": "Australia/Sydney" } } self.assertEqual(query._req, expected)