Skip to content

Commit

Permalink
test(integration): test get_entry and get_entries
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamNol committed Jun 25, 2022
1 parent 7996614 commit 410960e
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ jobs:
- name: Test
run: |
python -m pytest
python -m pytest test/unittests
7 changes: 6 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,17 @@
// cSpell custom dictionary
"cSpell.words": [
"aiohttp",
"asyncio",
"asyncutils",
"getattrs",
"mccabe",
"mypy",
"pydash",
"pystrapi",
"pytest",
"reqargs",
"strapi",
"testdata"
"testdata",
"testserver"
],
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,28 @@ asyncio.run(main())
```

## Development
### Install environment:
```
poetry install
```

### Lint
Run [prospector](https://prospector.landscape.io/):
```
prospector
```

### Unit tests
```
pytest test/unittests
```

### Integration tests
Run Strapi test server (see [instructions](testserver/README.md)), and run integration tests:
```
pytest test/integration
```

### Create new release

Push changes to 'main' branch following [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
Expand Down
36 changes: 35 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ prospector = {version = "^1.7.7", extras = ["with_mypy", "with_bandit"]}
autopep8 = "^1.6.0"
pytest = "^7.1.2"
types-requests = "^2.27.30"
pytest-asyncio = "^0.18.3"
pydash = "^5.1.0"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
asyncio_mode = auto
Empty file added test/__init__.py
Empty file.
Empty file added test/integration/__init__.py
Empty file.
36 changes: 36 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import pytest
from typing import Any, Type

from test.types import AnyStrapiClient
from pystrapi.strapi_client import StrapiClient
from pystrapi.strapi_client_sync import StrapiClientSync


@pytest.fixture(params=[StrapiClientSync, StrapiClient])
def client(request: Any) -> AnyStrapiClient:
client_type: Type[AnyStrapiClient] = request.param
return client_type()


@pytest.fixture
def post1() -> dict:
return {
"id": 1,
"attributes": {
"description": "The first post",
"content": "Hello",
"title": "First Post",
}
}


@pytest.fixture
def post2() -> dict:
return {
"id": 2,
"attributes": {
"description": "The second post",
"content": "Hello again",
"title": "Second Post"
}
}
17 changes: 17 additions & 0 deletions test/integration/test_strapi_client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from test.types import AnyStrapiClient
from test.utils import asyncutils
from pystrapi.types import StrapiEntriesResponse, StrapiEntryResponse

from pydash.predicates import is_match # type: ignore


async def test_get_entry(client: AnyStrapiClient, post1: dict) -> None:
res: StrapiEntryResponse = await asyncutils.value(client.get_entry('posts', 1))
assert is_match(res['data'], post1)


async def test_get_entries(client: AnyStrapiClient, post1: dict, post2: dict) -> None:
res: StrapiEntriesResponse = await asyncutils.value(client.get_entries('posts'))
assert res['data']
assert is_match(res['data'][0], post1)
assert is_match(res['data'][1], post2)
6 changes: 6 additions & 0 deletions test/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from typing import Union

from pystrapi.strapi_client import StrapiClient
from pystrapi.strapi_client_sync import StrapiClientSync

AnyStrapiClient = Union[StrapiClientSync, StrapiClient]
Empty file added test/unittests/__init__.py
Empty file.
14 changes: 14 additions & 0 deletions test/utils/asyncutils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import inspect
from typing import Any, Coroutine, TypeVar, Union


V = TypeVar("V", bound="Any")


async def value(val: Union[V, Coroutine[Any, Any, V]]) -> V:
"""
Return the given value. await if it's awaitable.
"""
if inspect.isawaitable(val):
return await val # type: ignore
return val # type: ignore

0 comments on commit 410960e

Please sign in to comment.