Fakultas Ilmu Komputer UI

tests.py 6.42 KB
Newer Older
1
2
3
4
import json

from django.core import serializers
from django.test import Client, TestCase
5
from django.urls import resolve
6
7
from authentication.models import User
from django.core.files import File
8
from .models import Category, Comment, Materi
Saul Andre's avatar
Saul Andre committed
9
from .views import DaftarKatalog, DetailMateri
10
11
12
13
14


class DaftarKatalogTest(TestCase):
    def test_daftar_katalog_url_exist(self):
        url = "/"
15
16
        response = Client().get(f"{url}")
        self.assertEqual(response.status_code, 200)
17
18

    def test_daftar_katalog_using_daftar_katalog_template(self):
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

    # def test_daftar_kategori(self):
    #     kategori = Category()
    #     kategori.name = "baru"
    #     kategori.description = "kategori baru saya"
    #     kategori.save()

    #     kategori2 = Category()
    #     kategori2.name = "baru2"
    #     kategori2.save()

    #     materi1 = Materi()
    #     materi1.title = "baru lah"
    #     materi1.save()
    #     materi1.categories.add(kategori.id)
    #     materi1.save()

    #     materi2 = Materi()
    #     materi2.save()
    #     materi2.categories.add(kategori2)
    #     materi2.save()

    #     cl = Client()
    #     res = cl.get('/?kategori='+str(kategori.pk))
    #     self.assertIn(materi1, res.context['materi_list'])
    #     self.assertNotIn(materi2,res.context['materi_list'])
47

48
    def test_daftar_katalog_using_daftar_katalog_func(self):
49
        found = resolve("/")
50
51
52
53
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

    def test_fields(self):
        materi = Materi()
54
55
56
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
57
58
        materi.save()

59
60
61
62
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


Saul Andre's avatar
Saul Andre committed
63
class DetailMateriTest(TestCase):
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    def setUp(self):
        User.objects._create_user(email="alice@acme.com", password="acmecorp", is_admin=True)
        User.objects._create_user(email="kontrib@kontrib.com", password="acmecorp", is_contributor=True)
    
    def get_url_for_detail_materi():
        materi = Materi.objects.create()
        materi.title = "wahyu"
        materi.author = "Saul Andre Lumban Gaol"
        materi.publisher = "Diskominfo Depok"
        materi.descriptions = "This is test descriptions"
        url = "/materi/" + str(materi.id) + "/"
        return url

    def test_detail_materi_url_exist(self):
        response = Client().get(self.get_url_for_detail_materi())
        self.assertEqual(response.status_code,200)
        self.assertNotEqual(response.status_code, 404)

    def test_detail_materi_using_detail_materi_template(self):
        response = Client().get(self.get_url_for_detail_materi())
        self.assertTemplateUsed(response, 'app/detail_materi.html')
85

Saul Andre's avatar
Saul Andre committed
86
    def test_detail_materi_using_detail_materi_func(self):
87
        found = resolve(self.get_url_for_detail_materi())
Saul Andre's avatar
Saul Andre committed
88
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
89

Saul Andre's avatar
Saul Andre committed
90
91
92
93
94
95
96
    def test_category_models_can_create_new_object(self):
        test = Category.objects.create(id="1", name="medis", description="kategori medis")
        countData = Category.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "medis")
        self.assertNotEqual(test.__str__(), "saul")
Samuel Dimas's avatar
Samuel Dimas committed
97

Saul Andre's avatar
Saul Andre committed
98
    def test_comment_models_can_create_new_object(self):
99
        test = Comment.objects.create(user="saul", profile="121212", comment="232323")
Saul Andre's avatar
Saul Andre committed
100
101
102
103
104
        countData = Comment.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "saul")
        self.assertNotEqual(test.__str__(), "userlain")
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    
    def test_post_blank_comment(self):
        url = self.get_url_for_detail_materi()
        response = Client().post(url, {"comment": ""})
        self.assertIn("error_message", response.context_data)
        self.assertIn("Anda belum menuliskan komentar", response.context_data["error_message"])

    def test_comment_rendered_to_interface(self):
        url = self.get_url_for_detail_materi()
        Client().post(url, {"comment": "This is my new comment"})
        response = Client().get(url)
        self.assertContains(response, "This is my new comment")
    
    def test_comment_by_admin(self):
        url = self.get_url_for_detail_materi()
        Client().post("/login_admin/", {"email": "alice@acme.com", "pass": "acmecorp"})
        Client().post(url, {"comment": "This is new comment by Admin"})
        response = Client.get(url)
        self.assertContains(response, "Admin")

    def test_comment_by_kontributor(self):
        url = self.get_url_for_detail_materi()
        Client().post("/login/", {"email": "kontrib@kontrib.com", "pass": "acmecorp"})
        Client().post(url, {"comment": "This is new comment by Contributor"})
        response = Client.get(url)
        self.assertContains(response, "Contributor")

    def test_comment_by_anonymous(self):
        url = self.get_url_for_detail_materi()
        Client().get("/logout/")
        Client().post(url, {"comment": "This is new comment by Anonymous"})
        response = Client.get(url)
        self.assertContains(response, "Anonymous")
    
    def test_detail_materi_contains_form_comment(self):
        response = Client.get(self.get_url_for_detail_materi())
        self.assertContains(response, "komentar...")

    
144

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
class TemplateLoaderTest(TestCase):
    def test_template_loader_url_exist(self):
        url = "/test-page.html"
        response = Client().get(url)
        self.assertEqual(response.status_code, 200)

    def test_template_loader_using_template_loader_template(self):
        url = "/test-page.html"
        response = Client().get(url)
        expected_template_name = "test-page.html"
        self.assertTemplateUsed(response, expected_template_name)

    def test_template_loader_using_template_loader_func(self):
        url = "/test-page.html"
        found = resolve(url)
        expected_view_name = "pages"
        self.assertEqual(found.func.__name__, expected_view_name)

    def test_template_loader_handle_non_existent_html(self):
        url = "/test.html"
        expected_template_name = "error-404.html"
        response = Client().get(url)
167
        self.assertEqual(response.status_code, 200)
168
        self.assertTemplateUsed(response, expected_template_name)