Repository : http://git.fedorahosted.org/cgit/copr.git
On branch : master
commit 36a4c15ebd71fbf25bf288cb44db45475ed976ee Author: Bohuslav Kabrda bkabrda@redhat.com Date: Fri Feb 15 13:56:58 2013 +0100
Introduce build_finished signal
coprs_frontend/coprs/logic/builds_logic.py | 3 + coprs_frontend/coprs/signals.py | 5 + .../test_views/test_backend_ns/test_general.py | 98 +++++++++++--------- 3 files changed, 64 insertions(+), 42 deletions(-)
diff --git a/coprs_frontend/coprs/logic/builds_logic.py b/coprs_frontend/coprs/logic/builds_logic.py index b936a0c..2ee9a07 100644 --- a/coprs_frontend/coprs/logic/builds_logic.py +++ b/coprs_frontend/coprs/logic/builds_logic.py @@ -3,6 +3,7 @@ import time from coprs import db from coprs import exceptions from coprs import models +from coprs import signals
from coprs.logic import coprs_logic
@@ -88,6 +89,8 @@ class BuildsLogic(object): value = upd_dict.get(attr, None) if value != None: setattr(build, attr, value) + if attr == 'ended_on': + signals.build_finished.send(cls, build=build)
db.session.add(build)
diff --git a/coprs_frontend/coprs/signals.py b/coprs_frontend/coprs/signals.py new file mode 100644 index 0000000..cc9b996 --- /dev/null +++ b/coprs_frontend/coprs/signals.py @@ -0,0 +1,5 @@ +import blinker + +coprs_signals = blinker.Namespace() + +build_finished = coprs_signals.signal('build-finished') diff --git a/coprs_frontend/tests/test_views/test_backend_ns/test_general.py b/coprs_frontend/tests/test_views/test_backend_ns/test_general.py index f7328a1..d5433d7 100644 --- a/coprs_frontend/tests/test_views/test_backend_ns/test_general.py +++ b/coprs_frontend/tests/test_views/test_backend_ns/test_general.py @@ -2,6 +2,7 @@ import json
import flask
+from coprs.signals import build_finished from tests.coprs_test_case import CoprsTestCase
class TestWaitingBuilds(CoprsTestCase): @@ -13,14 +14,7 @@ class TestWaitingBuilds(CoprsTestCase): assert len(json.loads(r.data)['builds']) == 2
class TestUpdateBuilds(CoprsTestCase): - def test_updating_requires_password(self, f_users, f_coprs, f_builds, f_db): - r = self.tc.post('/backend/update_builds/', - content_type = 'application/json', - data = '') - assert 'You have to provide the correct password' in r.data - - def test_update_build_started(self, f_users, f_coprs, f_builds, f_db): - data = """ + data1=""" { "builds":[ { @@ -30,21 +24,9 @@ class TestUpdateBuilds(CoprsTestCase): "started_on": 1234 } ] -} - """ - r = self.tc.post('/backend/update_builds/', - content_type='application/json', - headers = self.auth_header, - data = data) - assert json.loads(r.data)["updated_builds_ids"] == [1] - assert json.loads(r.data)["non_existing_builds_ids"] == [] - - updated = self.models.Build.query.filter(self.models.Build.id == 1).first() - assert updated.results == 'http://server/results/foo/bar/' - assert updated.started_on == 1234 +}"""
- def test_update_build_ended(self, f_users, f_coprs, f_builds, f_db): - data = """ + data2 = """ { "builds":[ { @@ -54,21 +36,9 @@ class TestUpdateBuilds(CoprsTestCase): "ended_on": 12345 } ] -} - """ - r = self.tc.post('/backend/update_builds/', - content_type='application/json', - headers = self.auth_header, - data = data) - assert json.loads(r.data)["updated_builds_ids"] == [1] - assert json.loads(r.data)["non_existing_builds_ids"] == [] +}"""
- updated = self.models.Build.query.filter(self.models.Build.id == 1).first() - assert updated.status == 1 - assert updated.ended_on == 12345 - - def test_update_more_existent_and_non_existent_builds(self, f_users, f_coprs, f_builds, f_db): - data = """ + data3 = """ { "builds":[ { @@ -96,20 +66,64 @@ class TestUpdateBuilds(CoprsTestCase): "started_on": 1234 } ] -} - """ +}""" + def test_updating_requires_password(self, f_users, f_coprs, f_builds, f_db): + r = self.tc.post('/backend/update_builds/', + content_type = 'application/json', + data = '') + assert 'You have to provide the correct password' in r.data + + def test_update_build_started(self, f_users, f_coprs, f_builds, f_db): r = self.tc.post('/backend/update_builds/', content_type='application/json', - headers = self.auth_header, - data = data) + headers=self.auth_header, + data=self.data1) + assert json.loads(r.data)["updated_builds_ids"] == [1] + assert json.loads(r.data)["non_existing_builds_ids"] == [] + + updated = self.models.Build.query.filter(self.models.Build.id==1).first() + assert updated.results == 'http://server/results/foo/bar/' + assert updated.started_on == 1234 + + def test_update_build_ended(self, f_users, f_coprs, f_builds, f_db): + r = self.tc.post('/backend/update_builds/', + content_type='application/json', + headers=self.auth_header, + data=self.data2) + assert json.loads(r.data)["updated_builds_ids"] == [1] + assert json.loads(r.data)["non_existing_builds_ids"] == [] + + updated = self.models.Build.query.filter(self.models.Build.id==1).first() + assert updated.status == 1 + assert updated.ended_on == 12345 + + def test_update_more_existent_and_non_existent_builds(self, f_users, f_coprs, f_builds, f_db): + r = self.tc.post('/backend/update_builds/', + content_type='application/json', + headers=self.auth_header, + data=self.data3)
assert sorted(json.loads(r.data)["updated_builds_ids"]) == [1, 2] assert sorted(json.loads(r.data)["non_existing_builds_ids"]) == [123321, 1234321]
- started = self.models.Build.query.filter(self.models.Build.id == 1).first() + started = self.models.Build.query.filter(self.models.Build.id==1).first() assert started.results == 'http://server/results/foo/bar/' assert started.started_on == 1234
- ended = self.models.Build.query.filter(self.models.Build.id == 2).first() + ended = self.models.Build.query.filter(self.models.Build.id==2).first() assert ended.status == 0 assert ended.ended_on == 123456 + + def test_build_ended_emmits_signal(self, f_users, f_coprs, f_builds, f_db): + # TODO: this should probably be mocked... + signals_received = [] + def test_receiver(sender, **kwargs): + signals_received.append(kwargs['build']) + build_finished.connect(test_receiver) + r = self.tc.post('/backend/update_builds/', + content_type='application/json', + headers=self.auth_header, + data=self.data3) + assert len(signals_received) == 1 + self.db.session.add(self.b2) + assert signals_received[0].id == 2
copr-devel@lists.fedorahosted.org