Fakultas Ilmu Komputer UI

tests.py 3.22 KB
Newer Older
Putu Agastya's avatar
Putu Agastya committed
1
2
from django.test import TestCase, Client
from django.urls import resolve
3
from .views import DaftarKatalog
Samuel Dimas's avatar
Samuel Dimas committed
4

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

    def test_daftar_katalog_using_daftar_katalog_template(self):
        response = Client().get('/')
13
        self.assertTemplateUsed(response, 'app/katalog_materi.html')
14
15
16
17

    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
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
49
50
51
    def test_fields(self):
        materi = Materi()
        materi.title = 'tes'
        materi.cover = 'https://scontent.fbdo6-2.fna.fbcdn.net/v/t1.0-9/60274038_2279638138757984_7988420823012605952_n.jpg?_nc_cat=108&_nc_sid=110474&_nc_oc=AQlRDnDhg1kJjuoXEwYCYWosatAaFqwSiZdMmfbX_ORoTXu7cvJoawUihJPCjHXCqr0&_nc_ht=scontent.fbdo6-2.fna&oh=169b2c36df1a49f6a394208f21d09f7b&oe=5E94FF4E''
        materi.author = 'input'
        materi.save()

        resp = Materi.objects.get(materi.id)
        self.assertEqual(resp,materi)
    
    
    def test_resolution(self):
        resolver = resolve('/listMateri')
        self.assertEqual(resolver, listMateri)


    def test_fungsi_get(self):
        self.materi = Materi()
        self.materi.title = 'Wahyu'
        self.materi.cover = 'https://scontent.fbdo6-2.fna.fbcdn.net/v/t1.0-9/60274038_2279638138757984_7988420823012605952_n.jpg?_nc_cat=108&_nc_sid=110474&_nc_oc=AQlRDnDhg1kJjuoXEwYCYWosatAaFqwSiZdMmfbX_ORoTXu7cvJoawUihJPCjHXCqr0&_nc_ht=scontent.fbdo6-2.fna&oh=169b2c36df1a49f6a394208f21d09f7b&oe=5E94FF4E''
        self.materi.author = 'Saul Andre Lumban Gaol'
        self.materi.save()

        client = Client()
        res = client.get('/listMateri')
        
        expected_output = json.dumps({
      cover : 'https://scontent.fbdo6-2.fna.fbcdn.net/v/t1.0-9/60274038_2279638138757984_7988420823012605952_n.jpg?_nc_cat=108&_nc_sid=110474&_nc_oc=AQlRDnDhg1kJjuoXEwYCYWosatAaFqwSiZdMmfbX_ORoTXu7cvJoawUihJPCjHXCqr0&_nc_ht=scontent.fbdo6-2.fna&oh=169b2c36df1a49f6a394208f21d09f7b&oe=5E94FF4E',
      title : 'Wahyu',
      author : 'Saul Andre Lumban Gaol',
    })
        self.assertJSONEqual(res, expected_output)

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