def test_addition_to_drop_point(self): dp_number = 3 first_description = "here" first_lat = -23.5 first_lng = 84 first_level = 3 second_description = "there" second_lat = 3.14 second_lng = -2.71828 second_level = 2 first_time = datetime.today() - timedelta(hours=2) second_time = datetime.today() - timedelta(hours=2 - 1) dp = DropPoint(dp_number, description=first_description, lat=first_lat, lng=first_lng, level=first_level, time=first_time) db.session.commit() self.assertEqual(len(dp.locations), 1, "Drop point does not have exactly one location.") self.assertEqual( db.session.query(Location).count(), 1, "Not exactly one location in the database.") second_location = Location(dp, description=second_description, lat=second_lat, lng=second_lng, level=second_level, time=second_time) db.session.commit() self.assertEqual(len(dp.locations), 2, "Drop point does not have exactly two locations.") self.assertEqual( db.session.query(Location).count(), 2, "Not exactly two locations in the database.") self.assertEqual( dp.get_current_location(), second_location, "Current drop point location is not second location.") self.assertEqual(dp.locations[1].time - dp.locations[0].time, second_time - first_time, "Wrong time difference between locations.")
def setUp(self): super().setUp() self.dp = DropPoint(dp_number, time=time, description=description, lat=lat, lng=lng, level=level) db.session.commit()
def test_dp_created_in_future(): with pytest.raises(ValueError) as e: time_in_future = datetime.today() + timedelta(hours=1) DropPoint(dp_number + 1, time=time_in_future, lat=0, lng=0, level=1) assert "future" in str(e)
def test_construction_exceptions(self): dp = DropPoint(1, lat=0, lng=0, level=1) with self.assertRaisesRegexp(ValueError, "drop point"): Location("foo") time_in_future = datetime.today() + timedelta(hours=1) with self.assertRaisesRegexp(ValueError, "future"): Location(dp, time=time_in_future, lat=0, lng=0, level=1) with self.assertRaisesRegexp(ValueError, "not a datetime"): Location(dp, time="foo", lat=0, lng=0, level=1) start_time = datetime.today() with self.assertRaisesRegexp(ValueError, "older than current"): Location(dp, time=start_time, lat=0, lng=0, level=1) db.session.commit() Location(dp, time=start_time - timedelta(hours=1)) invalid_lat = ("foo", -180, 91, None) invalid_lng = ("bar", -181, 251.5, None) invalid_level = ("quux", None) for lat in invalid_lat: with self.assertRaisesRegexp(ValueError, "lat"): Location(dp, lat=lat, lng=0, level=1) for lng in invalid_lng: with self.assertRaisesRegexp(ValueError, "lng"): Location(dp, lat=0, lng=lng, level=1) for level in invalid_level: with self.assertRaisesRegexp(ValueError, "level"): Location(dp, lat=0, lng=0, level=level) too_long = "a" * (Location.max_description + 1) with self.assertRaisesRegexp(ValueError, "too long"): Location(dp, lat=0, lng=0, level=1, description=too_long)
def test_construction_exceptions(self): actions = Visit.actions dp = DropPoint(1, lat=0, lng=0, level=1) with self.assertRaisesRegexp(ValueError, "drop point"): Visit(None) with self.assertRaisesRegexp(ValueError, "action"): Visit(dp) time_in_future = datetime.today() + timedelta(hours=1) with self.assertRaisesRegexp(ValueError, "future"): Visit(dp, time=time_in_future, action=actions[0]) with self.assertRaisesRegexp(ValueError, "not a datetime"): Visit(dp, time="foo", action=actions[0]) with self.assertRaisesRegexp(ValueError, "action"): Visit(dp, action="whatever")
def test_construction_exceptions(self): states = Report.states dp = DropPoint(1, lat=0, lng=0, level=1) with self.assertRaisesRegexp(ValueError, "drop point"): Report(None) with self.assertRaisesRegexp(ValueError, "state"): Report(dp) time_in_future = datetime.today() + timedelta(hours=1) with self.assertRaisesRegexp(ValueError, "future"): Report(dp, time=time_in_future, state=states[0]) with self.assertRaisesRegexp(ValueError, "not a datetime"): Report(dp, time="foo", state=states[0]) with self.assertRaisesRegexp(ValueError, "state"): Report(dp, state="whatever")
def test_construction(self): actions = Visit.actions dp = DropPoint(1, lat=0, lng=0, level=1) first_time = datetime.today() first_action = actions[1] first_visit = Visit(dp, action=first_action, time=first_time) db.session.commit() self.assertEqual( first_visit.time, first_time, "Visit creation time not as expected." ) self.assertEqual( first_visit.action, first_action, "Visit action not as expected." ) self.assertEqual( dp.visits[0], first_visit, "First visit not first visit of associated drop point." ) self.assertEqual( dp.get_last_visit(), first_visit, "get_last_visit() did not return first visit." ) report_time = datetime.today() report_state = Report.states[0] report = Report(dp, state=report_state, time=report_time) second_time = datetime.today() second_action = actions[0] second_visit = Visit(dp, action=second_action, time=second_time) db.session.commit() self.assertEqual( second_visit.action, second_action, "Visit action not as expected." ) self.assertEqual( dp.visits[-1], second_visit, "Second visit not last visit of associated drop point." ) self.assertEqual( dp.get_last_visit(), second_visit, "get_last_visit() did not return second visit." ) self.assertNotEqual( dp.get_last_state(), report_state, "get_last_state() returns unchanged state after visit." ) self.assertEqual( dp.get_new_report_count(), 0, "get_new_report_count() nonzero after visit." ) self.assertFalse( dp.get_new_reports(), "get_new_reports() returned something not False after visit." ) self.assertEqual( dp.get_last_report(), report, "get_last_report() did not return report after visit." ) self.assertEqual( dp.get_total_report_count(), 1, "get_total_report_count() not as expected after visit." )
def test_dps_json_is_not_empty(): assert len(DropPoint.get_dps_json()) > 1
def test_dp_number_unique(): with pytest.raises(ValueError) as e: DropPoint(dp_number, lat=0, lng=0, level=1) assert "already exists" in str(e)
def test_dp_illegal_number(): for num in [-1, "foo", None]: with pytest.raises(ValueError) as e: DropPoint(num, lat=0, lng=0, level=1) assert "number" in str(e)
def test_dps_json_before_creation_not_empty(): assert DropPoint.get_dps_json(time - timedelta(seconds=1)) != {}
def test_dp_invalid_creation_time(): with pytest.raises(ValueError) as e: DropPoint(dp_number + 1, time="foo", lat=0, lng=0, level=1) assert "not a datetime" in str(e)
def test_dps_json_since_creation_empty(): assert DropPoint.get_dps_json(time) == "{}"
def test_fresh_dps_json_is_empty(): assert DropPoint.get_dps_json(datetime.today()) == "{}"
def test_dp_getter_returns_dp(self): assert DropPoint.get(dp_number) == self.dp
def test_dps_json_is_string(): assert type(DropPoint.get_dps_json()) is str
def test_dp_getter_return_none_for_nonexistent(): for num in [-1, "foo", None]: assert DropPoint.get(num) is None