Exemplo n.º 1
0
    def test_set_wifi_inconsistent(self):
        wifis1 = WifiShardFactory.build_batch(2)
        self.cache.set(
            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(
            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 = Query(wifi=self.wifi_model_query(wifis1 + wifis2))
        self.assertEqual(
            self.cache.get(query),
            ((wifis1[0].lat + wifis2[0].lat) / 2, wifis1[0].lon, 200, None))

        # different lat/lon
        wifis3 = WifiShardFactory.build_batch(2, lat=wifis1[0].lat + 10.0)
        self.cache.set(
            Query(wifi=self.wifi_model_query(wifis3)),
            ExternalResult(wifis3[0].lat, wifis3[0].lon, 300, None))

        # check combined query, inconsistent result
        query = 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']),
        ])
Exemplo n.º 2
0
    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']),
        ])
Exemplo n.º 3
0
    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']),
        ])
Exemplo n.º 4
0
    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
        )
Exemplo n.º 5
0
    def add_reports(self, num=1, blue_factor=0, cell_factor=1, wifi_factor=2,
                    api_key='test', nickname=None,
                    blue_key=None, cell_mcc=None, wifi_key=None,
                    lat=None, lon=None):
        reports = []
        for i in range(num):
            pos = CellShardFactory.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 = CellShardFactory.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,
                            nickname=nickname).get()
        return reports
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[{
         'macAddress': wifi.mac,
         'channel': 3,
         'frequency': 2412,
         'signalStrength': -70,
         'signalToNoiseRatio': 13,
         'ssid': 'wifi'
     } for wifi in wifis])
     data = self._call(query.json())
     assert (data == {
         'wifi': [{
             'bssid': wifis[0].mac,
             'channel': 3,
             'frequency': 2412,
             'signal': -70,
             'signalToNoiseRatio': 13,
         }, {
             'bssid': wifis[1].mac,
             'channel': 3,
             'frequency': 2412,
             'signal': -70,
             'signalToNoiseRatio': 13,
         }],
         'fallbacks': {
             'lacf': True
         },
         'token':
         None,
     })
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[{
         'macAddress': wifi.mac,
         'age': 2000,
         'channel': 11,
         'signalStrength': -90,
         'signalToNoiseRatio': 13
     } for wifi in wifis])
     data = self._call(query.json())
     assert (data == {
         'wifiAccessPoints': [{
             'macAddress': wifis[0].mac,
             'age': 2000,
             'channel': 11,
             'signalStrength': -90,
             'signalToNoiseRatio': 13,
         }, {
             'macAddress': wifis[1].mac,
             'age': 2000,
             'channel': 11,
             'signalStrength': -90,
             'signalToNoiseRatio': 13,
         }],
         'considerIp':
         False,
     })
Exemplo n.º 8
0
    def test_mixed_cell_wifi(self):
        cells = CellShardFactory.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)
Exemplo n.º 9
0
 def test_high_hit(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(self._make_result(accuracy=1000.0), wifi=wifis)
     self.check_stats(counter=[
         ('locate.result',
             ['key:key', 'country:none', 'accuracy:high', 'status:hit']),
     ])
Exemplo n.º 10
0
    def test_check_already_good_result(self):
        wifis = WifiShardFactory.build_batch(2)
        internal_pos = Position(
            source=DataSource.internal, lat=1.0, lon=1.0, accuracy=1.0)

        query = self.model_query(wifis=wifis)
        self.check_should_search(query, False, results=[internal_pos])
Exemplo n.º 11
0
 def test_high_hit(self, geoip_db, metricsmock):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(geoip_db, self._make_result(accuracy=500.0), wifi=wifis)
     metricsmock.assert_incr_once(
         "locate.result",
         tags=["key:test", "fallback_allowed:false", "accuracy:high", "status:hit"],
     )
Exemplo n.º 12
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(
         wifi=[
             {
                 "macAddress": wifi.mac,
                 "age": 2000,
                 "signalStrength": -90,
                 "ssid": "wifi",
             }
             for wifi in wifis
         ]
     )
     data = self._call(query.json())
     assert data == {
         "wifiAccessPoints": [
             {
                 "macAddress": wifis[0].mac,
                 "age": 2000,
                 "signalStrength": -90,
                 "ssid": "wifi",
             },
             {
                 "macAddress": wifis[1].mac,
                 "age": 2000,
                 "signalStrength": -90,
                 "ssid": "wifi",
             },
         ],
         "fallbacks": {"lacf": True},
     }
Exemplo n.º 13
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[{
         'macAddress': wifi.mac,
         'age': 2000,
         'signalStrength': -90,
         'ssid': 'wifi'
     } for wifi in wifis])
     data = OUTBOUND_SCHEMA.deserialize(query.json())
     assert (data == {
         'wifiAccessPoints': [{
             'macAddress': wifis[0].mac,
             'age': 2000,
             'signalStrength': -90,
             'ssid': 'wifi',
         }, {
             'macAddress': wifis[1].mac,
             'age': 2000,
             'signalStrength': -90,
             'ssid': 'wifi',
         }],
         'fallbacks': {
             'lacf': True
         },
     })
Exemplo n.º 14
0
 def test_get_wifi(self, cache, stats):
     wifis = WifiShardFactory.build_batch(2)
     query = self._query(wifi=self.wifi_model_query(wifis))
     assert cache.get(query) is None
     stats.check(counter=[
         ('locate.fallback.cache', 1, 1, ['status:miss']),
     ])
Exemplo n.º 15
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(
         wifi=[
             {
                 "macAddress": wifi.mac,
                 "age": 2000,
                 "channel": 11,
                 "signalStrength": -90,
                 "signalToNoiseRatio": 13,
             }
             for wifi in wifis
         ]
     )
     data = self._call(query.json())
     assert data == {
         "wifiAccessPoints": [
             {
                 "macAddress": wifis[0].mac,
                 "age": 2000,
                 "channel": 11,
                 "signalStrength": -90,
                 "signalToNoiseRatio": 13,
             },
             {
                 "macAddress": wifis[1].mac,
                 "age": 2000,
                 "channel": 11,
                 "signalStrength": -90,
                 "signalToNoiseRatio": 13,
             },
         ],
         "considerIp": False,
     }
Exemplo n.º 16
0
    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)])
Exemplo n.º 17
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[{
         "macAddress": wifi.mac,
         "channel": 3,
         "frequency": 2412,
         "signalStrength": -70,
         "signalToNoiseRatio": 13,
         "ssid": "wifi",
     } for wifi in wifis])
     data = self._call(query.json())
     assert data == {
         "wifi": [
             {
                 "bssid": wifis[0].mac,
                 "channel": 3,
                 "frequency": 2412,
                 "signal": -70,
                 "signalToNoiseRatio": 13,
             },
             {
                 "bssid": wifis[1].mac,
                 "channel": 3,
                 "frequency": 2412,
                 "signal": -70,
                 "signalToNoiseRatio": 13,
             },
         ],
         "fallbacks": {
             "lacf": True
         },
         "token":
         None,
     }
Exemplo n.º 18
0
    def test_database_error(self, db_errors=0):
        cells = [
            CellShardFactory.build(radio=Radio.gsm),
            CellShardFactory.build(radio=Radio.wcdma),
            CellShardFactory.build(radio=Radio.lte),
        ]
        wifis = WifiShardFactory.build_batch(2)

        for model in (CellArea, CellOCID, CellAreaOCID):
            self.session.execute(text('drop table %s;' % model.__tablename__))
        for name in set([cell.__tablename__ for cell in cells]):
            self.session.execute(text('drop table %s;' % name))
        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)])
Exemplo n.º 19
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[
         {'macAddress': wifi.mac, 'channel': 3, 'frequency': 2412,
          'signalStrength': -70, 'signalToNoiseRatio': 13,
          'ssid': 'wifi'}
         for wifi in wifis])
     data = self._call(query.json())
     assert (data == {
         'wifi': [{
             'bssid': wifis[0].mac,
             'channel': 3,
             'frequency': 2412,
             'signal': -70,
             'signalToNoiseRatio': 13,
         }, {
             'bssid': wifis[1].mac,
             'channel': 3,
             'frequency': 2412,
             'signal': -70,
             'signalToNoiseRatio': 13,
         }],
         'fallbacks': {'lacf': True},
         'token': None,
     })
Exemplo n.º 20
0
 def test_satisfies_fail(self):
     wifis = WifiShardFactory.build_batch(2)
     wifi_query = [{'macAddress': wifi.mac} for wifi in wifis]
     positions = PositionResultList(
         Position(lat=1.0, lon=1.0, accuracy=2500.0, score=2.0))
     query = Query(api_type='locate', wifi=wifi_query)
     assert not positions.satisfies(query)
Exemplo n.º 21
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=[{
         'mac': wifi.mac,
         'age': 2000,
         'signal': -90,
         'ssid': 'wifi'
     } for wifi in wifis])
     data = self.schema.deserialize(query.internal_query())
     self.assertEqual(
         data, {
             'wifiAccessPoints': [{
                 'macAddress': wifis[0].mac,
                 'age': 2000,
                 'signalStrength': -90,
                 'ssid': 'wifi',
             }, {
                 'macAddress': wifis[1].mac,
                 'age': 2000,
                 'signalStrength': -90,
                 'ssid': 'wifi',
             }],
             'fallbacks': {
                 'lacf': True
             },
         })
Exemplo n.º 22
0
 def test_satisfies_fail(self):
     wifis = WifiShardFactory.build_batch(2)
     wifi_query = [{'mac': wifi.mac} for wifi in wifis]
     positions = PositionResultList(
         Position(lat=1.0, lon=1.0, accuracy=2500.0, score=2.0))
     query = Query(api_type='locate', wifi=wifi_query)
     self.assertFalse(positions.satisfies(query))
Exemplo n.º 23
0
    def test_api_key_disallows(self):
        api_key = ApiKeyFactory.build(allow_fallback=False)
        cells = CellShardFactory.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)
Exemplo n.º 24
0
    def test_database_error(self, db_errors=0):
        cells = [
            CellShardFactory.build(radio=Radio.gsm),
            CellShardFactory.build(radio=Radio.wcdma),
            CellShardFactory.build(radio=Radio.lte),
        ]
        wifis = WifiShardFactory.build_batch(2)

        for model in (CellArea, CellOCID, CellAreaOCID):
            self.session.execute(text("drop table %s;" % model.__tablename__))
        for name in set([cell.__tablename__ for cell in cells]):
            self.session.execute(text("drop table %s;" % name))
        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)])
Exemplo n.º 25
0
    def test_wifi_miss(self, geoip_db, http_session, session, source):
        wifis = WifiShardFactory.build_batch(2, samples=10)
        session.flush()

        query = self.model_query(geoip_db, http_session, session, wifis=wifis)
        results = source.search(query)
        self.check_model_results(results, None)
Exemplo n.º 26
0
    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)
Exemplo n.º 27
0
 def test_get_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = Query(wifi=self.wifi_model_query(wifis))
     self.assertEqual(self.cache.get(query), None)
     self.check_stats(counter=[
         ('locate.fallback.cache', 1, 1, ['status:miss']),
     ])
Exemplo n.º 28
0
    def test_wifi_not_found(self, app, data_queues, metricsmock, logs):
        """A failed WiFi-based lookup emits several metrics."""
        wifis = WifiShardFactory.build_batch(2)

        query = self.model_query(wifis=wifis)

        res = self._call(app, body=query, status=404)
        self.check_response(data_queues, res, "not_found")
        metricsmock.assert_incr_once(
            "request", tags=[self.metric_path, "method:post", "status:404"]
        )
        metricsmock.assert_incr_once(
            self.metric_type + ".request", tags=[self.metric_path, "key:test"]
        )
        metricsmock.assert_incr_once(
            self.metric_type + ".query",
            tags=["key:test", "geoip:false", "blue:none", "cell:none", "wifi:many"],
        )
        metricsmock.assert_incr_once(
            self.metric_type + ".result",
            tags=["key:test", "accuracy:high", "fallback_allowed:false", "status:miss"],
        )
        metricsmock.assert_incr_once(
            self.metric_type + ".source",
            tags=["key:test", "source:internal", "accuracy:high", "status:miss"],
        )
        assert logs.only_entry["wifi_valid"] == 2
Exemplo n.º 29
0
 def test_get_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = self._query(wifi=self.wifi_model_query(wifis))
     self.assertEqual(self.cache.get(query), None)
     self.check_stats(counter=[
         ('locate.fallback.cache', 1, 1, ['status:miss']),
     ])
Exemplo n.º 30
0
    def test_database_error(self, app, data_queues,
                            raven, session, stats, restore_db):
        cells = [
            CellShardFactory.build(radio=Radio.gsm),
            CellShardFactory.build(radio=Radio.wcdma),
            CellShardFactory.build(radio=Radio.lte),
        ]
        wifis = WifiShardFactory.build_batch(2)

        for model in (CellArea, ):
            session.execute(text('drop table %s;' % model.__tablename__))
        for name in set([cell.__tablename__ for cell in cells]):
            session.execute(text('drop table %s;' % name))
        for name in set([wifi.__tablename__ for wifi in wifis]):
            session.execute(text('drop table %s;' % name))

        query = self.model_query(cells=cells, wifis=wifis)
        res = self._call(app, body=query, ip=self.test_ip)
        self.check_response(data_queues, res, 'ok', fallback='ipf')
        self.check_queue(data_queues, 0)
        stats.check(counter=[
            ('request', [self.metric_path, 'method:post', 'status:200']),
        ], timer=[
            ('request', [self.metric_path, 'method:post']),
        ])
        if self.apikey_metrics:
            stats.check(counter=[
                (self.metric_type + '.result',
                    ['key:test', 'region:GB', 'fallback_allowed:false',
                     'accuracy:high', 'status:miss']),
            ])

        raven.check([('ProgrammingError', 3)])
Exemplo n.º 31
0
    def test_wifi_not_found(self, app, data_queues, stats):
        wifis = WifiShardFactory.build_batch(2)

        query = self.model_query(wifis=wifis)

        res = self._call(app, body=query, status=self.not_found.code)
        self.check_response(data_queues, res, 'not_found')
        stats.check(counter=[
            ('request', [
                self.metric_path, 'method:post',
                'status:%s' % self.not_found.code
            ]),
            (self.metric_type + '.request', [self.metric_path, 'key:test']),
            (self.metric_type + '.query', [
                'key:test', 'region:none', 'geoip:false', 'blue:none',
                'cell:none', 'wifi:many'
            ]),
            (self.metric_type + '.result', 'fallback_allowed:false',
             ['key:test', 'region:none', 'accuracy:high', 'status:miss']),
            (self.metric_type + '.source', [
                'key:test', 'region:none', 'source:internal', 'accuracy:high',
                'status:miss'
            ]),
        ],
                    timer=[
                        ('request', [self.metric_path, 'method:post']),
                    ])
Exemplo n.º 32
0
    def test_wifi_miss(self):
        wifis = WifiShardFactory.build_batch(2, samples=10)
        self.session.flush()

        query = self.model_query(wifis=wifis)
        results = self.source.search(query)
        self.check_model_results(results, None)
Exemplo n.º 33
0
 def test_satisfies(self):
     wifis = WifiShardFactory.build_batch(2)
     wifi_query = [{'mac': wifi.mac} for wifi in wifis]
     positions = PositionResultList([
         Position(lat=1.0, lon=1.0, accuracy=100.0, score=0.5),
         Position(lat=1.0, lon=1.0, accuracy=10000.0, score=0.6)])
     query = Query(api_type='locate', wifi=wifi_query)
     self.assertTrue(positions.satisfies(query))
Exemplo n.º 34
0
    def test_gzip(self):
        wifis = WifiShardFactory.build_batch(2)
        query = self.model_query(wifis=wifis)

        body = util.encode_gzip(json.dumps(query))
        headers = {"Content-Encoding": "gzip"}
        res = self._call(body=body, headers=headers, method="post", status=self.not_found.code)
        self.check_response(res, "not_found")
Exemplo n.º 35
0
    def test_check_empty_result(self, geoip_db, http_session,
                                session, source, stats):
        wifis = WifiShardFactory.build_batch(2)

        query = self.model_query(
            geoip_db, http_session, session, stats,
            wifis=wifis)
        self.check_should_search(source, query, True)
Exemplo n.º 36
0
 def test_mixed_miss(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         self._make_result(accuracy=1001.0), wifi=wifis, ip=self.london_ip)
     self.check_stats(counter=[
         ('locate.result',
             ['key:key', 'country:GB', 'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 37
0
 def test_high_miss(self, geoip_db, metricsmock):
     wifis = WifiShardFactory.build_batch(2)
     results = self._make_results(accuracy=10000.0)
     self._make_query(geoip_db, DataSource.geoip, results, wifi=wifis)
     metricsmock.assert_incr_once(
         "locate.source",
         tags=["key:test", "source:geoip", "accuracy:high", "status:miss"],
     )
Exemplo n.º 38
0
 def test_get_wifi(self, cache, stats):
     wifis = WifiShardFactory.build_batch(2)
     query = self._query(wifi=self.wifi_model_query(wifis))
     assert cache.get(query) is None
     stats.check(counter=[
         ('locate.fallback.cache', 1, 1,
             [self.fallback_tag, 'status:miss']),
     ])
Exemplo n.º 39
0
 def test_high_hit(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(self._make_result(accuracy=1000.0), wifi=wifis)
     self.check_stats(counter=[
         ('locate.result',
             ['key:key', 'region:none', 'fallback_allowed:false',
              'accuracy:high', 'status:hit']),
     ])
Exemplo n.º 40
0
    def test_mixed_cell_wifi(self):
        cells = CellShardFactory.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)
Exemplo n.º 41
0
 def test_no_results(self, geoip_db, metricsmock):
     wifis = WifiShardFactory.build_batch(2)
     results = PositionResultList()
     self._make_query(geoip_db, DataSource.internal, results, wifi=wifis)
     metricsmock.assert_incr_once(
         "locate.source",
         tags=["key:test", "source:internal", "accuracy:high", "status:miss"],
     )
Exemplo n.º 42
0
    def test_mixed_cell_wifi(self):
        cells = CellShardFactory.build_batch(1)
        wifis = WifiShardFactory.build_batch(2)

        query = Query(cell=self.cell_model_query(cells),
                      wifi=self.wifi_model_query(wifis))
        assert query.expected_accuracy is DataAccuracy.high
        assert query.geoip_only is False
Exemplo n.º 43
0
 def test_high_miss(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(self._make_result(accuracy=2500.0), wifi=wifis)
     self.check_stats(counter=[
         ('locate.result',
             ['key:key', 'region:none', 'fallback_allowed:false',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 44
0
 def test_high_hit(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         DataSource.internal, self._make_result(accuracy=100.0), wifi=wifis)
     self.check_stats(counter=[
         ('locate.source',
             ['key:key', 'region:none', 'source:internal',
              'accuracy:high', 'status:hit']),
     ])
Exemplo n.º 45
0
    def test_mixed_cell_wifi(self):
        cells = CellShardFactory.build_batch(1)
        wifis = WifiShardFactory.build_batch(2)

        query = Query(
            cell=self.cell_model_query(cells),
            wifi=self.wifi_model_query(wifis))
        assert query.expected_accuracy is DataAccuracy.high
        assert query.geoip_only is False
Exemplo n.º 46
0
 def test_satisfies(self):
     wifis = WifiShardFactory.build_batch(2)
     wifi_query = [{'mac': wifi.mac} for wifi in wifis]
     positions = PositionResultList([
         Position(lat=1.0, lon=1.0, accuracy=100.0, score=0.5),
         Position(lat=1.0, lon=1.0, accuracy=10000.0, score=0.6)
     ])
     query = Query(api_type='locate', wifi=wifi_query)
     self.assertTrue(positions.satisfies(query))
Exemplo n.º 47
0
    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']),
        ])
Exemplo n.º 48
0
 def test_mixed_miss(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         self._make_result(accuracy=2001.0), wifi=wifis, ip=self.london_ip)
     self.check_stats(counter=[
         ('locate.result',
             ['key:key', 'region:GB', 'fallback_allowed:false',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 49
0
    def test_api_key_disallows(self, geoip_db, http_session, session, source):
        api_key = KeyFactory(allow_fallback=False)
        cells = CellShardFactory.build_batch(2)
        wifis = WifiShardFactory.build_batch(2)

        query = self.model_query(
            geoip_db, http_session, session, cells=cells, wifis=wifis, api_key=api_key
        )
        self.check_should_search(source, query, False)
Exemplo n.º 50
0
 def test_no_results(self):
     wifis = WifiShardFactory.build_batch(2)
     results = PositionResultList()
     self._make_query(DataSource.internal, results, wifi=wifis)
     self.check_stats(counter=[
         ('locate.source',
             ['key:key', 'region:none', 'source:internal',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 51
0
 def test_high_miss(self):
     wifis = WifiShardFactory.build_batch(2)
     results = self._make_results(accuracy=10000.0)
     self._make_query(DataSource.ocid, results, wifi=wifis)
     self.check_stats(counter=[
         ('locate.source',
             ['key:key', 'region:none', 'source:ocid',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 52
0
    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']),
        ])
Exemplo n.º 53
0
 def test_wifi(self):
     wifis = WifiShardFactory.build_batch(2)
     query = self.model_query(wifis=wifis)
     res = self._call(
         body=query,
         ip='127.0.0.1',
         status=404)
     self.check_response(res, 'not_found')
     self.check_db_calls(rw=0, ro=0)
Exemplo n.º 54
0
 def test_high_miss(self):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         DataSource.ocid, self._make_result(accuracy=10000.0), wifi=wifis)
     self.check_stats(counter=[
         ('locate.source',
             ['key:key', 'region:none', 'source:ocid',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 55
0
 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"])
Exemplo n.º 56
0
 def test_high_hit(self, geoip_db, stats):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         geoip_db, stats,
         self._make_result(accuracy=500.0), wifi=wifis)
     stats.check(counter=[
         ('locate.result',
             ['key:test', 'region:none', 'fallback_allowed:false',
              'accuracy:high', 'status:hit']),
     ])
Exemplo n.º 57
0
 def test_mixed_miss(self, geoip_db, stats):
     wifis = WifiShardFactory.build_batch(2)
     self._make_query(
         geoip_db, stats,
         self._make_result(accuracy=2001.0), wifi=wifis, ip=self.london_ip)
     stats.check(counter=[
         ('locate.result',
             ['key:test', 'region:GB', 'fallback_allowed:false',
              'accuracy:high', 'status:miss']),
     ])
Exemplo n.º 58
0
    def test_apikey_error(self, db_errors=0):
        cells = CellShardFactory.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)])
Exemplo n.º 59
0
    def test_wifi_miss(self, geoip_db, http_session,
                       session, source, stats):
        wifis = WifiShardFactory.build_batch(2, samples=10)
        session.flush()

        query = self.model_query(
            geoip_db, http_session, session, stats,
            wifis=wifis)
        results = source.search(query)
        self.check_model_results(results, None)