def test_that_invalid_location_gives_a_json_404(self): response = self.client.get(self.BASE_PATH + 'invalid/') assert_equal(404, response.status_code) data = decode_json(response.content) assert_equal( {'detail': 'Invalid location: "invalid". See locations endpoint.'}, data)
def test_that_no_start_parameter_is_an_error(self): response = self.client.get( self.PATH + 'liverpool/?end=2014-01-01T00:00:00Z') assert_equal(400, response.status_code) assert_equal( 'Missing parameter `start`. Format: 2014-11-30T00:00:00Z', decode_json(response.content)['detail'])
def test_that_each_location_has_correct_fields(self): expected_fields = set(['name', 'slug', 'url', 'min_depth', 'under_keal']) self.response = self.client.get(self.PATH) for location in decode_json(self.response.content)['locations']: assert_equal(expected_fields, set(location.keys()))
def test_that_no_start_parameter_is_an_error(self): response = self.client.get( self.BASE_PATH + 'liverpool/?end=2014-01-01T00:00:00Z') assert_equal(400, response.status_code) assert_equal( 'Missing parameter `start`. Format: 2014-11-30T00:00:00Z', decode_json(response.content)['detail'])
def _get_locations(self): response = self.client.get(self.PATH) assert_equal(200, response.status_code) return set([ data['slug'] for data in decode_json(response.content)['locations'] ])
def test_that_end_filter_includes_previous_datetime(self): path = self._make_path(9, 0, 10, 1) data = decode_json(self.client.get(path).content)['raw_measurements'] assert_equal([{ 'datetime': '2014-06-10T10:00:00Z', 'height': 1.23 }], data)
def test_that_interval_must_be_below_61(self): response = self._get_response_for_interval('61') assert_equal(400, response.status_code) assert_equal( {'detail': 'Invalid interval: must be between 1 and 60 minutes'}, decode_json(response.content))
def test_that_the_end_parameter_filters_exclusively(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:02:00Z') data = decode_json(response.content) assert_not_in('2014-06-17T09:02:00Z', [t['datetime'] for t in data['tide_levels']])
def test_that_latest_returns_record(self): with freeze_time("2014-06-01T10:00:00Z"): response = self.client.get( self.BASE_PATH + 'liverpool/latest/') data = decode_json(response.content) assert_equal(1, len(data['sea_levels']))
def test_that_tide_window_records_have_correct_structure(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-01T09:00:00Z' '&end=2014-06-01T11:00:00Z' '&tide_level=10.7') data = decode_json(response.content) tide_windows = data['tide_windows'] expected = { 'start': { 'datetime': '2014-06-01T10:01:00Z', 'tide_level': 10.9 }, 'end': { 'datetime': '2014-06-01T10:02:00Z', 'tide_level': 10.8 }, 'high_tide': { 'datetime': '2014-06-01T10:01:00Z', 'tide_level': 10.9 }, 'duration': { 'total_seconds': 120 } } assert_equal(expected, tide_windows[0])
def test_that_each_location_has_correct_fields(self): expected_fields = set( ['name', 'slug', 'url', 'min_depth', 'under_keal']) self.response = self.client.get(self.PATH) for location in decode_json(self.response.content)['locations']: assert_equal(expected_fields, set(location.keys()))
def test_that_non_integer_interval_gives_http_400(self): response = self._get_response_for_interval('foo') assert_equal(400, response.status_code) assert_equal( {'detail': 'Invalid interval: expected integer (minutes)'}, decode_json(response.content))
def test_that_envelope_has_tide_levels_field(self): response = self.client.get( self.PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) assert_in('tide_levels', data)
def test_that_five_minute_interval_works(self): response = self._get_response_for_interval('5') tide_levels = decode_json(response.content)['tide_levels'] assert_equal(12, len(tide_levels)) assert_equal('2014-06-01T10:30:00Z', tide_levels[0]['datetime']) assert_equal('2014-06-01T10:35:00Z', tide_levels[1]['datetime']) assert_equal('2014-06-01T10:40:00Z', tide_levels[2]['datetime'])
def test_that_results_are_limited_to_24_hours_1440_records(self): response = self.client.get( self.BASE_PATH + 'liverpool/' '?start=2014-06-01T00:00:00Z' '&end=2014-06-03T00:00:00Z' ) data = decode_json(response.content) assert_equal(24 * 60, len(data['tide_levels']))
def test_that_no_location_gives_link_to_locations_endpoint(self): response = self.client.get(self.PATH) assert_equal(404, response.status_code) data = decode_json(response.content) assert_equal( {'detail': 'No location given, see locations endpoint.'}, data )
def test_that_invalid_location_gives_a_json_404(self): response = self.client.get(self.PATH + 'invalid/') assert_equal(404, response.status_code) data = decode_json(response.content) assert_equal( {'detail': 'Invalid location: "invalid". See locations endpoint.'}, data )
def test_that_user_without__add_weather__permission_gets_403(self): token = Token.objects.get(user__username='******').key response = self._post_json( [self.good_data], HTTP_AUTHORIZATION='Token {}'.format(token)) assert_equal(403, response.status_code) assert_equal( {'detail': 'You do not have permission to perform this action.'}, decode_json(response.content))
def test_that_missing_tide_level_param_gives_400_error(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-01T09:00:00Z' '&end=2014-06-01T11:05:00Z') data = decode_json(response.content) assert_equal(400, response.status_code) assert_equal( {'detail': 'Missing required query parameter `tide_level`'}, data)
def test_that_tides_are_given_for_southampton(self): response = self.client.get(self.BASE_PATH + 'southampton/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) tide_levels = data['tide_levels'] assert_equal( [4.0, 4.3, 4.1], # Southampton values [t['tide_level'] for t in tide_levels])
def test_that_end_filter_includes_previous_datetime(self): path = self._make_path(9, 0, 10, 1) data = decode_json(self.client.get(path).content)['raw_measurements'] assert_equal([ { 'datetime': '2014-06-10T10:00:00Z', 'height': 1.23 }], data)
def test_that_tides_are_given_for_liverpool(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) tide_levels = data['tide_levels'] assert_equal( [10.3, 10.8, 10.9], # Liverpool values [t['tide_level'] for t in tide_levels])
def test_that_now_searches_from_now_to_24_hours(self): with freeze_time("2014-06-01T10:00:00Z"): response = self.client.get(self.BASE_PATH + 'liverpool/now/') data = decode_json(response.content) assert_equal(5, len(data['sea_levels'])) assert_equal('2014-06-01T10:00:00Z', data['sea_levels'][0]['datetime']) assert_equal('2014-06-01T10:04:00Z', data['sea_levels'][-1]['datetime'])
def test_that_now_searches_from_now_to_24_hours(self): with freeze_time("2014-06-01T10:00:00Z"): response = self.client.get(self.BASE_PATH + 'liverpool/now/?tide_level=4.5') data = decode_json(response.content) assert_equal(1, len(data['tide_windows'])) assert_equal(('2014-06-01T10:00:00Z', 4.50, '2014-06-01T10:05:00Z', 4.55, '2014-06-01T10:03:00Z', 5.1, 360), self.parse_window(data['tide_windows'][0]))
def test_that_datetime_with_seconds_is_rejected(self): doc = {"datetime": "2014-06-10T10:34:45Z", "height": 0.23} response = self._post_json([doc]) assert_equal(400, response.status_code) assert_equal([{ 'datetime': [ 'Datetime has wrong format. Use one of these ' 'formats instead: YYYY-MM-DDThh:mm:00Z.' ] }], decode_json(response.content))
def test_that_missing_tide_level_param_gives_400_error(self): response = self.client.get( self.PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) assert_equal(400, response.status_code) assert_equal( {'detail': 'Missing required query parameter `tide_level`'}, data)
def test_that_measurements_can_be_retrieved_for_gauge_2(self): path = (BASE_PATH + 'gladstone-2/' '?start=2014-06-10T09:00:00Z&end=2014-06-10T11:00:00Z') data = decode_json(self.client.get(path).content) assert_equal( [ {'datetime': '2014-06-10T10:00:00Z', 'height': 5.67}, {'datetime': '2014-06-10T10:30:00Z', 'height': 5.89} ], data['raw_measurements'])
def test_that_default_interval_is_one_minute(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-01T00:00:00Z' '&end=2014-06-03T00:00:00Z') tide_levels = decode_json(response.content)['tide_levels'] assert_equal(60, len(tide_levels)) assert_equal('2014-06-01T10:30:00Z', tide_levels[0]['datetime']) assert_equal('2014-06-01T10:31:00Z', tide_levels[1]['datetime']) assert_equal('2014-06-01T10:32:00Z', tide_levels[2]['datetime'])
def test_that_tide_level_records_have_correct_structure(self): response = self.client.get(self.EXAMPLE_FULL_PATH) data = decode_json(response.content) tide_levels = data['tide_levels'] expected = { 'datetime': '2014-06-17T09:00:00Z', 'tide_level': 10.3, 'is_high_tide': False, } assert_equal(expected, tide_levels[0])
def test_that_tides_are_given_for_liverpool(self): response = self.client.get( self.BASE_PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) tide_levels = data['tide_levels'] assert_equal( [10.3, 10.8, 10.9], # Liverpool values [t['tide_level'] for t in tide_levels] )
def test_that_the_end_parameter_filters_exclusively(self): response = self.client.get( self.BASE_PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:02:00Z' ) data = decode_json(response.content) assert_not_in( '2014-06-17T09:02:00Z', [t['datetime'] for t in data['tide_levels']] )
def test_that_now_searches_from_now_to_24_hours(self): with freeze_time("2014-06-01T10:00:00Z"): response = self.client.get( self.PATH + 'liverpool/now/') data = decode_json(response.content) assert_equal(5, len(data['tide_levels'])) assert_equal('2014-06-01T10:00:00Z', data['tide_levels'][0]['datetime']) assert_equal('2014-06-01T10:04:00Z', data['tide_levels'][-1]['datetime'])
def test_that_results_are_ordered_by_datetime(self): response = self.client.get(self.BASE_PATH + 'liverpool/' '?start=2014-06-01T00:00:00Z' '&end=2014-06-02T00:00:00Z') data = decode_json(response.content) datetimes = [t['datetime'] for t in data['tide_levels']] assert_equal([ '2014-06-01T10:30:00Z', '2014-06-01T10:31:00Z', '2014-06-01T10:32:00Z', '2014-06-01T10:33:00Z', '2014-06-01T10:34:00Z' ], datetimes)
def test_that_measurements_can_be_retrieved_for_gauge_2(self): path = (BASE_PATH + 'gladstone-2/' '?start=2014-06-10T09:00:00Z&end=2014-06-10T11:00:00Z') data = decode_json(self.client.get(path).content) assert_equal([{ 'datetime': '2014-06-10T10:00:00Z', 'height': 5.67 }, { 'datetime': '2014-06-10T10:30:00Z', 'height': 5.89 }], data['raw_measurements'])
def test_that_non_visible_locations_arent_listed(self): hidden_location = self.create_location(slug='hidden', name='hidden', visible=False) self.response = self.client.get(self.PATH) endpoint_slugs = set([l['slug'] for l in decode_json( self.response.content)['locations']]) assert_equal(set(['southampton', 'liverpool']), endpoint_slugs) hidden_location.delete()
def test_that_tides_are_given_for_southampton(self): response = self.client.get( self.BASE_PATH + 'southampton/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) tide_levels = data['tide_levels'] assert_equal( [4.0, 4.3, 4.1], # Southampton values [t['tide_level'] for t in tide_levels] )
def test_that_tide_level_records_have_correct_structure(self): response = self.client.get( self.PATH + 'liverpool/' '?start=2014-06-17T09:00:00Z' '&end=2014-06-17T09:05:00Z') data = decode_json(response.content) tide_levels = data['tide_levels'] expected = { 'datetime': '2014-06-17T09:00:00Z', 'tide_level': 10.3 } assert_equal(expected, tide_levels[0])
def test_that_default_interval_is_one_minute(self): response = self.client.get( self.BASE_PATH + 'liverpool/' '?start=2014-06-01T00:00:00Z' '&end=2014-06-03T00:00:00Z' ) tide_levels = decode_json(response.content)['tide_levels'] assert_equal(60, len(tide_levels)) assert_equal('2014-06-01T10:30:00Z', tide_levels[0]['datetime']) assert_equal('2014-06-01T10:31:00Z', tide_levels[1]['datetime']) assert_equal('2014-06-01T10:32:00Z', tide_levels[2]['datetime'])
def test_that_datetime_with_seconds_is_rejected(self): doc = { "datetime": "2014-06-10T10:34:45Z", "height": 0.23 } response = self._post_json([doc]) assert_equal(400, response.status_code) assert_equal( [{ 'datetime': ['Datetime has wrong format. Use one of these ' 'formats instead: YYYY-MM-DDThh:mm:00Z.'] }], decode_json(response.content))
def test_that_no_supplier_is_rejected(self): doc = { "datetime": "2014-06-10T10:34:00Z", "surge_level": 0.23, } response = self._post_json([doc]) assert_equal(400, response.status_code) assert_equal( { 'detail': 'Failed to deserialize item [0].', 'provider': ['This field is required.'] }, decode_json(response.content))
def test_that_root_api_has_correct_links(self): response = self.client.get('/1/') data = decode_json(response.content) expected_paths = [ '/1/locations/', '/1/predictions/tide-levels/', '/1/predictions/tide-windows/'] assert_equal( set([self.expand_path(p) for p in expected_paths]), set([link['href'] for link in data['links']]) )
def test_that_non_visible_locations_arent_listed(self): hidden_location = self.create_location(slug='hidden', name='hidden', visible=False) self.response = self.client.get(self.PATH) endpoint_slugs = set([ l['slug'] for l in decode_json(self.response.content)['locations'] ]) assert_equal(set(['southampton', 'liverpool']), endpoint_slugs) hidden_location.delete()
def test_that_now_searches_from_now_to_24_hours(self): with freeze_time("2014-06-01T10:00:00Z"): response = self.client.get( self.BASE_PATH + 'liverpool/now/?tide_level=4.5') data = decode_json(response.content) assert_equal(1, len(data['tide_windows'])) assert_equal( ('2014-06-01T10:00:00Z', 4.50, '2014-06-01T10:05:00Z', 4.55, '2014-06-01T10:03:00Z', 5.1, 360), self.parse_window(data['tide_windows'][0]) )
def test_that_tide_level_records_have_correct_structure(self): response = self.client.get(self.EXAMPLE_FULL_PATH) data = decode_json(response.content) sea_levels = data['sea_levels'] expected = { u'datetime': u'2014-06-17T09:00:00Z', u'predicted_tide_level': 10.3, u'predicted_is_high': False, u'predicted_surge_level': None, u'predicted_sea_level': None, u'observed_sea_level': None, u'derived_surge_level': None, } assert_equal(expected, sea_levels[0])
def test_that_locations_are_serialized_correctly(self): self.response = self.client.get(self.PATH) expected = [ { 'name': 'Liverpool', 'slug': 'liverpool', 'url': 'http://testserver/1/locations/liverpool/', }, { 'name': 'Southampton', 'slug': 'southampton', 'url': 'http://testserver/1/locations/southampton/' }] assert_equal(expected, decode_json(self.response.content)['locations'])
def test_that_datetime_with_seconds_is_rejected(self): doc = { "datetime": "2014-06-10T10:34:45Z", "surge_level": 0.23 } response = self._post_json([doc]) assert_equal(400, response.status_code) assert_equal( { 'detail': 'Failed to deserialize item [0].', 'datetime': ['Datetime has wrong format. Use one of these ' 'formats instead: YYYY-MM-DDThh:mm:00Z.'] }, decode_json(response.content))
def test_that_tide_level_records_have_correct_structure(self): response = self.client.get(self.EXAMPLE_FULL_PATH) data = decode_json(response.content) sea_levels = data['sea_levels'] expected = { u'datetime': u'2014-06-17T09:00:00Z', u'predicted_tide_level': 10.3, u'predicted_is_high': False, u'predicted_surge_level': None, u'predicted_sea_level': None, u'predicted_deviation': None, u'observed_sea_level': None, u'derived_surge_level': None, } assert_equal(expected, sea_levels[0])
def test_that_locations_are_serialized_correctly(self): self.response = self.client.get(self.PATH) expected = [{ 'name': 'Liverpool', 'slug': 'liverpool', 'url': 'http://testserver/1/locations/liverpool/', 'min_depth': 2.1, 'under_keal': 4.5, }, { 'name': 'Southampton', 'slug': 'southampton', 'url': 'http://testserver/1/locations/southampton/', 'min_depth': 2.1, 'under_keal': 4.5, }] assert_equal(expected, decode_json(self.response.content)['locations'])
def test_that_results_are_ordered_by_datetime(self): response = self.client.get( self.BASE_PATH + 'liverpool/' '?start=2014-06-01T00:00:00Z' '&end=2014-06-02T00:00:00Z' ) data = decode_json(response.content) datetimes = [t['datetime'] for t in data['tide_levels']] assert_equal( [ '2014-06-01T10:30:00Z', '2014-06-01T10:31:00Z', '2014-06-01T10:32:00Z', '2014-06-01T10:33:00Z', '2014-06-01T10:34:00Z' ], datetimes)
def test_that_datetime_with_seconds_is_rejected(self): doc = { "datetime": "2014-06-10T10:34:45Z", "surge_level": 0.23, "provider": "met_office", } response = self._post_json([doc]) assert_equal(400, response.status_code) assert_equal( { 'detail': 'Failed to deserialize item [0].', 'datetime': [ 'Datetime has wrong format. Use one of these ' 'formats instead: YYYY-MM-DDThh:mm:00Z.' ] }, decode_json(response.content))
def test_that_root_api_has_correct_links(self): response = self.client.get('/1/') data = decode_json(response.content) expected_paths = [ '/1/locations/', '/1/observations/weather/', '/1/predictions/tide-levels/', '/1/predictions/tide-windows/', '/1/predictions/weather/', '/1/sea-levels/', '/1/schedule/', ] assert_equal( set([self.expand_path(p) for p in expected_paths]), set([link['href'] for link in data['links']]) )
def test_that_identical_start_and_end_gives_no_results(self): path = self._make_path(10, 0, 10, 0) data = decode_json(self.client.get(path).content)['raw_measurements'] assert_equal([], data)
def test_that_end_filter_excludes_end_datetime(self): path = self._make_path(9, 0, 10, 0) data = decode_json(self.client.get(path).content)['raw_measurements'] assert_equal([], data)
def test_that_no_location_gives_link_to_locations_endpoint(self): response = self.client.get(self.BASE_PATH) assert_equal(404, response.status_code) data = decode_json(response.content) assert_equal({'detail': 'No location given, see locations endpoint.'}, data)