def test_crud(self): with self.app_context(): store = StoreModel("test_store") self.assertIsNone(StoreModel.find_by_name("test_store")) store.save_to_db() self.assertIsNotNone(StoreModel.find_by_name("test_store")) store.delete_from_db() self.assertIsNone(StoreModel.find_by_name("test_store"))
def test_create_store(self): with self.app() as client: with self.app_context(): resp = client.post("/store/test_store") self.assertEqual(resp.status_code, 201) self.assertIsNotNone(StoreModel.find_by_name("test_store")) self.assertDictEqual( { "id": 1, "name": "test_store", "items": [] }, json.loads(resp.data))
def post(self, name): if StoreModel.find_by_name(name): return { 'message': "A store with name '{}' already exists.".format(name) }, 400 store = StoreModel(name) try: store.save_to_db() except: return {"message": "An error occurred creating the store."}, 500 return store.json(), 201
def test_find_store(self): with self.app() as client: with self.app_context(): resp = client.get("/store/test_store") self.assertEqual(resp.status_code, 404) self.assertDictEqual({"message": "Store not found"}, json.loads(resp.data)) client.post("/store/test_store") resp = client.get("/store/test_store") self.assertEqual(resp.status_code, 200) self.assertDictEqual( { "id": 1, "name": "test_store", "items": [] }, json.loads(resp.data)) self.assertIsNotNone(StoreModel.find_by_name("test_store"))
def get(self, name): store = StoreModel.find_by_name(name) if store: return store.json(), 200 return {'message': 'Store not found'}, 404
def delete(self, name): store = StoreModel.find_by_name(name) if store: store.delete_from_db() return {'message': 'Store deleted'}, 200