Fakultas Ilmu Komputer UI

tests.py 4.94 KB
Newer Older
1
2
3
import json

from django.core import serializers
4
from django.test import Client, TestCase, RequestFactory
5
from django.urls import resolve
6

7
from .models import Category, Comment, Materi
Saul Andre's avatar
Saul Andre committed
8
from .views import DaftarKatalog, DetailMateri
9

10
11
12
from app.views import UploadMateriHTML, UploadMateriView
from authentication.models import User

13
14
15
16

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

    def test_daftar_katalog_using_daftar_katalog_template(self):
21
22
23
        response = Client().get("/")
        self.assertTemplateUsed(response, "app/katalog_materi.html")

24
    def test_daftar_katalog_using_daftar_katalog_func(self):
25
        found = resolve("/")
26
27
28
29
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

    def test_fields(self):
        materi = Materi()
30
31
32
        materi.title = "tes"
        materi.cover = "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg"
        materi.author = "input"
33
34
        materi.save()

35
36
37
38
        resp = Materi.objects.get(id=materi.id)
        self.assertEqual(resp, materi)


Saul Andre's avatar
Saul Andre committed
39
class DetailMateriTest(TestCase):
40

Saul Andre's avatar
Saul Andre committed
41
    def test_detail_materi_using_detail_materi_func(self):
42
        found = resolve("/materi/1/")
Saul Andre's avatar
Saul Andre committed
43
        self.assertEqual(found.func.__name__, DetailMateri.as_view().__name__)
44

Saul Andre's avatar
Saul Andre committed
45
46
47
48
49
50
51
    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
52

Saul Andre's avatar
Saul Andre committed
53
    def test_comment_models_can_create_new_object(self):
54
        test = Comment.objects.create(user="saul", profile="121212", comment="232323")
Saul Andre's avatar
Saul Andre committed
55
56
57
58
59
        countData = Comment.objects.all().count()
        self.assertEqual(1, countData)
        self.assertNotEqual(0, countData)
        self.assertEqual(test.__str__(), "saul")
        self.assertNotEqual(test.__str__(), "userlain")
60

61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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)
84
        self.assertEqual(response.status_code, 200)
85
        self.assertTemplateUsed(response, expected_template_name)
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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


class UploadPageTest(TestCase):
    def setUp(self):
        self.client=Client()
        self.user = User.objects._create_user(email="kontributor@gov.id",
                                              password="kontributor", is_contributor=True)

    def test_upload_page_using_login_func(self):
        found = resolve("/unggah/")
        self.assertEqual(found.func.__name__, UploadMateriView.as_view().__name__)

    def test_uplaod_page_url_is_exist(self):
        #Positive test
        self.client.login(email="kontributor@gov.id",
                          password="kontributor")
        response = self.client.get("/unggah/")
        self.assertEqual(response.status_code,200)

        # Negative tests
        self.client.login(email="kontributor@gov.id",
                       password="kontributor")
        response = Client().get("/fake/")
        self.assertEqual(response.status_code, 404)

    def test_upload_page_template(self):
        url = "/unggah/"
        self.client.login(email="kontributor@gov.id",
                       password="kontributor")
        response = self.client.get(url)
        expected_template_name = "unggah.html"
        self.assertTemplateUsed(response, expected_template_name)

    def test_upload_page_title(self):
        self.client.login(email="kontributor@gov.id",
                       password="kontributor")
        response = self.client.get("/unggah/")

        # Positive tests
        self.assertContains(response, "Unggah Materi")

        # Negative tests
        self.assertNotContains(response, "Anything")

    def test_upload_page_form_field(self):
        self.client.login(email="kontributor@gov.id",
                       password="kontributor")
        response = self.client.get("/unggah/")

        # Positive tests
        self.assertContains(response, "title")

        # Negative tests
        self.assertNotContains(response, "anything")