def test_create_supplier(self): """ Create a new Supplier """ supplier = SupplierFactory() resp = self.app.post( "/suppliers", json=supplier.serialize(), content_type="application/json" ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = resp.headers.get("Location", None) self.assertIsNotNone(location) app.logger.debug(location) # Check the data is correct new_supplier = resp.get_json() self.assertEqual(new_supplier["name"], supplier.name, "Name does not match") self.assertEqual(new_supplier["category"], supplier.category, "Category does not match") self.assertEqual(new_supplier["address"], supplier.address, "Address does not match") self.assertEqual(new_supplier["email"], supplier.email, "Email does not match") self.assertEqual(new_supplier["phone_number"], supplier.phone_number, "Phone does not match") self.assertEqual(new_supplier["products"], supplier.products, "Product does not match") resp = self.app.get(location, content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_200_OK) new_supplier = resp.get_json() self.assertEqual(new_supplier["name"], supplier.name, "Names does not match") self.assertEqual(new_supplier["category"], supplier.category, "Category does not match") self.assertEqual(new_supplier["address"], supplier.address, "Address does not match") self.assertEqual(new_supplier["email"], supplier.email, "Email does not match") self.assertEqual(new_supplier["phone_number"], supplier.phone_number, "Phone does not match") self.assertEqual(new_supplier["products"], (supplier.products), "Product does not match")
def test_unsupported_media_type(self): """ Send wrong media type """ supplier = SupplierFactory() resp = self.app.post( "/suppliers", json=supplier.serialize(), content_type="test/html" ) self.assertEqual(resp.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE)
def _create_suppliers(self, count): """ Factory method to create suppliers in bulk """ suppliers = [] for _ in range(count): supplier = SupplierFactory() resp = self.app.post( "/suppliers", json=supplier.serialize(), content_type="application/json" ) self.assertEqual( resp.status_code, status.HTTP_201_CREATED, "Could not create test Supplier" ) new_supplier = resp.get_json() supplier.id = new_supplier["id"] suppliers.append(supplier) return suppliers
def test_find_by_email(self): """ Find Suppliers by email """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() results = Supplier.find_by_email(suppliers[0].email) self.assertNotEqual(results, []) self.assertEqual(results[0].email, suppliers[0].email)
def test_find_by_address(self): """ Find Suppliers by address """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() results = Supplier.find_by_address(suppliers[0].address) self.assertNotEqual(results, []) self.assertEqual(results[0].address, suppliers[0].address)
def test_find_by_name(self): """ Find a Supplier by Name """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() results = Supplier.find_by_name(suppliers[0].name) self.assertNotEqual(results, []) self.assertEqual(results[0].name, suppliers[0].name)
def test_find_by_phone_number(self): """ Find Suppliers by phone_number """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() results = Supplier.find_by_phone_number(suppliers[0].phone_number) self.assertNotEqual(results, []) self.assertEqual(results[0].phone_number, suppliers[0].phone_number)
def test_bad_request(self): """ Send wrong media type """ supplier = SupplierFactory() resp = self.app.post( "/suppliers", json={"name": "not enough data"}, content_type="application/json" ) self.assertEqual(resp.status_code, status.HTTP_400_BAD_REQUEST)
def test_find_or_404_found(self): """ Find or return 404 found """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() supplier = Supplier.find_or_404(suppliers[1].id) self.assertIsNot(supplier, None) self.assertEqual(supplier.id, suppliers[1].id) self.assertEqual(supplier.name, suppliers[1].name)
def test_update_supplier(self): """ Update an existing Supplier """ # create a supplier to update test_supplier = SupplierFactory() resp = self.app.post( "/suppliers", json=test_supplier.serialize(), content_type="application/json" ) self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # update the supplier new_supplier = resp.get_json() new_supplier["category"] = "unknown" resp = self.app.put( "/suppliers/{}".format(new_supplier["id"]), json=new_supplier, content_type="application/json", ) self.assertEqual(resp.status_code, status.HTTP_200_OK) updated_supplier = resp.get_json() self.assertEqual(updated_supplier["category"], "unknown")
def test_find_by_category(self): """ Find Suppliers by Category """ suppliers = SupplierFactory.create_batch(3) suppliers[0].category = "home furnishings" suppliers[0].create() suppliers[1].category = "health & beauty" suppliers[1].create() suppliers[2].category = "health & beauty" suppliers[2].create() results = Supplier.find_by_category("health & beauty") logging.debug(results) self.assertNotEqual(results, []) self.assertEqual(results[0].category, "health & beauty")
def test_reset_supplier_database(self): """ RESET Suppliers Database """ supplier = SupplierFactory.create_batch(3) resp = self.app.delete( "/suppliers/reset", content_type="application/json" ) self.assertEqual(resp.status_code, status.HTTP_204_NO_CONTENT) self.assertEqual(len(resp.data), 0) # make sure they are deleted resp = self.app.get( "/suppliers/{}", content_type="application/json" ) self.assertEqual(resp.status_code, status.HTTP_404_NOT_FOUND)
def _create_supplier(self, products=[]): """ Creates a supplier from a Factory """ fake_supplier = SupplierFactory() supplier = Supplier(name=fake_supplier.name, category=fake_supplier.category, address=fake_supplier.address, email=fake_supplier.email, phone_number=fake_supplier.phone_number, preferred=fake_supplier.preferred, products=products) self.assertTrue(supplier != None) self.assertEqual(supplier.id, None) return supplier
def test_delete_a_supplier(self): """ Delete a Supplier """ supplier = SupplierFactory() supplier.create() self.assertEqual(len(Supplier.all()), 1) # delete the supplier and make sure it isn't in the database supplier.delete() self.assertEqual(len(Supplier.all()), 0)
def test_create_a_supplier(self): """ Create a Supplier and assert that it exists """ fake_supplier = SupplierFactory() supplier = Supplier(name=fake_supplier.name, category=fake_supplier.category, address=fake_supplier.address, email=fake_supplier.email, phone_number=fake_supplier.phone_number, preferred=fake_supplier.preferred) self.assertTrue(supplier != None) self.assertEqual(supplier.id, None) self.assertEqual(supplier.name, fake_supplier.name) self.assertEqual(supplier.address, fake_supplier.address) self.assertEqual(supplier.email, fake_supplier.email) self.assertEqual(supplier.phone_number, fake_supplier.phone_number)
def test_find_by_preferred(self): """ Find a Supplier by Preferred """ suppliers = SupplierFactory.create_batch(3) suppliers[0].preferred = "false" suppliers[0].create() suppliers[1].name = "Mark Jacob" suppliers[1].preferred = "true" suppliers[1].create() suppliers[2].preferred = "false" suppliers[2].create() results = Supplier.find_by_preferred("true") logging.debug(results) self.assertNotEqual(results, []) self.assertEqual(results[0].name, "Mark Jacob") self.assertEqual(results[0].preferred, "true")
def test_find_supplier(self): """ Find a Supplier by ID """ suppliers = SupplierFactory.create_batch(3) for supplier in suppliers: supplier.create() logging.debug(suppliers) # make sure they got saved self.assertEqual(len(Supplier.all()), 3) # find the 2nd supplier in the list supplier = Supplier.find(suppliers[1].id) self.assertIsNot(supplier, None) self.assertEqual(supplier.id, suppliers[1].id) self.assertEqual(supplier.name, suppliers[1].name) self.assertEqual(supplier.category, suppliers[1].category) self.assertEqual(supplier.email, suppliers[1].email) self.assertEqual(supplier.address, suppliers[1].address) self.assertEqual(supplier.phone_number, suppliers[1].phone_number)
def test_update_a_supplier(self): """ Update a Supplier """ supplier = SupplierFactory() logging.debug(supplier) supplier.create() logging.debug(supplier) self.assertEqual(supplier.id, 1) # Change it an save it supplier.category = "suppliers" original_id = supplier.id supplier.save() self.assertEqual(supplier.id, original_id) self.assertEqual(supplier.category, "suppliers") # Fetch it back and make sure the id hasn't changed # but the data did change suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) self.assertEqual(suppliers[0].id, 1) self.assertEqual(suppliers[0].category, "suppliers")
def test_mark_supplier_preference(self): """ Mark a Preferred Supplier (action test) """ supplier = SupplierFactory.create_batch(1) supplier[0].name="Erlich Bachman" supplier[0].preferred="false" supplier[0].create() resp = self.app.get('/suppliers/{}'.format(supplier[0].id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() logging.debug('data = %s', data) #Ensure that preferred flag is false prior to action self.assertEqual(data["preferred"], "false") resp = self.app.put('/suppliers/{}/preferred'.format(supplier[0].id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) resp = self.app.get('/suppliers/{}'.format(supplier[0].id), content_type='application/json') self.assertEqual(resp.status_code, status.HTTP_200_OK) data = resp.get_json() logging.debug('data = %s', data) #Ensure that preferred flag is set to True after action self.assertEqual(data["preferred"], "true")