Skip to content

src(attachment): Use attrs + cattrs #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions cellengine/resources/attachment.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,37 @@
from __future__ import annotations
from dataclasses import dataclass, field
from dataclasses_json.cfg import config
from typing import Optional

from attr import define, field

import cellengine as ce
from cellengine.utils.dataclass_mixin import DataClassMixin, ReadOnly
from cellengine.utils import converter, readonly


try:
from typing import Literal
except ImportError:
from typing_extensions import Literal

@dataclass
class Attachment(DataClassMixin):

@define
class Attachment:
"""A class representing a CellEngine attachment.
Attachments are non-data files that are stored in an experiment.
"""

_id: str = field(on_setattr=readonly)
filename: str
_id: str = field(
metadata=config(field_name="_id"), default=ReadOnly()
) # type: ignore
crc32c: str = field(repr=False, default=ReadOnly()) # type: ignore
experiment_id: str = field(repr=False, default=ReadOnly()) # type: ignore
md5: str = field(repr=False, default=ReadOnly()) # type: ignore
size: int = field(repr=False, default=ReadOnly()) # type: ignore
crc32c: str = field(on_setattr=readonly, repr=False)
experiment_id: str = field(on_setattr=readonly, repr=False)
md5: str = field(on_setattr=readonly, repr=False)
size: int = field(on_setattr=readonly, repr=False)
type: Optional[Literal["textbox", "image"]] = None

@property
def path(self):
return f"experiments/{self.experiment_id}/attachments/{self._id}".rstrip(
"/None"
)

@classmethod
def get(cls, experiment_id: str, _id: str = None, name: str = None) -> Attachment:
Expand All @@ -39,20 +51,16 @@ def upload(experiment_id: str, filepath: str, filename: str = None) -> Attachmen
return ce.APIClient().upload_attachment(experiment_id, filepath, filename)

def update(self):
"""Save changes to this Attachment to CellEngine.
"""Save changes to this Attachment to CellEngine."""
res = ce.APIClient().update(self)
self.__setstate__(res.__getstate__()) # type: ignore

Returns:
None: Updates the Attachment on CellEngine and synchronizes the
local Attachment object properties with remote state.
"""
res = ce.APIClient().update_entity(
self.experiment_id, self._id, "attachments", body=self.to_dict()
)
self.__dict__.update(Attachment.from_dict(res).__dict__)
@classmethod
def from_dict(cls, data: dict):
return converter.structure(data, cls)

def delete(self):
"""Delete this attachment."""
return ce.APIClient().delete_entity(self.experiment_id, "attachments", self._id)
def to_dict(self):
return converter.unstructure(self)

def download(self, to_file: str = None):
"""Download the attachment.
Expand All @@ -74,3 +82,6 @@ def download(self, to_file: str = None):
f.write(res)
else:
return res

def delete(self):
return ce.APIClient().delete_entity(self.experiment_id, "attachments", self._id)
36 changes: 31 additions & 5 deletions tests/unit/resources/test_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


@pytest.fixture(scope="module")
def attachment(ENDPOINT_BASE, client, attachments):
def attachment(client, ENDPOINT_BASE, attachments):
att = attachments[0]
att.update({"experimentId": EXP_ID})
return Attachment.from_dict(att)
Expand All @@ -24,7 +24,18 @@ def attachments_tester(attachment):


@responses.activate
def test_should_get_attachment(ENDPOINT_BASE, attachment):
def test_client_gets_attachment(client, ENDPOINT_BASE, attachment):
responses.add(
responses.GET,
ENDPOINT_BASE + f"/experiments/{EXP_ID}/attachments",
json=[attachment.to_dict()],
)
att = client.get_attachment(EXP_ID, attachment._id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Relates to #112 (comment)

It looks like we still have Attachment.get/upload now, but they're no longer tested. Copy these tests to test both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you still need one for Attachment.upload; only client.upload_attachment is tested.

attachments_tester(att)


@responses.activate
def test_attachment_gets_attachment(client, ENDPOINT_BASE, attachment):
responses.add(
responses.GET,
ENDPOINT_BASE + f"/experiments/{EXP_ID}/attachments",
Expand All @@ -35,7 +46,22 @@ def test_should_get_attachment(ENDPOINT_BASE, attachment):


@responses.activate
def test_should_create_attachment(ENDPOINT_BASE, experiment, attachments):
def test_should_create_attachment(client, ENDPOINT_BASE, experiment, attachments):
"""Test creation of a new attachment.
This test must be run from the project root directory"""
responses.add(
responses.POST,
ENDPOINT_BASE + f"/experiments/{EXP_ID}/attachments",
json=attachments[0],
)
att = client.upload_attachment(experiment._id, "tests/data/text.txt")
attachments_tester(att)


@responses.activate
def test_classmethod_should_create_attachment(
client, ENDPOINT_BASE, experiment, attachments
):
"""Test creation of a new attachment.
This test must be run from the project root directory"""
responses.add(
Expand All @@ -58,7 +84,7 @@ def test_should_delete_attachment(ENDPOINT_BASE, attachment):


@responses.activate
def test_update_attachment(ENDPOINT_BASE, experiment, attachment, attachments):
def test_update_attachment(ENDPOINT_BASE, attachment):
"""Test that the .update() method makes the correct call. Does not test
that the correct response is made; this should be done with an integration
test.
Expand All @@ -78,7 +104,7 @@ def test_update_attachment(ENDPOINT_BASE, experiment, attachment, attachments):


@responses.activate
def test_download_attachment(ENDPOINT_BASE, experiment, attachment):
def test_download_attachment(client, ENDPOINT_BASE, attachment):
responses.add(
responses.GET,
ENDPOINT_BASE + f"/experiments/{EXP_ID}/attachments/{attachment._id}",
Expand Down