Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new modal build for filter by build params #28

Merged
merged 2 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cdws_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from testreport.models import TestPlan
from testreport.models import Launch
from testreport.models import Build
from testreport.models import TestResult
from testreport.models import LaunchItem
from testreport.models import Bug
Expand Down Expand Up @@ -82,16 +83,23 @@ def to_representation(self, value):
return output


class BuildSerializer(serializers.ModelSerializer):
class Meta:
model = Build
fields = ('version', 'hash', 'branch')


class LaunchSerializer(serializers.ModelSerializer):
counts = serializers.ReadOnlyField()
tasks = TasksResultField(source='get_tasks', read_only=True)
parameters = serializers.ReadOnlyField(source='get_parameters')
build = BuildSerializer(read_only=True)

class Meta:
model = Launch
fields = ('id', 'test_plan', 'created', 'counts', 'tasks',
'state', 'started_by', 'created', 'finished', 'parameters',
'duration')
'duration', 'build')


class TestResultSerializer(serializers.ModelSerializer):
Expand Down
64 changes: 64 additions & 0 deletions cdws_api/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from common.models import Project, Settings
from testreport.models import TestPlan
from testreport.models import Launch
from testreport.models import Build
from testreport.models import Bug
from testreport.models import PASSED, FAILED, INIT_SCRIPT, ASYNC_CALL, SKIPPED
from testreport.models import STOPPED
Expand Down Expand Up @@ -240,6 +241,10 @@ def test_execute_success(self):
launch_id = output['launch_id']
launch = self._get_launch(launch_id)
self.assertEqual(len(launch['tasks']), count + 1)
self.assertTrue(launch['build'])
self.assertFalse(launch['build']['version'])
self.assertFalse(launch['build']['hash'])
self.assertFalse(launch['build']['branch'])

def test_execute_failure(self):
test_plan = TestPlan.objects.get(name='DummyTestPlan')
Expand All @@ -249,6 +254,37 @@ def test_execute_failure(self):
{'options': {'started_by': 'http://2gis.local/'}})
self.assertIsNotNone(output.get('message'))

def test_execute_build_options(self):
test_plan = TestPlan.objects.get(name='DummyTestPlan')

self._create_launch_item({
'test_plan': test_plan.id,
'command': 'touch init_file',
'type': INIT_SCRIPT,
'timeout': 10,
})

count = random.choice([2, 3, 4, 5])
for x in range(0, count):
self._create_launch_item({
'test_plan': test_plan.id,
'command': 'touch file_'.format(x),
'type': ASYNC_CALL,
'timeout': 10,
})

output = self._tp_execute(
test_plan.id, {'options': {'started_by': 'http://2gis.local/',
'version': '123', 'hash': '123',
'branch': '123'}})
launch_id = output['launch_id']
launch = self._get_launch(launch_id)
self.assertEqual(len(launch['tasks']), count + 1)
self.assertTrue(launch['build'])
self.assertEqual(launch['build']['version'], '123')
self.assertEqual(launch['build']['hash'], '123')
self.assertEqual(launch['build']['branch'], '123')

def test_deploy_script_duplication(self):
test_plan = TestPlan.objects.get(name='DummyTestPlan')
self._create_launch_item({
Expand Down Expand Up @@ -455,6 +491,7 @@ def test_creation(self):
launch = self._create_launch(test_plan.id)
self.assertEqual(len(self.get_launches()['results']), 1)
self.assertEqual(launch['test_plan'], test_plan.id)
self.assertFalse(launch['build'])

def test_termination(self):
test_plan = TestPlan.objects.get(name='DummyTestPlan')
Expand Down Expand Up @@ -514,6 +551,33 @@ def test_update_duration(self):
'launches/{0}/'.format(launch['id']), {'duration': 360})
self.assertEqual(360, response['duration'])

def test_build_filter(self):
test_plan = TestPlan.objects.get(name='DummyTestPlan')
Launch(test_plan=test_plan).save()
launch2 = Launch(test_plan=test_plan)
launch2.save()
Build(launch=launch2, version=123, branch=123, hash=123).save()
self.assertEqual(len(self.get_launches()['results']), 2)

response = self._call_rest(
'get', 'launches/custom_list/?version=123')
self.assertEqual(len(response['results']), 1)
self.assertEqual(response['results'][0]['id'], launch2.id)

response = self._call_rest(
'get', 'launches/custom_list/?branch=123')
self.assertEqual(len(response['results']), 1)
self.assertEqual(response['results'][0]['id'], launch2.id)

response = self._call_rest(
'get', 'launches/custom_list/?hash=123')
self.assertEqual(len(response['results']), 1)
self.assertEqual(response['results'][0]['id'], launch2.id)

response = self._call_rest(
'get', 'launches/custom_list/?version=333')
self.assertEqual(len(response['results']), 0)


class TestResultApiTestCase(AbstractEntityApiTestCase):
def setUp(self):
Expand Down
16 changes: 16 additions & 0 deletions cdws_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

from testreport.models import TestPlan
from testreport.models import Launch
from testreport.models import Build
from testreport.models import TestResult
from testreport.models import LaunchItem
from testreport.models import Bug
Expand Down Expand Up @@ -181,6 +182,12 @@ def execute(self, request, pk=None):
state=INITIALIZED)
launch.save()

build = Build(launch=launch,
version=options.get('version'),
branch=options.get('branch'),
hash=options.get('hash'))
build.save()

# env create
env = {'WORKSPACE':
os.path.join(settings.CDWS_DEPLOY_DIR, workspace_path),
Expand Down Expand Up @@ -317,6 +324,15 @@ def custom_list(self, request, *args, **kwargs):
delta = datetime.datetime.today() - datetime.timedelta(
days=int(request.GET['days']))
self.queryset = self.queryset.filter(created__gt=delta)
if 'version' in request.GET:
self.queryset = self.queryset.filter(
build__version=request.GET['version'])
if 'hash' in request.GET:
self.queryset = self.queryset.filter(
build__hash=request.GET['hash'])
if 'branch' in request.GET:
self.queryset = self.queryset.filter(
build__branch=request.GET['branch'])
return self.list(request, *args, **kwargs)

@detail_route(methods=['get'],
Expand Down
27 changes: 27 additions & 0 deletions testreport/migrations/0041_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
('testreport', '0040_extuser_result_preview'),
]

operations = [
migrations.CreateModel(
name='Build',
fields=[
('id', models.AutoField(verbose_name='ID', primary_key=True, serialize=False, auto_created=True)),
('version', models.CharField(null=True, blank=True, max_length=16, default=None)),
('hash', models.CharField(null=True, blank=True, max_length=64, default=None)),
('branch', models.CharField(null=True, blank=True, max_length=128, default=None)),
('launch', models.OneToOneField(related_name='build', to='testreport.Launch')),
],
options={
},
bases=(models.Model,),
),
]
14 changes: 14 additions & 0 deletions testreport/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,20 @@ def __str__(self):
return '{0} -> Launch: {1}'.format(self.test_plan, self.pk)


class Build(models.Model):
launch = models.OneToOneField(Launch, related_name='build')
version = models.CharField(
max_length=16, default=None, null=True, blank=True)
hash = models.CharField(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

64 is good enough for hash

max_length=64, default=None, null=True, blank=True)
branch = models.CharField(
max_length=128, default=None, null=True, blank=True)

def __str__(self):
return '{0} -> LaunchBuild: {1}/{2}/{3}'.format(
self.launch, self.version, self.hash, self. branch)


class TestResult(models.Model):
launch = models.ForeignKey(Launch)
name = models.CharField(_('Name'), max_length=128, db_index=True)
Expand Down