def test_get_cell_multi(self): cells = CellFactory.build_batch(2) query = Query(cell=self.cell_model_query(cells)) self.assertEqual(self.cache.get(query), None) self.check_stats(counter=[ ('locate.fallback.cache', 1, 1, ['status:bypassed']), ])
def test_no_call_made_when_not_allowed_for_apikey(self): cells = CellFactory.build_batch(2) wifis = WifiFactory.build_batch(2) self.provider.api_key.allow_fallback = False query = self.model_query(cells=cells, wifis=wifis) self.check_should_locate(query, False)
def test_database_error(self, db_errors=0): cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) for model in (Cell, CellArea, CellOCID, CellAreaOCID): self.session.execute(text('drop table %s;' % model.__tablename__)) for name in set([wifi.__tablename__ for wifi in wifis]): self.session.execute(text('drop table %s;' % name)) query = self.model_query(cells=cells, wifis=wifis) res = self._call(body=query, ip=self.test_ip) self.check_response(res, 'ok') self.check_stats(counter=[ ('request', [self.metric_path, 'method:post', 'status:200']), ], timer=[ ('request', [self.metric_path, 'method:post']), ]) if self.apikey_metrics: self.check_stats(counter=[ (self.metric_type + '.result', ['key:test', 'region:GB', 'fallback_allowed:false', 'accuracy:high', 'status:miss']), ]) self.check_raven([('ProgrammingError', db_errors)])
def test_database_error(self, db_errors=0): for tablename in ('cell', 'cell_area', 'ocid_cell', 'ocid_cell_area'): self.session.execute(text('drop table %s;' % tablename)) for i in range(16): self.session.execute(text( 'drop table wifi_shard_%s;' % hex(i)[2:])) cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) query = self.model_query(cells=cells, wifis=wifis) res = self._call(body=query, ip=self.test_ip) self.check_response(res, 'ok') self.check_stats(counter=[ ('request', [self.metric_path, 'method:post', 'status:200']), ], timer=[ ('request', [self.metric_path, 'method:post']), ]) if self.apikey_metrics: self.check_stats(counter=[ (self.metric_type + '.result', ['key:test', 'country:GB', 'accuracy:high', 'status:miss']), ]) self.check_raven([('ProgrammingError', db_errors)])
def setUp(self): super(TestFallbackProvider, self).setUp() self.provider.api_key.allow_fallback = True self.response_location = { 'location': { 'lat': 51.5366, 'lng': 0.03989, }, 'accuracy': 1500, 'fallback': 'lacf', } self.cells = [] for cell in CellFactory.build_batch(2): self.cells.append({ 'radio': cell.radio, 'mcc': cell.mcc, 'mnc': cell.mnc, 'lac': cell.lac, 'cid': cell.cid, 'signal': -70, }) self.cells[0]['ta'] = 1 self.wifis = [] for wifi in WifiFactory.build_batch(2): self.wifis.append({ 'key': wifi.key, 'signal': -77, }) self.wifis[0]['channel'] = 6 self.wifis[0]['frequency'] = 2437 self.wifis[0]['snr'] = 13
def test_medium_hit(self): cells = CellFactory.build_batch(1) self._make_query(self._make_result(accuracy=30000.0), cell=cells) self.check_stats(counter=[ ('locate.result', ['key:key', 'country:none', 'accuracy:medium', 'status:hit']), ])
def test_api_key_disallows(self): api_key = ApiKeyFactory.build(allow_fallback=False) cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) query = self.model_query(cells=cells, wifis=wifis, api_key=api_key) self.check_should_search(query, False)
def test_fallback_used_when_geoip_also_present(self): cells = CellFactory.build_batch(2, radio=Radio.wcdma) wifis = WifiFactory.build_batch(3) api_key = ApiKey.getkey(self.session, 'test') api_key.allow_fallback = True self.session.flush() with requests_mock.Mocker() as mock: response_location = { 'location': { 'lat': 1.0, 'lng': 1.0, }, 'accuracy': 100, } mock.register_uri( 'POST', requests_mock.ANY, json=response_location) query = self.model_query(cells=cells, wifis=wifis) res = self._call(body=query, ip=self.test_ip) send_json = mock.request_history[0].json() self.assertEqual(len(send_json['cellTowers']), 2) self.assertEqual(len(send_json['wifiAccessPoints']), 3) self.check_model_response(res, None, lat=1.0, lon=1.0, accuracy=100) self.check_stats( timer=[self.metric_url], counter=[self.metric + '.api_key.test', self.metric + '.fallback_hit', self.metric_url + '.200', self.metric + '.api_log.test.fallback_hit'], )
def test_medium_miss_low(self): cells = CellFactory.build_batch(1) self._make_query(self._make_result(accuracy=50000.1), cell=cells) self.check_stats(counter=[ ('locate.result', ['key:key', 'region:none', 'fallback_allowed:false', 'accuracy:medium', 'status:miss']), ])
def test_mixed_cell_wifi(self): cells = CellFactory.build_batch(1) wifis = WifiShardFactory.build_batch(2) query = Query( cell=self.cell_model_query(cells), wifi=self.wifi_model_query(wifis)) self.assertEqual(query.expected_accuracy, DataAccuracy.high)
def test_mixed_hit(self): cells = CellFactory.build_batch(2) self._make_query( self._make_result(accuracy=500.0), cell=cells, ip=self.london_ip) self.check_stats(counter=[ ('locate.result', ['key:key', 'country:GB', 'accuracy:medium', 'status:hit']), ])
def test_one(self): cells = CellFactory.build_batch(1) wifis = WifiShardFactory.build_batch(1) self._make_query(cell=cells, wifi=wifis, ip=self.london_ip) self.check_stats(total=1, counter=[ ('locate.query', ['key:key', 'country:GB', 'cell:one', 'wifi:one']), ])
def test_many(self): cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(3) self._make_query(cell=cells, wifi=wifis, ip=self.london_ip) self.check_stats(total=1, counter=[ ('locate.query', ['key:key', 'region:GB', 'cell:many', 'wifi:many']), ])
def add_reports(self, num=1, blue_factor=0, cell_factor=1, wifi_factor=2, api_key='test', email=None, ip=None, nickname=None, blue_key=None, cell_mcc=None, wifi_key=None, lat=None, lon=None): reports = [] for i in range(num): pos = CellFactory.build() report = { 'timestamp': time.time() * 1000.0, 'position': {}, 'bluetoothBeacons': [], 'cellTowers': [], 'wifiAccessPoints': [], } report['position']['latitude'] = lat or pos.lat report['position']['longitude'] = lon or pos.lon report['position']['accuracy'] = 17.0 + i blues = WifiShardFactory.build_batch(blue_factor, lat=pos.lat, lon=pos.lon) for blue in blues: blue_data = { 'macAddress': blue_key or blue.mac, 'signalStrength': -100 + i, } report['bluetoothBeacons'].append(blue_data) cells = CellFactory.build_batch(cell_factor, lat=pos.lat, lon=pos.lon) for cell in cells: cell_data = { 'radioType': cell.radio.name, 'mobileCountryCode': cell_mcc or cell.mcc, 'mobileNetworkCode': cell.mnc, 'locationAreaCode': cell.lac, 'cellId': cell.cid, 'primaryScramblingCode': cell.psc, 'signalStrength': -110 + i, } report['cellTowers'].append(cell_data) wifis = WifiShardFactory.build_batch(wifi_factor, lat=pos.lat, lon=pos.lon) for wifi in wifis: wifi_data = { 'macAddress': wifi_key or wifi.mac, 'signalStrength': -90 + i, 'ssid': 'my-wifi', } report['wifiAccessPoints'].append(wifi_data) reports.append(report) queue_reports.delay(reports=reports, api_key=api_key, email=email, ip=ip, nickname=nickname).get() return reports
def test_apikey_error(self, db_errors=0): cells = CellFactory.build_batch(2) wifis = WifiShardFactory.build_batch(2) self.session.execute(text('drop table %s;' % ApiKey.__tablename__)) query = self.model_query(cells=cells, wifis=wifis) res = self._call(body=query, ip=self.test_ip) self.check_response(res, 'ok') self.check_raven([('ProgrammingError', db_errors)])
def test_get_mixed(self): cells = CellFactory.build_batch(1) wifis = WifiShardFactory.build_batch(2) query = Query( cell=self.cell_model_query(cells), wifi=self.wifi_model_query(wifis)) self.assertEqual(self.cache.get(query), None) self.check_stats(counter=[ ('locate.fallback.cache', 1, 1, ['status:bypassed']), ])
def test_no_db_query_for_incomplete_keys(self): cells = CellFactory.build_batch(4) cells[0].radio = None cells[1].mcc = None cells[2].mnc = None cells[3].lac = None with self.db_call_checker() as check_db_calls: query = self.model_query(cells=cells) location = self.provider.locate(query) self.check_model_location(location, None) check_db_calls(rw=0, ro=0)
def test_database_error(self, db_errors=0): for tablename in ('wifi', 'cell', 'cell_area', 'ocid_cell', 'ocid_cell_area'): self.session.execute(text('drop table %s;' % tablename)) cells = CellFactory.build_batch(2) wifis = WifiFactory.build_batch(2) query = self.model_query(cells=cells, wifis=wifis) res = self._call(body=query, ip=self.test_ip) self.check_response(res, 'ok') self.check_stats( timer=[self.metric_url], counter=[ self.metric_url + '.200', self.metric + '.geoip_hit', ], ) self.check_raven([('ProgrammingError', db_errors)])
def test_provider_should_not_locate_if_lacf_disabled(self): cells = CellFactory.build_batch(2) query = self.model_query(cells=cells, fallbacks={"lacf": False}) self.check_should_locate(query, False)