def test_write(self):
        (DbProxy.get_instance()).write = MagicMock(return_value=1)
        res = UserData.get_instance().write("eytan", "pswd")
        self.assertEqual(1, res)

        (DbProxy.get_instance()).write = MagicMock(return_value=0)
        res = UserData.get_instance().write("eytan", "pswd")
        self.assertEqual(0, res)
    def test_delete_users(self):
        UserData.get_instance().delete = MagicMock(return_value=0)
        self.assertEqual(0, DataAccessFacade.get_instance().delete_users())

        UserData.get_instance().delete = MagicMock(return_value=1)
        self.assertEqual(1, DataAccessFacade.get_instance().delete_users())

        UserData.get_instance().delete = MagicMock(return_value=2)
        self.assertEqual(2, DataAccessFacade.get_instance().delete_users())
    def test_write_user(self):
        UserData.get_instance().write = MagicMock(return_value=0)
        self.assertEqual(
            0,
            DataAccessFacade.get_instance().write_user("eytan", "password"))

        UserData.get_instance().write = MagicMock(return_value=1)
        self.assertEqual(
            1,
            DataAccessFacade.get_instance().write_user("eytan", "password"))
    def test_delete(self):
        (DbProxy.get_instance()).delete = MagicMock(return_value=1)
        res = (UserData.get_instance()).delete(username="******")
        self.assertEqual(1, res)

        res = (UserData.get_instance()).delete(password="******")
        self.assertEqual(1, res)

        (DbProxy.get_instance()).delete = MagicMock(return_value=0)
        res = (UserData.get_instance()).delete(is_system_manager=True)
        self.assertEqual(0, res)
    def test_update_users(self):
        UserData.get_instance().update = MagicMock(return_value=0)
        self.assertEqual(
            0,
            DataAccessFacade.get_instance().update_users(
                new_username="******"))

        UserData.get_instance().update = MagicMock(return_value=1)
        self.assertEqual(1, DataAccessFacade.get_instance().update_users())

        UserData.get_instance().update = MagicMock(return_value=2)
        self.assertEqual(2, DataAccessFacade.get_instance().update_users())
    def test_read_users(self):
        UserData.get_instance().read = MagicMock(return_value=[])
        self.assertListEqual([], DataAccessFacade.get_instance().read_users())

        UserData.get_instance().read = MagicMock(
            return_value=[{
                "username": "******",
                "password": "******",
                "is_system_manager": False
            }])
        self.assertListEqual([{
            "username": "******",
            "password": "******",
            "is_system_manager": False
        }],
                             DataAccessFacade.get_instance().read_users())
    def test_update(self):
        try:
            (UserData.get_instance()).update()
            self.fail()
        except AttributeError:
            pass
        except Exception:
            self.fail()

        DbProxy.get_instance().update = MagicMock(return_value=1)
        res = UserData.get_instance().update(new_is_system_manager=True)
        self.assertEqual(res, 1)

        DbProxy.get_instance().update = MagicMock(return_value=1)
        res = UserData.get_instance().update(old_password="******",
                                             old_is_system_manager=True,
                                             new_is_system_manager=False)
        self.assertEqual(res, 1)
    def test_read(self):
        try:
            (UserData.get_instance()).read(["not_attribute"])
            self.fail()
        except AttributeError:
            pass
        except Exception:
            self.fail()

        (DbProxy.get_instance()).read = MagicMock(
            return_value=[StubUserData("eytan", "password", False)])
        res = (UserData.get_instance()).read([], username="******")
        self.assertEqual((res[0]['username'], res[0]['password'],
                          res[0]['is_system_manager']),
                         ("eytan", "password", False))

        res = (UserData.get_instance()).read([], password="******")
        self.assertEqual((res[0]['username'], res[0]['password'],
                          res[0]['is_system_manager']),
                         ("eytan", "password", False))

        (DbProxy.get_instance()).read = MagicMock(return_value=[])
        res = (UserData.get_instance()).read([], is_system_manager=True)
        self.assertListEqual([], res)