def test_repr(self): results = ResultList([Position(lat=1.0), Position(lat=2.0)]) rep = repr(results) self.assertTrue(rep.startswith('ResultList'), rep) self.assertTrue('Position<' in rep, rep) self.assertTrue('lat:1.0' in rep, rep) self.assertTrue('lat:2.0' in rep, rep)
def test_satisfies(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=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) assert positions.satisfies(query)
def test_json(self): assert (Position().json() == {'position': {'source': 'query'}}) assert (Position(lat=1.0, lon=1.0, accuracy=2.0).json() == { 'position': { 'latitude': 1.0, 'longitude': 1.0, 'accuracy': 2.0, 'source': 'query' } })
def test_json(self): assert Position().json() == {"position": {"source": "query"}} assert Position(lat=1.0, lon=1.0, accuracy=2.0).json() == { "position": { "latitude": 1.0, "longitude": 1.0, "accuracy": 2.0, "source": "query", } }
def _make_results(self, accuracy=None): london = GEOIP_DATA['London'] return PositionResultList(Position( lat=london['latitude'], lon=london['longitude'], accuracy=accuracy, score=0.5))
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))
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)
def _make_result(self, accuracy=None): london = GEOIP_DATA["London"] return Position( lat=london["latitude"], lon=london["longitude"], accuracy=accuracy, score=0.5, )
def test_repr(self): position = Position(lat=1.0, lon=-1.1, accuracy=100.0, score=2.0) rep = repr(position) assert rep.startswith('Position') assert '1.0' in rep assert '-1.1' in rep assert '100.0' in rep assert position.score == 2.0
def test_repr(self): position = Position(lat=1.0, lon=-1.1, accuracy=100.0, score=2.0) rep = repr(position) self.assertTrue(rep.startswith('Position'), rep) self.assertTrue('1.0' in rep, rep) self.assertTrue('-1.1' in rep, rep) self.assertTrue('100.0' in rep, rep) self.assertAlmostEqual(position.score, 2.0, 4)
def test_repr(self): position = Position(lat=1.0, lon=-1.1, accuracy=100.0, score=2.0) rep = repr(position) assert rep.startswith("Position") assert "1.0" in rep assert "-1.1" in rep assert "100.0" in rep assert position.score == 2.0
def test_best(self): gb1 = Position(lat=51.5, lon=-0.1, accuracy=100000.0, score=0.6) gb2 = Position(lat=51.5002, lon=-0.1, accuracy=10000.0, score=1.5) gb3 = Position(lat=51.7, lon=-0.1, accuracy=1000.0, score=5.0) bt1 = Position(lat=27.5002, lon=90.5, accuracy=1000.0, score=0.5) bt2 = Position(lat=27.5, lon=90.5, accuracy=2000.0, score=2.0) bt3 = Position(lat=27.7, lon=90.7, accuracy=500.0, score=5.0) bt4 = Position(lat=27.9, lon=90.7, accuracy=300.0, score=5.0) # single result works self.assertAlmostEqual(PositionResultList([gb1]).best().lat, 51.5, 4) # the lowest accuracy result from the best cluster wins self.assertAlmostEqual( PositionResultList([bt1, bt2]).best().lat, 27.5002, 4) self.assertAlmostEqual( PositionResultList([gb1, bt2]).best().lat, 27.5, 4) self.assertAlmostEqual( PositionResultList([gb1, gb2, bt2]).best().lat, 51.5002, 4) self.assertAlmostEqual( PositionResultList([gb1, gb3, bt1, bt2]).best().lat, 51.7, 4) self.assertAlmostEqual( PositionResultList([gb1, gb2, bt2, bt3]).best().lat, 27.7, 4) # break tie by accuracy self.assertAlmostEqual( PositionResultList([gb3, bt3]).best().lat, 27.7, 4) self.assertAlmostEqual( PositionResultList([bt3, bt4]).best().lat, 27.9, 4)
def test_check_already_good_result(self, geoip_db, http_session, session, source): wifis = WifiShardFactory.build_batch(2) results = PositionResultList( Position( source=DataSource.internal, lat=1.0, lon=1.0, accuracy=100.0, score=1.0 ) ) query = self.model_query(geoip_db, http_session, session, wifis=wifis) self.check_should_search(source, query, False, results=results)
def test_check_geoip_result(self): london = self.london_model wifis = WifiShardFactory.build_batch(2) results = PositionResultList( Position(source=DataSource.geoip, lat=london.lat, lon=london.lon, accuracy=float(london.radius), score=0.6)) query = self.model_query(wifis=wifis, ip=london.ip) self.check_should_search(query, True, results=results)
def test_check_geoip_result(self, london_model, geoip_db, http_session, session, source, stats): wifis = WifiShardFactory.build_batch(2) results = PositionResultList( Position(source=DataSource.geoip, lat=london_model.lat, lon=london_model.lon, accuracy=float(london_model.radius), score=0.6)) query = self.model_query(geoip_db, http_session, session, stats, wifis=wifis, ip=london_model.ip) self.check_should_search(source, query, True, results=results)
def _make_results(self, accuracy=None): return PositionResultList( Position(lat=self.london['latitude'], lon=self.london['longitude'], accuracy=accuracy, score=0.5))
def _make_result(self): return Position(lat=1.0, lon=1.0, accuracy=10.0, score=0.5, source=DataSource.internal)
def check(accuracy, expected): pos = Position(lat=1.0, lon=1.0, accuracy=accuracy) assert pos.data_accuracy is expected
def _make_result(self, accuracy=None): return Position( lat=self.london['latitude'], lon=self.london['longitude'], accuracy=accuracy)
def _position(accuracy=None): return Position(lat=1.0, lon=1.0, accuracy=accuracy)
def test_empty(self): self.assertTrue(Position(lat=1.0, lon=1.0, accuracy=None).empty()) self.assertTrue(Position(lat=1.0, lon=None, accuracy=1.0).empty()) self.assertTrue(Position(lat=None, lon=1.0, accuracy=1.0).empty())
def test_satisfies(self): wifis = WifiShardFactory.build_batch(2) wifi_query = [{'mac': wifi.mac} for wifi in wifis] position = Position(lat=1.0, lon=1.0, accuracy=100.0) query = Query(api_type='locate', wifi=wifi_query) self.assertTrue(position.satisfies(query))
def test_not_empty(self): self.assertFalse(Position(lat=1.0, lon=1.0, accuracy=1.0).empty()) self.assertFalse(Position(lat=0.0, lon=0.0, accuracy=0.0).empty())