Fakultas Ilmu Komputer UI

Commit 4bfc7216 authored by Yumna Pratista's avatar Yumna Pratista
Browse files

deleted migrations , changed 3 files

parent 414efaaf
# Generated by Django 2.2.1 on 2019-10-10 03:32
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('app_homepage', '0002_item_text'),
]
operations = [
migrations.CreateModel(
name='List',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
]
# Generated by Django 2.2.1 on 2019-10-10 03:40
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('app_homepage', '0003_list'),
]
operations = [
migrations.AddField(
model_name='item',
name='list',
field=models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='app_homepage.List'),
),
]
from django.db import models
class List(models.Model):
pass
class Item(models.Model):
text = models.TextField(default = '')
list = models.ForeignKey(List, default = None, on_delete = models.CASCADE)
......@@ -2,7 +2,7 @@ from django.test import TestCase
from django.urls import resolve
from django.http import HttpRequest
from .views import home_page
from .models import Item
from .models import Item, List
from django.template.loader import render_to_string
# Create your tests here.
......@@ -13,7 +13,7 @@ class HomePageTest(TestCase):
self.assertTemplateUsed(response, 'app/home.html')
def test_can_save_a_POST_request(self):
response = self.client.post('/', data={'item_text':'A new list item'})
response = self.client.post('/lists/new', data={'item_text':'A new list item'})
self.assertEqual(Item.objects.count(),1)
new_item = Item.objects.first()
self.assertEqual(new_item.text, 'A new list item')
......@@ -43,24 +43,34 @@ class HomePageTest(TestCase):
# self.assertIn('oh tidak', response.content.decode())
class ItemModelTest(TestCase):
class ListAndItemModelTest(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 ListViewTest(TestCase):
......@@ -69,10 +79,11 @@ class ListViewTest(TestCase):
self.assertTemplateUsed(response, 'app/list.html')
def test_displays_all_items(self):
list_ = List.objects.create()
Item.objects.create(text='itemey 1')
Item.objects.create(text='itemey 1', list = list_)
Item.objects.create(text='itemey 2')
Item.objects.create(text='itemey 2', list = list_)
response = self.client.get('/lists/the-only-list-in-the-world/')
self.assertContains(response, 'itemey 1')
......
from django.shortcuts import render, redirect
from django.http import HttpResponse
from .models import Item
from .models import Item, List
# Create your views here.
def home_page(request):
......@@ -19,5 +19,6 @@ def view_list(request):
return render(request, 'app/list.html', {'items': items})
def new_list(request):
Item.objects.create(text=request.POST['item_text'])
return redirect('/lists/the-only-list-in-the-world/')
\ No newline at end of file
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
No preview for this file type
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