Fakultas Ilmu Komputer UI

tests.py 5.05 KB
Newer Older
1
2
from django.test import TestCase, Client
from django.urls import resolve
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
46
47
48
from .views import DaftarKatalog, listMateri
from .models import Materi
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')
    
    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)
    
    
    def test_resolution(self):
        resolver = resolve('/list-materi/')
        self.assertEqual(resolver.func, listMateri)


    def test_fungsi_get(self):
        self.materi = Materi()
        self.materi.title = 'Wahyu'
        self.materi.cover = 'https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg'
        self.materi.author = 'Saul Andre Lumban Gaol'
        self.materi.save()
        client = Client()
        
        queryset = client.get('/list-materi/')
        
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
        expected_output = json.dumps({"pk": self.materi.pk, "model": "app.materi", 
                                    "fields": {"cover": "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg", 
                                    "title": "Wahyu", 
                                    "author": "Saul Andre Lumban Gaol",
                                    "uploader": "Bayu",
                                    "publisher": "Diskominfo Depok",
                                    "description": "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.",
                                    "category": "#medis"}})
        unexpected_output = json.dumps({"pk": self.materi.pk, "model": "app.materi", 
                                    "fields": {"cover": "", 
                                    "title": "", 
                                    "author": "",
                                    "uploader": "",
                                    "publisher": "",
                                    "description": "",
                                    "category": ""}})
65
        self.assertJSONEqual(queryset.json()[1:-1], expected_output)
66
67
68
69
70
71
72
73
        self.assertJSONNotEqual(queryset.json()[1:-1], unexpected_output)

class DetailMateriTest(TestCase):
    def test_detail_materi_url_exist(self):
        url = "/detail-materi"
        response = Client().get(f'{url}')
        self.assertEqual(response.status_code,200)
        self.assertNotEqual(response.status_code, 404)
Samuel Dimas's avatar
Samuel Dimas committed
74

75
76
77
78
79
80
81
    def test_detail_materi_using_daftar_katalog_template(self):
        response = Client().get('/')
        self.assertTemplateUsed(response, 'app/detail_materi.html')
    
    def test_detail_materi_using_detail_materi_func(self):
        found = resolve('/')
        self.assertEqual(found.func.__name__, detail_materi.as_view().__name__)
82

83
84
85
86
87
88
    def test_kategori_models_can_create_new_object(self):
        test = Category.objects.create(id="1", name="medis", desc="kategori medis")
        countData = Category.objects.all().count()
        self.assertEqual(1, countData)
        self.assertEqual(test.__str__(), "medis")

89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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)
        self.assertEqual(response.status_code, 200)
        self.assertTemplateUsed(response, expected_template_name)