def test_update(self): self.auth_user() branch = Branch(name="Hello") branch.save() update_data = { "name": "Sport", "parent_id": "f" * 24, "features": [{ "name": "Hours", "representation": "number" }], } response = self.client.put('/branch/{}/'.format(branch.id), data=json.dumps(update_data), content_type="application/json") self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(data["id"], str(branch.id)) self.assertEqual(data["name"], update_data["name"]) self.assertEqual(data["parent_id"], update_data["parent_id"]) self.assertEqual(len(data["features"]), 1) self.assertEqual(data["features"][0]["name"], update_data["features"][0]["name"]) self.assertEqual(data["features"][0]["representation"], update_data["features"][0]["representation"])
def post(self): args = branch_parser.parse_args() branch = BranchModel(args) try: branch.validate() except DataError, e: errors = json.loads(str(e)) return errors, 400
def test_update_error(self): branch = Branch(dict(name="Hello")) branch.save() update_data = {"name": "S"} response = self.app.put('/branch/{}'.format(branch._id), data=update_data) assert response.status_code == 400 assert "name" in response.json
def test_top_level_list(self): branch = Branch(dict(name="Hello")) branch.save() response = self.app.get('/branches') assert response.status_code == 200 assert len(response.json) == 1 item = response.json[0] assert item["id"] == str(branch.id)
def test_top_level_list(self): self.auth_user() branch = Branch(name="Hello") branch.save() response = self.client.get('/branches/') self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(len(data), 1) self.assertEqual(data[0]["id"], str(branch.id))
def test_access_error(self): branch = Branch(name="Hello") branch.save() update_data = {"name": "New name"} response = self.client.put('/branch/{}/'.format(branch.id), data=json.dumps(update_data), content_type="application/json") self.assertEqual(response.status_code, 401) self.assertEqual(response.json()["detail"], "Authentication credentials were not provided.")
def test_update_error(self): self.auth_user() branch = Branch(name="Hello") branch.save() update_data = {"name": "S"} response = self.client.put('/branch/{}/'.format(branch.id), data=json.dumps(update_data), content_type="application/json") self.assertEqual(response.status_code, 400) self.assertIn("name", response.json())
def test_sub_list(self): top_branch = Branch(dict(name="Hello")) top_branch.save() sub_branch = Branch(dict(name="Hi", parent_id=top_branch.id)) sub_branch.save() response = self.app.get('/branches?parent_id={}'.format(top_branch.id)) assert response.status_code == 200 assert len(response.json) == 1 item = response.json[0] assert item["id"] == str(sub_branch.id)
def put(self, uid): branch = BranchModel.get(uid) args = branch_parser.parse_args() for k, v in args.items(): setattr(branch, k, v) try: branch.validate() except DataError, e: errors = json.loads(str(e)) return errors, 400
def test_post(self): branch = Branch(dict( name="Hello", features=[ {"name": "distance", "representation": "number"} ], )) branch.save() create_data = { "text": "run 12km this morning. got tired", "values": { "distance": 12000, } } response = self.app.post( '/branch/{}/posts'.format(branch._id), data=create_data ) self.assertEqual(response.status_code, 201) self.assertIn("id", response.json)
def test_sub_list(self): self.auth_user() top_branch = Branch(name="Hello") top_branch.save() sub_branch = Branch(name="Hi there", parent_id=top_branch.id) sub_branch.save() response = self.client.get('/branches/?parent_id={}'.format(top_branch.id)) self.assertEqual(response.status_code, 200) data = response.json() self.assertEqual(len(data), 1) self.assertEqual(data[0]["id"], str(sub_branch.id))
def test_post(self): self.auth_user() branch = Branch( name="Hello", features=[{ "name": "distance", "representation": "number" }], ) branch.save() create_data = { "text": "run 12km this morning. got tired", "values": { "distance": 12000, } } response = self.client.post('/branch/{}/posts/'.format(branch.id), data=json.dumps(create_data), content_type="application/json") self.assertEqual(response.status_code, 201) self.assertIn("id", response.json())
def test_update(self): branch = Branch(dict(name="Hello", )) branch.save() update_data = { "name": "Sport", "parent_id": "f" * 24, "features": [{ "name": "Hours", "representation": "number" }], } response = self.app.put('/branch/{}'.format(branch._id), data=update_data) assert response.status_code == 202 data = response.json assert data["id"] == str(branch.id) assert data["name"] == update_data["name"] assert data["parent_id"] == update_data["parent_id"] self.assertEqual(len(data["features"]), 1) self.assertEqual(data["features"][0]["name"], update_data["features"][0]["name"]) self.assertEqual(data["features"][0]["representation"], update_data["features"][0]["representation"])
def get(self): filters = {} parent_id = request.args.get('parent_id') filters["parent_id"] = parent_id items = BranchModel.get_list(filters=filters) return items
def delete(self, uid): BranchModel.delete(uid) return '', 204
def get(self, uid): branch = BranchModel.get(uid) return branch.to_output()