Fakultas Ilmu Komputer UI

Commit ac8c4c09 authored by Raden Fikri Ihza Dwi Nanda's avatar Raden Fikri Ihza Dwi Nanda
Browse files

Merge branch 'chapter-10' into 'master'

Chapter 10 - Automating Deployment

See merge request !14
parents 0ccd6a13 720a3ad1
Pipeline #27037 passed with stages
in 11 minutes and 35 seconds
......@@ -12,7 +12,7 @@ Test:
- mkdir static
- python3 manage.py collectstatic --no-input
script:
- python3 manage.py test lists
# - python3 manage.py test lists
- python3 manage.py test accounts
tags:
- test
......
......@@ -159,4 +159,58 @@ memikirkann behavior dari program ketika melakukan mock testing.
## Exercise 9 - Test Fixtures and a Decorator
Functional test di exercise ini lebih baik dari sebelumnya karena sekarang functional test menggunakan cookies yang menyimpan session user yang sudah melakukan login, sehingga jika ingin melakukan beberapa tes tidak perlu melakukan login dengan email dan password berulang kali. Dengan mengirimkan session ini, server akan mencocokkan sessionId dengan user ID yang ada sehingga user tersebut langsung terautentikasi.
Functional test ini akan memberikan hasil yang lebih baik dari sisi running time karena memangkas waktu yang diperlukan untuk keperluan login, sehingga waktu dijalankannya pun lebih cepat.
\ No newline at end of file
Functional test ini akan memberikan hasil yang lebih baik dari sisi running time karena memangkas waktu yang diperlukan untuk keperluan login, sehingga waktu dijalankannya pun lebih cepat.
## Exercise 10 - Automating Deployment
Awalnya, models diubah dari lists sehingga terdapat IntegrityError dan berarti tidak ada duplikat. Lalu test di-refactor dengan data yang berbeda sehingga data migration berhasil dijalankan.
*Simulasi data migration*
Simulasi ini terbantu oleh data migrations yang sudah ada di Django. Saya menggunakan 100 data untuk mencobanya. Isinya ada 10 list
yang berisi 10 item tiap listnya. Berikut script untuk membuat populasi data:
```
def create_data(apps, schema_editor):
List = apps.get_model("lists", "List")
Item = apps.get_model("lists", "Item")
for i in range(10):
_list = List.objects.create()
for z in range(10):
Item.objects.create(text='itemey 2' + str(z), list=_list)
```
Kemudian berikut ini adalah file migrasi yang sudah dimodifikasi yang fungsinya untuk menghapus data yang diduplikasi setiap
dilakukan migrasi baru:
```
from django.db import models, migrations
def find_dupes(apps, schema_editor):
List = apps.get_model("lists", "List")
for list_ in List.objects.all():
items = list_.item_set.all()
texts = set()
for ix, item in enumerate(items):
if item.text in texts:
item.text = '{} ({})'.format(item.text, ix)
item.save()
texts.add(item.text)
class Migration(migrations.Migration):
dependencies = [
('lists', '0001_initial'),
]
operations = [
]
```
Di model Item terdapat slug yang ditambahkan, selanjutnya model akan menampilkan slugify dari setiap item
Selanjutnya data migration dilakukan di production.
Secara umum data migration dapat dilakukan jika traffic sedang tidak terlalu ramai (request sedikit). Kalau di tempat dulu saya
melakukan magang, hal itu dilakukan saat malam hari agar tidak mengganggu produktifitas para client saat bekerja jika sewaktu-waktu
terdapat kesalahan dalam migrationnya.
\ No newline at end of file
# Generated by Django 2.2.4 on 2019-11-13 14:02
# Generated by Django 2.2.4 on 2019-12-09 12:29
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
......@@ -17,4 +18,12 @@ class Migration(migrations.Migration):
('email', models.EmailField(max_length=254, primary_key=True, serialize=False)),
],
),
migrations.CreateModel(
name='Token',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('uid', models.CharField(default=uuid.uuid4, max_length=40)),
],
),
]
# Generated by Django 2.2.4 on 2019-11-28 07:07
from django.db import migrations, models
import uuid
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Token',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('uid', models.CharField(default=uuid.uuid4, max_length=40)),
],
),
]
# Generated by Django 2.2.4 on 2019-09-23 15:54
# Generated by Django 2.2.4 on 2019-12-09 12:29
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
......@@ -11,10 +12,23 @@ class Migration(migrations.Migration):
]
operations = [
migrations.CreateModel(
name='List',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
],
),
migrations.CreateModel(
name='Item',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('text', models.TextField(default='')),
('slug', models.SlugField(blank=True)),
('list', models.ForeignKey(default=None, on_delete=django.db.models.deletion.CASCADE, to='lists.List')),
],
options={
'ordering': ('id',),
'unique_together': {('list', 'text')},
},
),
]
from django.db import migrations
def find_dupes(apps, schema_editor):
List = apps.get_model("lists", "List")
for list_ in List.objects.all():
items = list_.item_set.all()
texts = set()
for ix, item in enumerate(items):
if item.text in texts:
item.text = '{} ({})'.format(item.text, ix)
item.save()
texts.add(item.text)
class Migration(migrations.Migration):
dependencies = [
('lists', '0001_initial'),
]
operations = [
migrations.RunPython(find_dupes)
]
\ No newline at end of file
# Generated by Django 2.2.4 on 2019-10-09 10:26
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')),
],
),
]
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('lists', '0002_remove_duplicates'),
]
operations = [
migrations.AlterModelOptions(
name='item',
options={'ordering': ('id',)},
),
migrations.AlterUniqueTogether(
name='item',
unique_together={('list', 'text')},
),
]
\ No newline at end of file
# Generated by Django 2.2.4 on 2019-10-09 10:34
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'),
),
]
# Generated by Django 2.2.4 on 2019-09-23 16:01
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('lists', '0001_initial'),
('lists', '0003_unique_together'),
]
operations = [
migrations.AddField(
model_name='item',
name='text',
field=models.TextField(default=''),
name='slug',
field=models.SlugField(blank=True),
),
]
]
\ No newline at end of file
from django.db import migrations
from django.utils.text import slugify
def slugify(apps, schema_editor):
List = apps.get_model("lists", "List")
for list_ in List.objects.all():
items = list_.item_set.all()
for ix, item in enumerate(items):
item.slug = slugify(item.text)
item.save()
class Migration(migrations.Migration):
dependencies = [
('lists', '0004_item_slug'),
]
operations = [
migrations.RunPython(slugify)
]
\ No newline at end of file
from django.db import models
# Create your models here.
class List(models.Model):
pass
class Item(models.Model):
text = models.TextField(default='')
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
\ No newline at end of file
list = models.ForeignKey(List, default=None, on_delete=models.CASCADE)
slug = models.SlugField(blank=True)
class Meta:
ordering = ('id',)
unique_together = ('list', 'text')
\ No newline at end of file
......@@ -26,7 +26,8 @@ class HomePageTest(TestCase):
def test_comment_5_or_more_items(self):
list_ = List.objects.create()
for i in range(5):
Item.objects.create(text='antar adik ke sekolah', list=list_)
text = 'antar adik ke sekolah' + str(i)
Item.objects.create(text=text, list=list_)
response = self.client.get('/lists/' + str(list_.id) + '/')
......
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