Ejemplo n.º 1
0
 def setUp(self) -> None:
     if not ("testing" in rel_path):
         raise ReferenceError(
             "The Data Base is not the testing data base.\n"
             "\t\t\t\tPlease go to src.main.DataAccessLayer.ConnectionProxy.RealDb.rel_path\n"
             "\t\t\t\t and change rel_path to test_rel_path.\n"
             "\t\t\t\tThanks :D")
     self.__store_owner_or_manager_role: StoreOwnerOrManagerRole = StoreOwnerOrManagerRole(
     )
     self.__trade_control_mock: TradeControl = TradeControl.get_instance()
     self.store_mock = Store("store1")
Ejemplo n.º 2
0
    def test_remove_owner_appointees(self):
        store: Store = Store("store")
        store.get_owners_appointments().append(StoreAppointment(None, self.owner, []))
        owner1 = User()
        owner1.register("owner1", "password")
        store.add_owner(self.owner.get_nickname(), owner1)

        owner2 = User()
        owner2.register("owner2", "password")
        store.add_owner("owner1", owner2)

        owner3 = User()
        owner3.register("owner3", "password")
        store.add_owner("owner2", owner3)

        manager1 = User()
        manager1.register("manager1", "password")
        store.add_manager(owner3, manager1, [])

        manager2 = User()
        manager2.register("manager2", "password")
        store.add_manager(self.owner, manager2, [])

        res = store.remove_owner_appointees("owner1")

        self.assertEqual(res, ['owner2 removed as owner', 'owner3 removed as owner', 'manager1 removed as manager'])
        self.assertTrue(store.is_owner('owner1'))
        self.assertFalse(store.is_owner('owner2'))
        self.assertFalse(store.is_owner('owner3'))
        self.assertFalse(store.is_manager('manager1'))
        self.assertTrue(store.is_manager('manager2'))
Ejemplo n.º 3
0
 def setUp(self):
     self.store: Store = Store("myStore")
     self.owner = User()
     self.owner.register("shani", "passwordd45646")
     self.store.get_owners_appointments().append(
         StoreAppointment(None, self.owner, []))
     self.manager = User()
     self.manager.register("dani", "passwordd45646")
     self.store.get_store_manager_appointments().append(
         StoreAppointment(self.owner, self.manager,
                          [ManagerPermission.EDIT_INV]))
     self.purchase_policy_mock = PurchasePolicy()
Ejemplo n.º 4
0
 def setUp(self):
     self.__shopping_cart = ShoppingCart()
     self.__product = Product("Eytan's product", 12, "Eytan's category")
     self.__store: Store = Store("myStore")
     self.__product_as_dictionary = {
         "product": self.__product,
         "amount": 4,
         "store_name": self.__store.get_name(),
         "discount_type": DiscountType.DEFAULT,
         "purchase_type": PurchaseType.DEFAULT
     }
     self.__product_to_add = (self.__product_as_dictionary['product'],
                              self.__product_as_dictionary['amount'])
Ejemplo n.º 5
0
    def setUp(self):
        self.__valid_name = "anna9218"
        self.__valid_password = "******"
        self.__user = User()
        self.__user.register(self.__valid_name, self.__valid_password)

        self.__product = Product("Eytan's product", 12, "Eytan's category")
        self.__store: Store = Store("myStore")
        self.__products_to_add = [{
            "product": self.__product,
            "store_name": self.__store.get_name(),
            "amount": 1,
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }]  # products_stores_quantity_ls
Ejemplo n.º 6
0
    def test_remove_owner(self):
        store: Store = Store("store1")
        store.get_owners_appointments().append(StoreAppointment(None, self.owner, []))
        owner1 = User()
        owner1.register("owner1", "password")
        store.add_owner(self.owner.get_nickname(), owner1)

        owner2 = User()
        owner2.register("owner2", "password")
        store.add_owner("owner1", owner2)

        owner3 = User()
        owner3.register("owner3", "password")
        store.add_owner("owner2", owner3)

        manager1 = User()
        manager1.register("manager1", "password")
        store.add_manager(owner3, manager1, [])

        manager2 = User()
        manager2.register("manager2", "password")
        store.add_manager(self.owner, manager2, [])

        # failed owner2 didn't appoint owner 2 as owner
        res = store.remove_owner("owner2", "owner1")
        self.assertEqual(res['response'], [])
        self.assertEqual(res['msg'], "Error! remove store owner failed.")

        # failed
        res = store.remove_owner("owner", "owner1")
        self.assertEqual(res['response'], [])
        self.assertEqual(res['msg'], "Error! remove store owner failed.")

        # success
        res = store.remove_owner(self.owner.get_nickname(), "owner1")

        self.assertEqual(res['response'], ['owner1 removed as owner', 'owner2 removed as owner', 'owner3 removed as owner', 'manager1 removed as manager'])
        self.assertEqual(res['msg'], "Store owner owner1 and his appointees were removed successfully.")
        self.assertFalse(store.is_owner('owner1'))
        self.assertFalse(store.is_owner('owner2'))
        self.assertFalse(store.is_owner('owner3'))
        self.assertFalse(store.is_manager('manager1'))
        self.assertTrue(store.is_manager('manager2'))
Ejemplo n.º 7
0
    def setUp(self):
        self.store: Store = Store("myStore")
        self.owner = User()
        self.owner.register("Eytan", "password")
        self.store.get_owners_appointments().append(StoreAppointment(None, self.owner, []))
        self.manager = User()
        self.manager.register("Not Eytan", "Yes Password")
        self.store.get_store_manager_appointments().append(StoreAppointment(self.owner, self.manager,
                                                                            [ManagerPermission.EDIT_INV]))

        dis_details = {'name': "p1", 'product': "Eytan"}
        later_date = datetime(2021, 8, 21)
        pre_con__details = {'product': "Eytan", 'min_amount': 2, 'min_basket_price': None}
        leaf_pol1 = ConditionalDiscountPolicy(2.5, later_date, dis_details, pre_con__details)
        dis_details = {'name': "p2", 'product': "Eytan"}
        pre_con__details = {'product': "all", 'min_amount': None, 'min_basket_price': 1}
        leaf_pol2 = ConditionalDiscountPolicy(5, later_date, dis_details, pre_con__details)
        self.__policy = DiscountPolicy(jsonpickle.encode(leaf_pol1), jsonpickle.encode(leaf_pol2),
                                       CompositeFlag.XOR, 10, "Comp_Pol", later_date)
        self.store.get_discount_policies().insert(0, self.__policy)
Ejemplo n.º 8
0
class StoreOwnerOrManagerTests(unittest.TestCase):
    def setUp(self) -> None:
        if not ("testing" in rel_path):
            raise ReferenceError(
                "The Data Base is not the testing data base.\n"
                "\t\t\t\tPlease go to src.main.DataAccessLayer.ConnectionProxy.RealDb.rel_path\n"
                "\t\t\t\t and change rel_path to test_rel_path.\n"
                "\t\t\t\tThanks :D")
        self.__store_owner_or_manager_role: StoreOwnerOrManagerRole = StoreOwnerOrManagerRole(
        )
        self.__trade_control_mock: TradeControl = TradeControl.get_instance()
        self.store_mock = Store("store1")

    def test_add_products(self):
        TradeControl.get_instance().add_products = MagicMock(return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.add_products("Store", {}))

        TradeControl.get_instance().add_products = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.add_products("Store", {}))

    def test_remove_products(self):
        TradeControl.get_instance().remove_products = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.remove_products("Store", []))

        TradeControl.get_instance().remove_products = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.remove_products("Store", []))

    def test_edit_product(self):
        TradeControl.get_instance().edit_product = MagicMock(return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.edit_product(
                "Store", "product_name", "op", "new_value"))

        TradeControl.get_instance().edit_product = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.edit_product(
                "Store", "product_name", "op", "new_value"))

    def test_appoint_additional_owner(self):
        TradeControl.get_instance().appoint_additional_owner = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.appoint_additional_owner(
                "New Owner", "Store"))

        TradeControl.get_instance().appoint_additional_owner = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.appoint_additional_owner(
                "New Owner", "Store"))

    def test_update_agreement_participants(self):
        TradeControl.get_instance().update_agreement_participants = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.update_agreement_participants(
                "Appointee", "Store", AppointmentStatus.APPROVED))

        TradeControl.get_instance().update_agreement_participants = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.update_agreement_participants(
                "Appointee", "Store", AppointmentStatus.APPROVED))

    def test_get_appointment_status(self):
        TradeControl.get_instance().get_appointment_status = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.get_appointment_status(
                "Appointee", "Store"))

        TradeControl.get_instance().get_appointment_status = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.get_appointment_status(
                "Appointee", "Store"))

    def test_appoint_store_manager(self):
        TradeControl.get_instance().appoint_store_manager = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.appoint_store_manager(
                "New Manager", "Store",
                [ManagerPermission.WATCH_PURCHASE_HISTORY]))

        TradeControl.get_instance().appoint_store_manager = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.appoint_store_manager(
                "New Manager", "Store", []))

    def test_edit_manager_permissions(self):
        TradeControl.get_instance().edit_manager_permissions = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.edit_manager_permissions(
                "Store", "Appointee",
                [ManagerPermission.WATCH_PURCHASE_HISTORY]))

        TradeControl.get_instance().edit_manager_permissions = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.edit_manager_permissions(
                "Store", "Appointee", []))

    def test_remove_manager(self):
        TradeControl.get_instance().remove_manager = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.remove_manager(
                "Store", "Appointee"))

        TradeControl.get_instance().remove_manager = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.remove_manager(
                "Store", "Appointee"))

    def test_remove_owner(self):
        TradeControl.get_instance().remove_owner = MagicMock(
            return_value={
                'response': ['owner was removed'],
                'msg': "success"
            })

        res = self.__store_owner_or_manager_role.remove_owner(
            "owner", "myStore")
        self.assertEqual(res['response'], ['owner was removed'])
        self.assertEqual(res['msg'], "success")

    def test_display_store_purchases(self):
        TradeControl.get_instance().display_store_purchases = MagicMock(
            return_value=True)
        self.assertTrue(
            self.__store_owner_or_manager_role.display_store_purchases(
                "Store"))

        TradeControl.get_instance().display_store_purchases = MagicMock(
            return_value=False)
        self.assertFalse(
            self.__store_owner_or_manager_role.display_store_purchases(
                "Store"))

    def setup_for_policies(self):
        self.__trade_control_mock.get_store = MagicMock(
            return_value=self.store_mock)
        self.store_mock.get_name = MagicMock(return_value="store1")
        self.store_mock.is_owner = MagicMock(return_value=True)

    # TODO: add get tests for discount policies
    def test_get_policies(self):
        self.setup_for_policies()

        # ------------- purchase policies -------------
        # policies exist
        self.__trade_control_mock.get_policies = MagicMock(
            return_value={
                "response": ["policy1"],
                "msg": "ok"
            })

        res = self.__store_owner_or_manager_role.get_policies(
            "purchase", self.store_mock.get_name())["response"]
        self.assertTrue(len(res) > 0)

        # no policy exist
        self.__trade_control_mock.get_policies = MagicMock(return_value={
            "response": [],
            "msg": "ok"
        })

        res = self.__store_owner_or_manager_role.get_policies(
            "purchase", self.store_mock.get_name())["response"]
        self.assertTrue(len(res) == 0)
        # ------------- purchase policies -------------

    def test_update_purchase_policy(self):
        self.setup_for_policies()

        # valid update
        self.__trade_control_mock.update_purchase_policy = MagicMock(
            return_value={
                'response': True,
                'msg': "updated"
            })
        res = self.__store_owner_or_manager_role.update_purchase_policy(
            self.store_mock.get_name(), {
                "name": "policy2",
                "products": ["product1", "product2"],
                "min_amount": 9
            })["response"]
        self.assertTrue(res)

        # invalid update
        self.__trade_control_mock.update_purchase_policy = MagicMock(
            return_value={
                'response': False,
                'msg': "invalid update"
            })
        res = self.__store_owner_or_manager_role.update_purchase_policy(
            self.store_mock.get_name(), {
                "name": "policy2",
                "products": ["product1", "product2"],
                "min_amount": 9
            })["response"]
        self.assertFalse(res)

    def test_define_purchase_policy(self):
        self.setup_for_policies()

        # valid define
        self.__trade_control_mock.define_purchase_policy = MagicMock(
            return_value={
                "response": True,
                "msg": "policy added"
            })

        res = self.__store_owner_or_manager_role.define_purchase_policy(
            self.store_mock.get_name(), {
                "name": "policy2",
                "products": ["product1", "product2"],
                "min_amount": 9
            })["response"]
        self.assertTrue(res)

        # invalid define
        self.__trade_control_mock.define_purchase_policy = MagicMock(
            return_value={
                "response": False,
                "msg": "policy not added"
            })

        res = self.__store_owner_or_manager_role.define_purchase_policy(
            self.store_mock.get_name(), {
                "name": "policy2",
                "products": ["product1", "product2"],
                "min_amount": 9
            })["response"]
        self.assertFalse(res)

    def tearDown(self):
        self.__trade_control_mock.__delete__()

    def __repr__(self):
        return repr("StoreOwnerOrManagerRoleTests")
Ejemplo n.º 9
0
    def test_update_quantity(self):
        # def update_quantity(products_details: [{"product_name": str, "store_name": str, "amount": int}]):
        product1: Product = Product("not Eytan's product", 9,
                                    "Eytan's category")
        store1: Store = Store("Not my store")
        product2: Product = Product("maybe Eytan's product", 8,
                                    "Eytan's category")
        product_as_dictionary_var1 = {
            "product": product1,
            "amount": 3,
            "store_name": self.__store.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        product_as_dictionary_var3 = {
            "product": product1,
            "amount": 12,
            "store_name": store1.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        product_as_dictionary_var2 = {
            "product": product2,
            "amount": 5,
            "store_name": store1.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        self.__shopping_cart.add_products([
            self.__product_as_dictionary, product_as_dictionary_var1,
            product_as_dictionary_var2, product_as_dictionary_var3
        ])
        # All Valid - product only in one store
        self.assertTrue(
            self.__shopping_cart.update_quantity([{
                "product_name":
                self.__product.get_name(),
                "store_name":
                self.__store.get_name(),
                "amount":
                123
            }])['response'])
        self.assertEqual(
            123,
            self.__shopping_cart.get_store_basket(
                self.__store.get_name()).get_product_amount(
                    self.__product.get_name()))

        # All Valid - product in two stores, but change only in one
        self.assertTrue(
            self.__shopping_cart.update_quantity([{
                "product_name":
                product1.get_name(),
                "store_name":
                self.__store.get_name(),
                "amount":
                234
            }])['response'])
        self.assertEqual(
            234,
            self.__shopping_cart.get_store_basket(
                self.__store.get_name()).get_product_amount(
                    product1.get_name()))
        self.assertEqual(
            12,
            self.__shopping_cart.get_store_basket(
                store1.get_name()).get_product_amount(product1.get_name()))

        # Invalid - product not in store
        self.assertFalse(
            self.__shopping_cart.update_quantity([{
                "product_name":
                product2.get_name(),
                "store_name":
                self.__store.get_name(),
                "amount":
                234
            }])['response'])
        self.assertNotEqual(
            234,
            self.__shopping_cart.get_store_basket(
                store1.get_name()).get_product_amount(product2.get_name()))

        # Invalid - negative amount
        self.assertFalse(
            self.__shopping_cart.update_quantity([{
                "product_name":
                self.__product.get_name(),
                "store_name":
                self.__store.get_name(),
                "amount":
                -999
            }])['response'])
        self.assertNotEqual(
            -999,
            self.__shopping_cart.get_store_basket(
                self.__store.get_name()).get_product_amount(
                    self.__product.get_name()))
Ejemplo n.º 10
0
    def test_remove_products(self):
        product1: Product = Product("not Eytan's product", 9,
                                    "Eytan's category")
        store1: Store = Store("Not my store")
        product2: Product = Product("maybe Eytan's product", 8,
                                    "Eytan's category")
        product_as_dictionary_var1 = {
            "product": product1,
            "amount": 3,
            "store_name": self.__store.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        product_as_dictionary_var2 = {
            "product": product2,
            "amount": 5,
            "store_name": store1.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        product_as_dictionary_var3 = {
            "product": product1,
            "amount": 12,
            "store_name": store1.get_name(),
            "discount_type": DiscountType.DEFAULT,
            "purchase_type": PurchaseType.DEFAULT
        }
        self.__shopping_cart.add_products([
            self.__product_as_dictionary, product_as_dictionary_var1,
            product_as_dictionary_var2, product_as_dictionary_var3
        ])

        # All valid
        self.assertTrue(
            self.__shopping_cart.remove_products([{
                "product_name":
                self.__product.get_name(),
                "store_name":
                self.__store.get_name()
            }]))
        self.assertEqual(len(self.__shopping_cart.get_shopping_baskets()), 2)
        stores = [
            store_and_basket['store_name'] for store_and_basket in
            self.__shopping_cart.get_shopping_baskets()
        ]
        self.assertTrue(self.__store.get_name() in stores)
        self.assertTrue(store1.get_name() in stores)
        product_as_dictionary_lst_self_store = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products_self_store = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst_self_store
        ]
        product_as_dictionary_lst_store1 = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products_store1 = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst_store1
        ]
        self.assertTrue(product1 in products_self_store)
        self.assertTrue(product1 in products_store1)
        self.assertTrue(product2 in products_store1)
        self.assertFalse(
            self.__product in product_as_dictionary_lst_self_store)

        # Invalid - product isn't in the store
        self.assertFalse(
            self.__shopping_cart.remove_products([{
                "product_name":
                product2.get_name(),
                "store_name":
                self.__store.get_name()
            }])['response'])
        self.assertEqual(len(self.__shopping_cart.get_shopping_baskets()), 2)
        stores = [
            store_and_basket['store_name'] for store_and_basket in
            self.__shopping_cart.get_shopping_baskets()
        ]
        self.assertTrue(self.__store.get_name() in stores)
        self.assertTrue(store1.get_name() in stores)
        product_as_dictionary_lst_self_store = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products_self_store = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst_self_store
        ]
        product_as_dictionary_lst_store1 = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products_store1 = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst_store1
        ]
        self.assertTrue(product1 in products_self_store)
        self.assertTrue(product1 in products_store1)
        self.assertTrue(product2 in products_store1)
        self.assertFalse(
            self.__product in product_as_dictionary_lst_self_store)

        # All valid - make the basket empty
        self.assertTrue(
            self.__shopping_cart.remove_products([{
                "product_name":
                product1.get_name(),
                "store_name":
                self.__store.get_name()
            }])['response'])
        self.assertEqual(len(self.__shopping_cart.get_shopping_baskets()), 1)
        stores = [
            store_and_basket['store_name'] for store_and_basket in
            self.__shopping_cart.get_shopping_baskets()
        ]
        self.assertFalse(self.__store.get_name() in stores)
        self.assertTrue(store1.get_name() in stores)
        product_as_dictionary_lst = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst
        ]
        self.assertTrue(product1 in products)
        self.assertTrue(product2 in products)

        # Invalid - product doesn't exist
        self.assertFalse(
            self.__shopping_cart.remove_products([{
                "product_name":
                "product1.get_name()",
                "store_name":
                store1.get_name()
            }])['response'])
        self.assertEqual(len(self.__shopping_cart.get_shopping_baskets()), 1)
        stores = [
            store_and_basket['store_name'] for store_and_basket in
            self.__shopping_cart.get_shopping_baskets()
        ]
        self.assertTrue(store1.get_name() in stores)
        product_as_dictionary_lst = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst
        ]
        self.assertTrue(product1 in products)
        self.assertTrue(product2 in products)

        # Invalid - store doesn't exist
        self.assertFalse(
            self.__shopping_cart.remove_products([{
                "product_name":
                product1.get_name(),
                "store_name":
                "store1.get_name()"
            }])['response'])
        self.assertEqual(len(self.__shopping_cart.get_shopping_baskets()), 1)
        stores = [
            store_and_basket['store_name'] for store_and_basket in
            self.__shopping_cart.get_shopping_baskets()
        ]
        self.assertTrue(store1.get_name() in stores)
        product_as_dictionary_lst = self.__shopping_cart.get_store_basket(
            store1.get_name()).get_products()
        products = [
            product_as_dictionary['product']
            for product_as_dictionary in product_as_dictionary_lst
        ]
        self.assertTrue(product1 in products)
        self.assertTrue(product2 in products)