-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Added test for appending batch and model for batch * Added controller fo batch * Added new test case for append batch and created controller for this * Implementing batch append, #943 * Added commit for mockup of append batch test * A wrong indent
- Loading branch information
Showing
10 changed files
with
309 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 | ||
|
||
from ..exceptions import StatusIssueIdIsNull, StatusInvalidIssueIdType, \ | ||
StatusIssueIdNotInForm, StatusIssueNotFound | ||
from ..models import Batch, Issue | ||
|
||
|
||
class BatchController(ModelRestController): | ||
__model__ = Batch | ||
|
||
@authorize | ||
@json | ||
@Batch.expose | ||
@Batch.validate(fields=dict( | ||
issueIds=dict( | ||
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') | ||
|
||
issue_id = context.form['issueIds'] | ||
issue = DBSession.query(Issue) \ | ||
.filter(Issue.id == issue_id) \ | ||
.one_or_none() | ||
|
||
if issue is None: | ||
raise StatusIssueNotFound(issue_id) | ||
|
||
batch.issues.append(issue) | ||
|
||
return batch | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from restfulpy.orm import Field, DeclarativeBase, relationship | ||
from restfulpy.orm.metadata import MetadataField | ||
from sqlalchemy import Integer, ForeignKey, String, Unicode, UniqueConstraint | ||
|
||
|
||
class Batch(DeclarativeBase): | ||
|
||
__tablename__ = 'batch' | ||
|
||
id = Field( | ||
Integer, | ||
primary_key=True, | ||
readonly=True, | ||
not_none=True, | ||
required=False, | ||
label='ID', | ||
minimum=1, | ||
example=1, | ||
protected=False, | ||
) | ||
title = Field( | ||
Unicode(50), | ||
min_length=2, | ||
label='Batch title', | ||
watermark='Lorem Ipsum', | ||
not_none=True, | ||
required=True, | ||
python_type=str, | ||
protected=False, | ||
example='Lorem Ipsum' | ||
) | ||
project_id = Field( | ||
Integer, | ||
ForeignKey('project.id'), | ||
not_none=True, | ||
readonly=True | ||
) | ||
issues = relationship( | ||
'Issue', | ||
back_populates='batches', | ||
protected=True | ||
) | ||
projects = relationship( | ||
'Project', | ||
back_populates='batches', | ||
protected=True | ||
) | ||
|
||
__table_args__ = ( | ||
UniqueConstraint( | ||
title, | ||
project_id, | ||
name='uix_title_project_id' | ||
), | ||
) | ||
|
||
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 | ||
) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
from auditor.context import Context as AuditLogContext | ||
from bddrest import status, response, when, given | ||
from nanohttp.contexts import Context | ||
from nanohttp import context | ||
|
||
from dolphin.models import Workflow, Group, Release, Member, Batch, Issue, \ | ||
Project | ||
from dolphin.tests.helpers import LocalApplicationTestCase, oauth_mockup_server | ||
|
||
|
||
class TestBatch(LocalApplicationTestCase): | ||
|
||
@classmethod | ||
@AuditLogContext(dict()) | ||
def mockup(cls): | ||
session = cls.create_session() | ||
|
||
cls.member1 = Member( | ||
title='First Member', | ||
email='[email protected]', | ||
access_token='access token', | ||
phone=123456789, | ||
reference_id=2 | ||
) | ||
|
||
workflow = Workflow(title='Default') | ||
group = Group(title='default') | ||
|
||
release1 = Release( | ||
title='My first release', | ||
description='A decription for my first release', | ||
cutoff='2030-2-20', | ||
launch_date='2030-2-20', | ||
manager=cls.member1, | ||
room_id=0, | ||
group=group, | ||
) | ||
|
||
cls.project1 = Project( | ||
release=release1, | ||
workflow=workflow, | ||
group=group, | ||
manager=cls.member1, | ||
title='My first project', | ||
description='A decription for my project', | ||
room_id=1001 | ||
) | ||
session.add(cls.project1) | ||
session.commit() | ||
|
||
cls.batch1 = Batch(title='01') | ||
cls.project1.batches.append(cls.batch1) | ||
|
||
with Context(dict()): | ||
context.identity = cls.member1 | ||
|
||
cls.issue1 = Issue( | ||
title='First issue', | ||
description='This is description of first issue', | ||
kind='feature', | ||
days=1, | ||
room_id=2 | ||
) | ||
cls.project1.issues.append(cls.issue1) | ||
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', | ||
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 | ||
|
||
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|