This commit is contained in:
1 parent
d860157540
commit
b865a4a389
11 files changed
+274
-9
No files matched your search
@@ -131,6 +131,18 @@ class ApiTests(unittest.TestCase):
|
||||
self.assertEqual(status, expect, body)
|
||||
return body, headers.get("Content-Type"), headers
|
||||
|
||||
def delete(self, path, expect=200):
|
||||
req = urllib.request.Request(self.base + path, method="DELETE")
|
||||
try:
|
||||
with urllib.request.urlopen(req, timeout=10) as resp:
|
||||
status = resp.status
|
||||
body = resp.read().decode("utf-8")
|
||||
except urllib.error.HTTPError as exc:
|
||||
status = exc.code
|
||||
body = exc.read().decode("utf-8")
|
||||
self.assertEqual(status, expect, body)
|
||||
return json.loads(body) if body else None
|
||||
|
||||
def post(self, path, payload, expect=200):
|
||||
data = json.dumps(payload).encode("utf-8")
|
||||
req = urllib.request.Request(
|
||||
@@ -367,6 +379,46 @@ class ApiTests(unittest.TestCase):
|
||||
"limit": 5})
|
||||
self.assertTrue(data["results"])
|
||||
|
||||
def test_custom_mood_crud(self):
|
||||
try:
|
||||
data = self.post("/api/moods", {
|
||||
"name": "test-cozy", "description": "Тестовый профиль",
|
||||
"genres": ["Simulation"], "tags": {"Cozy": 1.5, "Relaxing": 1.0}})
|
||||
self.assertFalse(data["builtin"])
|
||||
self.assertEqual(data["mood"]["tags"]["Cozy"], 1.5)
|
||||
|
||||
moods = {mood["name"]: mood for mood in self.get("/api/moods")["moods"]}
|
||||
self.assertIn("test-cozy", moods)
|
||||
self.assertFalse(moods["test-cozy"]["builtin"])
|
||||
self.assertTrue(moods["dark-fantasy"]["builtin"])
|
||||
|
||||
updated = self.post("/api/moods", {
|
||||
"name": "test-cozy", "description": "Обновлён",
|
||||
"genres": [], "tags": {"Cozy": 2.0}})
|
||||
self.assertEqual(updated["mood"]["description"], "Обновлён")
|
||||
|
||||
self.assertEqual(self.post("/api/moods", {"name": "dark-fantasy", "tags": {"Cozy": 1}},
|
||||
expect=400)["error"], "builtin profile")
|
||||
self.assertEqual(self.post("/api/moods", {"name": "Bad Name", "tags": {"Cozy": 1}},
|
||||
expect=400)["error"], "invalid name")
|
||||
self.assertEqual(self.post("/api/moods", {"name": "empty-profile"}, expect=400)["error"],
|
||||
"empty profile")
|
||||
|
||||
self.assertTrue(self.delete("/api/moods/test-cozy")["deleted"])
|
||||
moods = {mood["name"] for mood in self.get("/api/moods")["moods"]}
|
||||
self.assertNotIn("test-cozy", moods)
|
||||
self.assertEqual(self.delete("/api/moods/dark-fantasy", expect=400)["error"],
|
||||
"builtin profile")
|
||||
self.assertEqual(self.delete("/api/moods/missing", expect=404)["error"],
|
||||
"unknown profile")
|
||||
finally:
|
||||
con = db.connect(self.db_path)
|
||||
try:
|
||||
con.execute("DELETE FROM moods WHERE name = 'test-cozy'")
|
||||
con.commit()
|
||||
finally:
|
||||
con.close()
|
||||
|
||||
def test_recommend_unknown_mood(self):
|
||||
data = self.post("/api/recommend", {"preset": "no-such-mood"}, expect=400)
|
||||
self.assertEqual(data["error"], "unknown mood")
|
||||
|
||||
Reference in new issue
Block a user