Repository : http://git.fedorahosted.org/cgit/copr.git
On branch : bkabrda-workspace
commit 2d295d007edf72fe4aea9b6fe826dd196634b2e6 Author: Bohuslav Kabrda bkabrda@redhat.com Date: Wed Nov 28 16:35:06 2012 +0100
Rewrite update_builds to be more legible, some more tests for it
.../coprs/views/backend_ns/backend_general.py | 14 ++-- .../test_views/test_backend_ns/test_general.py | 77 +++++++++++++++++++- 2 files changed, 83 insertions(+), 8 deletions(-)
diff --git a/coprs_frontend/coprs/views/backend_ns/backend_general.py b/coprs_frontend/coprs/views/backend_ns/backend_general.py index 1755e79..4c08e88 100644 --- a/coprs_frontend/coprs/views/backend_ns/backend_general.py +++ b/coprs_frontend/coprs/views/backend_ns/backend_general.py @@ -21,21 +21,21 @@ def waiting_builds(): @backend_ns.route('/update_builds/', methods = ['POST', 'PUT']) @misc.backend_authenticated def update_builds(): - build_ids = [] + to_update = {} for build in flask.request.json['builds']: # first get ids of sent builds - build_ids.append(build['id']) + to_update[build['id']] = build
- if not build_ids: + if not to_update: return json.dumps({'warning': 'No parsed builds'})
existing = {} # create a dict of existing builds {build.id: build, ...} - for build in builds_logic.BuildsLogic.get_by_ids(None, build_ids).all(): + for build in builds_logic.BuildsLogic.get_by_ids(None, to_update.keys()).all(): existing[build.id] = build
- non_existing_ids = list(set(build_ids) - set(existing.keys())) + non_existing_ids = list(set(to_update.keys()) - set(existing.keys()))
- for build in flask.request.json['builds']: # actually update existing builds - builds_logic.BuildsLogic.update_state_from_dict(None, existing[build['id']], build) + for i, build in existing.items(): # actually update existing builds + builds_logic.BuildsLogic.update_state_from_dict(None, build, to_update[i])
db.session.commit()
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 5f3eaa3..d7df4c2 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 @@ -26,7 +26,7 @@ class TestUpdateBuilds(CoprsTestCase): { "id": 1, "copr_id": 2, - "results": "http://server/results/$ownername/$coprname/", + "results": "http://server/results/foo/bar/", "started_on": 1234 } ] @@ -38,3 +38,78 @@ class TestUpdateBuilds(CoprsTestCase): 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): + data = """ +{ + "builds":[ + { + "id": 1, + "copr_id": 2, + "status": 1, + "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): + data = """ +{ + "builds":[ + { + "id": 1, + "copr_id": 2, + "results": "http://server/results/foo/bar/", + "started_on": 1234 + }, + { + "id": 2, + "copr_id": 1, + "status": 0, + "ended_on": 123456 + }, + { + "id": 123321, + "copr_id": 1, + "status": 0, + "ended_on": 123456 + }, + { + "id": 1234321, + "copr_id": 2, + "results": "http://server/results/foo/bar/", + "started_on": 1234 + } + ] +} + """ + r = self.tc.post('/backend/update_builds/', + content_type='application/json', + headers = self.auth_header, + data = data) + + 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() + 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() + assert ended.status == 0 + assert ended.ended_on == 123456
copr-devel@lists.fedorahosted.org