Skip to content

Commit c54ce84

Browse files
authored
Alpha tests (#3)
* tests * update pyproject include dev dependencies * Update tests to use fixtures, add new tests * remove unused fixture
1 parent 58adb96 commit c54ce84

14 files changed

+411
-9
lines changed

pyproject.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "FireFrame"
7-
version = "0.0.0-alpha.7"
7+
version = "0.0.0-alpha.8"
88
authors = [
99
{ name="Zachary Spar", email="[email protected]" },
1010
]
1111
description = "A firebase python web framework with ORM support."
1212
readme = "README.md"
13-
requires-python = ">=3.6"
13+
requires-python = ">=3.7"
1414
classifiers = [
1515
"Development Status :: 2 - Pre-Alpha",
1616
"Intended Audience :: Developers",
17-
"Programming Language :: Python :: 3.6",
1817
"Programming Language :: Python :: 3.7",
1918
"Programming Language :: Python :: 3.8",
2019
"Programming Language :: Python :: 3.9",
2120
"Programming Language :: Python :: 3.10",
21+
"Programming Language :: Python :: 3.11",
2222
"License :: OSI Approved :: BSD License",
2323
"Operating System :: OS Independent",
2424
]
@@ -29,6 +29,16 @@ dependencies = [
2929
"pydantic==2.5.2",
3030
]
3131

32+
[project.optional-dependencies]
33+
dev = [
34+
"black==23.11.0",
35+
"build==1.0.3",
36+
"httpx==0.25.2",
37+
"pytest==7.4.3",
38+
"pytest-xdist==3.5.0",
39+
"twine==4.0.2",
40+
]
41+
3242
[project.scripts]
3343
fireframe = "fireframe.cli:cli"
3444

src/fireframe/cli/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@
22

33
from .project import project_cli
44

5-
65
cli = click.CommandCollection(sources=[project_cli])

src/fireframe/cli/project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import click
21
import os
32
from pathlib import Path
43

4+
import click
5+
56

67
@click.group()
78
@click.pass_context

src/fireframe/core/mixins.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
Commonly used FireFrame mixins.
33
"""
44
from fastapi import APIRouter
5-
from .views import *
65

6+
from .views import *
77

88
__all__ = [
99
"crud_router",

src/fireframe/core/serializers.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55

66
from pydantic import BaseModel as BaseSerialzer, create_model
77
from pydantic.fields import FieldInfo
8-
98
from .models import Model
109

11-
1210
__all__ = [
1311
"ModelSerializer",
1412
]

src/fireframe/core/views.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
from fastapi import APIRouter, HTTPException
77
from fireo.queries.errors import ReferenceDocNotExist
8-
98
from .serializers import ModelSerializer
109

1110
__all__ = [

tests/views/__init__.py

Whitespace-only changes.

tests/views/_fixtures.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from uuid import uuid4
2+
3+
import pytest
4+
5+
__all__ = [
6+
"test_thread",
7+
]
8+
9+
10+
@pytest.fixture(autouse=True)
11+
def test_thread() -> str:
12+
"""
13+
Generate a unique thread id for each test.
14+
"""
15+
test_thread = uuid4()
16+
yield str(test_thread)

tests/views/test_base_api_view.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Test base API view class.
3+
"""
4+
import pytest
5+
6+
from fireframe.core.models import Model
7+
from fireframe.core.serializers import ModelSerializer
8+
from fireframe.core.views import BaseAPIView
9+
10+
11+
class TestBaseAPIView:
12+
def test_view_no_serializer(self):
13+
"""
14+
Test the BaseAPIView raises NotImplementedError when
15+
_generate_routes method is not implemented.
16+
"""
17+
18+
class TestBadView(BaseAPIView):
19+
pass
20+
21+
with pytest.raises(TypeError):
22+
TestBadView()
23+
24+
def test_view_no_generate_routes(self):
25+
class TestModel(Model):
26+
example: str
27+
28+
class TestSerializer(ModelSerializer):
29+
class Meta:
30+
model = TestModel
31+
fields = ["example"]
32+
33+
class TestBadView(BaseAPIView):
34+
serializer_class = TestSerializer
35+
36+
with pytest.raises(NotImplementedError):
37+
TestBadView()
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""
2+
Tests for the CreateAPIView class.
3+
"""
4+
5+
import pytest
6+
from fastapi.testclient import TestClient
7+
from fireframe.core.api import FireFrameAPI
8+
from fireframe.core.models import Model
9+
from fireframe.core.serializers import ModelSerializer
10+
from fireframe.core.views import BaseCreateAPIView
11+
from ._fixtures import test_thread
12+
13+
14+
class TestCreateAPIView:
15+
def test_view_no_serializer(self):
16+
"""
17+
Test the BaseCreateAPIView raises TypeError when
18+
serializer_class is not a subclass of ModelSerializer.
19+
"""
20+
21+
class TestBadView(BaseCreateAPIView):
22+
pass
23+
24+
with pytest.raises(TypeError):
25+
TestBadView()
26+
27+
@pytest.fixture(scope="function")
28+
def _TestModel(self, test_thread) -> Model:
29+
"""
30+
Cleanup the test collection after the test is done.
31+
"""
32+
33+
class TestModel(Model):
34+
example: str
35+
flag: bool
36+
price: float
37+
38+
class Meta:
39+
collection_name = f"test_collection_example_{test_thread}"
40+
41+
yield TestModel
42+
43+
# cleanup test collection
44+
TestModel.collection.delete_every(child=True)
45+
46+
def test_create_view_ok(self, test_thread, _TestModel):
47+
"""
48+
Test the BaseCreateAPIView works as expected.
49+
"""
50+
51+
class TestSerializer(ModelSerializer):
52+
class Meta:
53+
model = _TestModel
54+
fields = ["example", "flag", "price"]
55+
56+
class TestCreateView(BaseCreateAPIView):
57+
serializer_class = TestSerializer
58+
59+
app = FireFrameAPI()
60+
app.include_router(TestCreateView.as_router())
61+
client = TestClient(app)
62+
response = client.post(
63+
"/", json={"example": "This is a test from test_create_view_ok method", "flag": True, "price": 10.0}
64+
)
65+
assert response.status_code == 201
66+
assert response.json() == {
67+
"example": "This is a test from test_create_view_ok method",
68+
"flag": True,
69+
"price": 10.0,
70+
}

0 commit comments

Comments
 (0)