def __products_to_obj(self, purchase_products_dto: List[dict], purchase: Purchase): if not purchase_products_dto: purchase.products = list() return if purchase.products: dto_ids = [p.get("id") for p in purchase_products_dto] purchase.products[:] = [ p for p in purchase.products if p.product.id in dto_ids ] for dto in purchase_products_dto: purchase_product = next( (p for p in purchase.products if p.product.id == dto.get("id")), None) if not purchase_product: purchase_product = PurchaseProduct() purchase_product.product = self.product_mapper.to_object( dto, not_update=True) purchase_product.purchase = purchase purchase.products.append(purchase_product) purchase_product.quantity = dto.get("quantity") purchase_product.value = dto.get("value")
def test_get_product_types_and_values_without_product_type(self): p1 = Purchase() p1.id = 1 product = Product() product.id = 1 product.name = 'Test 1' pp = PurchaseProduct() pp.product = product pp.value = 12.3 pp.quantity = 1 p1.products = [pp] self.assertEqual([], p1.get_product_type_and_values())
def to_object(self, dto: dict, not_update: bool = False) -> Optional[Purchase]: if dto is None: return None purchase = None found = False if dto.get("id"): purchase = self.purchase_dao.find_by_id( dto.get("id"), AuthenticationContext.get_current_user()) found = purchase is not None if not found: purchase = Purchase() purchase.id = dto.get("id") purchase.created_at = dto.get("created_at") purchase.created_by = self.user_mapper.to_object( dto.get("created_by"), not_update=True) if not found or not not_update: purchase.name = dto.get("name") purchase.purchase_list = self.purchase_list_mapper.to_object( dto.get("purchase_list"), not_update=True) self.__products_to_obj(dto.get("products"), purchase) return purchase
def test_equal_with_different_id(self): p1 = Purchase() p1.id = 1 p2 = Purchase() p2.id = 132 self.assertNotEqual(p1, p2)
def test_equal_with_same_id(self): p1 = Purchase() p1.id = 1 p2 = Purchase() p2.id = 1 self.assertEqual(p1, p2)
import unittest from datetime import datetime from my_home_server.models.purchase import Purchase from my_home_server.services.purchase_service import PurchaseService purchase1 = Purchase() purchase1.created_at = datetime(2020, 10, 1) purchase1.total_value = 12 purchase2 = Purchase() purchase2.created_at = datetime(2020, 10, 12) purchase2.total_value = 13 purchase3 = Purchase() purchase3.created_at = datetime(2020, 11, 5) purchase3.total_value = 45 purchase4 = Purchase() purchase4.created_at = datetime(2020, 11, 30) purchase4.total_value = 31 purchase5 = Purchase() purchase5.created_at = datetime(2020, 12, 15) purchase5.total_value = 42 purchases_to_test_group_by_month = [ purchase1, purchase2, purchase3, purchase4, purchase5 ] class TestPurchaseService(unittest.TestCase): def setUp(self): self.service = PurchaseService(None, None, None, None, None)
def test_get_product_types_and_values_without_data(self): p1 = Purchase() p1.id = 1 p1.products = [] self.assertEqual([], p1.get_product_type_and_values())
product4.product_type = product_type4 purchase_product4 = PurchaseProduct() purchase_product4.product = product4 purchase_product4.value = 12 purchase_product4.quantity = 1 product5 = Product() product5.id = 5 product5.name = 'Test 4' product5.product_type = product_type4 purchase_product5 = PurchaseProduct() purchase_product5.product = product5 purchase_product5.value = 18.12 purchase_product5.quantity = 3 purchase_to_fill_total_value = Purchase() purchase_to_fill_total_value.products = [ purchase_product1, purchase_product2, purchase_product3, purchase_product4 ] purchase_to_fill_total_value_without_products = Purchase() purchase_to_test_get_product_types_values = Purchase() purchase_to_test_get_product_types_values.products = [ purchase_product1, purchase_product2, purchase_product3, purchase_product4, purchase_product5 ] class TestPurchase(unittest.TestCase): def test_equal_with_same_id(self):