Fakultas Ilmu Komputer UI

Commit e35df9a3 authored by Gregorius Aprisunnea's avatar Gregorius Aprisunnea
Browse files

pull from master

parents b02efd77 4f46489b
Pipeline #59945 passed with stages
in 18 minutes and 45 seconds
# Generated by Django 3.1 on 2020-10-29 10:59
# Generated by Django 3.1 on 2020-10-29 11:22
import app.models
from django.conf import settings
......
......@@ -134,6 +134,10 @@ class DetailMateriService:
user_name = request.user.name
return user_name
@staticmethod
def init_materi_download_count(context, materi):
context["materi_download_count"] = materi.unduh.all().count()
class CitationService:
@staticmethod
......
......@@ -164,6 +164,14 @@ div.review {
<p class="info-content">{{materi_data.content.size|filesizeformat}}</p>
</dd>
</div>
<div class="info" id="1">
<dl class="col col-4">
<dt class="info-name">Jumlah Download</dt>
</dl>
<dd>
<p class="info-content">{{materi_download_count}}</p>
</dd>
</div>
</div>
<div class="buttons d-flex flex-row bd-highlight mb-1">
<a href="{% url 'view-materi' materi_data.id %}"
......
import json, tempfile, os, mock, base64
import pandas as pd
from io import StringIO
import re
import time
from django.test import override_settings
......@@ -42,6 +43,10 @@ from .models import (
ViewStatistics,
)
from .services import (
DetailMateriService,
)
from .views import (
DaftarKatalog,
DashboardKontributorView,
......@@ -278,6 +283,20 @@ class DaftarKatalogPerKontributorTest(TestCase):
class DetailMateriTest(TestCase):
def _get_materi_info_html(self, info_name, info_value):
info_html = '<div class="info" id="1"><dl class="col col-4">'
info_html += f'<dt class="info-name">{info_name}</dt>' + '</dl><dd>'
info_html += f'<p class="info-content">{info_value}</p>' + '</dd></div>'
return info_html
def check_materi_info_in_html(self, info_name, info_value, html_content):
expected_content = self._get_materi_info_html(info_name, info_value)
self.assertIn(expected_content, re.sub(">\s*<","><", html_content))
def check_materi_info_not_in_html(self, info_name, info_value, html_content):
expected_content = self._get_materi_info_html(info_name, info_value)
self.assertNotIn(expected_content, re.sub(">\s*<","><", html_content))
def setUp(self):
self.client = Client()
self.admin_credential = {
......@@ -303,11 +322,19 @@ class DetailMateriTest(TestCase):
"ExampleCover921.jpg", b"Test file")
self.content = SimpleUploadedFile("ExampleFile921.pdf", b"Test file")
Materi(title="Materi 1", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 1",
status="APPROVE", cover=self.cover, content=self.content).save()
self.materi1 = Materi.objects.first()
self.materi1 = Materi.objects.create(title="Materi 1", author="Agas",
uploader=self.contributor, publisher="Kelas SC",
descriptions="Deskripsi Materi 1", status="APPROVE",
cover=self.cover, content=self.content)
self.materi2 = Materi.objects.create(title="Materi 2", author="Agad",
uploader=self.contributor, publisher="Kelas SM",
descriptions="Deskripsi Materi 2", status="APPROVE",
cover=self.cover, content=self.content)
self.url = "/materi/" + str(self.materi1.id) + "/"
self.download_url1 = self.url + "unduh"
self.url2 = "/materi/" + str(self.materi2.id) + "/"
self.download_url2 = self.url2 + "unduh"
self.dcount_info_name = "Jumlah Download"
self.materi_with_published_date = Materi.objects.create(title="Materi 1", author="Agas", uploader=self.contributor,
publisher="Kelas SC", descriptions="Deskripsi Materi 1",
......@@ -699,6 +726,95 @@ class DetailMateriTest(TestCase):
self.assertEqual(last_url, '/materi/%d/' % self.materi1.id)
self.assertEqual(status_code, 302)
def test_download_count_is_in_init_context(self):
context = {}
DetailMateriService.init_materi_download_count(context, self.materi1)
self.assertIn('materi_download_count', context.keys())
def test_download_count_is_integer(self):
context = {}
DetailMateriService.init_materi_download_count(context, self.materi1)
self.assertEqual(type(context['materi_download_count']), int)
def test_download_count_when_no_download(self):
context = {}
DetailMateriService.init_materi_download_count(context, self.materi1)
self.assertEqual(context['materi_download_count'], 0)
def test_download_count_when_single_download(self):
self.client.get(self.download_url1)
context = {}
DetailMateriService.init_materi_download_count(context, self.materi1)
self.assertEqual(context['materi_download_count'], 1)
def test_download_count_when_multiple_download(self):
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url1)
context = {}
DetailMateriService.init_materi_download_count(context, self.materi1)
self.assertEqual(context['materi_download_count'], 3)
def test_different_material_has_different_download_count(self):
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url2)
self.client.get(self.download_url2)
context1 = {}
context2 = {}
DetailMateriService.init_materi_download_count(context1, self.materi1)
DetailMateriService.init_materi_download_count(context2, self.materi2)
self.assertNotEqual(context1['materi_download_count'], context2['materi_download_count'])
self.assertEqual(context1['materi_download_count'], 3)
self.assertEqual(context2['materi_download_count'], 2)
def test_download_count_displayed_on_template_when_no_download(self):
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.dcount_info_name, 0, html)
def test_download_count_displayed_on_template_when_single_download(self):
self.client.get(self.download_url1)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.dcount_info_name, 1, html)
def test_download_count_displayed_on_template_when_multiple_download(self):
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url1)
response = self.client.get(self.url)
html = response.content.decode("utf-8")
self.check_materi_info_in_html(self.dcount_info_name, 4, html)
def test_different_material_has_different_download_count_on_templates(self):
self.client.get(self.download_url1)
self.client.get(self.download_url1)
self.client.get(self.download_url2)
self.client.get(self.download_url2)
self.client.get(self.download_url2)
self.client.get(self.download_url2)
response = self.client.get(self.url)
response2 = self.client.get(self.url2)
html = response.content.decode("utf-8")
html2 = response2.content.decode("utf-8")
dcount_materi1 = self.materi1.unduh.all().count()
dcount_materi2 = self.materi2.unduh.all().count()
self.check_materi_info_in_html(self.dcount_info_name, dcount_materi1, html)
self.check_materi_info_not_in_html(self.dcount_info_name, dcount_materi2, html)
self.check_materi_info_in_html(self.dcount_info_name, dcount_materi2, html2)
self.check_materi_info_not_in_html(self.dcount_info_name, dcount_materi1, html2)
class PostsViewTest(TestCase):
......
......@@ -112,6 +112,7 @@ class DetailMateri(TemplateView):
DetailMateriService.init_context_data(context, materi, self.request.session)
published_date = DetailMateriService.set_published_date(materi)
DetailMateriService.init_citation_and_materi_rating(context, materi, published_date, self.request)
DetailMateriService.init_materi_download_count(context, materi)
if self.request.user.is_authenticated:
materi_rating = Rating.objects.filter(materi=materi, user=self.request.user).first()
......
Markdown is supported
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