Fakultas Ilmu Komputer UI

tests.py 2.77 KB
Newer Older
Putu Agastya's avatar
Putu Agastya committed
1
2
from django.test import TestCase, Client
from django.urls import resolve
3
4
5
6
7
from .views import DaftarKatalog, listMateri
from .models import Materi
from django.core import serializers
import json

Samuel Dimas's avatar
Samuel Dimas committed
8

Putu Agastya's avatar
Putu Agastya committed
9
10
11
12
class DaftarKatalogTest(TestCase):
    def test_daftar_katalog_url_exist(self):
        url = "/"
        response = Client().get(f'{url}')
13
14
15
16
        self.assertEqual(response.status_code,200)

    def test_daftar_katalog_using_daftar_katalog_template(self):
        response = Client().get('/')
17
        self.assertTemplateUsed(response, 'app/katalog_materi.html')
18
    
19
20
21
22

    def test_daftar_katalog_using_daftar_katalog_func(self):
        found = resolve('/')
        self.assertEqual(found.func.__name__, DaftarKatalog.as_view().__name__)
Samuel Dimas's avatar
Samuel Dimas committed
23

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

31
        resp = Materi.objects.get(id = materi.id)
32
33
34
35
        self.assertEqual(resp,materi)
    
    
    def test_resolution(self):
36
37
        resolver = resolve('/list-materi/')
        self.assertEqual(resolver.func, listMateri)
38
39
40
41
42


    def test_fungsi_get(self):
        self.materi = Materi()
        self.materi.title = 'Wahyu'
43
        self.materi.cover = 'https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg'
44
45
46
47
        self.materi.author = 'Saul Andre Lumban Gaol'
        self.materi.save()
        client = Client()
        
48
49
        queryset = client.get('/list-materi/')
        
50
        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"}})
51
        self.assertJSONEqual(queryset.json()[1:-1], expected_output)
52

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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"
69
70
71
72
73
74
75
        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)
76
        self.assertTemplateUsed(response, expected_template_name)