Exemple #1
0
    async def asetup(self):
        self.models1 = create_models1()
        registry = orm.mappers.Registry(create_metadata([self.models1]))

        self.LocalMapperClass.create(registry, db_id='db1')
        self.RemoteMapperClass.create(registry, db_id='db1')

        local_mapper = registry['db1']['Local']
        remote_mapper = registry['db1']['Remote']
        registry.create_schema()

        state1 = DbState()
        state1['local'] = TableState(local_mapper.table)
        state1['remote'] = TableState(remote_mapper.table)
        state1['m2m'] = TableState(
            local_mapper.relations['m2m'].table,
            primary_keys=['local_id', 'remote_id'],
        )
        state1['m2m_ordered'] = TableState(
            local_mapper.relations['m2m_ordered'].table,
            primary_keys=['local_id', 'remote_id'],
        )
        state1['remote'].append({'id': 1, 'title': 'title1'})
        state1['remote'].append({'id': 2, 'title': 'title2'})
        state1['remote'].append({'id': 3, 'title': 'title3'})

        state2 = state1.copy()
        state2['local'].append({'id': 1, 'title': 'title1'})
        state2['m2m'].append({'local_id': 1, 'remote_id': 1})
        state2['m2m'].append({'local_id': 1, 'remote_id': 2})
        state2['m2m'].append({'local_id': 1, 'remote_id': 3})
        state2['m2m_ordered'].append({'local_id': 1, 'remote_id': 1, 'order': 2})
        state2['m2m_ordered'].append({'local_id': 1, 'remote_id': 2, 'order': 1})
        state2['m2m_ordered'].append({'local_id': 1, 'remote_id': 3, 'order': 3})

        app = MagicMock()
        del app.db
        app.cfg.DATABASES = {
            'db1': DB_URL,
        }

        Component = component(mappers=registry)

        db = await Component.create(app)
        async with await db() as session:
            conn1 = await session.get_connection(db.engines['db1'])
            await self.models1.reset(conn1)
        return {
            'db': db,
            'local_mapper': local_mapper,
            'remote_mapper': remote_mapper,
            'state1': state1,
            'state2': state2,
        }
Exemple #2
0
    async def _db_test(self, db_url):
        app = MagicMock()
        del app.db
        app.cfg.DATABASES = {
            'db1': db_url,
            'db2': db_url,
        }

        session_cls_mock = MagicMock()
        Component = component(
            mappers=mappers.Registry(create_metadata(
                self.test_models.values())),
            session_cls=session_cls_mock,
        )
        db = await Component.create(app)
        self.assertEqual(set(db.engines.keys()), set(app.cfg.DATABASES.keys()))
        self.assertEqual(
            db.binds, {
                self.test_models['db1'].test_table1: db.engines['db1'],
                self.test_models['db2'].test_table2: db.engines['db2'],
            })
        session = await db()
        self.assertEqual(session, session_cls_mock(db.engines, db.binds))
Exemple #3
0
    async def _db_test(self, db_url):
        app = MagicMock()
        del app.db
        app.cfg.DATABASES = {
            'db1': db_url,
            'db2': db_url,
        }

        session_cls_mock = MagicMock()
        Component = component(
            mappers=mappers.Registry(create_metadata(self.test_models.values())),
            session_cls=session_cls_mock,
        )
        db = await Component.create(app)
        self.assertEqual(set(db.engines.keys()), set(app.cfg.DATABASES.keys()))
        self.assertEqual(
            db.binds,
            {
                self.test_models['db1'].test_table1: db.engines['db1'],
                self.test_models['db2'].test_table2: db.engines['db2'],
            }
        )
        session = await db()
        self.assertEqual(session, session_cls_mock(db.engines, db.binds))
Exemple #4
0
    async def asetup(self):
        registry = orm.mappers.Registry(
            create_metadata([self.models1, self.models2]))
        self.mapper_cls.from_model(registry, [self.models1.Test])

        app = MagicMock()
        app.cfg.DATABASES = {
            'db1': cfg.DB_URL,
            'db2': cfg.DB_URL,
        }
        del app.db
        db = await ikcms.ws_components.db.component(mappers=registry
                                                    ).create(app)
        async with await db() as session:
            conn1 = await session.get_connection(db.engines['db1'])
            await self.models1.reset(conn1)
            conn2 = await session.get_connection(db.engines['db2'])
            await self.models2.reset(conn2)

        class l_id(list_fields.id):
            widget = MagicMock()

        class l_title(list_fields.title):
            widget = MagicMock()

        class l_date(list_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

        class f_id(filter_fields.id):
            widget = MagicMock()

        class f_title(filter_fields.title):
            widget = MagicMock()

        class f_date(filter_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

        class f_title2(filter_fields.title):
            name = 'title2'
            title = 'title2'
            widget = MagicMock()

        class i_id(item_fields.id):
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                return 50000

        class i_title(item_fields.title):
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                test_kwarg = kwargs.get('test_kwarg', 'test_default')
                return '{}-{}-initials'.format(_self.name, test_kwarg)

        class i_date(item_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                return date(2005, 5, 5)

        class TestStream(Stream):
            max_limit = 50
            name = 'test_stream'
            title = 'test_stream_title'
            mapper_name = 'Test'
            db_id = 'db1'
            permissions = {'test_role': 'rwxcd'}

            list_fields = [
                l_id,
                l_title,
                l_date,
            ]
            filter_fields = [
                f_id,
                f_title,
                f_title2,
                f_date,
            ]
            item_fields = [
                i_id,
                i_title,
                i_date,
            ]

            def get_item_form(self, env, item=None, kwargs=None):
                kwargs = kwargs or {}
                raise_kwarg = kwargs.get('raise')
                if raise_kwarg:
                    raise raise_kwarg
                return super().get_item_form(env, item, kwargs)

            def check_perms(self, user, perms):
                pass

        stream = TestStream(MagicMock(app=app))
        env = MagicMock()
        env.app = app

        return {
            'db': db,
            'stream': stream,
            'env': env,
        }
Exemple #5
0
    async def asetup(self):
        self.models1 = create_models1()
        registry = orm.mappers.Registry(create_metadata([self.models1]))

        self.LocalMapperClass.create(registry, db_id='db1')
        self.RemoteMapperClass.create(registry, db_id='db1')

        local_mapper = registry['db1']['Local']
        remote_mapper = registry['db1']['Remote']
        registry.create_schema()

        state1 = DbState()
        state1['local'] = TableState(local_mapper.table)
        state1['remote'] = TableState(remote_mapper.table)
        state1['m2m'] = TableState(
            local_mapper.relations['m2m'].table,
            primary_keys=['local_id', 'remote_id'],
        )
        state1['m2m_ordered'] = TableState(
            local_mapper.relations['m2m_ordered'].table,
            primary_keys=['local_id', 'remote_id'],
        )
        state1['remote'].append({'id': 1, 'title': 'title1'})
        state1['remote'].append({'id': 2, 'title': 'title2'})
        state1['remote'].append({'id': 3, 'title': 'title3'})

        state2 = state1.copy()
        state2['local'].append({'id': 1, 'title': 'title1'})
        state2['m2m'].append({'local_id': 1, 'remote_id': 1})
        state2['m2m'].append({'local_id': 1, 'remote_id': 2})
        state2['m2m'].append({'local_id': 1, 'remote_id': 3})
        state2['m2m_ordered'].append({
            'local_id': 1,
            'remote_id': 1,
            'order': 2
        })
        state2['m2m_ordered'].append({
            'local_id': 1,
            'remote_id': 2,
            'order': 1
        })
        state2['m2m_ordered'].append({
            'local_id': 1,
            'remote_id': 3,
            'order': 3
        })

        app = MagicMock()
        del app.db
        app.cfg.DATABASES = {
            'db1': DB_URL,
        }

        Component = component(mappers=registry)

        db = await Component.create(app)
        async with await db() as session:
            conn1 = await session.get_connection(db.engines['db1'])
            await self.models1.reset(conn1)
        return {
            'db': db,
            'local_mapper': local_mapper,
            'remote_mapper': remote_mapper,
            'state1': state1,
            'state2': state2,
        }
Exemple #6
0
    async def asetup(self):
        registry = orm.mappers.Registry(
            create_metadata([self.models1, self.models2]))
        self.mapper_cls.from_model(registry, [self.models1.Test])

        app = MagicMock()
        app.cfg.DATABASES = {
            'db1': DB_URL,
            'db2': DB_URL,
        }
        del app.db
        db = await ikcms.ws_components.db.component(mappers=registry).create(app)
        async with await db() as session:
            conn1 = await session.get_connection(db.engines['db1'])
            await self.models1.reset(conn1)
            conn2 = await session.get_connection(db.engines['db2'])
            await self.models2.reset(conn2)

        class l_id(list_fields.id):
            widget = MagicMock()

        class l_title(list_fields.title):
            widget = MagicMock()

        class l_date(list_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

        class f_id(filter_fields.id):
            widget = MagicMock()

        class f_title(filter_fields.title):
            widget = MagicMock()

        class f_date(filter_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

        class f_title2(filter_fields.title):
            name = 'title2'
            title = 'title2'
            widget = MagicMock()

        class i_id(item_fields.id):
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                return 50000

        class i_title(item_fields.title):
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                test_kwarg = kwargs.get('test_kwarg', 'test_default')
                return '{}-{}-initials'.format(_self.name, test_kwarg)

        class i_date(item_fields.Date):
            name = 'date'
            title = 'date'
            widget = MagicMock()

            def get_initials(_self, **kwargs):
                return date(2005, 5, 5)


        class TestStream(Stream):
            max_limit = 50
            name = 'test_stream'
            title = 'test_stream_title'
            mapper_name = 'Test'
            db_id = 'db1'
            permissions = {'test_role': 'rwxcd'}

            list_fields = [
                l_id,
                l_title,
                l_date,
            ]
            filter_fields = [
                f_id,
                f_title,
                f_title2,
                f_date,
            ]
            item_fields = [
                i_id,
                i_title,
                i_date,
            ]

            def get_item_form(self, env, item=None, kwargs=None):
                kwargs = kwargs or {}
                raise_kwarg = kwargs.get('raise')
                if raise_kwarg:
                    raise raise_kwarg
                return super().get_item_form(env, item, kwargs)

            def check_perms(self, user, perms):
                pass


        stream = TestStream(MagicMock(app=app))
        env = MagicMock()
        env.app = app

        return {
            'db': db,
            'stream': stream,
            'env': env,
        }