Add api tests

This commit is contained in:
Paul Konstantin Gerke 2022-03-06 19:47:47 +01:00
parent 5f23eecb84
commit 7d8ead594c
3 changed files with 66 additions and 3 deletions

View File

@ -552,3 +552,56 @@ class WeightAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, entry)
class TagsAPITestCase(TestBase.BabyBuddyAPITestCaseBase):
endpoint = reverse("api:tag-list")
model = models.Tag
def test_get(self):
response = self.client.get(self.endpoint)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictEqual(
dict(response.data["results"][0]),
{
"name": "a name",
"slug": "a-name",
"color": "#FF0000",
"last_used": "2017-11-18T11:00:00-05:00",
},
)
def test_post(self):
data = {"name": "new tag", "color": "#123456"}
response = self.client.post(self.endpoint, data, format="json")
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
response = self.client.get(self.endpoint)
results = response.json()["results"]
results_by_name = {r["name"]: r for r in results}
tag_data = results_by_name["new tag"]
self.assertDictContainsSubset(data, tag_data)
self.assertEqual(tag_data["slug"], "new-tag")
self.assertTrue(tag_data["last_used"])
def test_patch(self):
endpoint = f"{self.endpoint}a-name/"
modified_data = {
"name": "A different name",
"color": "#567890",
}
response = self.client.patch(
endpoint,
modified_data,
)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertDictContainsSubset(modified_data, response.data)
def test_delete(self):
endpoint = f"{self.endpoint}a-name/"
response = self.client.delete(endpoint)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
response = self.client.delete(endpoint)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

View File

@ -462,5 +462,16 @@
"date": "2017-11-18",
"notes": "before feed"
}
},
{
"model": "core.tag",
"pk": 1,
"fields":
{
"name": "a name",
"slug": "a-name",
"color": "#FF0000",
"last_used": "2017-11-18T16:00:00Z"
}
}
]

View File

@ -106,8 +106,7 @@ def random_color():
class Tag(TagBase):
class Meta:
verbose_name = _("Tag")
verbose_name_plural = _("Tags")
verbose_name = _("Tags")
color = models.CharField(
verbose_name=_("Color"),