Skip to content

Commit

Permalink
refactor(client): add author, updated_at and parent_commit_id for Draft
Browse files Browse the repository at this point in the history
PR Closed: #1108
  • Loading branch information
marshallmallows committed Nov 22, 2021
1 parent 2797e00 commit e6538f9
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 7 deletions.
33 changes: 31 additions & 2 deletions tensorbay/client/struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,14 +435,17 @@ def _loads(self, contents: Dict[str, Any]) -> None:
self.committer = User.loads(contents["committer"])


class Draft(AttrsMixin, ReprMixin):
class Draft(AttrsMixin, ReprMixin): # pylint: disable=too-many-instance-attributes
"""This class defines the basic structure of a draft.
Arguments:
number: The number of the draft.
title: The title of the draft.
branch_name: The branch name.
status: The status of the draft.
parent_commit_id: The parent commit id.
author: The author of the draft.
updated_at: The time of last update.
description: The draft description.
"""
Expand All @@ -455,15 +458,29 @@ class Draft(AttrsMixin, ReprMixin):
title: str = attr()
branch_name: str = attr(key=camel)
status: str = attr()
parent_commit_id: str = attr(key=camel)
author: User = attr()
updated_at: int = attr(key=camel)
description: str = attr(default="")

def __init__( # pylint: disable=too-many-arguments
self, number: int, title: str, branch_name: str, status: str, description: str = ""
self,
number: int,
title: str,
branch_name: str,
status: str,
parent_commit_id: str,
author: User,
updated_at: int,
description: str = "",
) -> None:
self.number = number
self.title = title
self.branch_name = branch_name
self.status = status
self.parent_commit_id = parent_commit_id
self.author = author
self.updated_at = updated_at
self.description = description

def _repr_head(self) -> str:
Expand All @@ -481,6 +498,12 @@ def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
"title": <str>
"branchName": <str>
"status": "OPEN", "CLOSED" or "COMMITTED"
"parentCommitId": <str>
"author": {
"name": <str>
"date": <int>
}
"updatedAt": <int>
"description": <str>
}
Expand All @@ -501,6 +524,12 @@ def dumps(self) -> Dict[str, Any]:
"title": <str>
"branchName": <str>
"status": "OPEN", "CLOSED" or "COMMITTED"
"parentCommitId": <str>
"author": {
"name": <str>
"date": <int>
}
"updatedAt": <int>
"description": <str>
}
Expand Down
26 changes: 23 additions & 3 deletions tensorbay/client/tests/test_gas.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tensorbay.client.dataset import DatasetClient, FusionDatasetClient
from tensorbay.client.gas import DEFAULT_BRANCH, DEFAULT_IS_PUBLIC, GAS
from tensorbay.client.status import Status
from tensorbay.client.struct import ROOT_COMMIT_ID, Draft
from tensorbay.client.struct import ROOT_COMMIT_ID, Draft, User
from tensorbay.client.tests.utility import mock_response
from tensorbay.dataset import Dataset
from tensorbay.exception import DatasetTypeError, ResourceNotExistError
Expand Down Expand Up @@ -509,7 +509,17 @@ def test_upload_dataset(self, mocker):
# upload the dataset in main branch containing a draft
list_drafts = mocker.patch(
f"{gas.__name__}.DatasetClient.list_drafts",
return_value=[Draft(1, "title", DEFAULT_BRANCH, "OPEN")],
return_value=[
Draft(
1,
"title",
DEFAULT_BRANCH,
"OPEN",
"4c564ea07f4e47679ec8c63d238bb3a1",
User("test", 1636967807),
1637223060,
)
],
)
checkout = mocker.patch(f"{gas.__name__}.DatasetClient.checkout")
self.gas_client.upload_dataset(dataset)
Expand All @@ -533,7 +543,17 @@ def test_upload_dataset(self, mocker):
# upload the dataset in dev branch containing a draft
list_drafts = mocker.patch(
f"{gas.__name__}.DatasetClient.list_drafts",
return_value=[Draft(1, "title", "dev", "OPEN")],
return_value=[
Draft(
1,
"title",
"dev",
"OPEN",
"4c564ea07f4e47679ec8c63d238bb3a1",
User("test", 1636967807),
1637223060,
)
],
)
checkout = mocker.patch(f"{gas.__name__}.DatasetClient.checkout")
self.gas_client.upload_dataset(dataset, branch_name="dev")
Expand Down
32 changes: 30 additions & 2 deletions tensorbay/client/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@
_DRAFT_TITLE = "draft title"
_DRAFT_BRANCH_NAME = "main"
_DRAFT_STATUS = "OPEN"
_DRAFT_PARENT_COMMIT_ID = "4c564ea07f4e47679ec8c63d238bb3a1"
_DRAFT_UPDATED_AT = 1637223060
_DRAFT_AUTHOR = User("draft author", 1636967807)
_DRAFT_DESCRIPTION = "description"
_DRAFT_DATA = {
"number": _DRAFT_NUMBER,
"title": _DRAFT_TITLE,
"branchName": _DRAFT_BRANCH_NAME,
"status": _DRAFT_STATUS,
"parentCommitId": _DRAFT_PARENT_COMMIT_ID,
"author": {"name": "draft author", "date": 1636967807},
"updatedAt": _DRAFT_UPDATED_AT,
}


Expand Down Expand Up @@ -139,20 +145,42 @@ def test_loads(self):
class TestDraft:
def test_init(self):
draft = Draft(
_DRAFT_NUMBER, _DRAFT_TITLE, _DRAFT_BRANCH_NAME, _DRAFT_STATUS, _DRAFT_DESCRIPTION
_DRAFT_NUMBER,
_DRAFT_TITLE,
_DRAFT_BRANCH_NAME,
_DRAFT_STATUS,
_DRAFT_PARENT_COMMIT_ID,
_DRAFT_AUTHOR,
_DRAFT_UPDATED_AT,
_DRAFT_DESCRIPTION,
)
assert draft.number == _DRAFT_NUMBER
assert draft.title == _DRAFT_TITLE
assert draft.status == _DRAFT_STATUS
assert draft.branch_name == _DRAFT_BRANCH_NAME
assert draft.description == _DRAFT_DESCRIPTION
assert draft.parent_commit_id == _DRAFT_PARENT_COMMIT_ID
assert draft.author == _DRAFT_AUTHOR
assert draft.updated_at == _DRAFT_UPDATED_AT

def test_loads(self):
draft = Draft.loads(_DRAFT_DATA)
assert draft.number == _DRAFT_DATA["number"]
assert draft.title == _DRAFT_DATA["title"]
assert draft.branch_name == _DRAFT_DATA["branchName"]
assert draft.status == _DRAFT_DATA["status"]
assert draft.parent_commit_id == _DRAFT_DATA["parentCommitId"]
assert draft.author == User.loads(_DRAFT_DATA["author"])
assert draft.updated_at == _DRAFT_DATA["updatedAt"]

def test_dumps(self):
draft = Draft(_DRAFT_NUMBER, _DRAFT_TITLE, _DRAFT_BRANCH_NAME, _DRAFT_STATUS)
draft = Draft(
_DRAFT_NUMBER,
_DRAFT_TITLE,
_DRAFT_BRANCH_NAME,
_DRAFT_STATUS,
_DRAFT_PARENT_COMMIT_ID,
_DRAFT_AUTHOR,
_DRAFT_UPDATED_AT,
)
assert draft.dumps() == _DRAFT_DATA
6 changes: 6 additions & 0 deletions tensorbay/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,19 @@ def mock_list_drafts(mocker, branch_name):
"title": "draft1",
"branchName": branch_name,
"status": "OPEN",
"parentCommitId": "4c564ea07f4e47679ec8c63d238bb3a1",
"author": {"name": "draft author", "date": 1636967807},
"updatedAt": 1636967807,
"description": "first draft of test_draft",
},
{
"number": 2,
"title": "",
"branchName": branch_name,
"status": "CLOSED",
"parentCommitId": "4c564ea07f4e47679ec8c63d238bb3a1",
"author": {"name": "draft author", "date": 1636967807},
"updatedAt": 1636967807,
"description": "",
},
]
Expand Down

0 comments on commit e6538f9

Please sign in to comment.