def test_ambiguous_mcc(self, geoip_db, http_session, session, source, stats): now = util.utcnow() regions = GEOCODER.regions_for_mcc(234, metadata=True) area = CellAreaFactory(mcc=234, num_cells=10) session.flush() query = self.model_query(geoip_db, http_session, session, stats, cells=[area]) results = source.search(query) self.check_model_results(results, regions) assert results.best().region_code == 'GB' for result in results: score = 0.25 if result.region_code == 'GB': score += area_score(area, now) assert result.score == score stats.check(counter=[ (self.api_type + '.source', [ 'key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit' ]), ])
def test_blue(self, geoip_db, http_session, session, source, stats): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] blue1 = BlueShardFactory(samples=10) blue2 = BlueShardFactory(samples=20) blue3 = BlueShardFactory.build(region='DE', samples=100) session.flush() query = self.model_query(geoip_db, http_session, session, stats, blues=[blue1, blue2, blue3]) results = source.search(query) self.check_model_results(results, [region]) best_result = results.best() assert best_result.region_code == region.code assert (best_result.score == station_score(blue1, now) + station_score(blue2, now)) stats.check(counter=[ (self.api_type + '.source', [ 'key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit' ]), ])
def test_ambiguous_mcc(self, geoip_db, http_session, session, source, metricsmock): now = util.utcnow() regions = GEOCODER.regions_for_mcc(234, metadata=True) area = CellAreaFactory(mcc=234, num_cells=10) session.flush() query = self.model_query(geoip_db, http_session, session, cells=[area]) results = source.search(query) self.check_model_results(results, regions) assert results.best().region_code == "GB" for result in results: score = 0.25 if result.region_code == "GB": score += area_score(area, now) assert result.score == score assert metricsmock.has_record( "incr", self.api_type + ".source", value=1, tags=[ "key:test", "region:none", "source:internal", "accuracy:low", "status:hit", ], )
def test_blue(self, geoip_db, http_session, session, source, metricsmock): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] blue1 = BlueShardFactory(samples=10) blue2 = BlueShardFactory(samples=20) blue3 = BlueShardFactory.build(region="DE", samples=100) session.flush() query = self.model_query( geoip_db, http_session, session, blues=[blue1, blue2, blue3] ) results = source.search(query) self.check_model_results(results, [region]) best_result = results.best() assert best_result.region_code == region.code assert best_result.score == station_score(blue1, now) + station_score( blue2, now ) assert metricsmock.has_record( "incr", self.api_type + ".source", value=1, tags=[ "key:test", "region:none", "source:internal", "accuracy:low", "status:hit", ], )
def test_ambiguous_mcc(self): regions = GEOCODER.regions_for_mcc(234, metadata=True) cell = CellFactory.build(mcc=234) query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_result(results, regions) self.check_stats(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_ambiguous_mcc(self): regions = GEOCODER.regions_for_mcc(234, metadata=True) cell = CellShardFactory.build(mcc=234) query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_result(results, regions) self.check_stats(counter=[ (self.api_type + '.source', [ 'key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit' ]), ])
def test_no_match(self): assert GEOCODER.regions_for_mcc(None) == [] assert GEOCODER.regions_for_mcc(None, metadata=True) == [] assert GEOCODER.regions_for_mcc(1) == [] assert GEOCODER.regions_for_mcc(1, metadata=True) == [] assert GEOCODER.regions_for_mcc('') == [] assert GEOCODER.regions_for_mcc('1', metadata=True) == []
def test_no_match(self): self.assertEqual(GEOCODER.regions_for_mcc(None), []) self.assertEqual(GEOCODER.regions_for_mcc(None, metadata=True), []) self.assertEqual(GEOCODER.regions_for_mcc(1), []) self.assertEqual(GEOCODER.regions_for_mcc(1, metadata=True), []) self.assertEqual(GEOCODER.regions_for_mcc(''), []) self.assertEqual(GEOCODER.regions_for_mcc('1', metadata=True), [])
def test_no_match(self): assert GEOCODER.regions_for_mcc(None) == [] assert GEOCODER.regions_for_mcc(None, metadata=True) == [] assert GEOCODER.regions_for_mcc(1) == [] assert GEOCODER.regions_for_mcc(1, metadata=True) == [] assert GEOCODER.regions_for_mcc("") == [] assert GEOCODER.regions_for_mcc("1", metadata=True) == []
def test_multiple_mcc(self): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] cell = CellAreaFactory(mcc=234, num_cells=6) cell2 = CellAreaFactory(mcc=235, num_cells=8) self.session.flush() query = self.model_query(cells=[cell, cell2]) results = self.source.search(query) self.assertTrue(len(results) > 2) best_result = results.best(query.expected_accuracy) self.assertEqual(best_result.region_code, region.code) self.assertAlmostEqual(best_result.score, 1.25 + cell.score(now), 4)
def test_multiple_mcc(self, geoip_db, http_session, session, source): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] area = CellAreaFactory(mcc=234, num_cells=6) area2 = CellAreaFactory(mcc=235, num_cells=8) session.flush() query = self.model_query(geoip_db, http_session, session, cells=[area, area2]) results = source.search(query) assert len(results) > 2 best_result = results.best() assert best_result.region_code == region.code assert best_result.score == 1.25 + area_score(area, now)
def test_multiple_mcc(self): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] cell = CellAreaFactory(mcc=234, num_cells=6) cell2 = CellAreaFactory(mcc=235, num_cells=8) self.session.flush() query = self.model_query(cells=[cell, cell2]) results = self.source.search(query) self.assertTrue(len(results) > 2) best_result = results.best() self.assertEqual(best_result.region_code, region.code) self.assertAlmostEqual(best_result.score, 1.25 + cell.score(now), 4)
def test_from_mcc(self, geoip_db, http_session, session, source, metricsmock): region = GEOCODER.regions_for_mcc(235, metadata=True)[0] area = CellAreaFactory(mcc=235, num_cells=10) session.flush() query = self.model_query(geoip_db, http_session, session, cells=[area]) results = source.search(query) self.check_model_results(results, [region]) assert results[0].score == 1.0 metricsmock.assert_incr_once( self.api_type + ".source", tags=["key:test", "source:internal", "accuracy:low", "status:hit"], )
def test_from_mcc(self): region = GEOCODER.regions_for_mcc(235, metadata=True)[0] cell = CellAreaFactory(mcc=235, num_cells=10) self.session.flush() query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_results(results, [region]) self.assertAlmostEqual(results[0].score, 1.0, 4) self.check_stats(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def search_cell(self, query): results = self.result_list() now = util.utcnow() ambiguous_cells = [] regions = [] for cell in list(query.cell) + list(query.cell_area): code = cell.mobileCountryCode mcc_regions = GEOCODER.regions_for_mcc(code, metadata=True) # Divide score by number of possible regions for the mcc score = 1.0 / (len(mcc_regions) or 1.0) for mcc_region in mcc_regions: regions.append((mcc_region, score)) if len(mcc_regions) > 1: ambiguous_cells.append(cell) # Group by region code grouped_regions = {} for region, score in regions: code = region.code if code not in grouped_regions: grouped_regions[code] = [region, score] else: # Sum up scores of multiple matches grouped_regions[code][1] += score if ambiguous_cells: # Only do a database query if the mcc is ambiguous. # Use the area models for area and cell entries, # as we are only interested in the region here, # which won't differ between individual cells inside and area. areas = query_areas(query, ambiguous_cells, self.area_model, self.raven_client) for area in areas: code = area.region if code and code in grouped_regions: grouped_regions[code][1] += area_score(area, now) for region, score in grouped_regions.values(): results.add( self.result_type( region_code=region.code, region_name=region.name, accuracy=region.radius, score=score, )) return results
def test_multiple_mcc(self, geoip_db, http_session, session, source, stats): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] area = CellAreaFactory(mcc=234, num_cells=6) area2 = CellAreaFactory(mcc=235, num_cells=8) session.flush() query = self.model_query( geoip_db, http_session, session, stats, cells=[area, area2]) results = source.search(query) assert len(results) > 2 best_result = results.best() assert best_result.region_code == region.code assert best_result.score == 1.25 + area_score(area, now)
def search_cell(self, query): results = self.result_list() now = util.utcnow() ambiguous_cells = [] regions = [] for cell in list(query.cell) + list(query.cell_area): code = cell.mobileCountryCode mcc_regions = GEOCODER.regions_for_mcc(code, metadata=True) # Divide score by number of possible regions for the mcc score = 1.0 / (len(mcc_regions) or 1.0) for mcc_region in mcc_regions: regions.append((mcc_region, score)) if len(mcc_regions) > 1: ambiguous_cells.append(cell) # Group by region code grouped_regions = {} for region, score in regions: code = region.code if code not in grouped_regions: grouped_regions[code] = [region, score] else: # Sum up scores of multiple matches grouped_regions[code][1] += score if ambiguous_cells: # Only do a database query if the mcc is ambiguous. # Use the area models for area and cell entries, # as we are only interested in the region here, # which won't differ between individual cells inside and area. areas = query_areas( query, ambiguous_cells, self.area_model, self.raven_client) for area in areas: code = area.region if code and code in grouped_regions: grouped_regions[code][1] += area_score(area, now) for region, score in grouped_regions.values(): results.add(self.result_type( region_code=region.code, region_name=region.name, accuracy=region.radius, score=score)) return results
def test_from_mcc(self, geoip_db, http_session, session, source, stats): region = GEOCODER.regions_for_mcc(235, metadata=True)[0] area = CellAreaFactory(mcc=235, num_cells=10) session.flush() query = self.model_query( geoip_db, http_session, session, stats, cells=[area]) results = source.search(query) self.check_model_results(results, [region]) assert results[0].score == 1.0 stats.check(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def search_mcc(self, query): results = ResultList() codes = set() for cell in list(query.cell) + list(query.cell_area): codes.add(cell.mcc) regions = [] for code in codes: regions.extend(GEOCODER.regions_for_mcc(code, metadata=True)) for region in regions: region_code = region.code results.add(self.result_type( region_code=region_code, region_name=region.name, accuracy=region.radius)) return results
def test_from_mcc(self, geoip_db, http_session, session, source, stats): region = GEOCODER.regions_for_mcc(235, metadata=True)[0] area = CellAreaFactory(mcc=235, num_cells=10) session.flush() query = self.model_query(geoip_db, http_session, session, stats, cells=[area]) results = source.search(query) self.check_model_results(results, [region]) assert results[0].score == 1.0 stats.check(counter=[ (self.api_type + '.source', [ 'key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit' ]), ])
def test_ambiguous_mcc(self): now = util.utcnow() regions = GEOCODER.regions_for_mcc(234, metadata=True) cell = CellAreaFactory(mcc=234, num_cells=10) self.session.flush() query = self.model_query(cells=[cell]) results = self.source.search(query) self.check_model_results(results, regions) self.assertEqual(results.best().region_code, 'GB') for result in results: score = 0.25 if result.region_code == 'GB': score += cell.score(now) self.assertAlmostEqual(result.score, score, 4) self.check_stats(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_wifi(self): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] wifi1 = WifiShardFactory(samples=10) wifi2 = WifiShardFactory(samples=20) wifi3 = WifiShardFactory.build(region='DE', samples=100) self.session.flush() query = self.model_query(wifis=[wifi1, wifi2, wifi3]) results = self.source.search(query) self.check_model_results(results, [region]) best_result = results.best(query.expected_accuracy) self.assertEqual(best_result.region_code, region.code) self.assertAlmostEqual( best_result.score, wifi1.score(now) + wifi2.score(now), 4) self.check_stats(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_blue(self): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] blue1 = BlueShardFactory(samples=10) blue2 = BlueShardFactory(samples=20) blue3 = BlueShardFactory.build(region='DE', samples=100) self.session.flush() query = self.model_query(blues=[blue1, blue2, blue3]) results = self.source.search(query) self.check_model_results(results, [region]) best_result = results.best() self.assertEqual(best_result.region_code, region.code) self.assertAlmostEqual( best_result.score, blue1.score(now) + blue2.score(now), 4) self.check_stats(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_wifi(self, geoip_db, http_session, session, source, metricsmock): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] wifi1 = WifiShardFactory(samples=10) wifi2 = WifiShardFactory(samples=20) wifi3 = WifiShardFactory.build(region="DE", samples=100) session.flush() query = self.model_query(geoip_db, http_session, session, wifis=[wifi1, wifi2, wifi3]) results = source.search(query) self.check_model_results(results, [region]) best_result = results.best() assert best_result.region_code == region.code assert best_result.score == station_score(wifi1, now) + station_score( wifi2, now) metricsmock.assert_incr_once( self.api_type + ".source", tags=["key:test", "source:internal", "accuracy:low", "status:hit"], )
def test_ambiguous_mcc(self, geoip_db, http_session, session, source, stats): now = util.utcnow() regions = GEOCODER.regions_for_mcc(234, metadata=True) area = CellAreaFactory(mcc=234, num_cells=10) session.flush() query = self.model_query( geoip_db, http_session, session, stats, cells=[area]) results = source.search(query) self.check_model_results(results, regions) assert results.best().region_code == 'GB' for result in results: score = 0.25 if result.region_code == 'GB': score += area_score(area, now) assert result.score == score stats.check(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_blue(self, geoip_db, http_session, session, source, stats): now = util.utcnow() region = GEOCODER.regions_for_mcc(235, metadata=True)[0] blue1 = BlueShardFactory(samples=10) blue2 = BlueShardFactory(samples=20) blue3 = BlueShardFactory.build(region='DE', samples=100) session.flush() query = self.model_query( geoip_db, http_session, session, stats, blues=[blue1, blue2, blue3]) results = source.search(query) self.check_model_results(results, [region]) best_result = results.best() assert best_result.region_code == region.code assert (best_result.score == station_score(blue1, now) + station_score(blue2, now)) stats.check(counter=[ (self.api_type + '.source', ['key:test', 'region:none', 'source:internal', 'accuracy:low', 'status:hit']), ])
def test_single(self): assert set(GEOCODER.regions_for_mcc(262)) == set(['DE']) regions = GEOCODER.regions_for_mcc(262, metadata=True) assert set([r.code for r in regions]) == set(['DE'])
def test_filtered(self): # AX / Aland Islands is not in the GENC list regions = GEOCODER.regions_for_mcc(244) self.assertEqual(set(regions), set(['FI']))
def test_multiple(self): assert set(GEOCODER.regions_for_mcc(311)) == set(["GU", "US"]) regions = GEOCODER.regions_for_mcc(311, metadata=True) assert set([r.code for r in regions]) == set(["GU", "US"])
def test_single(self): assert set(GEOCODER.regions_for_mcc(262)) == set(["DE"]) regions = GEOCODER.regions_for_mcc(262, metadata=True) assert set([r.code for r in regions]) == set(["DE"])
def test_multiple(self): assert set(GEOCODER.regions_for_mcc(311)) == set(['GU', 'US']) regions = GEOCODER.regions_for_mcc(311, metadata=True) assert set([r.code for r in regions]) == set(['GU', 'US'])
def test_filtered(self): # AX / Aland Islands is not in the GENC list assert set(GEOCODER.regions_for_mcc(244)) == set(['FI'])
def test_all_valid_mcc(self): for mcc in ALL_VALID_MCCS: regions = set(GEOCODER.regions_for_mcc(mcc)) assert regions != set() assert regions - GEOCODER._valid_regions == set()
def test_single(self): regions = GEOCODER.regions_for_mcc(262) self.assertEqual(set(regions), set(['DE'])) regions = GEOCODER.regions_for_mcc(262, metadata=True) self.assertEqual(set([r.code for r in regions]), set(['DE']))
def test_multiple(self): regions = GEOCODER.regions_for_mcc(311) self.assertEqual(set(regions), set(['GU', 'US'])) regions = GEOCODER.regions_for_mcc(311, metadata=True) self.assertEqual(set([r.code for r in regions]), set(['GU', 'US']))
def test_filtered(self): # AX / Aland Islands is not in the GENC list assert set(GEOCODER.regions_for_mcc(244)) == set(["FI"])
def test_all_valid_mcc(self): for mcc in ALL_VALID_MCCS: regions = set(GEOCODER.regions_for_mcc(mcc)) self.assertNotEqual(regions, set()) self.assertEqual(regions - GEOCODER._valid_regions, set())