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_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_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_reg(self): filter = Filter() res = filter.reg(dimension='parent_station', pattern='NSWPTPST1[1-4]00') expected = { "filter": { "type": "regex", "dimension": "parent_station", "pattern": "NSWPTPST1[1-4]00" } } self.assertEqual(res.to_dict(), expected)
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_like(self): filter = Filter() res = filter.like(dimension='origin_building', pattern='%MALL%') expected = { "filter": { "type": "like", "dimension": "origin_building", "pattern": "%MALL%" } } self.assertEqual(res.to_dict(), expected)
def test_in_filter(self): filter = Filter() res = filter.in_filter("A", "B", "C", dimension='links') expected = { 'filter': { 'type': 'in', 'dimension': 'links', 'values': ['A', 'B', 'C'] } } self.assertEqual(res.to_dict(), expected)
def test_bound(self): filter = Filter() res = filter.bound(dimension='duration', lower=60, upper=180, ordering='numeric') expected = { "filter": { "type": "bound", "dimension": "duration", "lower": 60, "upper": 180, "ordering": "numeric" } } self.assertEqual(res.to_dict(), expected)
def test_filter_auto_renew(self): filter = Filter() res = filter.in_filter("A", "B", "C", dimension='links') res.to_dict() res2 = filter.in_filter("D","E", dimension='links') expected2 = { 'filter': { 'type': 'in', 'dimension': 'links', 'values': ['D', 'E'] } } self.assertEqual(res2.to_dict(), expected2)
def test_interval1(self): filter = Filter() res = filter.interval( "2019-08-01T16:00:00.000+10/2019-08-01T18:59:59.999+10", dimension='__time') expected = { "filter": { "type": "interval", "dimension": "__time", "intervals": ["2019-08-01T16:00:00.000+10/2019-08-01T18:59:59.999+10"] } } 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_query3(self): query = BasicQuery('linkmeta') query.dates(begin_date="2019-07-01", end_date="2019-07-28") query.time_series_reference("arrival") query.granularity(period="P7D") filter = Filter() query.filter( filter.in_filter("VIC17483860", "VIC6052402", "NSW500187142", dimension="link_id")) query.aggregate(metric="unique_agents", described_as="unique_agents") query.aggregate(metric="total_records") query.dumps() expected = { "dates": { "beginDate": "2019-07-01", "endDate": "2019-07-28", "type": "range" }, "aggregations": [{ "metric": "unique_agents", "type": "hyperUnique", "describedAs": "unique_agents" }, { "metric": "total_records", "type": "longSum" }], "queryGranularity": { "period": "P7D", "type": "period", "timeZone": "Australia/Sydney" }, "filter": { "dimension": "link_id", "type": "in", "values": ["VIC17483860", "VIC6052402", "NSW500187142"] }, "timeSeriesReference": "arrival" } self.assertEqual(query._req, 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_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)