def test_set_wifi_inconsistent(self, cache, stats): wifis1 = WifiShardFactory.build_batch(2) cache.set(self._query(wifi=self.wifi_model_query(wifis1)), ExternalResult(wifis1[0].lat, wifis1[0].lon, 100, None)) # similar lat/lon, worse accuracy wifis2 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 0.0001, lon=wifis1[0].lon) cache.set(self._query(wifi=self.wifi_model_query(wifis2)), ExternalResult(wifis2[0].lat, wifis2[0].lon, 200, None)) # check combined query, avg lat/lon, max accuracy query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2)) cached = cache.get(query) assert cached[0] == (wifis1[0].lat + wifis2[0].lat) / 2.0 assert cached[1] == wifis1[0].lon assert round(cached[2], 2) == 205.56 assert cached[3] is None # different lat/lon wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0) cache.set(self._query(wifi=self.wifi_model_query(wifis3)), ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None)) # check combined query, inconsistent result query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2 + wifis3)) assert cache.get(query) is None stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ('locate.fallback.cache', 1, 1, ['status:inconsistent']), ])
def test_set_wifi_inconsistent(self): wifis1 = WifiShardFactory.build_batch(2) self.cache.set(self._query(wifi=self.wifi_model_query(wifis1)), ExternalResult(wifis1[0].lat, wifis1[0].lon, 100, None)) # similar lat/lon, worse accuracy wifis2 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 0.0001, lon=wifis1[0].lon) self.cache.set(self._query(wifi=self.wifi_model_query(wifis2)), ExternalResult(wifis2[0].lat, wifis2[0].lon, 200, None)) # check combined query, avg lat/lon, max accuracy query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2)) cached = self.cache.get(query) self.assertAlmostEqual(cached[0], (wifis1[0].lat + wifis2[0].lat) / 2) self.assertAlmostEqual(cached[1], wifis1[0].lon) self.assertAlmostEqual(cached[2], 205.56, 2) self.assertTrue(cached[3] is None) # different lat/lon wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0) self.cache.set(self._query(wifi=self.wifi_model_query(wifis3)), ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None)) # check combined query, inconsistent result query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2 + wifis3)) self.assertEqual(self.cache.get(query), None) self.check_stats(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ('locate.fallback.cache', 1, 1, ['status:inconsistent']), ])
def test_set_wifi_inconsistent(self, cache, metricsmock): wifis1 = WifiShardFactory.build_batch(2) cache.set( self._query(wifi=self.wifi_model_query(wifis1)), ExternalResult(wifis1[0].lat, wifis1[0].lon, 100, None), ) # similar lat/lon, worse accuracy wifis2 = WifiShardFactory.build_batch( 2, lat=wifis1[0].lat + 0.0001, lon=wifis1[0].lon ) cache.set( self._query(wifi=self.wifi_model_query(wifis2)), ExternalResult(wifis2[0].lat, wifis2[0].lon, 200, None), ) # check combined query, avg lat/lon, max accuracy query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2)) cached = cache.get(query) assert cached[0] == (wifis1[0].lat + wifis2[0].lat) / 2.0 assert cached[1] == wifis1[0].lon assert round(cached[2], 2) == 205.56 assert cached[3] is None # different lat/lon wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0) cache.set( self._query(wifi=self.wifi_model_query(wifis3)), ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None), ) # check combined query, inconsistent result query = self._query(wifi=self.wifi_model_query(wifis1 + wifis2 + wifis3)) assert cache.get(query) is None assert ( len( metricsmock.filter_records( "incr", "locate.fallback.cache", value=1, tags=[self.fallback_tag, "status:hit"], ) ) == 1 ) assert ( len( metricsmock.filter_records( "incr", "locate.fallback.cache", value=1, tags=[self.fallback_tag, "status:inconsistent"], ) ) == 1 )
def test_set_blue(self, cache, metricsmock): blues = BlueShardFactory.build_batch(2) blue = blues[0] query = self._query(blue=self.blue_model_query(blues)) result = ExternalResult(blue.lat, blue.lon, blue.radius, None) cache.set(query, result) assert cache.get(query) == result metricsmock.assert_incr_once("locate.fallback.cache", tags=[self.fallback_tag, "status:hit"])
def test_set_wifi(self, cache, metricsmock): wifis = WifiShardFactory.build_batch(2) wifi = wifis[0] query = self._query(wifi=self.wifi_model_query(wifis)) result = ExternalResult(wifi.lat, wifi.lon, wifi.radius, None) cache.set(query, result) assert cache.get(query) == result metricsmock.assert_incr_once("locate.fallback.cache", tags=[self.fallback_tag, "status:hit"])
def test_set_blue(self, cache, stats): blues = BlueShardFactory.build_batch(2) blue = blues[0] query = self._query(blue=self.blue_model_query(blues)) result = ExternalResult(blue.lat, blue.lon, blue.radius, None) cache.set(query, result) assert cache.get(query) == result stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_wifi(self, cache, stats): wifis = WifiShardFactory.build_batch(2) wifi = wifis[0] query = self._query(wifi=self.wifi_model_query(wifis)) result = ExternalResult(wifi.lat, wifi.lon, wifi.radius, None) cache.set(query, result) assert cache.get(query) == result stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_cell_not_found(self, cache, redis, metricsmock): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(None, None, None, None) cache.set(query, result) keys = redis.keys("cache:fallback:ichnaea:v1:1:cell:*") assert len(keys) == 1 assert redis.get(keys[0]) == b'"404"' assert cache.get(query) == result metricsmock.assert_incr_once("locate.fallback.cache", tags=[self.fallback_tag, "status:hit"])
def test_set_cell(self, cache, redis, metricsmock): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(cell.lat, cell.lon, cell.radius, None) cache.set(query, result, expire=60) keys = redis.keys("cache:fallback:ichnaea:v1:1:cell:*") assert len(keys) == 1 assert 50 < redis.ttl(keys[0]) <= 60 assert cache.get(query) == result metricsmock.assert_incr_once("locate.fallback.cache", tags=[self.fallback_tag, "status:hit"])
def test_set_cell(self, cache, redis, stats): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(cell.lat, cell.lon, cell.radius, None) cache.set(query, result, expire=60) keys = redis.keys('cache:fallback:cell:*') assert len(keys) == 1 assert 50 < redis.ttl(keys[0]) <= 60 assert cache.get(query) == result stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_cell_not_found(self, cache, redis, stats): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(None, None, None, None) cache.set(query, result) keys = redis.keys('cache:fallback:cell:*') assert len(keys) == 1 assert redis.get(keys[0]) == b'"404"' assert cache.get(query) == result stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_cell_unwiredlabs(self, unwiredlabs_cache, redis, metricsmock): cell = CellShardFactory.build() query = self._query(api_key=UNWIREDLABS_KEY, cell=self.cell_model_query([cell])) result = ExternalResult(cell.lat, cell.lon, cell.radius, None) unwiredlabs_cache.set(query, result, expire=60) keys = redis.keys("cache:fallback:unwiredlabs:v1:1:cell:*") assert len(keys) == 1 assert 50 < redis.ttl(keys[0]) <= 60 assert unwiredlabs_cache.get(query) == result metricsmock.assert_incr_once("locate.fallback.cache", tags=["fallback_name:labs", "status:hit"])
def test_set_cell(self): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(cell.lat, cell.lon, cell.radius, None) self.cache.set(query, result, expire=60) keys = self.redis_client.keys('cache:fallback:cell:*') self.assertEqual(len(keys), 1) self.assertTrue(50 < self.redis_client.ttl(keys[0]) <= 60) self.assertEqual(self.cache.get(query), result) self.check_stats(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_cell_not_found(self): cell = CellShardFactory.build() query = self._query(cell=self.cell_model_query([cell])) result = ExternalResult(None, None, None, None) self.cache.set(query, result) keys = self.redis_client.keys('cache:fallback:cell:*') self.assertEqual(len(keys), 1) self.assertEqual(self.redis_client.get(keys[0]), b'"404"') self.assertEqual(self.cache.get(query), result) self.check_stats(counter=[ ('locate.fallback.cache', 1, 1, ['status:hit']), ])
def test_set_cell_unwiredlabs(self, unwiredlabs_cache, redis, stats): cell = CellShardFactory.build() query = self._query(api_key=UNWIREDLABS_KEY, cell=self.cell_model_query([cell])) result = ExternalResult(cell.lat, cell.lon, cell.radius, None) unwiredlabs_cache.set(query, result, expire=60) keys = redis.keys('cache:fallback:unwiredlabs:v1:1:cell:*') assert len(keys) == 1 assert 50 < redis.ttl(keys[0]) <= 60 assert unwiredlabs_cache.get(query) == result stats.check(counter=[ ('locate.fallback.cache', 1, 1, ['fallback_name:labs', 'status:hit']), ])
def test_set_blue(self, cache, metricsmock): blues = BlueShardFactory.build_batch(2) blue = blues[0] query = self._query(blue=self.blue_model_query(blues)) result = ExternalResult(blue.lat, blue.lon, blue.radius, None) cache.set(query, result) assert cache.get(query) == result assert ( len( metricsmock.filter_records( "incr", "locate.fallback.cache", value=1, tags=[self.fallback_tag, "status:hit"], ) ) == 1 )
def test_set_wifi(self, cache, metricsmock): wifis = WifiShardFactory.build_batch(2) wifi = wifis[0] query = self._query(wifi=self.wifi_model_query(wifis)) result = ExternalResult(wifi.lat, wifi.lon, wifi.radius, None) cache.set(query, result) assert cache.get(query) == result assert ( len( metricsmock.filter_records( "incr", "locate.fallback.cache", value=1, tags=[self.fallback_tag, "status:hit"], ) ) == 1 )
def test_not_found_accuracy(self): result = ExternalResult(1.0, 1.0, None, None) assert result.not_found()
def test_not_found(self): result = ExternalResult(None, None, None, None) assert result.not_found()
def test_score(self): result = ExternalResult(1.0, 1.0, 10, None) assert result.score == 10.0
def test_found(self): result = ExternalResult(1.0, 1.0, 10, None) self.assertFalse(result.not_found())
def test_score(self): result = ExternalResult(1.0, 1.0, 10, None) self.assertAlmostEqual(result.score, 10.0)
def test_score_fallback(self): result = ExternalResult(1.0, 1.0, 10, 'lacf') self.assertAlmostEqual(result.score, 5.0)
def test_found_fallback(self): result = ExternalResult(1.0, 1.0, 10, 'lacf') self.assertFalse(result.not_found())
def test_not_found(self): result = ExternalResult(None, None, None, None) self.assertTrue(result.not_found())
def test_not_found_accuracy(self): result = ExternalResult(1.0, 1.0, None, None) self.assertTrue(result.not_found())
def test_found(self): result = ExternalResult(1.0, 1.0, 10, None) assert not result.not_found()
def test_found_fallback(self): result = ExternalResult(1.0, 1.0, 10, 'lacf') assert not result.not_found()
def test_score_fallback(self): result = ExternalResult(1.0, 1.0, 10, 'lacf') assert result.score == 5.0