def test_item_json(self): with self.app_context(): store = StoreModel('test store') item = ItemModel('test item', 19.99, 1) store.save_to_db() item.save_to_db() item_json = item.json() expected = {'name': 'test item', 'price': 19.99} self.assertDictEqual(expected, item_json)
def test_crud(self): with self.app_context(): StoreModel('test').save_to_db() item = ItemModel('test', 19.99, 1) # verifica se o item não existe no banco de dados # o terceiro argumento é uma mensagem de erro personalizada, que pode ajudar muito no decorrer das execuções de testes self.assertIsNone( ItemModel.find_by_name('test'), f'Found an item with name {item.name!r} on database, but expected not to.' ) item.save_to_db() # verifica se o item existe no banco de dados self.assertIsNotNone( ItemModel.find_by_name('test'), f'Not found an item with name {item.name!r} on database, but expected to.' ) item.delete_from_db() # verifica se o item não existe no banco de dados self.assertIsNone( ItemModel.find_by_name('test'), f'Found an item with name {item.name!r} on database, but expected not to.' )
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 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 get(self): """ 유저 상점 아이템 보여주는 API """ items = StoreModel.objects(user=UserModel.objects( id=get_jwt_identity()).first()).all() return self.unicode_safe_json_dumps([{ 'cItemName': item.itemName, 'cItemNum': item.itemNum, 'money': item.money } for item in items], 200)
def post(self): itemName = request.json['itemName'] itemNum = request.json['itemNum'] money = request.json['money'] details = request.json['details'] StoreModel(user=UserModel.objects(id=get_jwt_identity()).first(), itemName=itemName, itemNum=itemNum, money=money, details=details).save() return '', 201
def post(self): """ 관리자 상점 추가 """ itemName = request.json['itemName'] itemNum = request.json['itemNum'] # 개당 가격 money = request.json['money'] details = request.json['details'] StoreModel(itemName=itemName, itemNum=itemNum, money=money, details=details).save() return '', 201
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 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 setUp(self): # on that point, we build a bridge to the BaseTest.setUp(), # so, we can access his properties, because, without that, # the ItemTest setUp() will override the BaseTest setUp() super(ItemTest, self).setUp() with self.app() as client: with self.app_context(): auth_data = {'username': '******', 'password': '******'} auth_header = {'Content-Type': 'application/json'} StoreModel('test store').save_to_db() UserModel('test', '1234').save_to_db() auth_response = client.post('/auth', data = json.dumps(auth_data), headers = auth_header) auth_token = json.loads(auth_response.data)['access_token'] self.access_token = f'JWT {auth_token}' self.header = { 'Authorization': self.access_token }
def test_create_store_items_empty(self): store = StoreModel('test') self.assertListEqual(store.items.all(), [])
def delete(self, name): store = StoreModel.find_by_name(name) if store: store.delete_from_db() return {'message': 'Store deleted'}
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
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 test_create_store(self): store = StoreModel('test') self.assertEqual(store.name, 'test')