Skip to content

Commit

Permalink
テストの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
KobashiYu committed Jun 25, 2024
1 parent df5d768 commit cc84f6b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 13 deletions.
18 changes: 16 additions & 2 deletions accounts/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.urls import reverse

from mysite.settings import LOGIN_REDIRECT_URL, LOGOUT_REDIRECT_URL
from tweets.models import Tweet

User = get_user_model()

Expand Down Expand Up @@ -259,8 +260,21 @@ def test_success_post(self):
self.assertNotIn(SESSION_KEY, self.client.session)


# class TestUserProfileView(TestCase):
# def test_success_get(self):
class TestUserProfileView(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.client.login(username="testuser", password="testpassword")

self.url = reverse("accounts:user_profile", kwargs={"username": self.user})
self.tweet = Tweet.objects.create(user=self.user, content="this is a tweet")

def test_success_get(self):
response = self.client.get(self.url)
tweets_db = Tweet.objects.all()

self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "accounts/profile.html")
self.assertQuerysetEqual(response.context["tweets"], tweets_db)


# class TestUserProfileEditView(TestCase):
Expand Down
81 changes: 70 additions & 11 deletions tweets/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,85 @@ def test_success_get(self):
self.assertQuerysetEqual(response.context["tweets"], tweets_db)


# class TestTweetCreateView(TestCase):
# def test_success_get(self):
class TestTweetCreateView(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.client.login(username="testuser", password="testpassword")
self.url = reverse("tweets:create")
self.tweet = Tweet.objects.create(user=self.user, content="this is a tweet")

# def test_success_post(self):
def test_success_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)

# def test_failure_post_with_empty_content(self):
def test_success_post(self):
valid_data = {"content": "tweet"}
response = self.client.post(self.url, valid_data)

# def test_failure_post_with_too_long_content(self):
self.assertRedirects(
response,
reverse("tweets:home"),
status_code=302,
)
self.assertTrue(Tweet.objects.filter(content=valid_data["content"]).exists())

def test_failure_post_with_empty_content(self):
invalid_data = {"content": ""}
response = self.client.post(self.url, invalid_data)
form = response.context["form"]

# class TestTweetDetailView(TestCase):
# def test_success_get(self):
self.assertEqual(response.status_code, 200)
self.assertFalse(Tweet.objects.filter(content=invalid_data["content"]).exists())
self.assertIn("このフィールドは必須です。", form.errors["content"])

def test_failure_post_with_too_long_content(self):
invalid_data = {"content": "a" * 281}
response = self.client.post(self.url, invalid_data)
form = response.context["form"]

# class TestTweetDeleteView(TestCase):
# def test_success_post(self):
self.assertEqual(response.status_code, 200)
self.assertFalse(Tweet.objects.filter(content=invalid_data["content"]).exists())
self.assertIn(f"この値は 140 文字以下でなければなりません( {len(invalid_data['content'])} 文字になっています)。", form.errors["content"])

# def test_failure_post_with_not_exist_tweet(self):

# def test_failure_post_with_incorrect_user(self):
class TestTweetDetailView(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.client.login(username="testuser", password="testpassword")
self.tweet = Tweet.objects.create(user=self.user, content="this is a tweet")
self.url = reverse("tweets:detail", kwargs={"pk": self.tweet.pk})

def test_success_get(self):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.context["tweet"], self.tweet)


class TestTweetDeleteView(TestCase):
def setUp(self):
self.user = User.objects.create_user(username="testuser", password="testpassword")
self.client.login(username="testuser", password="testpassword")
self.tweet = Tweet.objects.create(user=self.user, content="this is a tweet")
self.url = reverse("tweets:delete", kwargs={"pk": self.tweet.pk})

def test_success_post(self):
response = self.client.post(self.url)
self.assertRedirects(response, reverse("tweets:home"), status_code=302)
self.assertFalse(Tweet.objects.filter(pk=self.tweet.pk).exists())

def test_failure_post_with_not_exist_tweet(self):
self.non_exist_tweet_url = reverse("tweets:delete", kwargs={"pk": 9999})
response = self.client.post(self.non_exist_tweet_url)
self.assertEqual(response.status_code, 404)
self.assertTrue(Tweet.objects.filter(pk=self.tweet.pk).exists())

def test_failure_post_with_incorrect_user(self):
self.another_user = User.objects.create_user(username="another_testuser", password="another_testpassword")
self.another_tweet = Tweet.objects.create(user=self.another_user, content="this is another tweet")
self.another_url = reverse("tweets:delete", kwargs={"pk": self.another_tweet.pk})
response = self.client.post(self.another_url)
self.assertEqual(response.status_code, 403)
self.assertTrue(Tweet.objects.filter(pk=self.another_tweet.pk).exists())


# class TestLikeView(TestCase):
Expand Down

0 comments on commit cc84f6b

Please sign in to comment.