File: test_concurrency.py

package info (click to toggle)
python-beanie 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,480 kB
  • sloc: python: 14,427; makefile: 7; sh: 6
file content (39 lines) | stat: -rw-r--r-- 989 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio

from pymongo import AsyncMongoClient

from beanie import Document, init_beanie


class SampleModel(Document):
    s: str = "TEST"
    i: int = 10


class SampleModel2(SampleModel): ...


class SampleModel3(SampleModel2): ...


class TestConcurrency:
    async def test_without_init(self, settings):
        clients = []
        for _ in range(10):
            client = AsyncMongoClient(settings.mongodb_dsn)
            clients.append(client)
            db = client[settings.mongodb_db_name]
            await init_beanie(
                db, document_models=[SampleModel3, SampleModel, SampleModel2]
            )

            async def insert_find():
                await SampleModel2().insert()
                docs = await SampleModel2.find(SampleModel2.i == 10).to_list()
                return docs

            await asyncio.gather(*[insert_find() for _ in range(10)])

        await SampleModel2.delete_all()

        [await client.close() for client in clients]