def test_no_api_key(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000, total_measures=9), Wifi(key="B2", lat=10010000, lon=10020000, total_measures=9), Wifi(key="C3", lat=10020000, lon=10040000, total_measures=9), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/search', { "wifi": [ { "key": "A1" }, { "key": "B2" }, { "key": "C3" }, { "key": "D4" }, ] }, status=200) self.assertEqual(res.json, {"status": "not_found"}) self.check_expected_heka_messages(counter=['search.no_api_key'])
def test_wifi_ignore_outlier(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=10010000, lon=10020000), Wifi(key="C3", lat=10020000, lon=10040000), Wifi(key="D4", lat=20000000, lon=20000000), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/search?key=test', { "wifi": [ { "key": "A1" }, { "key": "B2" }, { "key": "C3" }, { "key": "D4" }, ] }, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, { "status": "ok", "lat": 1.0010000, "lon": 1.0020000, "accuracy": 500 })
def test_wifi_not_closeby(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=10010000, lon=10020000), Wifi(key="C3", lat=20020000, lon=20040000), Wifi(key="D4", lat=20000000, lon=20000000), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/search?key=test', { "wifi": [ { "key": "A1" }, { "key": "B2" }, { "key": "C3" }, { "key": "D4" }, ] }, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"status": "not_found"})
def test_ok_wifi(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="a1", lat=10000000, lon=10000000), Wifi(key="b2", lat=10010000, lon=10020000), Wifi(key="c3", lat=10020000, lon=10040000), Wifi(key="d4", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json( '/v1/geolocate?key=test', { "wifiAccessPoints": [ {"macAddress": "a1"}, {"macAddress": "b2"}, {"macAddress": "c3"}, {"macAddress": "d4"}, ]}, status=200) self.check_expected_heka_messages(counter=['geolocate.api_key.test']) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"location": {"lat": 1.0010000, "lng": 1.0020000}, "accuracy": 500.0})
def test_no_api_key(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="a1", lat=10000000, lon=10000000, total_measures=9), Wifi(key="b2", lat=10010000, lon=10020000, total_measures=9), Wifi(key="c3", lat=10020000, lon=10040000, total_measures=9), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/geolocate', { "wifiAccessPoints": [ { "macAddress": "a1" }, { "macAddress": "b2" }, { "macAddress": "c3" }, ] }, status=400) json_err = json.loads(res.body) self.assertEqual(u'No API key', json_err['error']['message']) self.assertEqual(res.content_type, 'application/json') self.check_expected_heka_messages(counter=['geolocate.no_api_key'])
def test_remove_wifi(self): observations = [] wifi_keys = [] m1 = 1.0 m2 = 2.0 for key in ['a%s1234567890' % i for i in range(5)]: wifi = Wifi(key=key) wifi_keys.append(wifi.hashkey()) observations.append(wifi) observations.append(WifiObservation(lat=m1, lon=m1, key=key)) observations.append(WifiObservation(lat=m2, lon=m2, key=key)) self.session.add_all(observations) self.session.flush() result = remove_wifi.delay(wifi_keys[:2]) self.assertEqual(result.get(), 2) wifis = self.session.query(Wifi).all() self.assertEqual(len(wifis), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 0) wifis = self.session.query(Wifi).all() self.assertEqual(len(wifis), 0)
def install_fixtures(self): session = self.db.session() PARIS_LAT_DEG = from_degrees(PARIS_LAT) PARIS_LON_DEG = from_degrees(PARIS_LON) qry = session.query(Cell) if qry.count() > 0: session.query(Cell).delete() lat = from_degrees(PARIS_LAT) lon = from_degrees(PARIS_LON) key = dict(mcc=FRANCE_MCC, mnc=2, lac=3) data = [ Cell(lat=lat, lon=lon, radio=2, cid=4, **key), Cell(lat=lat + 20000, lon=lon + 40000, radio=2, cid=5, **key), ] session.add_all(data) if session.query(Wifi).count() > 0: session.query(Wifi).delete() wifis = [ Wifi(key="A1", lat=PARIS_LAT_DEG, lon=PARIS_LON_DEG), Wifi(key="B2", lat=PARIS_LAT_DEG, lon=PARIS_LON_DEG), Wifi(key="C3", lat=PARIS_LAT_DEG, lon=PARIS_LON_DEG), Wifi(key="D4", lat=None, lon=None), ] session.add_all(wifis) session.commit()
def test_wifi(self): session = self.db_slave_session wifis = [{'key': '001122334455'}, {'key': '112233445566'}] session.add(Wifi( key=wifis[0]['key'], lat=GB_LAT, lon=GB_LON, range=200)) session.add(Wifi( key=wifis[1]['key'], lat=GB_LAT, lon=GB_LON + 0.00001, range=300)) session.flush() result = locate.search_all_sources( session, 'm', {'wifi': wifis}, client_addr=GB_IP, geoip_db=self.geoip_db, api_key_log=True, api_key_name='test') self.assertEqual(result, {'lat': GB_LAT, 'lon': GB_LON + 0.000005, 'accuracy': WIFI_MIN_ACCURACY}) self.check_stats( counter=[ 'm.wifi_found', 'm.wifi_hit', 'm.api_log.test.wifi_hit', ], )
def test_wifi_prefer_larger_cluster_over_high_signal(self): session = self.db_slave_session wifis = [Wifi(key=("0%X" % i).lower() * 6, lat=1 + i * 0.000010, lon=1 + i * 0.000012) for i in range(1, 6)] wifis += [ Wifi(key="d4" * 6, lat=2.0, lon=2.0), Wifi(key="e5" * 6, lat=2.001, lon=2.002), Wifi(key="f6" * 6, lat=2.002, lon=2.004), ] session.add_all(wifis) session.flush() observations = [dict(key=("0%X" % i) * 6, signal=-80) for i in range(1, 6)] observations += [ dict(key="D4" * 6, signal=-75), dict(key="E5" * 6, signal=-74), dict(key="F6" * 6, signal=-73) ] random.shuffle(observations) result = self._make_query(data={'wifi': observations}) self.assertEqual(result, {'lat': 1.00003, 'lon': 1.000036, 'accuracy': WIFI_MIN_ACCURACY})
def remove(self, wifi_keys): # BBB this might still get namedtuples encoded as a dicts for # one release, afterwards it'll get wifi hashkeys keys = [Wifi.to_hashkey(key=wifi['key']) for wifi in wifi_keys] query = Wifi.querykeys(self.session, keys) length = query.delete(synchronize_session=False) return length
def test_wifi_find_sparse_high_signal_cluster(self): app = self.app session = self.db_slave_session wifis = [Wifi(key="A%d" % i, lat=10000000 + i * 100, lon=10000000 + i * 120) for i in range(0, 100)] wifis += [ Wifi(key="D4", lat=20000000, lon=20000000), Wifi(key="E5", lat=20010000, lon=20020000), Wifi(key="F6", lat=20020000, lon=20040000), ] session.add_all(wifis) session.commit() measures = [dict(key="A%d" % i, signal=-80) for i in range(0, 100)] measures += [ dict(key="D4", signal=-75), dict(key="E5", signal=-74), dict(key="F6", signal=-73) ] random.shuffle(measures) res = app.post_json('/v1/search?key=test', {"wifi": measures}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"status": "ok", "lat": 2.0010000, "lon": 2.0020000, "accuracy": 500})
def test_remove_wifi(self): observations = [] wifi_keys = [] m1 = 1.0 m2 = 2.0 for key in ['a%s1234567890' % i for i in range(5)]: wifi = Wifi(key=key) self.session.add(wifi) wifi_keys.append(wifi.hashkey()) observations.append(WifiObservation(lat=m1, lon=m1, key=key)) observations.append(WifiObservation(lat=m2, lon=m2, key=key)) self.data_queue.enqueue(observations) self.session.flush() result = remove_wifi.delay(wifi_keys[:2]) self.assertEqual(result.get(), 2) wifis = self.session.query(Wifi).all() self.assertEqual(len(wifis), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 0) wifis = self.session.query(Wifi).all() self.assertEqual(len(wifis), 0)
def test_wifi_prefer_larger_cluster_over_high_signal(self): session = self.db_slave_session wifis = [Wifi(key=("0%X" % i).lower() * 6, lat=1 + i * 0.000010, lon=1 + i * 0.000012) for i in range(1, 6)] wifis += [ Wifi(key="d4" * 6, lat=2.0, lon=2.0), Wifi(key="e5" * 6, lat=2.001, lon=2.002), Wifi(key="f6" * 6, lat=2.002, lon=2.004), ] session.add_all(wifis) session.flush() measures = [dict(key=("0%X" % i) * 6, signal=-80) for i in range(1, 6)] measures += [ dict(key="D4" * 6, signal=-75), dict(key="E5" * 6, signal=-74), dict(key="F6" * 6, signal=-73) ] random.shuffle(measures) result = locate.search_all_sources( session, 'm', {'wifi': measures}) self.assertEqual(result, {'lat': 1.00003, 'lon': 1.000036, 'accuracy': WIFI_MIN_ACCURACY})
def test_ok_wifi(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=10010000, lon=10020000), Wifi(key="C3", lat=10020000, lon=10040000), Wifi(key="D4", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/search?key=test', {"wifi": [ {"key": "A1"}, {"key": "B2"}, {"key": "C3"}, {"key": "D4"}, ]}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"status": "ok", "lat": 1.0010000, "lon": 1.0020000, "accuracy": 500}) self.check_expected_heka_messages( total=4, timer=[('http.request', {'url_path': '/v1/search'})], counter=[('search.api_key.test', 1), ('search.wifi_hit', 1), ('http.request', 1)] )
def test_wifi_prefer_larger_cluster_over_high_signal(self): wifis = [Wifi(key=('0%X' % i).lower() * 6, lat=1 + i * 0.000010, lon=1 + i * 0.000012) for i in range(1, 6)] wifis += [ Wifi(key='d4' * 6, lat=2.0, lon=2.0), Wifi(key='e5' * 6, lat=2.001, lon=2.002), Wifi(key='f6' * 6, lat=2.002, lon=2.004), ] self.session.add_all(wifis) self.session.flush() observations = [dict(key=('0%X' % i) * 6, signal=-80) for i in range(1, 6)] observations += [ dict(key='D4' * 6, signal=-75), dict(key='E5' * 6, signal=-74), dict(key='F6' * 6, signal=-73) ] random.shuffle(observations) location = self.provider.locate({'wifi': observations}) self.assertEqual(location.lat, 1.00003) self.assertEqual(location.lon, 1.000036) self.assertEqual(location.accuracy, WIFI_MIN_ACCURACY)
def test_wifi_too_few_matches(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json( '/v1/search', {"wifi": [ { "key": "A1" }, { "key": "B2" }, { "key": "C3" }, ]}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.body, '{"status": "not_found"}')
def test_ok_wifi(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="a1", lat=10000000, lon=10000000), Wifi(key="b2", lat=10020000, lon=10040000), Wifi(key="c3", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/geolocate', { "wifiAccessPoints": [ { "macAddress": "a1" }, { "macAddress": "b2" }, { "macAddress": "c3" }, ] }, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual( res.body, '{"location": {"lat": 1.0010000, ' '"lng": 1.0020000}, "accuracy": 500.0}')
def test_ok_wifi(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=10020000, lon=10040000), Wifi(key="C3", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json( '/v1/search', {"wifi": [ { "key": "A1" }, { "key": "B2" }, { "key": "C3" }, ]}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual( res.body, '{"status": "ok", "lat": 1.0010000, ' '"lon": 1.0020000, "accuracy": 500}')
def test_ok_wifi(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="101010101010", lat=1.0, lon=1.0), Wifi(key="202020202020", lat=1.001, lon=1.002), Wifi(key="303030303030", lat=1.002, lon=1.004), Wifi(key="404040404040", lat=None, lon=None), ] session.add_all(wifis) session.commit() res = app.post_json( '%s?key=test' % self.url, { "wifiAccessPoints": [ {"macAddress": "101010101010"}, {"macAddress": "202020202020"}, {"macAddress": "303030303030"}, {"macAddress": "404040404040"}, ]}, status=200) self.check_stats( counter=[self.metric + '.api_key.test', self.metric + '.api_log.test.wifi_hit']) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"location": {"lat": 1.001, "lng": 1.002}, "accuracy": 248.6090897})
def test_wifi(self): session = self.db_slave_session london = self.geoip_data['London'] wifis = [{'key': '001122334455'}, {'key': '112233445566'}] session.add(Wifi( key=wifis[0]['key'], lat=GB_LAT, lon=GB_LON, range=200)) session.add(Wifi( key=wifis[1]['key'], lat=GB_LAT, lon=GB_LON + 0.00001, range=300)) session.flush() result = self._make_query( data={'wifi': wifis}, client_addr=london['ip'], api_key_log=True) self.assertEqual(result, {'lat': GB_LAT, 'lon': GB_LON + 0.000005, 'accuracy': WIFI_MIN_ACCURACY}) self.check_stats( counter=[ 'm.wifi_hit', 'm.api_log.test.wifi_hit', ], timer=[ 'm.wifi.provided', ], )
def test_wifi_max_min_range_update(self): from ichnaea.tasks import wifi_location_update session = self.db_master_session k1 = "ab1234567890" k2 = "cd1234567890" data = [ Wifi(key=k1, new_measures=2, total_measures=2), WifiMeasure(lat=10000000, lon=10000000, key=k1), WifiMeasure(lat=10020000, lon=10040000, key=k1), Wifi(key=k2, lat=20000000, lon=-20000000, max_lat=20010000, min_lat=19990000, max_lon=-19990000, min_lon=-20010000, new_measures=2, total_measures=4), WifiMeasure(lat=20020000, lon=-20040000, key=k2), WifiMeasure(lat=19980000, lon=-19960000, key=k2), ] session.add_all(data) session.commit() result = wifi_location_update.delay(min_new=1) self.assertEqual(result.get(), (2, 0)) wifis = dict(session.query(Wifi.key, Wifi).all()) self.assertEqual(set(wifis.keys()), set([k1, k2])) self.assertEqual(wifis[k1].lat, 10010000) self.assertEqual(wifis[k1].max_lat, 10020000) self.assertEqual(wifis[k1].min_lat, 10000000) self.assertEqual(wifis[k1].lon, 10020000) self.assertEqual(wifis[k1].max_lon, 10040000) self.assertEqual(wifis[k1].min_lon, 10000000) self.assertEqual(wifis[k2].lat, 20000000) self.assertEqual(wifis[k2].max_lat, 20020000) self.assertEqual(wifis[k2].min_lat, 19980000) self.assertEqual(wifis[k2].lon, -20000000) self.assertEqual(wifis[k2].max_lon, -19960000) self.assertEqual(wifis[k2].min_lon, -20040000) # independent calculation: the k1 bounding box is # (1.000, 1.000) to (1.002, 1.004), with centroid # at (1.001, 1.002); worst distance from centroid # to any corner is 249m self.assertEqual(wifis[k1].range, 249) # independent calculation: the k2 bounding box is # (1.998, -2.004) to (2.002, -1.996), with centroid # at (2.000, 2.000); worst distance from centroid # to any corner is 497m self.assertEqual(wifis[k2].range, 497)
def _query_database(self, wifi_keys): try: load_fields = ('key', 'lat', 'lon', 'range') wifi_iter = Wifi.iterkeys( self.session_db, [Wifi.to_hashkey(key=key) for key in wifi_keys], extra=lambda query: query.options(load_only(*load_fields)) .filter(Wifi.lat.isnot(None)) .filter(Wifi.lon.isnot(None))) return list(wifi_iter) except Exception: self.raven_client.captureException() return []
def _query_database(self, wifi_keys): queried_wifis = [] if len(wifi_keys) >= MIN_WIFIS_IN_QUERY: keys = [Wifi.to_hashkey(key=key) for key in wifi_keys] try: load_fields = ('key', 'lat', 'lon', 'range') query = (Wifi.querykeys(self.session_db, keys).options( load_only(*load_fields)).filter( Wifi.lat.isnot(None)).filter(Wifi.lon.isnot(None))) queried_wifis = query.all() except Exception: self.raven_client.captureException() return queried_wifis
def test_wifi_not_closeby(self): wifis = [ Wifi(key='101010101010', lat=1.0, lon=1.0), Wifi(key='202020202020', lat=1.001, lon=1.002), Wifi(key='303030303030', lat=2.002, lon=2.004), Wifi(key='404040404040', lat=2.0, lon=2.0), ] self.session.add_all(wifis) self.session.flush() location = self.provider.locate( {'wifi': [{'key': '101010101010'}, {'key': '303030303030'}]}) self.assertFalse(location.found()) self.assertTrue(location.query_data)
def test_unique_wifi_histogram(self): from ichnaea.content.tasks import unique_wifi_histogram session = self.db_master_session today = datetime.utcnow().date() yesterday = (today - timedelta(1)) two_days = (today - timedelta(2)) long_ago = (today - timedelta(3)) k1 = "ab1234567890" k2 = "bc1234567890" k3 = "cd1234567890" k4 = "de1234567890" k5 = "ef1234567890" wifis = [ Wifi(created=long_ago, key=k1), Wifi(created=two_days, key=k2), Wifi(created=yesterday, key=k3), Wifi(created=yesterday, key=k4), Wifi(created=today, key=k5), ] session.add_all(wifis) session.commit() result = unique_wifi_histogram.delay(ago=3) added = result.get() self.assertEqual(added, 1) stats = session.query(Stat).order_by(Stat.time).all() self.assertEqual(len(stats), 1) self.assertEqual(stats[0].key, STAT_TYPE['unique_wifi']) self.assertEqual(stats[0].time, long_ago) self.assertEqual(stats[0].value, 1) # fill up newer dates unique_wifi_histogram.delay(ago=2).get() unique_wifi_histogram.delay(ago=1).get() unique_wifi_histogram.delay(ago=0).get() # test duplicate execution unique_wifi_histogram.delay(ago=1).get() stats = session.query(Stat).order_by(Stat.time).all() self.assertEqual(len(stats), 4) self.assertEqual(stats[0].time, long_ago) self.assertEqual(stats[0].value, 1) self.assertEqual(stats[1].time, two_days) self.assertEqual(stats[1].value, 2) self.assertEqual(stats[2].time, yesterday) self.assertEqual(stats[2].value, 4) self.assertEqual(stats[3].time, today) self.assertEqual(stats[3].value, 5)
def _query_database(self, wifi_keys): queried_wifis = [] if len(wifi_keys) >= MIN_WIFIS_IN_QUERY: keys = [Wifi.to_hashkey(key=key) for key in wifi_keys] try: load_fields = ('key', 'lat', 'lon', 'range') query = (Wifi.querykeys(self.session_db, keys) .options(load_only(*load_fields)) .filter(Wifi.lat.isnot(None)) .filter(Wifi.lon.isnot(None))) queried_wifis = query.all() except Exception: self.raven_client.captureException() return queried_wifis
def test_wifi_too_few_candidates(self): app = self.app session = self.db_slave_session wifis = [ Wifi(key="A1", lat=10000000, lon=10000000), Wifi(key="B2", lat=10010000, lon=10020000), ] session.add_all(wifis) session.commit() res = app.post_json('/v1/search?key=test', {"wifi": [ {"key": "A1"}, {"key": "B2"}, ]}, status=200) self.assertEqual(res.json, {"status": "not_found"})
def test_remove_wifi(self): from ichnaea.tasks import remove_wifi session = self.db_master_session measures = [] wifi_keys = ["a%s1234567890" % i for i in range(5)] m1 = 10000000 m2 = 10000000 for key in wifi_keys: measures.append(Wifi(key=key)) measures.append(WifiMeasure(lat=m1, lon=m1, key=key)) measures.append(WifiMeasure(lat=m2, lon=m2, key=key)) session.add_all(measures) session.flush() result = remove_wifi.delay(wifi_keys[:2]) self.assertEqual(result.get(), 2) wifis = session.query(Wifi).all() self.assertEqual(len(wifis), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 0) wifis = session.query(Wifi).all() self.assertEqual(len(wifis), 0)
def test_remove_wifi(self): session = self.db_master_session measures = [] wifi_keys = [{'key': "a%s1234567890" % i} for i in range(5)] m1 = 1.0 m2 = 2.0 for key in wifi_keys: key = key['key'] measures.append(Wifi(key=key)) measures.append(WifiMeasure(lat=m1, lon=m1, key=key)) measures.append(WifiMeasure(lat=m2, lon=m2, key=key)) session.add_all(measures) session.flush() result = remove_wifi.delay(wifi_keys[:2]) self.assertEqual(result.get(), 2) wifis = session.query(Wifi).all() self.assertEqual(len(wifis), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 3) result = remove_wifi.delay(wifi_keys) self.assertEqual(result.get(), 0) wifis = session.query(Wifi).all() self.assertEqual(len(wifis), 0)
def test_wifi_not_found_cell_fallback(self): app = self.app session = self.db_slave_session key = dict(mcc=1, mnc=2, lac=3) data = [ Wifi(key="abcd", lat=30000000, lon=30000000), Cell(lat=10000000, lon=10000000, radio=2, cid=4, **key), Cell(lat=10020000, lon=10040000, radio=2, cid=5, **key), ] session.add_all(data) session.commit() res = app.post_json( '/v1/search?key=test', {"radio": "gsm", "cell": [ dict(radio="umts", cid=4, **key), dict(radio="umts", cid=5, **key), ], "wifi": [ {"key": "abcd"}, {"key": "cdef"}, ]}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"status": "ok", "lat": 1.0010000, "lon": 1.0020000, "accuracy": 35000})
def test_wifi_only_use_top_three_signals_in_noisy_cluster(self): app = self.app session = self.db_slave_session # all these should wind up in the same cluster since # clustering threshold is 500m and the 100 wifis are # spaced in increments of (+1m, +1.2m) wifis = [Wifi(key="A%d" % i, lat=10000000 + i * 100, lon=10000000 + i * 120) for i in range(0, 100)] session.add_all(wifis) session.commit() measures = [dict(key="A%d" % i, signal=-80) for i in range(3, 100)] measures += [ dict(key="A0", signal=-75), dict(key="A1", signal=-74), dict(key="A2", signal=-73) ] random.shuffle(measures) res = app.post_json('/v1/search?key=test', {"wifi": measures}, status=200) self.assertEqual(res.content_type, 'application/json') self.assertEqual(res.json, {"status": "ok", "lat": 1.0000100, "lon": 1.0000120, "accuracy": 500})
def test_wifi_too_few_candidates(self): wifis = [ Wifi(key='001122334455', lat=1.0, lon=1.0), Wifi(key='112233445566', lat=1.001, lon=1.002), ] self.session.add_all(wifis) self.session.flush() location = self.provider.locate({'wifi': [{'key': '001122334455'}]}) self.assertFalse(location.found()) self.assertFalse(location.query_data) self.check_stats( counter=[ 'm.wifi.provided_too_few', ], )
def test_wifi_only_use_top_five_signals_in_noisy_cluster(self): # all these should wind up in the same cluster since # clustering threshold is 500m and the 10 wifis are # spaced in increments of (+1m, +1.2m) wifis = [Wifi(key=('0%X'.lower() % i) * 6, lat=1 + i * 0.000010, lon=1 + i * 0.000012) for i in range(1, 11)] self.session.add_all(wifis) self.session.commit() observations = [dict(key=('0%X' % i) * 6, signal=-80) for i in range(6, 11)] observations += [ dict(key='010101010101', signal=-75), dict(key='020202020202', signal=-74), dict(key='030303030303', signal=-73), dict(key='040404040404', signal=-72), dict(key='050505050505', signal=-71), ] random.shuffle(observations) location = self.provider.locate({'wifi': observations}) self.assertEqual(location.lat, 1.00003) self.assertEqual(location.lon, 1.000036) self.assertEqual(location.accuracy, WIFI_MIN_ACCURACY)
def remove(self, wifi_keys): query = Wifi.querykeys(self.session, wifi_keys) length = query.delete(synchronize_session=False) return length