コード例 #1
0
 async def init_for(self, module: str, safe=False) -> None:
     if self.engine != "tortoise.backends.mysql":
         raise test.SkipTest("mysql only")
     with patch("aiomysql.connect", new=CoroutineMock()):
         await Tortoise.init({
             "connections": {
                 "default": {
                     "engine": "tortoise.backends.mysql",
                     "credentials": {
                         "database": "test",
                         "host": "127.0.0.1",
                         "password": "******",
                         "port": 3306,
                         "user": "******",
                         "connect_timeout": 1.5,
                         "charset": "utf-8",
                     },
                 }
             },
             "apps": {
                 "models": {
                     "models": [module],
                     "default_connection": "default"
                 }
             },
         })
         self.sqls = get_schema_sql(Tortoise._connections["default"],
                                    safe).split("; ")
コード例 #2
0
 async def init_for(self, module: str, safe=False) -> None:
     try:
         with patch("asyncpg.create_pool", new=CoroutineMock()):
             await Tortoise.init({
                 "connections": {
                     "default": {
                         "engine": "tortoise.backends.asyncpg",
                         "credentials": {
                             "database": "test",
                             "host": "127.0.0.1",
                             "password": "******",
                             "port": 3306,
                             "user": "******",
                         },
                     }
                 },
                 "apps": {
                     "models": {
                         "models": [module],
                         "default_connection": "default"
                     }
                 },
             })
             self.sqls = get_schema_sql(Tortoise._connections["default"],
                                        safe).split("; ")
     except ImportError:
         raise test.SkipTest("asyncpg not installed")
コード例 #3
0
 async def asyncSetUp(self):
     await super().asyncSetUp()
     if Tortoise._inited:
         await self._tearDownDB()
     self.db_config = test.getDBConfig(app_label="models", modules=["tests.testmodels"])
     if self.db_config["connections"]["models"]["engine"] != "tortoise.backends.asyncpg":
         raise test.SkipTest("PostgreSQL only")
コード例 #4
0
 async def init_for(self, module: str, safe=False) -> None:
     if self.engine != "tortoise.backends.sqlite":
         raise test.SkipTest("sqlite only")
     with patch(
             "tortoise.backends.sqlite.client.SqliteClient.create_connection",
             new=CoroutineMock()):
         await Tortoise.init({
             "connections": {
                 "default": {
                     "engine": "tortoise.backends.sqlite",
                     "credentials": {
                         "file_path": ":memory:"
                     },
                 }
             },
             "apps": {
                 "models": {
                     "models": [module],
                     "default_connection": "default"
                 }
             },
         })
         self.sqls = get_schema_sql(Tortoise._connections["default"],
                                    safe).split("; ")
         self.post_sqls = generate_post_table_sql(
             Tortoise._connections["default"], safe).split("; ")
コード例 #5
0
 async def asyncSetUp(self):
     await super().asyncSetUp()
     if Tortoise._inited:
         await self._tearDownDB()
     self.db_config = test.getDBConfig(app_label="models",
                                       modules=["tests.testmodels"])
     if not self.is_asyncpg and not self.is_psycopg:
         raise test.SkipTest("PostgreSQL only")
コード例 #6
0
    async def test_brands_raw_prefetch_limited_products(self):
        if Product._meta.db.capabilities.dialect == "mysql":
            raise test.SkipTest(
                "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
            )

        await create_store_objects()

        raw_subquery = """
            (select "U1"."id" "0"
            from "store_product" "U1"
            where "U1"."brand_id"={context.top.table}."brand_id"
            order by "U1"."id" asc
            limit 2)
        """

        subquery = Product.raw(raw_subquery)
        prefetch = Prefetch('products',
                            queryset=Product.filter(id__in=Subquery(subquery)))
        brands_fetched = await Brand.all().order_by('name').prefetch_related(
            prefetch)
        brands_distilled = [{
            'name': b.name,
            'products': {p.name
                         for p in b.products}
        } for b in brands_fetched]

        self.assertEqual(brands_distilled, [
            {
                'name': 'brand_1',
                'products': {'product_01'}
            },
            {
                'name': 'brand_2',
                'products': {'product_02', 'product_03'}
            },
            {
                'name': 'brand_3',
                'products': {'product_04', 'product_05'}
            },
            {
                'name': 'brand_4',
                'products': {'product_07', 'product_08'}
            },
            {
                'name': 'brand_5',
                'products': {'product_11', 'product_12'}
            },
            {
                'name': 'brand_6',
                'products': {'product_16', 'product_17'}
            },
        ])
コード例 #7
0
    async def init_for(self, module: str, safe=False) -> None:
        if self.engine != "tortoise.backends.sqlite":
            raise test.SkipTest("sqlite only")

        Tortoise.init(
            {
                "connections": {
                    "default": {
                        "engine": "tortoise.backends.sqlite",
                        "file_path": ":memory:",
                    }
                },
                "apps": {"models": {"models": [module], "default_connection": "default"}},
            }
        )
        self.sqls = Tortoise.get_schema_sql(Tortoise.get_db_client("default"), safe=safe).split(";\n")
コード例 #8
0
    async def test_brands_prefetch_limited_products(self):
        if Product._meta.db.capabilities.dialect == "mysql":
            raise test.SkipTest(
                "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
            )

        await create_store_objects()

        subquery = Product.filter(
            brand=OuterRef('brand')).limit(3).values_list('id', flat=True)
        prefetch = Prefetch('products',
                            queryset=Product.filter(id__in=Subquery(subquery)))
        brands_fetched = await Brand.all().order_by('name').prefetch_related(
            prefetch)
        brands_distilled = [{
            'name': b.name,
            'products': {p.name
                         for p in b.products}
        } for b in brands_fetched]

        self.assertEqual(brands_distilled, [
            {
                'name': 'brand_1',
                'products': {'product_01'}
            },
            {
                'name': 'brand_2',
                'products': {'product_02', 'product_03'}
            },
            {
                'name': 'brand_3',
                'products': {'product_04', 'product_05', 'product_06'}
            },
            {
                'name': 'brand_4',
                'products': {'product_07', 'product_08', 'product_09'}
            },
            {
                'name': 'brand_5',
                'products': {'product_11', 'product_12', 'product_13'}
            },
            {
                'name': 'brand_6',
                'products': {'product_16', 'product_17', 'product_18'}
            },
        ])
コード例 #9
0
    async def test_products_prefetch_limit_images(self):
        if Product._meta.db.capabilities.dialect == "mysql":
            raise test.SkipTest(
                "This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'"
            )

        await create_store_objects()

        subquery = Image.filter(
            product_set=OuterRef('product_set')).limit(4).values_list(
                'id', flat=True)
        prefetch = Prefetch('images',
                            queryset=Image.filter(id__in=Subquery(subquery)))
        products_fetched = await Product.all().order_by('name').limit(
            5).prefetch_related(prefetch)
        products_distilled = [{
            'name': p.name,
            'images': {img.src
                       for img in p.images}
        } for p in products_fetched]

        self.assertEqual(products_distilled, [
            {
                'name': 'product_01',
                'images': {'image_16', 'image_17', 'image_18', 'image_19'}
            },
            {
                'name': 'product_02',
                'images': {'image_11', 'image_12', 'image_13', 'image_14'}
            },
            {
                'name': 'product_03',
                'images': {'image_7', 'image_8', 'image_9', 'image_10'}
            },
            {
                'name': 'product_04',
                'images': {'image_4', 'image_5', 'image_6'}
            },
            {
                'name': 'product_05',
                'images': {'image_2', 'image_3'}
            },
        ])
コード例 #10
0
 def setUp(self):
     self.db_config = self.get_db_config()
     if self.db_config["connections"]["models"][
             "engine"] != "tortoise.backends.asyncpg":
         raise test.SkipTest("PostgreSQL only")
コード例 #11
0
 def continue_if_safe_indexes(supported: bool):
     db = Tortoise.get_connection("default")
     if db.capabilities.safe_indexes != supported:
         raise test.SkipTest("safe_indexes != {}".format(supported))
コード例 #12
0
ファイル: test_mysql.py プロジェクト: nextfit/tortoise-orm
 def setUp(self) -> None:
     self.db_config = self.get_db_config()
     if self.db_config["connections"]["models"][
             "engine"] != "tortoise.backends.mysql":
         raise test.SkipTest("MySQL only")
コード例 #13
0
 async def _setUpDB(self) -> None:
     try:
         await super()._setUpDB()
     except OperationalError:
         raise test.SkipTest("Works only with PostgreSQL")