Fakultas Ilmu Komputer UI

tests.py 5.98 KB
Newer Older
1
2
from django.test import TestCase, Client
from django.urls import resolve
3

Saul Andre's avatar
Saul Andre committed
4
5
from .views import DaftarKatalog, DetailMateri
from .models import Materi, Category, Comment
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from django.core import serializers
import json


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

    def test_daftar_katalog_using_daftar_katalog_template(self):
        response = Client().get('/')
        self.assertTemplateUsed(response, 'app/katalog_materi.html')
    
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
47
    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'])

48
49
50
51
52
53
54
55
56
57
58
59
60
61
    def test_daftar_katalog_using_daftar_katalog_func(self):
        found = resolve('/')
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)

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

        resp = Materi.objects.get(id = materi.id)
        self.assertEqual(resp,materi)
    
Saul Andre's avatar
Saul Andre committed
62
63
64
65
66
67
68
69
70
71
72
73
class DetailMateriTest(TestCase):
    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)
74

Saul Andre's avatar
Saul Andre committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    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')
    
    def test_detail_materi_using_detail_materi_func(self):
        found = resolve('/materi/1/')
        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
99
100
101
102
103
104
    def test_comment_models_can_create_new_object(self):
        test = Comment.objects.create(user="saul", profile="121212", comment= "232323")
        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

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)
128
        self.assertEqual(response.status_code,200)
129
        self.assertTemplateUsed(response, expected_template_name)