Fakultas Ilmu Komputer UI

Commit 65d3ac86 authored by Izzatul Muttaqin's avatar Izzatul Muttaqin
Browse files

rewrite tutorial-2

parent 7eacf913
stages:
- test
- deploy
- functional_test
# - functional_test
UnitTest:
image: python:3.6
......@@ -36,22 +36,22 @@ Deployment:
only:
- master
FunctionalTest:
image: python:3.6
stage: functional_test
before_script:
- wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
- echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
- pip3 install -r requirements.txt
- pip3 install requests
- pip3 install django-environ
- apt-get update -qq && apt-get install -y -qq unzip
- apt-get install -y google-chrome-stable
- apt-get install -y xvfb
- wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
- unzip chromedriver_linux64.zip
when: on_success
script:
- python3 functional_tests.py
only:
- master
# FunctionalTest:
# image: python:3.6
# stage: functional_test
# before_script:
# - wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add -
# - echo "deb http://dl.google.com/linux/chrome/deb/ stable main" > /etc/apt/sources.list.d/google.list
# - pip3 install -r requirements.txt
# - pip3 install requests
# - pip3 install django-environ
# - apt-get update -qq && apt-get install -y -qq unzip
# - apt-get install -y google-chrome-stable
# - apt-get install -y xvfb
# - wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
# - unzip chromedriver_linux64.zip
# when: on_success
# script:
# - python3 functional_tests.py
# only:
# - master
from selenium import webdriver
from django.test import LiveServerTestCase
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.chrome.options import Options
import time
import unittest
import environ
root = environ.Path(__file__)
env = environ.Env(DEBUG=(bool, False), )
environ.Env.read_env('.env')
HEROKU_APP_HOST = env("HEROKU_APP_HOST")
print("HEROKU_APP_HOST is", HEROKU_APP_HOST)
MAX_WAIT = 5
class NewVisitorTest(unittest.TestCase):
......@@ -30,7 +42,7 @@ class NewVisitorTest(unittest.TestCase):
self.assertIn(row_text, [row.text for row in rows])
def test_can_start_a_list_and_retrieve_it_later(self):
self.browser.get('https://pmpl-project.herokuapp.com/lists')
self.browser.get(HEROKU_APP_HOST)
self.assertIn('To-Do', self.browser.title)
header_text = self.browser.find_element_by_tag_name('h1').text
......
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class PracticeConfig(AppConfig):
name = 'practice'
def counter_comment(number):
if number == 0:
return 'yey, waktunya libur'
if (number < 5 and number > 0):
return 'sibuk tapi santai'
if number >= 5:
return 'oh tidak'
return 'input tidak valid'
from django.db import models
class Item(models.Model):
text = models.TextField(default='')
<html>
<title>To-Do lists {{ name }}</title>
<body>
<h1>Hello, this is {{ name }}.</h1>
<h1>Your To-Do list</h1>
<form method="POST">
<input name="item_text" id="id_new_item" placeholder="Enter a to-do item" />
{% csrf_token %}
</form>
<h1>{{ comment }}</h1>
<table id="id_list_table">
{% for item in items %}
<tr><td>{{ forloop.counter }}: {{ item.text }}</td></tr>
{% endfor %}
</table>
</body>
</html>
from django.urls import reverse
from django.test import TestCase, Client
from django.http import HttpRequest
from django.urls import resolve
from .models import Item
from .views import home_page
from .helper import counter_comment
class PersonalPage(TestCase):
def test_url_is_exist(self):
response = Client().get('/')
self.assertEqual(response.status_code,200)
def test_using_home_page_func(self):
found = resolve('/')
self.assertEqual(found.func, home_page)
def test_landing_page_is_completed(self):
request = HttpRequest()
response = home_page(request)
html_response = response.content.decode('utf8')
self.assertIn('Hello, this is Izzatul Muttaqin.', html_response)
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
response = Client().get('/')
self.assertEqual(response.status_code,200)
def test_home_page_returns_correct_html(self):
request = HttpRequest() #1
response = home_page(request) #2
self.assertTrue(response.content.startswith(b'<html>')) #3
self.assertIn(b'To-Do lists', response.content)
self.assertIn(b'yey, waktunya libur', response.content)
self.assertTrue(response.content.endswith(b'</html>')) #5
def test_home_page_only_saves_items_when_necessary(self):
request = HttpRequest()
home_page(request)
self.assertEqual(Item.objects.count(), 0)
def test_home_page_can_save_a_POST_request(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(Item.objects.count(), 1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
request = HttpRequest()
response = home_page(request)
self.assertIn(b'sibuk tapi santai', response.content)
def test_home_page_displays_all_list_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
request = HttpRequest()
response = home_page(request)
self.assertIn('itemey 1', response.content.decode())
self.assertIn('itemey 2', response.content.decode())
def test_home_page_redirects_after_POST(self):
request = HttpRequest()
request.method = 'POST'
request.POST['item_text'] = 'A new list item'
response = home_page(request)
self.assertEqual(response.status_code, 302)
self.assertEqual(response['location'], '/')
class OtherTest(TestCase):
def test_saving_and_retrieving_items(self):
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.save()
saved_items = Item.objects.all()
self.assertEqual(saved_items.count(), 2)
first_saved_item = saved_items[0]
second_saved_item = saved_items[1]
self.assertEqual(first_saved_item.text, 'The first (ever) list item')
self.assertEqual(second_saved_item.text, 'Item the second')
def test_helper_counterTest(self):
counter_zero = counter_comment(0)
counter_one = counter_comment(1)
counter_five = counter_comment(5)
counter_not_valid = counter_comment(-1)
self.assertEqual(counter_zero, 'yey, waktunya libur')
self.assertEqual(counter_one, 'sibuk tapi santai')
self.assertEqual(counter_five, 'oh tidak')
self.assertEqual(counter_not_valid, 'input tidak valid')
\ No newline at end of file
from django.conf.urls import url
from .views import home_page
#url for app
urlpatterns = [
url(r'^$', home_page, name='home_page')
]
from django.shortcuts import redirect, render
from .models import Item
from .helper import counter_comment
def home_page(request):
if request.method == 'POST':
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/')
items = Item.objects.all()
comment = counter_comment(Item.objects.count())
return render(request, 'homepage/home.html', {'items': items, 'comment' : comment, 'name': 'Izzatul Muttaqin'})
......@@ -25,7 +25,7 @@ SECRET_KEY = '@x!!h0m5z-2m0ioti&gc&&3021fol3%rs9a-)=x%fdw3v_r79('
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
ALLOWED_HOSTS = ['*']
# Application definition
......@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'practice'
]
MIDDLEWARE = [
......
......@@ -15,7 +15,11 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
import practice.urls as practice
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^', include((practice, 'practice'), namespace='practice')),
]
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