Skip to content

Commit

Permalink
Revise append batch, closes #943
Browse files Browse the repository at this point in the history
  • Loading branch information
mohadese-yousefi committed Jul 13, 2019
1 parent a448a03 commit 15d91b6
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 19 deletions.
41 changes: 31 additions & 10 deletions dolphin/controllers/batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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) \
Expand All @@ -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
2 changes: 1 addition & 1 deletion dolphin/controllers/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def create(self):
)
raise

batch = Batch(title='00')
batch = Batch(title='001')
project.batches.append(batch)

DBSession.add(project)
Expand Down
3 changes: 3 additions & 0 deletions dolphin/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'

50 changes: 42 additions & 8 deletions dolphin/tests/test_batch_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def mockup(cls):
email='[email protected]',
access_token='access token',
phone=123456789,
reference_id=2
reference_id=2,
)

workflow = Workflow(title='Default')
Expand All @@ -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
Expand All @@ -59,26 +61,36 @@ 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('[email protected]')
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
)
):
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
Expand Down Expand Up @@ -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',
Expand All @@ -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


5 changes: 5 additions & 0 deletions dolphin/tests/test_project_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 15d91b6

Please sign in to comment.