def test_check_stock_error_2(self): obj = product_repo.get_product_reference_by_reference("SAXXXY") product_repo.create_material_stock(obj, 25) obj = product_repo.get_product_reference_by_reference("MEFP2") with self.assertRaises(product_repo.ProductException) as ctx: product_repo.check_kit_stock_creation(obj, 10)
def test_check_kit_stock(self): obj = product_repo.get_product_reference_by_reference("SAXXXY") product_repo.create_material_stock(obj, 50) obj = product_repo.get_product_reference_by_reference("SAXXYY") product_repo.create_material_stock(obj, 50) obj = product_repo.get_product_reference_by_reference("MEFP2") self.assertTrue(product_repo.check_kit_stock_creation(obj, 10))
def test_count_all_Stock(self): obj = product_repo.get_product_reference_by_reference("SAXXXY") product_repo.create_material_stock(obj, 25) product_repo.create_material_stock(obj, 10) obj = product_repo.get_product_reference_by_reference("SAXXYY") product_repo.create_material_stock(obj, 10) obj = [ it for it in product_repo.count_all_stocks_by_reference_and_type().items ] self.assertEqual(obj, [('SAXXYY', product_repo.ProductType.materials, 10), ('SAXXXY', product_repo.ProductType.materials, 35)])
def post(self): params = request.json errors = product_validator.StockCreationRequest().validate(params, many=False) if errors: return { "errors": [{ "code": f"BAD_PARAM_{k.upper()}", "message": "\n".join(v) } for k, v in errors.items()] }, 400 product = product_repository.get_product_reference_by_reference( params.get('reference')) if product is None: return { "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)] }, 404 prod_type = product_repository.ProductType if product.type == prod_type.final: # check the material availability try: product_repository.check_kit_stock_creation( product, params.get("count")) except product_repository.ProductException as exc: return {"errors": exc.details}, 400 func = product_repository.create_kit_stock if product.type == prod_type.final \ else product_repository.create_material_stock obj = func(product, params.get("count")) return obj.to_json(), 200
def get(self, reference): obj = product_repository.get_product_reference_by_reference(reference) if obj is None: return { "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)] }, 404 return obj.to_json(), 200
def test_create_kit_stock(self): obj = product_repo.get_product_reference_by_reference("SAXXXY") product_repo.create_material_stock(obj, 55) obj = product_repo.get_product_reference_by_reference("SAXXYY") product_repo.create_material_stock(obj, 50) obj = product_repo.get_product_reference_by_reference("MEFP2") self.assertTrue(product_repo.create_kit_stock(obj, 10)) obj = [ it for it in product_repo.count_all_stocks_by_reference_and_type().items ] self.assertEqual(obj, [("MEFP2", product_repo.ProductType.kit, 10), ('SAXXXY', product_repo.ProductType.materials, 5)])
def put(self, reference): product = product_repository.get_product_reference_by_reference( reference) if product is None: return { "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)] }, 404 params = request.json errors = product_validator.ProductEquivalenceList().validate( params, many=False) if errors: return { "errors": [{ "code": "BAD_INPUT", "message": "\n".join(errors) }] }, 400 errors = [] if product.type != product_repository.ProductType.final: errors.append( get_error_messages(constant.BAD_FINAL_PRODUCT, product.reference)) materials = params.get("materials") for i, mat in enumerate(materials): material = product_repository.get_product_reference_by_reference( mat.get("reference")) if not material: errors.append( get_error_messages(constant.BAD_MATERIAL_PRODUCT, mat.get("reference"))) if errors: return {"errors": errors}, 400 for mat in materials: product_repository.create_equivalence(product, mat.get("reference"), mat.get("count")) product.refresh() return product.to_json(), 200
def test_count_stock(self): obj = product_repo.get_product_reference_by_reference("SAXXXY") product_repo.create_material_stock(obj, 25) product_repo.create_material_stock(obj, 10) obj = product_repo.count_stock_by_reference("SAXXXY") self.assertEqual(35, obj) obj = product_repo.list_stocks_by_reference("SAXXXY") self.assertEqual([(it.product.reference, it.count) for it in obj.all()], [('SAXXXY', 25), ('SAXXXY', 10)])
def post(self): params = request.json errors = product_validator.DeliveryItemCreationRequest().validate( params, many=False) if errors: return { "errors": [{ "code": f"BAD_PARAM_{k.upper()}", "message": "\n".join(v) } for k, v in errors.items()] }, 400 product = product_repository.get_product_reference_by_reference( params.get('reference')) if product is None: return { "errors": [get_error_messages(constant.UNKNOWN_RESOURCE)] }, 404 errors = [] prod_type = product_repository.ProductType if product.type == prod_type.materials: errors.append( get_error_messages(constant.CANNOT_DELIVER_MATERIAL_PRODUCT)) manufactor = orga_repository.get_organisation( params.get("manufactor_vid")) if not manufactor: errors.append( get_error_messages(constant.UNKNOWN_ORGANISATION, params.get("manufactor_vid"))) elif manufactor.role_obj.code != "man": errors.append( get_error_messages(constant.NOT_A_MANUFACTOR, params.get("manufactor_vid"))) if errors: return {"errors": errors}, 400 if params.get("kit") == 1: # check the material availability try: product_repository.check_kit_delivery_creation( product, params.get("count")) except product_repository.ProductException as exc: return {"errors": exc.details}, 400 func = product_repository.create_kit_delivery_stock if params.get("delivery_type") == "kit" \ else product_repository.create_final_delivery_stock func(product, params.get("count"), manufactor) return {}, 204
def test_create_final_delivery_stock(self): obj = product_repo.get_product_reference_by_reference("MEFP2+") manufactor = orga_repo.get_organisation(6) product_repo.create_final_delivery_stock(obj, 40, manufactor) obj1 = [ it for it in product_repo.count_all_stocks_by_reference_and_type().items ] self.assertEqual(obj1, [('MEFP2+', product_repo.ProductType.kit, 50)]) obj2 = [ it for it in product_repo.count_all_delivery_by_reference_and_type().items ] self.assertEqual( obj2, [('MTOILE', "Couturier 2", product_repo.ProductType.kit, product_repo.StatusType.submitted, 120), ('MEFP2+', 'Couturier 1', product_repo.ProductType.final, product_repo.StatusType.submitted, 40)])