PMPL Sprint 1 — Upgrading Django Version, Using the New JSONField, and Increasing Code Coverage in DIGIPUS Application

Introduction

About the Application

Background on the Task

  • Make sure to create tests for every implementation code that uses VerificationReport.report .
  • Upgrading Django Version to 3.1.
  • Replace django.contrib.postgres.fields.JSONField to django.db.models.JSONField for VerificationReport.report field.
  • Rerun all test cases to make sure that no regression occured.

Creating Test Cases for Implementation Code that Uses VerificationReport.report field

class GenerateDummyCommandTest(TestCase):

material_numbers = [5, 10, 25, 100]
invalid_material_numbers = [-100, -10, -1, 0, 1, 2, 3, 4]
stdout = StringIO()

def test_command_output_with_given_num_of_materi(self):
for num_of_materi in self.material_numbers:
call_command("generatedummy", num_of_materi, stdout=self.stdout)
self.assertIn(
f"Successfully created {num_of_materi} materi\n",
self.stdout.getvalue()
)

def test_command_should_generate_materi_objects(self):
for num_of_materi in self.material_numbers:
call_command("generatedummy", num_of_materi, stdout=self.stdout)
self.assertEqual(Materi.objects.count(), num_of_materi)
Materi.objects.all().delete()

def test_command_should_raise_exception_if_invalid_values_are_given(self):
with self.assertRaises(IndexError):
for num_of_materi in self.invalid_material_numbers:
call_command("generatedummy", num_of_materi)
  • The first test case, test_command_output_with_given_num_of_materi asks that when the generatedummy command is called with a number of material as its argument, it should return an output in format Successfully created {num_of_material} materi. Example: generatedummy 100 should produce Successfully created 100 materi to the console.
  • The second test case, test_command_should_generate_materi_objects asks that when the generatedummy command is called with a number of material as its argument, it should record the asked number of material to the database. Example: generatedummy 100 should record 100 Materi objects.
  • The third test case, test_command_should_raise_exception_if_invalid_values_are_given asks that when the generatedummy command is called with invalid number of material, that is, any value lower than 5 should raise an IndexError .

Upgrading Django Version to 3.1

...
asgiref==3.2.10
...
Django==3.1
...
pip install -r requirements.txt

Using the New JSONField

from django.db.models import JSONField
from django.db import models
from django.utils import timezone

from app.models import VERIFICATION_STATUS, Materi
from authentication.models import User

class VerificationReport(models.Model):
report = JSONField()
materi = models.ForeignKey(Materi, models.SET_NULL, null=True)
user = models.ForeignKey(User, models.SET_NULL, null=True)
timestamp = models.DateTimeField(default=timezone.now)
status = models.CharField(
max_length=30, choices=VERIFICATION_STATUS, default=VERIFICATION_STATUS[0][0])
python manage.py makemigrations
# Generated by Django 3.1 on 2020-09-29 14:25

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('authentication', '0005_auto_20200519_1021'),
]

operations = [
migrations.AlterField(
model_name='user',
name='first_name',
field=models.CharField(blank=True, max_length=150, verbose_name='first name'),
),
]

Merging to Master Branch and Code Coverage Change

Before merging to master branch
After merging to master branch

Lessons Learned

Conclusion

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Back to the future — part 3: how to convince “the business”

Medium Post on Agile

Apache Kafka — An Introduction

How to create an X.509 Public / Private Keypair — with no prompting

Why FlexBox?

ZooRena AMA Recap

Optimizing Conway’s Game of Life in JavaScript — Part IV

What is going on in there?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Farhan Azmi

Farhan Azmi

More from Medium

Fetch Requests with @ReactJS/Toolkit

HYSS (Highest Yield Savings Service )

How to Deploy Angular Universal on AWS Elastic Beanstalk

Best Practices for DeFi Project in Cronos