def test_POST_nodes_populate(self): """ Test that populate options creates the valid number of readings data """ populate_number = 3 site = SiteSeeder.seed_empty_site() args = { 'site_id': site.id, 'latitude': 13.2, 'longitude': 23.2, 'populate': populate_number } node = NodeSeeder.seed_node('ricefield', **args) url = UrlHelper.get_url(flapp, 'nodes') r = requests.get(url) api_response = ApiResponseHelper.assert_api_response(r) assert api_response.ok nodes = map(lambda n: n.json(), Node.query.all()) # get readings?sensor_id=xxxx and check whether the number of the value of readings is equal to 3 for n in nodes: for s in n['sensors']: url = UrlHelper.get_url(flapp, 'readings', sensor_id=s['id']) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.ok assert len(api_response.objects) == populate_number
def test_GET_nonexisting_reading_by_id_failure(self): """ GET /reading/<int> """ url = UrlHelper.get_url(flapp, 'reading', 191230) response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_DELETE_node_by_id(self): node = NodeSeeder.seed_node('empty') url = UrlHelper.get_url(flapp, 'node', node.id) response = requests.delete(url) assert response.ok response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success=False)
def test_DELETE_node_by_id(self): node = NodeSeeder.seed_node('empty') url = UrlHelper.get_url(flapp, 'node', node.id) response = requests.delete(url) assert response.ok response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_GET_reading_by_nodeid_but_no_sensoralias_failure(self): """ GET /reading?node_id=<int> This call should return an API error as specifying node_id but no sensor_alias is insuficcient to complete the query """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) url = UrlHelper.get_url(flapp, 'readings', **{'node_id' : node.id}) response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_POST_create_node_with_nonexisting_site(self): url = UrlHelper.get_url(flapp, 'node') data = { 'alias': 'mynode', 'site_id': 1111111111, 'latitude': 13.2, 'longitude': 23.2 } response = requests.post(url, data=data) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success=False)
def test_GET_reading_by_sensoralias_but_no_nodeid_failure(self): """ GET /reading?sensor_alias=<str> This call should return an API error as specifying sensor_alias but no node_id is insufficient to complete the query """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] url = UrlHelper.get_url(flapp, 'readings', **{'sensor_alias' : sensor.alias}) response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_GET_reading_by_sensor_id_with_until_date_filtering_success(self): """ GET /reading?sensor_id=<int>&from=2014-1-1&until=2014-2-1 """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] for day in range(20): Reading.create(sensor = sensor, value = 17, timestamp = datetime(2014, 12, 1) + timedelta(days=day)) url = UrlHelper.get_url(flapp, 'readings', **{'sensor_id' : sensor.id, 'until': '2014-12-1'}) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) readings_in_interval = map(lambda r: r.json(), filter(lambda r: r.timestamp <= datetime(2014,12,1), sensor.readings)) api_response = ApiResponseHelper.assert_api_response(response) assert sorted(readings_in_interval) == sorted(api_response.objects)
def test_GET_existing_site_by_id(self): site = SiteSeeder.seed_empty_site() url = UrlHelper.get_url(flapp, 'site', site.id) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.first() == site.json()
def test_POST_empty_site(self): url = UrlHelper.get_url(flapp, 'site') data = {'alias': 'Kamogawa_east'} response = requests.post(url, data=data) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) print api_response.objects assert api_response.first()['alias'] == 'Kamogawa_east'
def test_DELETE_site(self): site = SiteSeeder.seed_empty_site() url = UrlHelper.get_url(flapp, 'site', site.id) response = requests.delete(url) assert response.ok response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_POST_empty_site(self): url = UrlHelper.get_url(flapp, 'site') data = {'alias':'Kamogawa_east'} response = requests.post(url, data = data) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) print api_response.objects assert api_response.first()['alias'] == 'Kamogawa_east'
def test_POST_create_node_with_existing_site(self): site = SiteSeeder.seed_empty_site() url = UrlHelper.get_url(flapp, 'node') data = {'alias':'mynode', 'site_id': site.id, 'latitude' : 13.2, 'longitude': 23.2} response = requests.post(url, data = data) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) node = Node.query.first().json() assert node == api_response.first()
def test_DELETE_site(self): site = SiteSeeder.seed_empty_site() url = UrlHelper.get_url(flapp, 'site', site.id) response = requests.delete(url) assert response.ok response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response( response, expect_success=False)
def test_correct_post_reading(self): assert len(Reading.query.all()) == 0 node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] timestamp_str = datetime.now().strftime(DATETIME_FORMATS[0]) url = UrlHelper.get_url(flapp, 'reading') print url response = requests.post(url, data = {'sensor_id': sensor.id, 'value': 42, 'timestamp': timestamp_str}) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert len(Reading.query.all()) == 1
def test_GET_existing_node_by_id(self): """ GET /node/1 This call should return the node with id=1 """ node = NodeSeeder.seed_node('empty') url = UrlHelper.get_url(flapp, 'node', node.id) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.first() == node.json()
def test_GET_nodes_ApiResponse_status(self): """ Test that GET /node/all gives a JSON response that has no errors in it, and that an ApiResponse object can be instantiated from the response body. """ for i in range(5): NodeSeeder.seed_ricefield_node(n_readings = 3) url = UrlHelper.get_url(flapp, 'node', 'all') r = requests.get(url) api_response = ApiResponseHelper.assert_api_response(r) assert api_response.ok nodes = map(lambda n: n.json(), Node.query.all()) assert sorted(nodes) == sorted(api_response.objects)
def test_GET_readings_by_sensorid_with_nonexisting_sensor_response_empty(self): """ GET /reading?sensor_id=<int> This call should return no readings as sensor 100 does not exist """ NodeSeeder.seed_ricefield_node(n_readings = 3) url = UrlHelper.get_url(flapp, 'readings', **{'sensor_id' : 100}) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.first() == None
def test_GET_reading_by_sensorid_with_existing_sensor_success(self): """ GET /reading?sensor_id=<int> This call should return all the readings belonging to the sensor (in this case three readings) """ node = NodeSeeder.seed_ricefield_node(n_readings = 3) sensor = node.sensors[0] url = UrlHelper.get_url(flapp, 'readings', **{'sensor_id' : sensor.id}) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert sorted(map(lambda x: x.json(), sensor.readings)) == sorted(api_response.objects)
def test_GET_readings_by_nodeid_and_sensoralias_success(self): """ GET /reading?node_id=<int>&sensor_alias=<str> This call should return all readings belonging to the sensor in the specified node, in this case two readings """ node = NodeSeeder.seed_ricefield_node(n_readings = 3) sensor = node.sensors[0] url = UrlHelper.get_url(flapp, 'readings', **{'node_id' : node.id, 'sensor_alias' : sensor.alias}) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert sorted(map(lambda x: x.json(), sensor.readings)) == sorted(api_response.objects)
def test_post_reading_timestamp_integrity(self): """ Tests that timestamps are stored with the accuracy with which they were provided """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] for timestamp_format in DATETIME_FORMATS: timestamp = datetime.now() timestamp_str = timestamp.strftime(timestamp_format) post_url = UrlHelper.get_url(flapp, 'reading') ### POST reading with timestamp to API post_response = requests.post(post_url, data = {'sensor_id': sensor.id, 'value': 42, 'timestamp': timestamp_str}) post_response = ApiResponseHelper.assert_api_response(post_response) reading_id = post_response.objects[0]['id'] ### GET that same reading and check that the timestamp is correct get_url = UrlHelper.get_url(flapp, 'reading', reading_id) get_response = requests.get(get_url) get_response = ApiResponseHelper.assert_api_response(get_response) received_timestamp_str = get_response.objects[0]['timestamp'] received_timestamp = DatetimeHelper.convert_timestamp_to_datetime(received_timestamp_str) assert datetime.strptime(timestamp_str, timestamp_format) == received_timestamp
def test_GET_nodes_ApiResponse_status(self): """ Test that GET /node/all gives a JSON response that has no errors in it, and that an ApiResponse object can be instantiated from the response body. """ for i in range(5): NodeSeeder.seed_ricefield_node(n_readings=3) url = UrlHelper.get_url(flapp, 'node', 'all') r = requests.get(url) api_response = ApiResponseHelper.assert_api_response(r) assert api_response.ok nodes = map(lambda n: n.json(), Node.query.all()) assert sorted(nodes) == sorted(api_response.objects)
def test_GET_existing_reading_by_id_success(self): """ GET /reading/<int> """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] reading = Reading.create(sensor = sensor, value = 17) url = UrlHelper.get_url(flapp, 'reading', 1) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.first() == reading.json()
def test_GET_readings_by_sensor_id_with_nonsensical_time_parameters_returns_no_objects(self): """ GET /reading?node_id=<int>&sensor_alias=<str>&from=2014-12-10&until=2013-1-1 This call should return no objects as the specified until date is before the from_date """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] start_date = datetime(2014, 12, 1) for day in range(20): Reading.create(sensor = sensor, value = 17, timestamp = start_date + timedelta(days=day)) url = UrlHelper.get_url(flapp, 'readings', **{'node_id' : node.id, 'sensor_alias': sensor.alias, 'from': '2014-12-2', 'until': '2013-1-1'}) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.first() == None
def test_POST_nodes_populate(self): """ Test that populate options creates the valid number of readings data """ populate_number = 3 site = SiteSeeder.seed_empty_site() args = {'site_id': site.id, 'latitude': 13.2, 'longitude': 23.2, 'populate': populate_number} node = NodeSeeder.seed_node('ricefield', **args) url = UrlHelper.get_url(flapp, 'nodes') r = requests.get(url) api_response = ApiResponseHelper.assert_api_response(r) assert api_response.ok nodes = map(lambda n: n.json(), Node.query.all()) # get readings?sensor_id=xxxx and check whether the number of the value of readings is equal to 3 for n in nodes: for s in n['sensors']: url = UrlHelper.get_url(flapp, 'readings', sensor_id = s['id']) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) assert api_response.ok assert len(api_response.objects) == populate_number
def test_POST_create_node_with_existing_site(self): site = SiteSeeder.seed_empty_site() url = UrlHelper.get_url(flapp, 'node') data = { 'alias': 'mynode', 'site_id': site.id, 'latitude': 13.2, 'longitude': 23.2 } response = requests.post(url, data=data) assert response.ok api_response = ApiResponseHelper.assert_api_response(response) node = Node.query.first().json() assert node == api_response.first()
def test_GET_reading_by_node_id_and_sensor_alias_with_interval_time_filtering_success(self): """ GET /reading?node_id=<int>&sensor_alias=<str>&from=2014-12-2&until=2014-12-11 This call should return all the readings created in the interval from 2014-12-2 until 2014-12-11 for a sensor in the specified node """ node = NodeSeeder.seed_ricefield_node(n_readings = 0) sensor = node.sensors[0] start_date = datetime(2014, 12, 1) for day in range(20): Reading.create(sensor = sensor, value = 17, timestamp = start_date + timedelta(days=day)) url = UrlHelper.get_url(flapp, 'readings', **{'node_id' : node.id, 'sensor_alias': sensor.alias, 'from': '2014-12-2', 'until': '2014-12-11'}) response = requests.get(url) assert response.ok readings_in_interval = map(lambda r: r.json(), filter(lambda r: r.timestamp >= datetime(2014,12,2) and r.timestamp <= datetime(2014,12,11), sensor.readings)) api_response = ApiResponseHelper.assert_api_response(response) assert sorted(readings_in_interval) == sorted(api_response.objects)
def test_GET_nonexisting_node_by_id(self): url = UrlHelper.get_url(flapp, 'node', '123123123') response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_POST_create_node_with_nonexisting_site(self): url = UrlHelper.get_url(flapp, 'node') data = {'alias':'mynode', 'site_id': 1111111111, 'latitude' : 13.2, 'longitude': 23.2} response = requests.post(url, data = data) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_GET_nonexisting_site_by_id(self): url = UrlHelper.get_url(flapp, 'site', 123017) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response(response, expect_success = False)
def test_GET_nonexisting_node_by_id(self): url = UrlHelper.get_url(flapp, 'node', '123123123') response = requests.get(url) assert response.ok ApiResponseHelper.assert_api_response(response, expect_success=False)
def test_GET_nonexisting_site_by_id(self): url = UrlHelper.get_url(flapp, 'site', 123017) response = requests.get(url) assert response.ok api_response = ApiResponseHelper.assert_api_response( response, expect_success=False)