예제 #1
0
    def setUp(self):
        self.configuration = {
            "sqlalchemy.url": "sqlite://",
            "sqlalchemy.echo": False,
            "sqlalchemy.pool_recycle": 3600,
        }

        from rpi_database.models import SQLAlchemyMixin, User, KeyCode

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code

        self.user_data = {
            'first_name': "Derp",
            'last_name': "Derps",
            'email': "*****@*****.**",
        }

        self.key_data = {
            'code': '67890',
            'enabled': False,
        }

        self.key = KeyCode(**self.key_data)
        self.user = User(**self.user_data)
        self.user.key_code = self.key

        with self.sqlmixin.session_context() as session:
            session.add(self.user)
            session.commit()
예제 #2
0
    def test_validate_key_code(self):
        from rpi_database.models import SQLAlchemyMixin

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code
        user_one = {
            'code': '12345'
        }

        self.create_user_and_key(self.sqlmixin, **user_one)

        user_two = {
            'first_name': "Derp",
            'last_name': "Derps",
            'email': "*****@*****.**",
            'code': '67890',
            'enabled': False,
        }

        self.create_user_and_key(self.sqlmixin, **user_two)

        self.assertTrue(self.sqlmixin.validate_key_code(user_one['code']))

        self.assertFalse(self.sqlmixin.validate_key_code(user_two['code']))
예제 #3
0
    def test_create_user(self):
        from rpi_database.models import SQLAlchemyMixin, User

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        data = {
            'first_name': 'Derp',
            'last_name': 'Derps',
            'email': '*****@*****.**',
        }

        self.sqlmixin.create_user(data)
        user = User.query.filter_by(email=data['email']).one()
        self.assertIsNotNone(user)
        self.assertEquals(user.first_name, data['first_name'])
        self.assertEquals(user.last_name, data['last_name'])
        self.assertEquals(user.email, data['email'])
예제 #4
0
    def test_sqlalchemyminxin_init_stack(self):
        from rpi_database.models import SQLAlchemyMixin

        class TestClass():

            def __init__(self, *args, **kwargs):
                self.numbers += 5

        class MixinTest(SQLAlchemyMixin, TestClass):

            def __init__(self, *args, **kwargs):
                self.numbers = 5
                super(MixinTest, self).__init__(*args, **kwargs)

        self.sqlmixin = MixinTest(**self.configuration)
        self.sqlmixin.init_db()

        self.assertEquals(self.sqlmixin.numbers, 10)
예제 #5
0
class TestKeyCode(TestCase):

    def setUp(self):
        self.configuration = {
            "sqlalchemy.url": "sqlite://",
            "sqlalchemy.echo": False,
            "sqlalchemy.pool_recycle": 3600,
        }

        from rpi_database.models import SQLAlchemyMixin, KeyCode

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code

        self.key_data = {
            'code': '67890',
            'enabled': False,
        }

        self.key = KeyCode(**self.key_data)

        with self.sqlmixin.session_context() as session:
            session.add(self.key)
            session.commit()

    def test_enabled(self):
        self.assertIsNotNone(self.key.code)
        self.assertEquals(self.key.enabled, self.key_data['enabled'])
        self.assertIsInstance(self.key.enabled, bool)

    def test_code(self):
        self.assertIsNotNone(self.key.code)
        self.assertEquals(self.key.code, self.key_data['code'])

    def test_code_unique(self):
        from rpi_database.models import KeyCode
        from sqlalchemy.exc import IntegrityError
        key = KeyCode(**self.key_data)
        with self.sqlmixin.session_context() as session:
            session.add(key)
            with ShouldRaise(IntegrityError):
                session.commit()

    def tearDown(self):
        if self.sqlmixin:
            self.sqlmixin.drop_db()
예제 #6
0
    def setUp(self):
        self.configuration = {
            "sqlalchemy.url": "sqlite://",
            "sqlalchemy.echo": False,
            "sqlalchemy.pool_recycle": 3600,
        }

        from rpi_database.models import SQLAlchemyMixin, KeyCode

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code

        self.key_data = {
            'code': '67890',
            'enabled': False,
        }

        self.key = KeyCode(**self.key_data)

        with self.sqlmixin.session_context() as session:
            session.add(self.key)
            session.commit()
예제 #7
0
class TestSQLAlchemyMixin(TestCase):

    def setUp(self):
        self.configuration = {
            "sqlalchemy.url": "sqlite://",
            "sqlalchemy.echo": False,
            "sqlalchemy.pool_recycle": 3600,
        }

        self.sqlmixin = None

    def create_user_and_key(self, sqla_instance, **kwargs):
        from rpi_database.models import User, KeyCode

        first_name = kwargs.get('first_name', 'Derp')
        last_name = kwargs.get('last_name', 'Heaps')
        email = kwargs.get('email', '*****@*****.**')

        code = kwargs.get('code', '12345')
        enabled = kwargs.get('enabled', True)

        with sqla_instance.session_context() as session:
            user = User(first_name=first_name,
                        last_name=last_name,
                        email=email)
            key_code = KeyCode(code=code, enabled=enabled)
            user.key_code = key_code
            session.add(user)
            session.commit()

    def test_sqlalchemyminxin_init_stack(self):
        from rpi_database.models import SQLAlchemyMixin

        class TestClass():

            def __init__(self, *args, **kwargs):
                self.numbers += 5

        class MixinTest(SQLAlchemyMixin, TestClass):

            def __init__(self, *args, **kwargs):
                self.numbers = 5
                super(MixinTest, self).__init__(*args, **kwargs)

        self.sqlmixin = MixinTest(**self.configuration)
        self.sqlmixin.init_db()

        self.assertEquals(self.sqlmixin.numbers, 10)

    def test_validate_key_code(self):
        from rpi_database.models import SQLAlchemyMixin

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code
        user_one = {
            'code': '12345'
        }

        self.create_user_and_key(self.sqlmixin, **user_one)

        user_two = {
            'first_name': "Derp",
            'last_name': "Derps",
            'email': "*****@*****.**",
            'code': '67890',
            'enabled': False,
        }

        self.create_user_and_key(self.sqlmixin, **user_two)

        self.assertTrue(self.sqlmixin.validate_key_code(user_one['code']))

        self.assertFalse(self.sqlmixin.validate_key_code(user_two['code']))

    def test_create_user(self):
        from rpi_database.models import SQLAlchemyMixin, User

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        data = {
            'first_name': 'Derp',
            'last_name': 'Derps',
            'email': '*****@*****.**',
        }

        self.sqlmixin.create_user(data)
        user = User.query.filter_by(email=data['email']).one()
        self.assertIsNotNone(user)
        self.assertEquals(user.first_name, data['first_name'])
        self.assertEquals(user.last_name, data['last_name'])
        self.assertEquals(user.email, data['email'])

    def tearDown(self):
        if self.sqlmixin:
            self.sqlmixin.drop_db()
예제 #8
0
class TestUser(TestCase):

    def setUp(self):
        self.configuration = {
            "sqlalchemy.url": "sqlite://",
            "sqlalchemy.echo": False,
            "sqlalchemy.pool_recycle": 3600,
        }

        from rpi_database.models import SQLAlchemyMixin, User, KeyCode

        self.sqlmixin = SQLAlchemyMixin(**self.configuration)
        self.sqlmixin.init_db()

        # use defaults for the first user save for the code

        self.user_data = {
            'first_name': "Derp",
            'last_name': "Derps",
            'email': "*****@*****.**",
        }

        self.key_data = {
            'code': '67890',
            'enabled': False,
        }

        self.key = KeyCode(**self.key_data)
        self.user = User(**self.user_data)
        self.user.key_code = self.key

        with self.sqlmixin.session_context() as session:
            session.add(self.user)
            session.commit()

    def test_first_name(self):
        self.assertIsNotNone(self.user.first_name)
        self.assertEquals(self.user.first_name, self.user_data['first_name'])

    def test_last_name(self):
        self.assertIsNotNone(self.user.last_name)
        self.assertEquals(self.user.last_name, self.user_data['last_name'])

    def test_email(self):
        self.assertIsNotNone(self.user.email)
        self.assertEquals(self.user.email, self.user_data['email'])

    def test_unique_email(self):
        from rpi_database.models import User
        from sqlalchemy.exc import IntegrityError
        user = User(**self.user_data)
        with self.sqlmixin.session_context() as session:
            session.add(user)
            with ShouldRaise(IntegrityError):
                session.commit()

    def test_key_code(self):
        self.assertIsNotNone(self.user.key_code)
        self.assertEquals(self.user.key_code_id, 1)

    def tearDown(self):
        if self.sqlmixin:
            self.sqlmixin.drop_db()