From 8434b44d4b81fe8fd373797ce317f310046a2068 Mon Sep 17 00:00:00 2001 From: Bunga Amalia Kurniawati Date: Mon, 29 Mar 2021 13:53:29 +0700 Subject: [PATCH 1/3] [RED] membuat test_create_product_unit success dan fail --- api/tests.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/api/tests.py b/api/tests.py index fb6530a..172ef72 100644 --- a/api/tests.py +++ b/api/tests.py @@ -1239,6 +1239,33 @@ class ProductTest(rest_framework_test.APITestCase): ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + def test_create_product_unit_success(self): + data = seeds.PRODUCT_DATA + data['subcategory']= self.subcategory.id + data['unit'] = 'kg' + + response = request( + 'POST', + 'product-list', + data, + http_authorization=self.superuser_http_authorization + ) + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(models.Product.objects.count(), 1) + self.assertEqual(models.Product.objects.get(id=response.data['id']).name, data['name']) + + def test_create_product_no_unit_fail(self): + data = dict(seeds.PRODUCT_DATA, subcategory=self.subcategory.id) + data['unit'] = None + response = request( + 'POST', + 'product-list', + data, + http_authorization=self.superuser_http_authorization + ) + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(models.Product.objects.count(), 0) class ShoppingCartTest(rest_framework_test.APITestCase): def setUp(self): -- GitLab From dde1b84f2d971626dd13ceb882ebb5601f953236 Mon Sep 17 00:00:00 2001 From: Bunga Amalia Kurniawati Date: Mon, 29 Mar 2021 13:57:40 +0700 Subject: [PATCH 2/3] [GREEN] menambahkan kolom unit pada model produk --- api/migrations/0003_product_unit.py | 18 ++++++++++++++++++ api/models.py | 1 + api/seeds.py | 3 ++- api/serializers.py | 3 ++- 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 api/migrations/0003_product_unit.py diff --git a/api/migrations/0003_product_unit.py b/api/migrations/0003_product_unit.py new file mode 100644 index 0000000..2c92365 --- /dev/null +++ b/api/migrations/0003_product_unit.py @@ -0,0 +1,18 @@ +# Generated by Django 3.0.7 on 2021-03-27 05:42 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('api', '0002_auto_20201229_1028'), + ] + + operations = [ + migrations.AddField( + model_name='product', + name='unit', + field=models.CharField(default='buah', max_length=200, verbose_name='unit'), + ), + ] diff --git a/api/models.py b/api/models.py index 5ba7ff5..982bcf8 100644 --- a/api/models.py +++ b/api/models.py @@ -164,6 +164,7 @@ class Product(db_models.Model): validators=[validators.MinValueValidator(decimal.Decimal('0.01'))], verbose_name=_('total profit') ) + unit = db_models.CharField(default='buah', max_length=200, verbose_name=_('unit')) class Meta: ordering = ['subcategory', 'name', 'code', 'id'] verbose_name = _('product') diff --git a/api/seeds.py b/api/seeds.py index bcbb8fd..3e5f762 100644 --- a/api/seeds.py +++ b/api/seeds.py @@ -35,7 +35,8 @@ PRODUCT_DATA = { 'description': 'Dummy description.', 'price': '2000', 'stock': 10, - 'modal':'1000', + 'modal': '1000', + 'unit': 'kg', } TRANSACTION_DATA = { diff --git a/api/serializers.py b/api/serializers.py index 90263e5..863a6b2 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -264,7 +264,8 @@ class ProductSerializer(serializers.ModelSerializer): 'modal', 'profit', 'image', - 'total_profit' + 'total_profit', + 'unit' ] model = models.Product read_only_fields = ['id', 'code'] -- GitLab From 872b17aa3a29f5fb745acacdce6610c758070da9 Mon Sep 17 00:00:00 2001 From: Bunga Amalia Kurniawati Date: Mon, 29 Mar 2021 18:16:35 +0700 Subject: [PATCH 3/3] [REFACTOR] menghapus duplikasi method --- api/tests.py | 34 +++++----------------------------- 1 file changed, 5 insertions(+), 29 deletions(-) diff --git a/api/tests.py b/api/tests.py index 172ef72..a0e4b83 100644 --- a/api/tests.py +++ b/api/tests.py @@ -1169,6 +1169,7 @@ class ProductTest(rest_framework_test.APITestCase): def test_create_product_success(self): data = seeds.PRODUCT_DATA data['subcategory']= self.subcategory.id + data['unit'] = 'kg' response = request( 'POST', @@ -1184,6 +1185,7 @@ class ProductTest(rest_framework_test.APITestCase): def test_create_product_fail(self): data = dict(seeds.PRODUCT_DATA, subcategory=self.subcategory.id) data['name'] = None + data['unit'] = None response = request( 'POST', 'product-list', @@ -1200,7 +1202,8 @@ class ProductTest(rest_framework_test.APITestCase): data = { 'name': 'Dummy', 'price':'4000', - 'modal':'2000' + 'modal':'2000', + 'unit': 'gram' } response = request( 'PATCH', @@ -1229,6 +1232,7 @@ class ProductTest(rest_framework_test.APITestCase): )) data = { 'name': '', + 'unit': '' } response = request( 'PATCH', @@ -1239,34 +1243,6 @@ class ProductTest(rest_framework_test.APITestCase): ) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - def test_create_product_unit_success(self): - data = seeds.PRODUCT_DATA - data['subcategory']= self.subcategory.id - data['unit'] = 'kg' - - response = request( - 'POST', - 'product-list', - data, - http_authorization=self.superuser_http_authorization - ) - - self.assertEqual(response.status_code, status.HTTP_201_CREATED) - self.assertEqual(models.Product.objects.count(), 1) - self.assertEqual(models.Product.objects.get(id=response.data['id']).name, data['name']) - - def test_create_product_no_unit_fail(self): - data = dict(seeds.PRODUCT_DATA, subcategory=self.subcategory.id) - data['unit'] = None - response = request( - 'POST', - 'product-list', - data, - http_authorization=self.superuser_http_authorization - ) - self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) - self.assertEqual(models.Product.objects.count(), 0) - class ShoppingCartTest(rest_framework_test.APITestCase): def setUp(self): self.superuser = models.User.objects.create_superuser(**seeds.SUPERUSER_DATA) -- GitLab