Skip to content

Commit

Permalink
refactor(client): add author, update_time and parent_commit_id for Draft
Browse files Browse the repository at this point in the history
  • Loading branch information
marshallmallows committed Nov 18, 2021
1 parent 2797e00 commit e33508c
Show file tree
Hide file tree
Showing 4 changed files with 55 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,7 +435,7 @@ 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:
Expand All @@ -444,6 +444,9 @@ class Draft(AttrsMixin, ReprMixin):
branch_name: The branch name.
status: The status of the draft.
description: The draft description.
parent_commit_id: The parent commit id.
author: The author of the draft.
update_time: The time of last update.
"""

Expand All @@ -456,15 +459,29 @@ class Draft(AttrsMixin, ReprMixin):
branch_name: str = attr(key=camel)
status: str = attr()
description: str = attr(default="")
parent_commit_id: str = attr(key=camel)
author: User = attr()
update_time: int = attr(key=lambda _: "updateAt")

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,
author: User,
update_time: int,
parent_commit_id: str,
description: str = "",
) -> None:
self.number = number
self.title = title
self.branch_name = branch_name
self.status = status
self.description = description
self.parent_commit_id = parent_commit_id
self.author = author
self.update_time = update_time

def _repr_head(self) -> str:
return f"{self.__class__.__name__}({self.number})"
Expand All @@ -482,6 +499,12 @@ def loads(cls: Type[_T], contents: Dict[str, Any]) -> _T:
"branchName": <str>
"status": "OPEN", "CLOSED" or "COMMITTED"
"description": <str>
"parentCommitId": <str>
"committer": {
"name": <str>
"date": <int>
}
"update_time": <int>
}
Returns:
Expand All @@ -502,6 +525,12 @@ def dumps(self) -> Dict[str, Any]:
"branchName": <str>
"status": "OPEN", "CLOSED" or "COMMITTED"
"description": <str>
"parentCommitId": <str>
"committer": {
"name": <str>
"date": <int>
}
"update_time": <int>
}
"""
Expand Down
6 changes: 3 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,7 @@ 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", User("test", 1636967807), 1637223060,'4c564ea07f4e47679ec8c63d238bb3a1')],
)
checkout = mocker.patch(f"{gas.__name__}.DatasetClient.checkout")
self.gas_client.upload_dataset(dataset)
Expand All @@ -533,7 +533,7 @@ 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", User("test", 1636967807), 1637223060,'4c564ea07f4e47679ec8c63d238bb3a1')],
)
checkout = mocker.patch(f"{gas.__name__}.DatasetClient.checkout")
self.gas_client.upload_dataset(dataset, branch_name="dev")
Expand Down
17 changes: 15 additions & 2 deletions tensorbay/client/tests/test_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,17 @@
_DRAFT_BRANCH_NAME = "main"
_DRAFT_STATUS = "OPEN"
_DRAFT_DESCRIPTION = "description"
_DRAFT_PARENT_COMMIT_ID = "4c564ea07f4e47679ec8c63d238bb3a1"
_DRAFT_UPDATE_TIME = 1637223060
_DRAFT_AUTHOR = User("draft author", 1636967807)
_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},
"updateAt": _DRAFT_UPDATE_TIME
}


Expand Down Expand Up @@ -139,20 +145,27 @@ 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_AUTHOR, _DRAFT_UPDATE_TIME, _DRAFT_PARENT_COMMIT_ID, _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.author == _DRAFT_AUTHOR
assert draft.update_time == _DRAFT_UPDATE_TIME
assert draft.parent_commit_id == _DRAFT_PARENT_COMMIT_ID

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.update_time == _DRAFT_DATA["updateAt"]
assert draft.author == User.loads(_DRAFT_DATA["author"])

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_AUTHOR, _DRAFT_UPDATE_TIME, _DRAFT_PARENT_COMMIT_ID)
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},
'updateAt': 1636967807,
"description": "first draft of test_draft",
},
{
"number": 2,
"title": "",
"branchName": branch_name,
"status": "CLOSED",
'parentCommitId': '4c564ea07f4e47679ec8c63d238bb3a1',
'author': {'name': 'draft author', 'date': 1636967807},
'updateAt': 1636967807,
"description": "",
},
]
Expand Down

0 comments on commit e33508c

Please sign in to comment.