Fakultas Ilmu Komputer UI

Commit dd6efef8 authored by Mika dabelza abi's avatar Mika dabelza abi
Browse files

[GREEN] added backend function for Katalog-Materi and test syntax error correction

parent bbab743f
# Generated by Django 3.0.3 on 2020-04-08 12:29
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Materi',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('cover', models.URLField()),
('title', models.CharField(max_length=50)),
('author', models.CharField(max_length=30)),
],
),
]
# Generated by Django 3.0.3 on 2020-04-08 14:15
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='materi',
name='cover',
field=models.URLField(max_length=1000),
),
]
from django.db import models
# Create your models here.
class Materi(models.Model):
cover = models.URLField(max_length=1000)
title = models.CharField(max_length=50)
author = models.CharField(max_length=30)
\ No newline at end of file
from django.test import TestCase, Client
from django.urls import resolve
from .views import DaftarKatalog
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):
......@@ -19,35 +23,31 @@ class DaftarKatalogTest(TestCase):
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.cover = 'https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg'
materi.author = 'input'
materi.save()
resp = Materi.objects.get(materi.id)
resp = Materi.objects.get(id = materi.id)
self.assertEqual(resp,materi)
def test_resolution(self):
resolver = resolve('/listMateri')
self.assertEqual(resolver, listMateri)
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://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.cover = 'https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg'
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)
queryset = client.get('/list-materi/')
expected_output = json.dumps({"pk": 2, "model": "app.materi", "fields": {"cover": "https://cache.umusic.com/_sites/billieeilish/v2/images/pic-red.jpg", "title": "Wahyu", "author": "Saul Andre Lumban Gaol"}})
self.assertJSONEqual(queryset.json()[1:-1], expected_output)
class TemplateLoaderTest(TestCase):
def test_template_loader_url_exist(self):
......
......@@ -6,4 +6,5 @@ urlpatterns = [
path("", views.DaftarKatalog.as_view(), name='daftar_katalog'),
# Matches any html file
re_path(r'^.*\.html', views.pages, name='pages'),
path('list-materi/', views.listMateri, name='listMateri'),
]
\ No newline at end of file
......@@ -2,6 +2,8 @@ from django.shortcuts import render, get_object_or_404, redirect
from django.template import loader
from django.http import HttpResponse, JsonResponse, HttpResponseRedirect
from django.views.generic import TemplateView
from django.core import serializers
from .models import Materi
class DaftarKatalog(TemplateView):
template_name = "app/katalog_materi.html"
......@@ -12,6 +14,10 @@ class DaftarKatalog(TemplateView):
def get(self, request, *args, **kwargs):
context = self.get_context_data(**kwargs)
return self.render_to_response(context=context)
def listMateri(self):
queries = Materi.objects.all()
return JsonResponse(serializers.serialize('json', queries), safe=False)
def pages(request):
context = {}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment