def test_group_add(self): sample_data = [("麥當勞", "雞塊旁的路燈"), ("肯德基", "雞米花上的路燈"), ("漢堡王", "小華堡裡的路燈")] input_data = {"device_groups": [dict(name=name[0], display_name=name[1]) for count, name in zip(range(3), sample_data)]} token = self.login() resp = self.client.post("/api/v1/device/group/add", data=json.dumps(input_data), content_type="application/json", headers={"Authorization": f"Bearer {token}"}) data = json.loads(resp.data.decode()) expect = ExpectResponse().compose_response_body().count(3, 0).group_id().name().response() expect['device_groups'] = [dict(id=str(count+1), name=name[0]) for count, name in zip(range(3), sample_data)] self.assertStatus(resp, 201, message=None) self.assertEqual(data, expect) stmt = db.select([sdDeviceGroup.name, sdDeviceGroup.display_name]).where(sdDeviceGroup.cust_id == 1) db_changed = [dict(name=name, display_name=display_name) for name, display_name in db.session.execute(stmt).fetchall()] # test db changed self.assertEqual(len(db_changed), 3) self.assertEqual(db_changed, input_data['device_groups'])
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_account_update(self): # There are 9 users in db (id: 1 ~ 9), and 5 updated users # updated users id: 2,5,8 ; added users id: 10, 11 user_insertion() input_data = update_user_generate() token = self.login() resp = self.client.put("api/v1/account/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([sdUser.id]).where(sdUser.display_name == "update_name") db_changed = [i[0] for i in db.session.execute(stmt).fetchall()] self.assertEqual(db_changed, [2, 5, 8, 10, 11])
def test_account_group_update(self): # TAG add into DB 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 update DB update_data = update_user_groups() resp = self.client.put("api/v1/account/group/update", data=json.dumps(update_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([sdUserGroup.id]) db_changed = [i[0] for i in db.session.execute(stmt).fetchall()] self.assertEqual(db_changed, [3, 4, 1, 2])
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_account_delete(self): user_insertion() delete_data = {"users": ["2", "3", "4"]} token = self.login() resp = self.client.delete("/api/v1/account/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([sdUser.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, [1, 5, 6, 7, 8, 9])
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)
def _likes_count_expression(cls): return (db.select([ db.func.count(likes.c.username).label("likes_count") ]).where(likes.c.recipe_id == cls.id).label("sum_likes"))
def _remix_count_expression(cls): q = db.select([db.func.count(Recipe.parent_id)]).\ where(Recipe.parent_id == cls.id).\ label("remix_count") return q