def test_account_group_delete(self): input_data = add_user_groups() token = self.login() resp = self.client.post("api/v1/account/group/add", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) FakeDataCreation().insert_device_group(5, 0) my_user_group = db.session.query(sdUserGroup).filter(sdUserGroup.id.in_(["1", "2"])).all() rel_list = [] for num in range(1, 6): query_obj = db.session.query(sdDeviceGroup).filter(sdDeviceGroup.id == num).first() rel_list.append(query_obj) for user_group in my_user_group: user_group.rel_dg_ug = rel_list db.session.commit() input_list = dict(user_groups=["1", "2"]) resp = self.client.delete("api/v1/account/group/delete", data=json.dumps(input_list), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) self.assert200(resp) self.assertEqual(json.loads(resp.data.decode()), {"ret_code": 0, "ret_desc": "OK"})
def test_group_delete(self): FakeDataCreation().insert_device_group(5, 0).insert_device(20) delete_data = {"device_groups": ["1", "2", "3"]} token = self.login() resp = self.client.delete("/api/v1/device/group/delete", data=json.dumps(delete_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) logger.debug("Delete Reponse " + resp.status) data = json.loads(resp.data.decode()) expect = ret.http_resp(ret.RET_OK) stmt = db.select([sdDeviceGroup.id]) db_changed = [i[0] for i in db.session.execute(stmt).fetchall()] self.assert200(resp) self.assertEqual(data, expect) # test db changed self.assertEqual(db_changed, [4, 5])
def test_group_update(self): FakeDataCreation().insert_device_group(5, 0) rename_data = {"device_groups": [dict(id=str(count), name=f"{name}", display_name="McDonald") for count, name in zip(range(1, 4), ["hungry", "milk tea", "buddha tea"])]} token = self.login() resp = self.client.put("/api/v1/device/group/update", data=json.dumps(rename_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) logger.debug("Rename Response " + resp.status) data = json.loads(resp.data.decode()) query = db.select([func.count()]).where(sdDeviceGroup.display_name == 'McDonald') db_changed = db.session.execute(query).fetchone()[0] expect = ret.http_resp(ret.RET_OK) self.assert200(resp) self.assertEqual(data, expect) # test db changed self.assertEqual(db_changed, 3)
def test_ug_leave_dg(self): # TAG add device group FakeDataCreation().insert_device_group(5, 0).insert_device(20) # TAG add usergroup input_data = add_user_groups() token = self.login() resp = self.client.post("api/v1/account/group/add", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) # TAG join usergroup input_data = {"user_groups": ["2", "3"]} resp = self.client.put("/api/v1/device/group/2/join_ug", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) # TAG leave usergroup input_data = {"user_groups": ["2"]} resp = self.client.put("/api/v1/device/group/2/leave_ug", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) data = json.loads(resp.data.decode()) expect = ret.http_resp(ret.RET_OK) self.assert200(resp) self.assertEqual(data, expect) query = db.session.query(rel_dg_ug).filter(rel_dg_ug.c.device_group_id == 2).all() self.assertTrue([(2, 3)] == query)
def test_add_device(self): expect = { 'ret_code': 0, 'ret_desc': 'OK', 'devices': [{ 'id': "1", 'name': 'testname_1' }, { 'id': "2", 'name': 'testname_2' }, { 'id': "3", 'name': 'testname_3' }] } FakeDataCreation().insert_device_model(2) input_data = compose_input_for_device(3) token = self.login() resp = self.client.post("/api/v1/device/add", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) self.assertEqual(json.loads(resp.data.decode()), expect) # add newcase for testing miss led or controller input_data = { "devices": [ dict(id=f"{10}", name=f"testname_{10}", display_name="淡水廠的路燈", comment="這是測試device的小天地,@#$%^&*[]_=+-*", wgs_x=27.0, wgs_y=29.0, address="write something") ] } token = self.login() resp = self.client.post("/api/v1/device/add", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) expect = { 'ret_code': 0, 'ret_desc': 'OK', 'devices': [{ 'id': '4', 'name': 'testname_10' }] } self.assertEqual(json.loads(resp.data.decode()), expect)
def test_group_getall(self): FakeDataCreation().insert_device_group(5, 0).insert_device(20) expect = ExpectResponse().compose_response_body().count(5, 20).group_id().name().display_name().user_groups()\ .devices().response() token = self.login() resp = self.client.get("/api/v1/device/group/getall", headers={"Authorization": f"Bearer {token}"}) logger.debug(resp.status) # If not 200, this will crash data = json.loads(resp.data.decode()) # Expect response is HTTP status 200 self.assert200(resp) self.assertEqual(data, expect)
def test_getdetail_device(self): FakeDataCreation().insert_device_group(5, 0).insert_device(10) getdetail_data = {"devices": ["1", "2", "3"]} token = self.login() resp = self.client.post("/api/v1/device/getdetail", data=json.dumps(getdetail_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) logger.debug("get detail Reponse " + resp.status) text = resp.data.decode() json_text = json.loads(text) json_text = json.dumps(json_text, indent=4) ref_ans = ref_json() ref_ans = json.dumps(ref_ans, indent=4) self.assertEqual(json_text, ref_ans)
def test_getall_geojson(self): FakeDataCreation().insert_device_group(1, 0) add_device_geojson_to_db(self.max_device) ref_json = add_device_geojson_to_db_refjson() token = self.login() resp = self.client.post("/api/v1/device/getall", data=json.dumps({ "user": self.user_name, "max_device": self.max_device }), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) json_text = json.loads( resp.data.decode()) # for comparing with reference answer ref_json = json.dumps(ref_json, indent=4) json_text = json.dumps(json_text, indent=4) self.assertEqual(json_text, ref_json)
def test_group_getdetail(self): FakeDataCreation().insert_schedule_group(2).insert_schedule_item().insert_device_group(5, 5).insert_device(20) token = self.login() input_data = {"device_groups": ["1", "2"]} resp = self.client.post("/api/v1/device/group/getdetail", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) logger.debug(resp.status) # If not 200, this will crash data = json.loads(resp.data.decode()) with open(file_dir + r"\stubs\device_group_getdetail.json", "r", encoding="utf-8") as reader: expect = json.loads(reader.read()) # Expect response is HTTP status 200 self.assert200(resp) self.assertEqual(data, expect)
def test_group_getallsummary(self): # FakeDataCreation().insert_device_group(5, 0).insert_device(20) FakeDataCreation().insert_schedule_group(6).insert_schedule_item().insert_device_group(5, 0) device_group = db.session.query(sdDeviceGroup).filter(sdDeviceGroup.id == 1).first() user_group = db.session.query(sdUserGroup).filter(sdUserGroup.id == 1).first() user_group.rel_dg_ug.append(device_group) db.session.commit() # device_group.user_groups.append(user_group) token = self.login() resp = self.client.get("/api/v1/device/group/getallsummary", headers={"Authorization": f"Bearer {token}"}) logger.debug(resp.status) # If not 200, this will crash rawdata = json.loads(resp.data.decode()) # dg = rawdata.get("device_groups") sorted_data = sorted(rawdata.get("device_groups"), key=lambda issue: issue.get("id")) rawdata["device_groups"] = sorted_data data = json.dumps(rawdata, indent=4) expect = device_group_getallsummary_ref_ans() expect = json.dumps(expect, indent=4) self.assert200(resp) self.assertEqual(data, expect)
def test_group_join(self): FakeDataCreation().insert_device_group(5, 0).insert_device(20) input_data = {"devices": ["17", "18", "19"]} token = self.login() resp = self.client.put("/api/v1/device/group/3/join", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) logger.debug("Join Group {} {}".format(resp.status, input_data['devices'])) data = json.loads(resp.data.decode()) expect = ret.http_resp(ret.RET_OK) self.assert200(resp) self.assertEqual(data, expect) query = db.select([sdDevice.id]).where(sdDevice.device_group_id == 3) devices = [device_id[0] for device_id in db.session.execute(query).fetchall()] self.assertTrue([9, 10, 11, 12, 17, 18, 19] == devices)
def test_update_device(self): FakeDataCreation().insert_device_group(1, 0).insert_device(5) # There are 3 device in db, and I put 5 device into update # So 3 device in db will be changed, and 2 device will be add input_data = compose_input_for_device(5) token = self.login() resp = self.client.put("/api/v1/device/update", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) data = json.loads(resp.data.decode()) self.assert200(resp) self.assertEqual(data, {'ret_code': 0, 'ret_desc': 'OK'}) stmt = db.select([sdDevice.id ]).where(sdDevice.display_name == "淡水廠的路燈") db_changed = [i[0] for i in db.session.execute(stmt).fetchall()] stmt = db.select([sdDevice.wgs_x, sdDevice.wgs_y, sdDevice.address]) point = [tuple(i) for i in db.session.execute(stmt).fetchall()] # Total 5 leds and 5 controllers db_led = db.session.execute(db.select([func.count(sdLed.id) ])).fetchone()[0] db_controller = db.session.execute( db.select([func.count(sdController.id)])).fetchone()[0] self.assertEqual(db_changed, [1, 2, 3, 4, 5]) self.assertEqual(set(point), {(27.0, 29.0, 'write something')}) self.assertEqual(db_led, 5) self.assertEqual(db_controller, 5) # change only device input_data = { "devices": [ dict(id=f"{1}", name=f"testname_{1}", display_name="安泰路燈", comment="安泰的路燈好棒棒@#$%^&*[]_=+-*", wgs_x=23.5, wgs_y=77.2, address="紅樹林") ] } token = self.login() resp = self.client.put("/api/v1/device/update", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) expect = {'ret_code': 0, 'ret_desc': 'OK'} self.assertEqual(json.loads(resp.data.decode()), expect) input_data = { "devices": [ dict(id=f"{1}", name=f"testname_{1}", display_name="安泰路燈", comment="安泰的路燈好棒棒@#$%^&*[]_=+-*", wgs_x=23.5, wgs_y=77.2, address="紅樹林", controller={ "model_name": "GPD002-000GT", "name": "ABCCEDFG", "display_name": "2019/12/19", "comment": "controller xorowo", "serial_no": "BX0007" }) ] } token = self.login() resp = self.client.put("/api/v1/device/update", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) expect = {'ret_code': 0, 'ret_desc': 'OK'} self.assertEqual(json.loads(resp.data.decode()), expect) input_data = { "devices": [ dict(id=f"{1}", name=f"testname_{1}", display_name="安泰路燈", comment="安泰的路燈好棒棒@#$%^&*[]_=+-*", wgs_x=23.5, wgs_y=77.2, address="紅樹林", led={ "model_name": "LM1109-I1FGT", "name": "5698", "display_name": "2019/12/19", "comment": "pluha", "serial_no": "A7kjL" }) ] } token = self.login() resp = self.client.put("/api/v1/device/update", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) expect = {'ret_code': 0, 'ret_desc': 'OK'} self.assertEqual(json.loads(resp.data.decode()), expect)