-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make ids endpoint work with UUID primary key columns * more custom pk fixes The detail endpoints now work if non-integers are passed in * more tests * more tests * make sure custom primary keys are excluded from pydantic model * add a test for invalid ID
- Loading branch information
1 parent
26d0adc
commit 83fe185
Showing
3 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from unittest import TestCase | ||
|
||
from piccolo.columns.column_types import UUID, Integer, Varchar | ||
from piccolo.table import Table | ||
from starlette.testclient import TestClient | ||
|
||
from piccolo_api.crud.endpoints import PiccoloCRUD | ||
|
||
|
||
class Movie(Table): | ||
id = UUID(primary_key=True) | ||
name = Varchar(length=100, required=True) | ||
rating = Integer() | ||
|
||
|
||
class TestCustomPK(TestCase): | ||
""" | ||
Make sure PiccoloCRUD works with Tables with a custom primary key column. | ||
""" | ||
|
||
def setUp(self): | ||
Movie.create_table(if_not_exists=True).run_sync() | ||
self.movie = Movie.objects().create(name="Star Wars").run_sync() | ||
self.client = TestClient(PiccoloCRUD(table=Movie, read_only=False)) | ||
|
||
def tearDown(self): | ||
Movie.alter().drop_table().run_sync() | ||
|
||
def test_get_ids(self): | ||
response = self.client.get("/ids/") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.json(), {str(self.movie.id): str(self.movie.id)} | ||
) | ||
|
||
def test_get_list(self): | ||
response = self.client.get("/") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.json(), | ||
{ | ||
"rows": [ | ||
{ | ||
"id": str(self.movie.id), | ||
"name": self.movie.name, | ||
"rating": self.movie.rating, | ||
} | ||
] | ||
}, | ||
) | ||
|
||
def test_get_single(self): | ||
response = self.client.get(f"/{str(self.movie.id)}") | ||
self.assertEqual(response.status_code, 200) | ||
self.assertEqual( | ||
response.json(), | ||
{ | ||
"id": str(self.movie.id), | ||
"name": self.movie.name, | ||
"rating": self.movie.rating, | ||
}, | ||
) | ||
|
||
def test_post(self): | ||
Movie.delete(force=True).run_sync() | ||
response = self.client.post( | ||
"/", json={"name": "Lord of the Rings", "rating": 1000} | ||
) | ||
self.assertEqual(response.status_code, 201) | ||
|
||
movie = Movie.select(Movie.name, Movie.rating).first().run_sync() | ||
self.assertEqual(movie, {"name": "Lord of the Rings", "rating": 1000}) | ||
|
||
def test_delete(self): | ||
response = self.client.delete(f"/{self.movie.id}/") | ||
self.assertEqual(response.status_code, 204) | ||
self.assertEqual(Movie.count().run_sync(), 0) | ||
|
||
def test_patch(self): | ||
response = self.client.patch( | ||
f"/{self.movie.id}/", json={"rating": 2000} | ||
) | ||
self.assertEqual(response.status_code, 200) | ||
movie = Movie.select().first().run_sync() | ||
self.assertEqual( | ||
movie, {"id": self.movie.id, "name": "Star Wars", "rating": 2000} | ||
) | ||
|
||
def test_invalid_id(self): | ||
response = self.client.get("/abc123/") | ||
self.assertEqual(response.status_code, 400) | ||
self.assertEqual(response.content, b"The ID is invalid") |