def test_update_localities(self): from_es = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) from_events = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=43.6529, longitude=-79.3849, radius=50) ]) update = locality.update(from_es, from_events) last_action = update.state.localities[0].lastaction hour_ago = toUTC(datetime.now()) - timedelta(hours=1) assert update.did_update assert update.state.username == 'user1' assert last_action > hour_ago
def test_update_records_new_localities(self): from_es = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) from_events = locality.State('locality', 'user1', [ locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=52.520008, longitude=13.404954, radius=50) ]) update = locality.update(from_es, from_events) cities = [loc.city for loc in update.state.localities] assert update.did_update assert sorted(cities) == ['Berlin', 'Toronto']
def test_journaling(self): journal = locality.wrap_journal(self.es_client) test_entry = locality.Entry( 'testingid1', locality.State('locality', 't1', [ locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50), locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(minutes=30), latitude=52.520008, longitude=13.404954, radius=50) ])) journal(test_entry, self.event_index_name) self.refresh(self.event_index_name) query_iface = locality.wrap_query(self.es_client) loc_cfg = config.Localities(self.event_index_name, 30, 50.0) entry = locality.find(query_iface, 't1', loc_cfg.es_index) assert entry is not None assert entry.state.username == 't1'
def test_remove_outdated_removes_old_localities(self): test_state = locality.State('locality', 'tester1', [ locality.Locality( sourceipaddress='32.64.128.255', city='Berlin', country='DE', lastaction=toUTC(datetime.now()) - timedelta(days=10), latitude=52.520008, longitude=13.404954, radius=50), locality.Locality( sourceipaddress='1.2.3.4', city='Toronto', country='CA', lastaction=toUTC(datetime.now()) - timedelta(days=3), latitude=43.6529, longitude=-79.3849, radius=50) ]) update = locality.remove_outdated(test_state, 5) assert update.did_update assert len(test_state.localities) == 2 assert len(update.state.localities) == 1 assert update.state.localities[0].city == 'Toronto'
def state(username, locs): return geomodel.State('locality', username, locs)