def test_store_not_found(self): with self.app() as client: with self.app_context(): resp = client.get('/store/test') self.assertEqual(resp.status_code, 404) self.assertDictEqual(json.loads(resp.data), {'message': 'Store not found'}) self.assertIsNone(StoreModel.find_by_name('test'))
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_create_store(self): with self.app() as client: with self.app_context(): response = client.post('/store/test') self.assertIsNotNone(StoreModel.find_by_name('test')) self.assertEqual(response.status_code, 201) self.assertDictEqual(json.loads(response.data), { 'name': 'test', 'items': [] })
def post(self, name): if StoreModel.find_by_name(name): return {"message": "Store already exists"}, 409 store = StoreModel(name) try: store.save() except: return {"message: An error occurered while creating the store." }, 500 return store.json(), 201
def test_create_duplicate_store(self): with self.app() as client: with self.app_context(): resp1 = client.post('/store/test') self.assertIsNotNone(StoreModel.find_by_name('test')) self.assertEqual(resp1.status_code, 201) self.assertDictEqual(json.loads(resp1.data), { 'name': 'test', 'items': [] }) resp2 = client.post('/store/test') self.assertEqual(resp2.status_code, 400) self.assertDictEqual( json.loads(resp2.data), {'message': "A store with name 'test' already exists."})
def delete(self, name): store = StoreModel.find_by_name(name) if store: store.delete_from_db() return {'message': 'Store deleted'}
def get(self, name): store = StoreModel.find_by_name(name) if store: return store.json() return {'message': 'Store not found'}, 404
def get(self, name): store = StoreModel.find_by_name(name) if store: return store.json() return {"message": "Store not found"}, 404
def delete(self, name): store = StoreModel.find_by_name(name) if store: store.delete() return {"message": "Store deleted"} return {"message": "Store was not found"}, 404