Skip to content

Commit

Permalink
Apply default value for release component type.
Browse files Browse the repository at this point in the history
* Set default value as rpm for type
* Add the validation for this field not allowed set to null

JIRA: PDC-888
  • Loading branch information
erichuanggit committed Aug 18, 2015
1 parent 1020ef5 commit 705f735
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
40 changes: 40 additions & 0 deletions pdc/apps/component/migrations/0006_auto_20150818_0614.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2015 Red Hat
# Licensed under The MIT License (MIT)
# http://opensource.org/licenses/MIT
#
from __future__ import unicode_literals

from django.db import models, migrations
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('component', '0005_auto_20150727_0929'),
]

operations = [
migrations.AlterField(
model_name='releasecomponent',
name='type',
field=models.ForeignKey(related_name='release_components', on_delete=django.db.models.deletion.SET_DEFAULT, default=1, to='component.ReleaseComponentType'),
),
migrations.AlterField(
model_name='releasecomponentgroup',
name='group_type',
field=models.ForeignKey(related_name='release_component_groups', on_delete=django.db.models.deletion.PROTECT, to='component.GroupType'),
),
migrations.AlterField(
model_name='releasecomponentrelationship',
name='from_component',
field=models.ForeignKey(related_name='from_release_components', to='component.ReleaseComponent'),
),
migrations.AlterField(
model_name='releasecomponentrelationship',
name='to_component',
field=models.ForeignKey(related_name='to_release_components', to='component.ReleaseComponent'),
),
]
4 changes: 2 additions & 2 deletions pdc/apps/component/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ class ReleaseComponent(models.Model):
release = models.ForeignKey(Release)
global_component = models.ForeignKey(GlobalComponent)
bugzilla_component = models.ForeignKey(BugzillaComponent, blank=True, null=True, on_delete=models.SET_NULL)
type = models.ForeignKey(ReleaseComponentType, blank=True, null=True,
on_delete=models.SET_NULL, related_name='release_components',)
type = models.ForeignKey(ReleaseComponentType, default=1, on_delete=models.SET_DEFAULT,
related_name='release_components')
name = models.CharField(max_length=100)
dist_git_branch = models.CharField(max_length=100, blank=True, null=True)
contacts = models.ManyToManyField(RoleContact, blank=True)
Expand Down
2 changes: 2 additions & 0 deletions pdc/apps/component/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ def validate_type(self, value):
if not isinstance(value, ReleaseComponentType):
if value is not None and value.strip() != "":
value = get_object_or_404(ReleaseComponentType, name=value.strip())
else:
raise serializers.ValidationError("This field can't be set to null.")
return value

class Meta:
Expand Down
19 changes: 13 additions & 6 deletions pdc/apps/component/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,7 +837,7 @@ def test_create_release_component(self):
del response.data['srpm']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data.update({'contacts': [], 'dist_git_branch': "release_branch", 'id': 3,
'bugzilla_component': None, 'active': True, 'type': None})
'bugzilla_component': None, 'active': True, 'type': 'rpm'})
self.assertEqual(sorted(response.data), sorted(data))
self.assertNumChanges([1])

Expand Down Expand Up @@ -914,6 +914,14 @@ def test_create_release_component_with_bugzillacomponent(self):
self.assertEqual(response1.status_code, status.HTTP_201_CREATED)
self.assertNumChanges([1, 1])

def test_create_release_component_without_type(self):
url = reverse('releasecomponent-list')
data = {'release': 'release-1.0', 'global_component': 'python', 'name': 'python26', 'brew_package': 'python-pdc'}
response = self.client.post(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(response.data.get('type'), 'rpm')
self.assertNumChanges([1])

def test_create_release_component_with_wrong_bugzillacomponent(self):
url = reverse('releasecomponent-list')
data = {'release': 'release-1.0', 'global_component': 'python', 'name': 'python26', 'brew_package': 'python-pdc', 'bugzilla_component': 'python26/python/python_pdc'}
Expand Down Expand Up @@ -1044,7 +1052,7 @@ def test_create_active_release_component(self):
del response.data['dist_git_web_url']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data.update({'contacts': [], 'dist_git_branch': "release_branch", 'id': 3,
'bugzilla_component': None, 'srpm': None, 'active': True, 'type': None})
'bugzilla_component': None, 'srpm': None, 'active': True, 'type': 'rpm'})
self.assertEqual(sorted(response.data), sorted(data))
self.assertNumChanges([1])

Expand All @@ -1054,7 +1062,7 @@ def test_create_active_release_component(self):
del response.data['dist_git_web_url']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data.update({'contacts': [], 'dist_git_branch': "release_branch", 'id': 3,
'bugzilla_component': None, 'srpm': None, 'active': True, 'type': None})
'bugzilla_component': None, 'srpm': None, 'active': True, 'type': 'rpm'})
self.assertEqual(sorted(response.data), sorted(data))
self.assertNumChanges([1, 1])

Expand All @@ -1065,7 +1073,7 @@ def test_create_inactive_release_component(self):
del response.data['dist_git_web_url']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
data.update({'contacts': [], 'dist_git_branch': "release_branch", 'id': 3,
'bugzilla_component': None, 'srpm': None, 'active': False, 'type': None})
'bugzilla_component': None, 'srpm': None, 'active': False, 'type': 'rpm'})
self.assertEqual(sorted(response.data), sorted(data))
self.assertNumChanges([1])

Expand Down Expand Up @@ -1257,8 +1265,7 @@ def test_update_release_component_type_to_null(self):
url = reverse('releasecomponent-detail', kwargs={'pk': 1})
data = {'type': None}
response = self.client.patch(url, data, format='json')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertIsNone(response.data['type'])
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

def test_update_with_patch_wrong_format_srpm(self):
url = reverse('releasecomponent-detail', kwargs={'pk': 1})
Expand Down

0 comments on commit 705f735

Please sign in to comment.