Esempio n. 1
0
class DbTestCase(TestCase):
    def setUp(self):
        super(DbTestCase, self).setUp()

        from sqlalchemy.orm import clear_mappers
        clear_mappers()

        from paste.deploy.loadwsgi import appconfig
        self.config = appconfig('config:test.ini', relative_to=conf_dir)

        from fivecents.lib.sqlalchemymanager import SQLAlchemyManager
        from authkit.users import sqlalchemy_04_driver
        from fivecents.model import setup_model

        self.manager = SQLAlchemyManager(None, self.config, 
            [sqlalchemy_04_driver.setup_model, setup_model])
        self.connection = self.manager.engine.connect()
        self.db = self.manager.session_maker(bind=self.connection)
        self.model = self.manager.model

    def tearDown(self):
        self.model = None
        del self.model
        self.db.close()
        self.connection.close()
            
        super(DbTestCase, self).tearDown()
Esempio n. 2
0
class BalanceFacadeTest(TestCase):
    def setUp(self):
        super(BalanceFacadeTest, self).setUp()

        from sqlalchemy.orm import clear_mappers
        clear_mappers()

        from paste.deploy.loadwsgi import appconfig
        self.config = appconfig('config:test.ini', relative_to=conf_dir)

        from fivecents.lib.sqlalchemymanager import SQLAlchemyManager
        from authkit.users import sqlalchemy_04_driver
        from fivecents.model import setup_model

        self.manager = SQLAlchemyManager(None, self.config, 
            [sqlalchemy_04_driver.setup_model, setup_model])
        self.connection = self.manager.engine.connect()
        self.db = self.manager.session_maker(bind=self.connection)
        self.model = self.manager.model

    def tearDown(self):
        super(BalanceFacadeTest, self).tearDown()

        self.model = None
        del self.model
        self.db.close()
        self.connection.close()

    def test_add_delete_balance(self):
        userDao = ops.AuthenticatedUserDao(self.db, self.model)
        balanceFacade = ops.BalanceFacade(self.db, self.model)
        user = userDao.find_by_uid(1)
        self.assertEquals(u"Paweł Niewiadomski", user.screenname)
        self.assertEquals("*****@*****.**", user.email)
        
        balance = self.model.Balance(name = "Testing 1")

        balanceFacade.add_balance(user = user, balance = balance)
        assert balance.uid is not None

        balanceDao = ops.BalanceDao(self.db, self.model)
        new_balance = balanceDao.find_by_uid(balance.uid)
        assert new_balance is not None

        self.assertEquals(balance.name, new_balance.name)

        balanceDao.delete(new_balance)

    def test_add_fill_delete_balance(self):
        """
        Regression test: system fails if you want to delete a balance that has changes with tags with following error:
        
        "(IntegrityError) null value in column \"balance_uid\" violates not-null constraint 
        'UPDATE balance_tags SET balance_uid=%(balance_uid)s WHERE balance_tags.uid = %(balance_tags_uid)s' 
        {'balance_uid': None, 'balance_tags_uid': 36})"
        """
        userDao = ops.AuthenticatedUserDao(self.db, self.model)
        balanceDao = ops.BalanceDao(self.db, self.model)
        balanceChangeDao = ops.BalanceChangeDao(self.db, self.model)
        balanceFacade = ops.BalanceFacade(self.db, self.model)

        user = userDao.find_by_uid(1)
        self.assertEquals(u"Paweł Niewiadomski", user.screenname)
        self.assertEquals("*****@*****.**", user.email)
        
        balance = self.model.Balance(name = "Testing 1")

        balanceFacade.add_balance(user = user, balance = balance)
        assert balance.uid is not None

        new_balance = balanceDao.find_by_uid(balance.uid)
        assert new_balance is not None

        self.assertEquals(balance.name, new_balance.name)

        bc = self.model.BalanceChange(amount = "12.23", occurred_on = date.today(), is_income = True, income_category_uid = 1, user_uid = 1)
        balanceFacade.add_balance_change(new_balance, bc)

        bt = self.model.BalanceTag(tag = "tag1")
        balanceFacade.add_balance_tag(balance = new_balance, tag = bt)

        balanceDao.delete(new_balance)

    def test_elements_from_string(self):
        tests = [
            [ "", [] ],
            [ ",", [] ],
            [ " test,", [ "test" ] ],
            [ " test, asd, ", [ "test", "asd" ] ],
            [ "test, asd, test, asd1, zcxc", [ "test", "asd", "asd1", "zcxc" ] ],
            [ "Paweł, Łódź, test, asd1, zcxc, Paweł ", [ "paweł", "łódź", "test", "asd1", "zcxc" ] ],
        ]
        balanceFacade = ops.BalanceFacade(self.db, self.model)
        for test in tests:
            self.assertEquals(test[1], balanceFacade.elements_from_string(test[0]))
           
    def test_find_or_create_tags(self):
        balanceFacade = ops.BalanceFacade(self.db, self.model)
        balanceDao = ops.BalanceDao(self.db, self.model)
        
        balance = balanceDao.find_by_uid(1)
        assert balance is not None

        self.assertEquals([], balanceFacade.find_or_create_tags(balance, []))

        tags = balanceFacade.find_or_create_tags(balance, ["tag3"], create = False)
        self.assertEquals(len(tags), 1)
        self.assertEquals("tag3", tags[0].tag)

        tags = balanceFacade.find_or_create_tags(balance, ["tag3", "no such tag", "no such tag either"], create = False)
        self.assertEquals(len(tags), 1)
        self.assertEquals("tag3", tags[0].tag)

        # create = True is a default
        tags = balanceFacade.find_or_create_tags(balance, ["tag3", "no such tag", "no such tag either"])
        self.assertEquals(len(tags), 3)

        balanceTagDao = ops.BalanceTagDao(self.db, self.model)
        tag = balance.find_tag("no such tag")
        self.failIf(tag is None)
        balanceTagDao.delete(tag)

        tag = balance.find_tag("no such tag either")
        self.failIf(tag is None)
        balanceTagDao.delete(tag)

    def test_delete_unused_balance_tags(self):
        balanceFacade = ops.BalanceFacade(self.db, self.model)
        
        balance = balanceFacade.balanceDao.find_by_uid(1)
        self.failIf(balance is None)

        count = balance.tags.count()

        # create = True is a default
        tags = balanceFacade.find_or_create_tags(balance, ["tag3", "no such tag", "no such tag either"])
        self.assertEquals(len(tags), 3)

        tag = balance.find_tag("no such tag")
        self.failIf(tag is None)

        tag = balance.find_tag("no such tag either")
        self.failIf(tag is None)

        balanceFacade.delete_unused_balance_tags(balance)

        self.assertEquals(count, balance.tags.count())

        tag = balance.find_tag("no such tag")
        self.failIf(tag is not None)

        tag = balance.find_tag("no such tag either")
        self.failIf(tag is not None)

    def test_get_all_tags_starting_with(self):
        dbFacade = ops.DbFacade(self.db, self.model)

        balance = dbFacade.balances.balanceDao.find_by_uid(1)
        self.failIf(balance is None)

        tags = balance.get_tags_starting_with("J")
        self.assertEquals(1, len(tags))
        self.assertEquals("jedynka", tags[0].tag)

        tags = balance.get_tags_starting_with("j")
        self.assertEquals(1, len(tags))
        self.assertEquals("jedynka", tags[0].tag)

        tags = balance.get_tags_starting_with("j%")
        self.assertEquals(0, len(tags))

        tags = balance.get_tags_starting_with("tag_")
        self.assertEquals(0, len(tags))

        tags = balance.get_tags_starting_with("")
        self.assertEquals(3, len(tags))