Helper for unittest

For unittest, classes are available to offer some fonctionnalities

TestCase

from anyblok.tests.testcase import TestCase
class anyblok.tests.testcase.TestCase(methodName='runTest')

Bases: unittest.case.TestCase

Unittest class add helper for unit test in anyblok

classmethod createdb(keep_existing=False)

Create a database in fonction of variable of environment

cls.init_argsparse_manager()
cls.createdb()
Parameters:keep_existing – If false drop the previous db before create it
classmethod dropdb()

Drop a database in fonction of variable of environment

cls.init_argsparse_manager()
cls.dropdb()
getRegistry()

Return the registry for the database in argsparse i

registry = self.getRegistry()
Return type:registry instance
classmethod init_argsparse_manager(prefix=None, **env)

Initialise the argsparse manager with environ variable to launch the test

Warning

For the moment we not use the environ variable juste constante

Parameters:
  • prefix – prefix the database name
  • env – add another dict to merge with environ variable

DBTestCase

Warning

this testcase destroys the test database for each unittest

class anyblok.tests.testcase.DBTestCase(methodName='runTest')

Bases: anyblok.tests.testcase.TestCase

Test case for all the Field, Column, RelationShip

from anyblok.tests.testcase import DBTestCase


def simple_column(ColumnType=None, **kwargs):

    @Declarations.register(Declarations.Model)
    class Test:

        id = Declarations.Column.Integer(primary_key=True)
        col = ColumnType(**kwargs)


class TestColumns(DBTestCase):

    def test_integer(self):
        Integer = Declarations.Column.Integer

        registry = self.init_registry(simple_column,
                                      ColumnType=Integer)
        test = registry.Test.insert(col=1)
        self.assertEqual(test.col, 1)

Warning

The database are create and drop for each unit test

current_blok = 'anyblok-core'

In the blok to add the new model

init_registry(function, **kwargs)

call a function to filled the blok manager with new model

Parameters:
  • function – function to call
  • kwargs – kwargs for the function
Return type:

registry instance

parts_to_load = ['AnyBlok']

blok group to load

setUp()

Create a database and load the blok manager

classmethod setUpClass()

Intialialise the argsparse manager

tearDown()

Clear the registry, unload the blok manager and drop the database

upgrade(registry, **kwargs)

Upgrade the registry:

class MyTest(DBTestCase):

    def test_mytest(self):
        registry = self.init_registry(...)
        self.upgrade(registry, install=('MyBlok',))
Parameters:
  • registry – registry to upgrade
  • install – list the blok to install
  • update – list the blok to update
  • uninstall – list the blok to uninstall

BlokTestCase

class anyblok.tests.testcase.BlokTestCase(methodName='runTest')

Bases: anyblok.tests.testcase.TestCase

Use to test bloks without have to create new database for each test

from anyblok.tests.testcase import BlokTestCase


class MyBlokTest(BlokTestCase):

    parts_to_load = ['AnyBlok']
    need_blok = ['blok 1', 'blok 2', ..., 'blok n']

    def test_1(self):
        ...
need_blok = ['anyblok-core']

List of the blok need for this test

parts_to_load = None

Group of blok to load

classmethod setUpClass()

Intialialise the argsparse manager

Deactivate the commit method of the registry

tearDown()

Roll back the session

classmethod tearDownClass()

Clear the registry, unload the blok manager

upgrade(**kwargs)

Upgrade the registry:

class MyTest(DBTestCase):

    def test_mytest(self):
        self.registry.upgrade(install=('MyBlok',))
Parameters:
  • install – list the blok to install
  • update – list the blok to update
  • uninstall – list the blok to uninstall