style: format backend with black
Also drops the session-sliding comment in login_required.
This commit is contained in:
+48
-13
@@ -9,8 +9,14 @@ class BookTests(ApiTestCase):
|
||||
def test_create_returns_full_shape(self):
|
||||
book = self.create_book()
|
||||
self.assertEqual(book["title"], "Dune")
|
||||
self.assertEqual(book["genre"], {"id": self.genre_id("Science Fiction"), "name": "Science Fiction"})
|
||||
self.assertEqual((book["current_page"], book["total_pages"], book["status"]), (0, 412, "not_started"))
|
||||
self.assertEqual(
|
||||
book["genre"],
|
||||
{"id": self.genre_id("Science Fiction"), "name": "Science Fiction"},
|
||||
)
|
||||
self.assertEqual(
|
||||
(book["current_page"], book["total_pages"], book["status"]),
|
||||
(0, 412, "not_started"),
|
||||
)
|
||||
self.assertIn("T", book["created_at"]) # ISO 8601
|
||||
|
||||
def test_create_trims_text(self):
|
||||
@@ -19,25 +25,44 @@ class BookTests(ApiTestCase):
|
||||
|
||||
def test_progress_updates_status(self):
|
||||
book = self.create_book()
|
||||
reading = self.call("PATCH", f"/api/books/{book['id']}", {"current_page": 100}).get_json()
|
||||
reading = self.call(
|
||||
"PATCH", f"/api/books/{book['id']}", {"current_page": 100}
|
||||
).get_json()
|
||||
self.assertEqual((reading["current_page"], reading["status"]), (100, "reading"))
|
||||
finished = self.call("PATCH", f"/api/books/{book['id']}", {"current_page": 412}).get_json()
|
||||
finished = self.call(
|
||||
"PATCH", f"/api/books/{book['id']}", {"current_page": 412}
|
||||
).get_json()
|
||||
self.assertEqual(finished["status"], "finished")
|
||||
self.assertEqual(self.call("GET", f"/api/books/{book['id']}").get_json()["current_page"], 412)
|
||||
self.assertEqual(
|
||||
self.call("GET", f"/api/books/{book['id']}").get_json()["current_page"], 412
|
||||
)
|
||||
|
||||
def test_edit_details(self):
|
||||
book = self.create_book()
|
||||
response = self.call(
|
||||
"PATCH", f"/api/books/{book['id']}",
|
||||
{"title": "Dune Messiah", "genre_id": self.genre_id("Fantasy"), "total_pages": 256},
|
||||
"PATCH",
|
||||
f"/api/books/{book['id']}",
|
||||
{
|
||||
"title": "Dune Messiah",
|
||||
"genre_id": self.genre_id("Fantasy"),
|
||||
"total_pages": 256,
|
||||
},
|
||||
)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
updated = response.get_json()
|
||||
self.assertEqual((updated["title"], updated["genre"]["name"], updated["total_pages"]), ("Dune Messiah", "Fantasy", 256))
|
||||
self.assertEqual(
|
||||
(updated["title"], updated["genre"]["name"], updated["total_pages"]),
|
||||
("Dune Messiah", "Fantasy", 256),
|
||||
)
|
||||
self.assertGreater(updated["updated_at"], book["updated_at"])
|
||||
|
||||
def test_create_validation(self):
|
||||
valid = {"title": "Dune", "author": "Frank Herbert", "genre_id": self.genre_id("Fiction"), "total_pages": 412}
|
||||
valid = {
|
||||
"title": "Dune",
|
||||
"author": "Frank Herbert",
|
||||
"genre_id": self.genre_id("Fiction"),
|
||||
"total_pages": 412,
|
||||
}
|
||||
cases = [
|
||||
({**valid, "title": " "}, "title"),
|
||||
({k: v for k, v in valid.items() if k != "author"}, "author"),
|
||||
@@ -69,13 +94,19 @@ class BookTests(ApiTestCase):
|
||||
|
||||
def test_empty_patch_is_rejected(self):
|
||||
book = self.create_book()
|
||||
self.assertEqual(self.call("PATCH", f"/api/books/{book['id']}", {}).status_code, 400)
|
||||
self.assertEqual(
|
||||
self.call("PATCH", f"/api/books/{book['id']}", {}).status_code, 400
|
||||
)
|
||||
|
||||
def test_delete(self):
|
||||
book = self.create_book()
|
||||
self.assertEqual(self.call("DELETE", f"/api/books/{book['id']}").status_code, 204)
|
||||
self.assertEqual(
|
||||
self.call("DELETE", f"/api/books/{book['id']}").status_code, 204
|
||||
)
|
||||
self.assertEqual(self.call("GET", f"/api/books/{book['id']}").status_code, 404)
|
||||
self.assertEqual(self.call("DELETE", f"/api/books/{book['id']}").status_code, 404)
|
||||
self.assertEqual(
|
||||
self.call("DELETE", f"/api/books/{book['id']}").status_code, 404
|
||||
)
|
||||
|
||||
def test_out_of_range_id_is_404(self):
|
||||
self.assertEqual(self.call("GET", "/api/books/99999999999").status_code, 404)
|
||||
@@ -105,7 +136,11 @@ class BookIsolationTests(ApiTestCase):
|
||||
book = self.create_book()
|
||||
bob = self.other_user("bob")
|
||||
path = f"/api/books/{book['id']}"
|
||||
for method, payload in (("GET", None), ("PATCH", {"title": "Hacked"}), ("DELETE", None)):
|
||||
for method, payload in (
|
||||
("GET", None),
|
||||
("PATCH", {"title": "Hacked"}),
|
||||
("DELETE", None),
|
||||
):
|
||||
with self.subTest(method=method):
|
||||
response = self.call(method, path, payload, client=bob)
|
||||
self.assertEqual(response.status_code, 404)
|
||||
|
||||
Reference in New Issue
Block a user