def test_sensorinfo(self): sxapi = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.getSensorInfo("1234567890") call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("sensorinfo")) self.assertEqual(call[0][1]["params"]["device_id"], "1234567890")
def test_sensordata(self): t = int(time.time()) d1 = { "device_id": "1234567890", "metric": "ph", "data": [(t - 10, 2.0), (t, 2.0), (t + 10, 3.0)] } d2 = { "device_id": "1234567890", "metric": "temp", "data": [(t, 2.0), (t + 10, 3.0)] } sxapi = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.insertSensorData(**d1) call = patched_session.put.call_args_list self.assertTrue(call[0][0][0].endswith("sensordatabulk")) self.assertEqual(len(call[0][1]["json"]["sensordata"]), 1) self.assertEqual(call[0][1]["json"]["sensordata"][0]["data"], [(t - 10, 2.0), (t, 2.0), (t + 10, 3.0)]) self.assertEqual(call[0][1]["json"]["sensordata"][0]["metric"], "ph") self.assertEqual(call[0][1]["json"]["sensordata"][0]["device_id"], "1234567890") with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.insertSensorDataBulk([d1, d2]) call = patched_session.put.call_args_list self.assertTrue(call[0][0][0].endswith("sensordatabulk")) self.assertEqual(len(call[0][1]["json"]["sensordata"]), 2) self.assertEqual(call[0][1]["json"]["sensordata"][0]["data"], [(t - 10, 2.0), (t, 2.0), (t + 10, 3.0)]) self.assertEqual(call[0][1]["json"]["sensordata"][0]["metric"], "ph") self.assertEqual(call[0][1]["json"]["sensordata"][0]["device_id"], "1234567890") self.assertEqual(call[0][1]["json"]["sensordata"][1]["data"], [(t, 2.0), (t + 10, 3.0)]) self.assertEqual(call[0][1]["json"]["sensordata"][1]["metric"], "temp") self.assertEqual(call[0][1]["json"]["sensordata"][1]["device_id"], "1234567890") with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.getSensorData("1234567890", "ph", t - 30 * 24 * 60 * 60, t) call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("sensordatabulk")) self.assertEqual(call[0][1]["params"]["device_id"], "1234567890") self.assertEqual(call[0][1]["params"]["metrics"], ["ph"]) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.getLastSensorDataBulk("1234567890", ["ph", "temp"]) call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("lastsensordata")) self.assertEqual(call[0][1]["params"]["device_id"], "1234567890") self.assertEqual(call[0][1]["params"]["metrics"], ["ph", "temp"])
def test_status(self): api = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: r = api.get_public_status() self.assertTrue(r) call = patched_session.get.call_args_list self.assertEqual(call[0][0][0], "http://0.0.0.0:8989/publicapi/v1/service/status") with mock.patch('sxapi.low.BaseAPI.session') as patched_session: r = api.get_private_status() self.assertTrue(r) call = patched_session.get.call_args_list self.assertEqual(call[0][0][0], "http://0.0.0.0:8787/internapi/v0/")
def test_events(self): sxapi = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.getLastEventTimestamps("1234567890") call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("lasteventtimestamps")) self.assertEqual(call[0][1]["params"]["device_id"], "1234567890") with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.insertEvent("1234567890", 101, 1480773600, 6.5, {"foo": "bar"}) call = patched_session.put.call_args_list self.assertTrue(call[0][0][0].endswith("event")) self.assertEqual(call[0][1]["json"]["device_id"], "1234567890") self.assertEqual(call[0][1]["json"]["metadata"], {"foo": "bar", "value": 6.5}) self.assertEqual(call[0][1]["json"]["event_type"], 101) self.assertEqual(call[0][1]["json"]["level"], 10) self.assertEqual(call[0][1]["json"]["timestamp"], 1480773600) self.assertEqual(call[0][1]["json"]["disable_hooks"], 0) sxapi.updateEventMeta("1234567890", "123", {"foo": "fooz", "bar": "hurtz"}) call = patched_session.post.call_args_list self.assertTrue(call[0][0][0].endswith("event")) self.assertEqual(call[0][1]["json"]["metadata"], {"foo": "fooz", "bar": "hurtz"})
def test_device(self): sxapi = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.getDevice("1234567890") call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("device")) self.assertEqual(call[0][1]["params"]["device_id"], "1234567890") self.assertEqual(call[0][1]["params"]["with_animal"], 1) self.assertEqual(call[0][1]["params"]["with_organisation"], 1) self.assertEqual(call[0][1]["params"]["with_allmeta"], 1) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.setDeviceMeta("1234567890", {"hello": "world"}) call = patched_session.post.call_args_list self.assertTrue(call[0][0][0].endswith("devicemetadata")) self.assertEqual(call[0][1]["json"]["device_id"], "1234567890") self.assertEqual(call[0][1]["json"]["metadata"], {"hello": "world"})
def test_annotation(self): sxapi = InternAPI(endpoint=self.INTERN_ENDPOINT, public_endpoint=self.PUBLIC_ENDPOINT, api_key=self.API_KEY) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.get_annotation_definitions() call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("/annotation/definition")) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.get_annotation("myannoid") call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("/annotation/id")) self.assertEqual(call[0][1]["params"]["annotation_id"], "myannoid") with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.get_annotations_by_class("health", 1480773600, 1480773610) call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("/annotation/query")) self.assertEqual(call[0][1]["params"]["annotation_class"], "health") self.assertEqual(call[0][1]["params"]["from_date"], 1480773600) self.assertEqual(call[0][1]["params"]["to_date"], 1480773610) self.assertEqual(call[0][1]["params"]["offset"], 0) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.get_animal_annotations("myanimal", 1480773600, 1480773610) call = patched_session.get.call_args_list self.assertTrue(call[0][0][0].endswith("/annotation/query")) self.assertEqual(call[0][1]["params"]["animal_id"], "myanimal") self.assertEqual(call[0][1]["params"]["from_date"], 1480773600) self.assertEqual(call[0][1]["params"]["to_date"], 1480773610) self.assertEqual(call[0][1]["params"]["offset"], 0) with mock.patch('sxapi.low.BaseAPI.session') as patched_session: sxapi.insert_animal_annotation("myanimal", 34, 35, ["health", "desease"], [{"foo": "bar"}]) call = patched_session.put.call_args_list self.assertTrue(call[0][0][0].endswith("/annotation/animal")) self.assertEqual(call[0][1]["json"]["animal_id"], "myanimal") self.assertEqual(call[0][1]["json"]["ts"], 34) self.assertEqual(call[0][1]["json"]["end_ts"], 35) self.assertEqual(call[0][1]["json"]["classes"], ["health", "desease"]) self.assertEqual(call[0][1]["json"]["attributes"], [{"foo": "bar"}])