Fakultas Ilmu Komputer UI

tests.py 6.04 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 .models import Category, Comment, Materi
Saul Andre's avatar
Saul Andre committed
8
from .views import DaftarKatalog, DetailMateri
9
10
11
12
13


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

    def test_daftar_katalog_using_daftar_katalog_template(self):
18
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
        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'])
46

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

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

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


Saul Andre's avatar
Saul Andre committed
62
class DetailMateriTest(TestCase):
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    # def test_detail_materi_url_exist(self):
    #     materi = Materi.objects.create(title="wahyu",
    #                             cover='https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg',
    #                             author='Saul Andre Lumban Gaol',
    #                             publisher='Diskominfo Depok',
    #                             descriptions="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
    #                             )
    #     url = "/materi/" + str(materi.id) + "/"
    #     response = Client().get(url)
    #     self.assertEqual(response.status_code,200)
    #     self.assertNotEqual(response.status_code, 404)

    # def test_detail_materi_using_detail_materi_template(self):
    #     materi = Materi.objects.create(title="wahyu",
    #                             cover='https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg',
    #                             author='Saul Andre Lumban Gaol',
    #                             publisher='Diskominfo Depok',
    #                             descriptions="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.",
    #                             )
    #     url = "/materi/" + str(materi.id) + "/"
    #     response = Client().get(url)
    #     self.assertTemplateUsed(response, 'app/detail_materi.html')

Saul Andre's avatar
Saul Andre committed
86
    def test_detail_materi_using_detail_materi_func(self):
87
        found = resolve("/materi/1/")
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
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)
129
        self.assertEqual(response.status_code, 200)
130
        self.assertTemplateUsed(response, expected_template_name)