Skip to content

Commit

Permalink
fix: Fix README code snippets (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
jefersondaniel authored Jan 5, 2024
1 parent d8ad3b5 commit dac1347
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
24 changes: 16 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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})
Expand All @@ -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)
```
Empty file added integration_test/__init__.py
Empty file.
27 changes: 27 additions & 0 deletions integration_test/test_readme.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 7 additions & 0 deletions phulpyfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit dac1347

Please sign in to comment.