def test_store_relationship(self): with self.app_context(): store = StoreModel('test_store') item = ItemModel('test', 19.99, 1) store.save_to_db() item.save_to_db() self.assertEqual(item.store.name, 'test_store')
def test_delete_store(self): with self.app() as client: with self.app_context(): store_name = 'test_store' client.post('/store/{0}'.format(store_name)) self.assertIsNotNone(StoreModel.find_by_name(store_name)) response = client.delete('/store/{0}'.format(store_name)) self.assertEqual(response.status_code, 200) self.assertDictEqual({'message': 'Store deleted'}, json.loads(response.data)) self.assertIsNone(StoreModel.find_by_name(store_name))
def test_store_relationships(self): with self.app_context(): name = 'test' store = StoreModel(name) item_name = 'test_item' item = ItemModel(item_name, 19.99, 1) store.save_to_db() item.save_to_db() self.assertEqual(store.items.count(), 1) self.assertEqual(store.items.first().name, item_name)
def test_crud(self): with self.app_context(): name = 'test' store = StoreModel(name) self.assertIsNone(StoreModel.find_by_name(name)) store.save_to_db() self.assertIsNotNone(StoreModel.find_by_name(name)) store.delete_from_db() self.assertIsNone(StoreModel.find_by_name(name))
def setUp(self): super(ItemSystemTest, self).setUp() with self.app() as client: with self.app_context(): # item data self.item_name = 'ChromeBook' self.item_price = 199.99 self.store_id = 1 # create a store store_name = 'Electronics' StoreModel(store_name).save_to_db() # create a user user_name = 'user' user_pswd = '1234' UserModel(user_name, user_pswd).save_to_db() # authorisation auth_request = client.post( '/auth', data=json.dumps({ 'username': user_name, 'password': user_pswd }), headers={'Content-Type': 'application/json'}) # authorisation token jwt_token = json.loads(auth_request.data)["access_token"] self.header = {'Authorization': 'JWT ' + jwt_token}
def test_store_not_found(self): with self.app() as client: with self.app_context(): store_name = 'test_store' self.assertIsNone(StoreModel.find_by_name(store_name)) response = client.get('/store/{0}'.format(store_name)) self.assertEqual(response.status_code, 404) self.assertDictEqual({'message': 'Store not found'}, json.loads(response.data))
def test_store_json_items(self): with self.app_context(): name = 'test' store = StoreModel(name) item_name = 'test_item' item = ItemModel(item_name, 19.99, 1) store.save_to_db() item.save_to_db() expected = { 'name': name, 'items': [{ 'name': item_name, 'price': 19.99 }] } self.assertDictEqual(expected, store.json())
def test_create_store(self): with self.app() as client: with self.app_context(): store_name = 'test_store' response = client.post('/store/{0}'.format(store_name)) self.assertEqual(response.status_code, 201) self.assertIsNotNone(StoreModel.find_by_name(store_name)) self.assertDictEqual({ 'name': store_name, 'items': [] }, json.loads(response.data))
def test_crud(self): with self.app_context(): StoreModel('test').save_to_db() item = ItemModel('test', 19.99, 1) self.assertIsNone( ItemModel.find_by_name('test'), "Found an item with name {}, but expected not to.".format( item.name)) item.save_to_db() self.assertIsNotNone(ItemModel.find_by_name('test')) item.delete_from_db() self.assertIsNone(ItemModel.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_items_empty(self): store = StoreModel('test') self.assertListEqual([], store.items.all(), "The store's items list is not empty")
def test_store_json(self): name = 'test' store = StoreModel(name) expected = {'name': name, 'items': []} self.assertDictEqual(expected, store.json())
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_from_db() return {'message': 'Store deleted'}
def test_create_store(self): name = 'test store' store = StoreModel(name) self.assertEqual(name, store.name, 'The name of the store after creation does not equal the ' 'constructor argument.')