Fakultas Ilmu Komputer UI

Commit a4dd9630 authored by Muhammad Ilham Peruzzi's avatar Muhammad Ilham Peruzzi
Browse files

adjust model so that items are associated with different lists

parent 41e8ea49
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-29 15:35
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lists', '0002_item_text'),
]
operations = [
migrations.CreateModel(
name='List',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2019-09-29 15:43
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('lists', '0003_list'),
]
operations = [
migrations.AddField(
model_name='item',
name='list',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='lists.List'),
),
]
from django.db import models
# Create your models here.
class List(models.Model):
pass
class Item(models.Model):
text = models.TextField(default='')
\ No newline at end of file
text = models.TextField(default='')
list = models.ForeignKey(List, default=None)
......@@ -2,24 +2,37 @@ from django.urls import resolve
from django.test import TestCase
from django.http import HttpRequest
from django.template.loader import render_to_string
from lists.models import Item
from lists.models import Item, List
from lists.views import home_page, about_me, view_list
class ItemModelTest(TestCase):
class ListAndItemModelsTest(TestCase):
def test_saving_and_retrieving_items(self):
list_ = List()
list_.save()
first_item = Item()
first_item.text = 'The first (ever) list item'
first_item.list = list_
first_item.save()
second_item = Item()
second_item.text = 'Item the second'
second_item.list = list_
second_item.save()
saved_list = List.objects.first()
self.assertEqual(saved_list, list_)
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(first_saved_item.list, list_)
self.assertEqual(second_saved_item.text, 'Item the second')
self.assertEqual(second_saved_item.list, list_)
class HomePageTest(TestCase):
def test_root_url_resolves_to_home_page_view(self):
......@@ -60,8 +73,9 @@ class HomePageTest(TestCase):
self.assertIn('yey, waktunya berlibur', response.content.decode())
def test_automatic_comment_when_to_do_have_less_five_items(self):
Item.objects.create(text='Activity 1')
Item.objects.create(text='Activity 2')
list_ = List.objects.create()
Item.objects.create(text='Activity 1', list=list_)
Item.objects.create(text='Activity 2', list=list_)
self.assertEqual(Item.objects.count(), 2)
request = HttpRequest()
response = view_list(request)
......@@ -69,12 +83,13 @@ class HomePageTest(TestCase):
self.assertIn('sibuk tapi santai', response.content.decode())
def test_automatic_comment_when_to_do_have_more_than_or_equal_five_items(self):
Item.objects.create(text='Activity 1')
Item.objects.create(text='Activity 2')
Item.objects.create(text='Activity 3')
Item.objects.create(text='Activity 4')
Item.objects.create(text='Activity 5')
Item.objects.create(text='Activity 6')
list_ = List.objects.create()
Item.objects.create(text='Activity 1', list=list_)
Item.objects.create(text='Activity 2', list=list_)
Item.objects.create(text='Activity 3', list=list_)
Item.objects.create(text='Activity 4', list=list_)
Item.objects.create(text='Activity 5', list=list_)
Item.objects.create(text='Activity 6', list=list_)
self.assertEqual(Item.objects.count(), 6)
request = HttpRequest()
response = view_list(request)
......@@ -83,16 +98,14 @@ class HomePageTest(TestCase):
class ListViewTest(TestCase):
def test_displays_all_items(self):
list_ = List.objects.create()
Item.objects.create(text='itemey 1', list=list_)
Item.objects.create(text='itemey 2', list=list_)
def test_uses_list_template(self):
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertTemplateUsed(response, 'list.html')
def test_displays_all_items(self):
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 2')
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertContains(response, 'itemey 1')
self.assertContains(response, 'itemey 2')
def test_saving_a_POST_request(self):
self.client.post('/lists/new',data={'item_text': 'A new list item'})
......
from django.shortcuts import render, redirect
from django.http import HttpResponse
from lists.models import Item
from lists.models import Item, List
# Create your views here.
......@@ -21,5 +21,6 @@ def view_list(request):
return render(request, 'list.html', {'items': items})
def new_list(request):
Item.objects.create(text=request.POST['item_text'])
list_ = List.objects.create()
Item.objects.create(text=request.POST['item_text'], list=list_)
return redirect('/lists/the-only-list-in-the-world/')
\ No newline at end of file
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