From dac1347229d876b9a3de525f8566bbfc0c0c62bd Mon Sep 17 00:00:00 2001 From: Jeferson Daniel Date: Thu, 4 Jan 2024 21:24:14 -0300 Subject: [PATCH] fix: Fix README code snippets (#65) --- .github/workflows/integration_test.yml | 27 ++++++++++++++++++++++++++ README.md | 24 +++++++++++++++-------- integration_test/__init__.py | 0 integration_test/test_readme.py | 27 ++++++++++++++++++++++++++ phulpyfile.py | 7 +++++++ 5 files changed, 77 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/integration_test.yml create mode 100644 integration_test/__init__.py create mode 100644 integration_test/test_readme.py diff --git a/.github/workflows/integration_test.yml b/.github/workflows/integration_test.yml new file mode 100644 index 0000000..2f0e4f0 --- /dev/null +++ b/.github/workflows/integration_test.yml @@ -0,0 +1,27 @@ +name: Integration Tests +on: + push: + branches: + - main + pull_request: + types: [opened, synchronize] +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Start MongoDB + uses: supercharge/mongodb-github-action@1.10.0 + with: + mongodb-version: "4.4" + - uses: actions/checkout@v4 + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v4 + with: + python-version: "3.11" + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements_test.txt + - name: Run integration test + run: | + phulpy integration_test diff --git a/README.md b/README.md index 68394f1..7deeba1 100644 --- a/README.md +++ b/README.md @@ -15,18 +15,20 @@ pip install pydantic-mongo ### Example Code ```python +from bson import ObjectId from pydantic import BaseModel from pydantic_mongo import AbstractRepository, ObjectIdField from pymongo import MongoClient -from bson import ObjectId +from typing import List +import os class Foo(BaseModel): count: int size: float = None class Bar(BaseModel): - apple = 'x' - banana = 'y' + apple: str = 'x' + banana: str = 'y' class Spam(BaseModel): id: ObjectIdField = None @@ -37,18 +39,24 @@ class SpamRepository(AbstractRepository[Spam]): class Meta: collection_name = 'spams' -client = MongoClient(os.environ["MONGODB_URL"]) -database = client[os.environ["MONGODB_DATABASE"]] +client = MongoClient("mongodb://localhost:27017") +database = client["example"] spam = Spam(foo=Foo(count=1, size=1.0),bars=[Bar()]) +spam_with_predefined_id = Spam( + id=ObjectId("611827f2878b88b49ebb69fc"), + foo=Foo(count=2, size=2.0), + bars=[Bar()] +) + spam_repository = SpamRepository(database=database) # Insert / Update spam_repository.save(spam) # Insert / Update many items -spam_repository.save_many([spam]) +spam_repository.save_many([spam, spam_with_predefined_id]) # Delete spam_repository.delete(spam) @@ -58,6 +66,7 @@ result = spam_repository.find_one_by_id(spam.id) # Find One By Id using string if the id attribute is a ObjectIdField result = spam_repository.find_one_by_id(ObjectId('611827f2878b88b49ebb69fc')) +assert result.foo.count == 2 # Find One By Query result = spam_repository.find_one_by({'foo.count': 1}) @@ -67,6 +76,5 @@ results = spam_repository.find_by({'foo.count': {'$gte': 1}}) # Paginate using cursor based pagination edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1) -more_edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1, after=edges[-1].cursor) -last_model = more_edges[-1].node +more_edges = spam_repository.paginate({'foo.count': {'$gte': 1}}, limit=1, after=list(edges)[-1].cursor) ``` diff --git a/integration_test/__init__.py b/integration_test/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/integration_test/test_readme.py b/integration_test/test_readme.py new file mode 100644 index 0000000..94c64e5 --- /dev/null +++ b/integration_test/test_readme.py @@ -0,0 +1,27 @@ +import contextlib +import io +import os +import re + +def extract_python_snippets(content): + # Regular expression pattern for finding Python code blocks + pattern = r'```python(.*?)```' + snippets = re.findall(pattern, content, re.DOTALL) + + return snippets + +def evaluate_snippet(snippet): + # Capture the output of the snippet + output_buffer = io.StringIO() + with contextlib.redirect_stdout(output_buffer): + exec(snippet, globals()) + return output_buffer.getvalue() + + +class TestReadme: + def test_readme(self): + readme_path = os.path.join(os.path.dirname(__file__), "..", "README.md") + readme_contents = open(readme_path, "r").read().strip() + snippets = extract_python_snippets(readme_contents) + for snippet in snippets: + evaluate_snippet(snippet) diff --git a/phulpyfile.py b/phulpyfile.py index 3a3a71d..a9df97a 100644 --- a/phulpyfile.py +++ b/phulpyfile.py @@ -37,6 +37,13 @@ def unit_test(phulpy): raise Exception("Unit test is not fully covered") +@task +def integration_test(phulpy): + result = system("pytest integration_test") + if result: + raise Exception("Integration tests failed") + + @task def typecheck(phulpy): result = system(