From 15d91b6dda0c7bdaa507cd0d574cbccefe4c3cb1 Mon Sep 17 00:00:00 2001 From: mohadese-yousefi Date: Sat, 13 Jul 2019 15:22:18 +0430 Subject: [PATCH] Revise append batch, closes #943 --- dolphin/controllers/batch.py | 41 +++++++++++++++++------ dolphin/controllers/projects.py | 2 +- dolphin/exceptions.py | 3 ++ dolphin/tests/test_batch_append.py | 50 +++++++++++++++++++++++----- dolphin/tests/test_project_create.py | 5 +++ 5 files changed, 82 insertions(+), 19 deletions(-) diff --git a/dolphin/controllers/batch.py b/dolphin/controllers/batch.py index 98adf49c..cbcd4c24 100644 --- a/dolphin/controllers/batch.py +++ b/dolphin/controllers/batch.py @@ -2,9 +2,10 @@ from restfulpy.authorization import authorize from restfulpy.controllers import ModelRestController from restfulpy.orm import DBSession, commit +from sqlalchemy import and_ from ..exceptions import StatusIssueIdIsNull, StatusInvalidIssueIdType, \ - StatusIssueIdNotInForm, StatusIssueNotFound + StatusIssueIdNotInForm, StatusIssueNotFound, StatusInvalidBatch from ..models import Batch, Issue @@ -19,15 +20,10 @@ class BatchController(ModelRestController): required=StatusIssueIdNotInForm, type_=(int, StatusInvalidIssueIdType), not_none=StatusIssueIdIsNull, - ) + ), )) @commit - def append(self, id_): - id_ = int_or_notfound(id_) - batch = DBSession.query(Batch).filter(Batch.id == id_).one_or_none() - if batch is None: - raise HTTPNotFound('Batch with id: {id_} was not found') - + def append(self, title): issue_id = context.form['issueIds'] issue = DBSession.query(Issue) \ .filter(Issue.id == issue_id) \ @@ -36,7 +32,32 @@ def append(self, id_): if issue is None: raise StatusIssueNotFound(issue_id) - batch.issues.append(issue) + # Title is unique in per a project and always is numerical. + title = int_or_notfound(title) + batch = DBSession.query(Batch) \ + .filter(Batch.title == format(title, '03')) \ + .filter(Batch.project_id == issue.project_id) \ + .one_or_none() - return batch + last_batch = DBSession.query(Batch) \ + .filter(Batch.project_id == issue.project_id) \ + .order_by(Batch.id.desc()) \ + .first() + + if batch is None: + for i in range(title - int(last_batch.title)): + batch_title = int(last_batch.title) + i + 1 + if batch_title < 100: + batch = Batch(title=format(batch_title, '03')) + batch.project_id = issue.project_id + DBSession.add(batch) + + else: + raise StatusInvalidBatch + + batch.issues.append(issue) + else: + batch.issues.append(issue) + + return batch diff --git a/dolphin/controllers/projects.py b/dolphin/controllers/projects.py index 9f92f20e..c5dad87e 100644 --- a/dolphin/controllers/projects.py +++ b/dolphin/controllers/projects.py @@ -223,7 +223,7 @@ def create(self): ) raise - batch = Batch(title='00') + batch = Batch(title='001') project.batches.append(batch) DBSession.add(project) diff --git a/dolphin/exceptions.py b/dolphin/exceptions.py index 9b297ec0..24376973 100644 --- a/dolphin/exceptions.py +++ b/dolphin/exceptions.py @@ -525,3 +525,6 @@ class StatusDateNotInForm(HTTPKnownStatus): class StatusIssueIdNotInForm(HTTPKnownStatus): status = '723 Issue Id Not In Form' +class StatusInvalidBatch(HTTPKnownStatus): + status = '936 Invalid Batch More Than 100' + diff --git a/dolphin/tests/test_batch_append.py b/dolphin/tests/test_batch_append.py index 04d3fe2a..afd25ed0 100644 --- a/dolphin/tests/test_batch_append.py +++ b/dolphin/tests/test_batch_append.py @@ -20,7 +20,7 @@ def mockup(cls): email='member1@example.com', access_token='access token', phone=123456789, - reference_id=2 + reference_id=2, ) workflow = Workflow(title='Default') @@ -43,13 +43,15 @@ def mockup(cls): manager=cls.member1, title='My first project', description='A decription for my project', - room_id=1001 + room_id=1001, ) session.add(cls.project1) session.commit() - cls.batch1 = Batch(title='01') + cls.batch1 = Batch(title='002') + cls.batch2 = Batch(title='003') cls.project1.batches.append(cls.batch1) + cls.project1.batches.append(cls.batch2) with Context(dict()): context.identity = cls.member1 @@ -59,18 +61,28 @@ def mockup(cls): description='This is description of first issue', kind='feature', days=1, - room_id=2 + room_id=2, ) + cls.issue2 = Issue( + title='second issue', + description='This is description of second issue', + kind='feature', + days=1, + room_id=2, + ) + cls.project1.issues.append(cls.issue1) + cls.project1.issues.append(cls.issue2) session.commit() def test_append(self): session = self.create_session() self.login('member1@example.com') + title = '010' with oauth_mockup_server(), self.given( 'Appending a batch', - f'/apiv1/batches/id: {self.batch1.id}', + f'/apiv1/batches/id: {title}', 'APPEND', json=dict( issueIds=self.issue1.id @@ -78,7 +90,7 @@ def test_append(self): ): assert status == 200 assert response.json['id'] is not None - assert response.json['title'] == self.batch1.title + assert response.json['title'] == title assert response.json['projectId'] == self.project1.id assert self.issue1.id in response.json['issueIds'] assert len(response.json['issueIds']) == 1 @@ -109,9 +121,9 @@ def test_append(self): when( 'Inended batch with integer type not found', - url_parameters=dict(id=0) + url_parameters=dict(id=101) ) - assert status == 404 + assert status == '936 Invalid Batch More Than 100' when( 'Inended batch with string type not found', @@ -122,3 +134,25 @@ def test_append(self): when('Request is not authorized', authorization=None) assert status == 401 + session = self.create_session() + assert session.query(Batch) \ + .filter(Batch.project_id == self.project1.id) \ + .order_by(Batch.id.desc()) \ + .first() != self.batch2 + + with oauth_mockup_server(), self.given( + 'Appending a batch', + f'/apiv1/batches/id: {self.batch1.title}', + 'APPEND', + json=dict( + issueIds=self.issue2.id + ) + ): + assert status == 200 + assert response.json['id'] is not None + assert response.json['title'] == self.batch1.title + assert response.json['projectId'] == self.project1.id + assert self.issue2.id in response.json['issueIds'] + assert len(response.json['issueIds']) == 1 + + diff --git a/dolphin/tests/test_project_create.py b/dolphin/tests/test_project_create.py index d68c3fb6..e3faf54e 100644 --- a/dolphin/tests/test_project_create.py +++ b/dolphin/tests/test_project_create.py @@ -93,6 +93,11 @@ def test_create(self): created_project = session.query(Project).get(created_project_id) assert created_project.modified_by is None + session = self.create_session() + assert session.query(Project) \ + .get(response.json['id']) \ + .batches + assert len(logs) == 2 assert isinstance(logs[0], InstantiationLogEntry) assert isinstance(logs[1], RequestLogEntry)