Skip to content

Commit

Permalink
Implementing batch append, #943
Browse files Browse the repository at this point in the history
  • Loading branch information
farzeneka committed Jul 1, 2019
1 parent 873d871 commit cd11084
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 11 deletions.
3 changes: 2 additions & 1 deletion dolphin/controllers/batch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from nanohttp import json, context, HTTPNotFound
from nanohttp import json, context, HTTPNotFound, int_or_notfound
from restfulpy.authorization import authorize
from restfulpy.controllers import ModelRestController
from restfulpy.orm import DBSession, commit
Expand All @@ -23,6 +23,7 @@ class BatchController(ModelRestController):
))
@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')
Expand Down
3 changes: 2 additions & 1 deletion dolphin/controllers/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from ..exceptions import StatusChatRoomNotFound, \
StatusRoomMemberAlreadyExist, StatusRoomMemberNotFound, \
StatusManagerNotFound, StatusSecondaryManagerNotFound
from ..models import Project, Member, Subscription, Workflow, Group, Release
from ..models import Project, Member, Subscription, Workflow, Group, Release, \
Batch
from ..validators import project_validator, update_project_validator
from .files import FileController
from .issues import IssueController
Expand Down
7 changes: 5 additions & 2 deletions dolphin/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,11 @@ class StatusLaunchDateMustGreaterThanCutoffDate(HTTPKnownStatus):


class StatusIssueNotFound(HTTPKnownStatus):
def __init__(self, issue_id):
self.status = f'605 Issue Not Found: {issue_id}'
def __init__(self, issue_id=None):
# This + between strings are decided by PyLover.
# DO NOT DO THAT ANYWHERE
self.status = f'605 Issue Not Found' + \
(f': {issue_id}' if issue_id is not None else '')


class StatusMemberNotFound(HTTPKnownStatus):
Expand Down
17 changes: 17 additions & 0 deletions dolphin/models/batch.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from restfulpy.orm import Field, DeclarativeBase, relationship
from restfulpy.orm.metadata import MetadataField
from sqlalchemy import Integer, ForeignKey, String, Unicode, UniqueConstraint


Expand Down Expand Up @@ -53,3 +54,19 @@ class Batch(DeclarativeBase):
),
)

def to_dict(self):
batch_dict = super().to_dict()
batch_dict['issueIds'] = [i.id for i in self.issues]
return batch_dict

@classmethod
def iter_metadata_fields(cls):
yield from super().iter_metadata_fields()
yield MetadataField(
name='issue_ids',
key='issueIds',
label='Issues IDs',
required=False,
readonly=True
)

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from auditor.context import Context as AuditLogContext
from bddrest import status, response, when
from bddrest import status, response, when, given
from nanohttp.contexts import Context
from nanohttp import context

Expand Down Expand Up @@ -64,23 +64,59 @@ def mockup(cls):
session.commit()

def test_append(self):
session = self.create_session()
self.login('[email protected]')

with oauth_mockup_server(), self.given(
'Appending a batch',
f'/apiv1/batches/id: {self.batch1.id}',
'APPEND',
form=dict(
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['projectId'] == self.project1.id
# assert self.issue1.id in response.json['issueIds']
# assert len(response.json['issueIds']) == 1
assert self.issue1.id in response.json['issueIds']
assert len(response.json['issueIds']) == 1

when(
'Trying to pass without issue id',
json=given - 'issueIds'
)
assert status == '723 Issue Id Not In Form'

when(
'Trying to pass with invalid issue id type',
json=given | dict(issueIds='a')
)
assert status == '722 Invalid Issue Id Type'

when(
'Trying to pass with none issue id',
json=given | dict(issueIds=None)
)
assert status == '775 Issue Id Is Null'

when(
'Issue is not found',
json=given | dict(issueIds=0)
)
assert status == '605 Issue Not Found: 0'

when(
'Inended batch with integer type not found',
url_parameters=dict(id=0)
)
assert status == 404
when(
'Inended batch with string type not found',
url_parameters=dict(id='Alaphabet')
)
assert status == 404

when('Request is not authorized', authorization=None)
assert status == 401

# TODO: test when issueId missing
# TODO: test when invalid issueId type
# TODO: test when isuueId is none
39 changes: 39 additions & 0 deletions dolphin/tests/test_metadata_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from bddrest.authoring import status, response

from dolphin.tests.helpers import LocalApplicationTestCase


class TestBatch(LocalApplicationTestCase):

def test_metadata(self):
with self.given(
'Test metadata verb',
'/apiv1/batches',
'METADATA'
):
assert status == 200

fields = response.json['fields']

assert fields['id']['primaryKey'] is not None
assert fields['id']['readonly'] is not None
assert fields['id']['notNone'] is not None
assert fields['id']['required'] is not None
assert fields['id']['label'] is not None
assert fields['id']['minimum'] is not None
assert fields['id']['example'] is not None
assert fields['id']['protected'] is not None

assert fields['title']['minLength'] is not None
assert fields['title']['label'] is not None
assert fields['title']['watermark'] is not None
assert fields['title']['example'] is not None
assert fields['title']['notNone'] is not None
assert fields['title']['required'] is not None
assert fields['title']['protected'] is not None
assert fields['title']['type'] is not None

assert fields['projectId']['key'] is not None
assert fields['projectId']['notNone'] is not None
assert fields['projectId']['readonly'] is not None

0 comments on commit cd11084

Please sign in to comment.