def test_item_json(self): item = ItemModel('test', 19.99) expected = {'name': 'test', 'price': 19.99} self.assertEqual( item.json(), expected, "The JSON export of the item is incorrect. Received {}, expected {}." .format(item.json(), expected))
def test_jason(self): store = StoreModel('costco') item = ItemModel('pc', 1500, 1) store.save_to_db() item.save_to_db() expected = {'name': 'costco', 'items': [{'name': 'pc', 'price': 1500}]} self.assertEqual(store.json(), expected)
def test_store_relashionship(self): with self.app_context: store = StoreModel('costco') item = ItemModel('pc', 1500, 1) store.save_to_db() item.save_to_db() self.assertEqual(item.store.name, 'costco')
def test_relationship_items(self): with self.app_contex(): store = StoreModel('costco') item = ItemModel('pc', 1500, 1) store.save_to_db() item.save_to_db() self.assertEqual(store.items.count(), 1) self.assertEqual(store.items.first().name, 'pc')
def test_jason(self): store = StoreModel('costco') item = ItemModel('pc', 1500, 1) expected = [] self.assertEqual(store.json(), expected)
def test_get_item(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 19.99, 1).save_to_db() resp = client.get('/item/test', headers={'Authorization': self.access_token}) self.assertEqual(resp.status_code, 200)
def test_delete_item(self): with self.app() as client: with self.app_context(): StoreModel('costco').save_to_db() ItemModel('pc', 1500).save_to_db() request = client.delete('/item/pc') self.assertEqual(request.status_code, 200) self.assertDictEqual({'message': 'Item deleted'}, json.loads(request.data))
def test_delete_item(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 19.99, 1).save_to_db() resp = client.delete('/item/test') self.assertEqual(resp.status_code, 200) self.assertDictEqual({'message': 'Item deleted'}, json.loads(resp.data))
def test_put_update_item(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 5.99, 1).save_to_db() self.assertEqual(ItemModel.find_by_name('test').price, 5.99) resp = client.put('/item/test', data={ 'price': 17.99, 'store_id': 1 }) self.assertEqual(resp.status_code, 200) self.assertEqual(ItemModel.find_by_name('test').price, 17.99) self.assertDictEqual({ 'name': 'test', 'price': 17.99 }, json.loads(resp.data))
def test_store_found_with_items(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 19.99, 1).save_to_db() resp = client.get('/store/test') self.assertEqual(resp.status_code, 200) self.assertDictEqual({'id': 1, 'name': 'test', 'items': [{'name': 'test', 'price': 19.99}]}, json.loads(resp.data))
def test_create_item(self): item = ItemModel('test', 19.99) self.assertEqual( item.name, 'test', "The name of the item after creation does not equal the constructor argument." ) self.assertEqual( item.price, 19.99, "The price of the item after creation does not equal the constructor argument." )
def test_put_update_item(self): with self.app() as client: with self.app_context(): StoreModel('costco').save_to_db() ItemModel('pc', 1450, 1) request = client.get('/item/pc') self.assertDictEqual({'item': [{ 'name': 'pc', 'price': 1500 }]}, json.loads(request.data))
def test_crud(self): with self.app_context(): StoreModel('costco').save_to_db() item = ItemModel('costco', 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 test_item_list(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 5.99, 1).save_to_db() resp = client.get('/items') self.assertDictEqual( {'items': [{ 'name': 'test', 'price': 5.99 }]}, json.loads(resp.data))
def test_create_duplicate_item(self): with self.app() as client: with self.app_context(): StoreModel('costco').save_to_db() ItemModel('pc', 1500, 1).save_to_db() request = client.post('/item/pc', data={ 'name': 'pc', 'store_id': 1 }) self.assertEqual(request.status_code, 400) self.assertDictEqual( {'message': 'n item with name pc already exists.'}, json.loads(request.data))
def put(self, name): data = Item.parser.parse_args() item = ItemModel.find_by_name(name) if item is None: item = ItemModel(name, **data) else: item.price = data['price'] item.save_to_db() return item.json()
def test_store_found_with_items(self): with self.app() as client: with self.app_context(): StoreModel('costco').save_to_db() ItemModel('pc', 1500, 1).save_to_db() request = client.get('/store/costco') self.assertEqual(request.status_code, 200) self.assertDictEqual( { 'message': 'costco', 'item': [{ 'name': 'pc', 'price': 1500 }] }, json.loads(request.data))
def test_item_list(self): with self.app() as client: with self.app_context(): StoreModel('costco').save_to_db() ItemModel('pc', 1450, 1) request = client.put('/item/pc', data={ 'price': 1500, 'store_id': 1 }) self.assertEqual(request.status_code, 200) self.assertDictEqual({ 'name': 'pc', 'price': 1500 }, json.loads(request.data))
def test_create_duplicate_item(self): with self.app() as client: with self.app_context(): StoreModel('test').save_to_db() ItemModel('test', 17.99, 1).save_to_db() resp = client.post('/item/test', data={ 'price': 17.99, 'store_id': 1 }) self.assertEqual(resp.status_code, 400) self.assertDictEqual( {'message': 'An item with name \'test\' already exists.'}, json.loads(resp.data))
def test_store_found_with_items(self): with self.app as client: # to access the db with self.app_context(): StoreModel('test').save_to_db() ItemModel('testItem', 19.99, 1).save_to_db() response = client.get('/store/test') self.assertEqual(response.status_code, 200) self.assertDictEqual( { 'name': 'test', 'items': [{ 'name': 'testItem', 'price': 19.99 }] }, json.loads(response.data.decode('utf-8')))
def post(self, name): if ItemModel.find_by_name(name): return {'message': "An item with name '{}' already exists.".format(name)}, 400 data = Item.parser.parse_args() item = ItemModel(name, **data) try: item.save_to_db() except: return {"message": "An error occurred inserting the item."}, 500 return item.json(), 201
def delete(self, name): item = ItemModel.find_by_name(name) if item: item.delete_from_db() return {'message': 'Item deleted'}
def get(self, name): item = ItemModel.find_by_name(name) if item: return item.json() return {'message': 'Item not found'}, 404
def test_create_item(self): with self.app_context(): item_test = ItemModel('salt', 3.50) item_test.save_to_db() self.assertIsNotNone(ItemModel.find_by_name('salt'))
def test_create_json(self): test_item = ItemModel("salt", 3.20) expected_json = {"name": "salt", "price": 3.20} self.assertEqual(expected_json, test_item.json())
def test_create_item(self): item = ItemModel('test', 19.99) self.assertEqual(item.name, 'test', 'this did not work') self.assertEqual(item.price, 19.99)
def test_item_json(self): item = ItemModel('test', 19.99) expected = jsonify({'name': 'test', 'price': 19.99}) self.assertEqual(item.json(), expected)
def test_create_item(self): test_item = ItemModel("salt", 3.20) self.assertEqual(test_item.name, "salt") self.assertEqual(test_item.price, 3.20)