def test_update_supplier_product(self): """ Update an suppliers product """ suppliers = Supplier.all() self.assertEqual(suppliers, []) product = self._create_product() supplier = self._create_supplier(products=[product]) supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) # Fetch it back supplier = Supplier.find(supplier.id) old_product = supplier.products[0] self.assertEqual(old_product.desc, product.desc) old_product.desc = "XX" supplier.save() # Fetch it back again supplier = Supplier.find(supplier.id) product = supplier.products[0] self.assertEqual(product.desc, "XX")
def put(self, supplier_id): """ Update a supplier """ app.logger.info('Request to update a supplier with id [%s]', supplier_id) check_content_type('application/json') supplier = Supplier.find(supplier_id) data = request.get_json() if not supplier: return api.abort( status.HTTP_404_NOT_FOUND, "Supplier with id '{}' not found".format(supplier_id)) supplier.update(**data) # supplier.reload() supplier = Supplier.find(supplier.id) return json.loads(supplier.to_json()), status.HTTP_200_OK
def preferred_suppliers(supplier_id): """ Marking a supplier preferred """ supplier = Supplier.find(supplier_id) if not supplier: abort(status.HTTP_404_NOT_FOUND, "Supplier with id '{}' was not found.".format(supplier_id)) supplier.preferred = "true" supplier.save() return make_response(jsonify(supplier.serialize()), status.HTTP_200_OK)
def test_find_supplier(self): """ Find a Supplier by id """ Supplier("supplier1", 2, True, [1, 2, 3], 8.5).save() # saved_supplier = Supplier("kitty", "cat").save() saved_supplier = Supplier("supplier1", 2, True, [1, 2, 3], 8.5) saved_supplier.save() supplier = Supplier.find(saved_supplier.id) self.assertIsNot(supplier, None) self.assertEqual(supplier.id, saved_supplier.id) self.assertEqual(supplier.name, "supplier1")
def delete_suppliers(supplier_id): """ Delete a Supplier This endpoint will delete a Supplier based the id specified in the path """ app.logger.info("Request to delete supplier with id: %s", supplier_id) suppliers = Supplier.find(supplier_id) if suppliers: suppliers.delete() return make_response("", status.HTTP_204_NO_CONTENT)
def delete(self, supplier_id): """ Delete a Supplier This endpoint will delete a Supplier based the id specified in the path """ app.logger.info('Request to Delete a supplier with id [%s]', supplier_id) supplier = Supplier.find(supplier_id) if supplier: supplier.delete() return '', status.HTTP_204_NO_CONTENT
def delete_all_suppliers(): """ Returns IDs of the Suppliers """ app.logger.info("Request for Supplier list") suppliers = [] id = request.args.get("id") if id: suppliers = Supplier.find(id) else: suppliers = Supplier.all() results = [supplier.delete() for supplier in suppliers] return make_response("", status.HTTP_204_NO_CONTENT)
def put(self, supplier_id): """ Like a single Supplier This endpoint will update the like_count of the Supplier based on it's id in the database """ supplier = Supplier.find(supplier_id) if not supplier: raise NotFound( "Supplier with id '{}' was not found.".format(supplier_id)) supplier.like_count += 1 supplier.save() app.logger.info('You liked supplier with id [%s]!', supplier.id) return supplier.serialize(), status.HTTP_200_OK
def get(self, supplier_id): """ Retrieve a single Supplier This endpoint will return a Supplier based on it's id """ app.logger.info("Request to Retrieve a supplier with id [%s]", supplier_id) supplier = Supplier.find(supplier_id) if not supplier: api.abort( status.HTTP_404_NOT_FOUND, "Supplier with id '{}' was not found.".format(supplier_id)) return supplier.serialize(), status.HTTP_200_OK
def update_suppliers(supplier_id): """ Update a Supplier This endpoint will update a Supplier based the body that is posted """ app.logger.info("Request to update supplier with id: %s", supplier_id) check_content_type("application/json") supplier = Supplier.find(supplier_id) if not supplier: raise NotFound("Supplier with id '{}' was not found.".format(supplier_id)) supplier.deserialize(request.get_json()) supplier.id = supplier_id supplier.save() return make_response(jsonify(supplier.serialize()), status.HTTP_200_OK)
def test_add_supplier_product(self): """ Create a Supplier with a product and add it to the database """ suppliers = Supplier.all() self.assertEqual(suppliers, []) supplier = self._create_supplier() product = self._create_product() supplier.products.append(product) supplier.create() # Assert that it was assigned an id and shows up in the database self.assertEqual(supplier.id, 1) suppliers = Supplier.all() self.assertEqual(len(suppliers), 1) new_supplier = Supplier.find(supplier.id) self.assertEqual(supplier.products[0].name, product.name) product2 = self._create_product() supplier.products.append(product2) supplier.save() new_supplier = Supplier.find(supplier.id) self.assertEqual(len(supplier.products), 2) self.assertEqual(supplier.products[1].name, product2.name)
def delete(self, supplier_id): """ Delete a Supplier This endpoint will delete a Supplier based the id specified in the path """ app.logger.info('Request to Delete a Supplier with id [%s]', supplier_id) supplier = Supplier.find(supplier_id) if supplier: supplier.delete() app.logger.info("Supplier with ID [%s] delete complete.", supplier_id) else: app.logger.info("Supplier with ID [%s] does not exist.", supplier_id) return "", status.HTTP_204_NO_CONTENT
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 put(self, supplier_id): """ Preferred a Supplier This endpoint will make the Supplier a preferred supplier """ app.logger.info('Request to Preferred a Supplier') supplier = Supplier.find(supplier_id) if not supplier: api.abort( status.HTTP_404_NOT_FOUND, 'Supplier with id [{}] was not found.'.format(supplier_id)) if not supplier.preferred: api.abort( status.HTTP_409_CONFLICT, 'Supplier with id [{}] is not preferred.'.format(supplier_id)) supplier.preferred = False supplier.save() app.logger.info('Supplier with id [%s] has been preferredd!', supplier.id) return supplier.serialize(), status.HTTP_200_OK
def put(self, supplier_id): """ Update a supplier This endpoint will update a Supplier based the body that is posted """ app.logger.info('Request to Update a supplier with id [%s]', supplier_id) check_content_type('application/json') supplier = Supplier.find(supplier_id) if not supplier: return api.abort( status.HTTP_404_NOT_FOUND, "Supplier with id '{}' not found".format(supplier_id)) data = request.get_json() # Data type transfer data = data_type_transfer(data) app.logger.info(data) supplier.deserialize(data) supplier.id = supplier_id supplier.save() return supplier.serialize(), status.HTTP_200_OK
def test_supplier_not_found(self): """ Find a Supplier that doesnt exist """ Supplier("supplier1", 2, True, [1, 2, 3], 8.5).save() supplier = Supplier.find("2") self.assertIs(supplier, None)
def test_find_supplier_exception(self): """ Test exception raised by find. """ non_exist = Supplier.find("random_id") self.assertEqual(non_exist, None)
def test_find_with_no_suppliers(self): """ Find a Supplier with empty database """ supplier = Supplier.find("1") self.assertIs(supplier, None)