Skip to content

Commit

Permalink
[TSDK-286] Improve usage of read only fields in models (#215)
Browse files Browse the repository at this point in the history
This PR improves our usage of read only fields, adding a general set of read only fields used by all API models, and read only fields for specific API models.
  • Loading branch information
mrashed-dev authored Apr 14, 2022
1 parent 4e59137 commit 93bc034
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
30 changes: 28 additions & 2 deletions nylas/client/restful_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def as_json(self):


class NylasAPIObject(RestfulModel):
read_only_attrs = {"id", "account_id", "object", "job_status_id"}

def __init__(self, cls, api):
RestfulModel.__init__(self, cls, api)

Expand Down Expand Up @@ -638,6 +640,7 @@ class Calendar(NylasAPIObject):

def __init__(self, api):
NylasAPIObject.__init__(self, Calendar, api)
self.read_only_attrs.update({"is_primary", "read_only"})

@property
def events(self):
Expand Down Expand Up @@ -674,6 +677,16 @@ class Event(NylasAPIObject):

def __init__(self, api):
NylasAPIObject.__init__(self, Event, api)
self.read_only_attrs.update(
{
"ical_uid",
"message_id",
"owner",
"status",
"master_event_id",
"original_start_time",
}
)

def as_json(self):
dct = NylasAPIObject.as_json(self)
Expand Down Expand Up @@ -804,6 +817,13 @@ class JobStatus(NylasAPIObject):

def __init__(self, api):
NylasAPIObject.__init__(self, JobStatus, api)
self.read_only_attrs.update(
{
"action",
"status",
"original_data",
}
)

def is_successful(self):
return self.status == "successful"
Expand Down Expand Up @@ -871,13 +891,19 @@ class Component(NylasAPIObject):
"created_at": "created_at",
"updated_at": "updated_at",
}
read_only_attrs = {"id", "public_application_id", "created_at", "updated_at"}

collection_name = None
api_root = "component"

def __init__(self, api):
NylasAPIObject.__init__(self, Component, api)
self.read_only_attrs.update(
{
"public_application_id",
"created_at",
"updated_at",
}
)

def as_json(self):
dct = NylasAPIObject.as_json(self)
Expand All @@ -896,13 +922,13 @@ class Webhook(NylasAPIObject):
"application_id",
"version",
)
read_only_attrs = {"id", "application_id", "version"}

collection_name = "webhooks"
api_root = "a"

def __init__(self, api):
NylasAPIObject.__init__(self, Webhook, api)
self.read_only_attrs.update({"application_id", "version"})

def as_json(self):
dct = {}
Expand Down
27 changes: 25 additions & 2 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from datetime import datetime, timedelta
import pytest
from urlobject import URLObject
from requests import RequestException
from nylas.client.restful_models import Event


Expand All @@ -15,15 +14,39 @@ def blank_event(api_client):


@pytest.mark.usefixtures("mock_event_create_response")
def test_event_crud(api_client):
def test_event_crud(mocked_responses, api_client):
event1 = blank_event(api_client)
event1.object = "should not send"
event1.account_id = "should not send"
event1.job_status_id = "should not send"
event1.ical_uid = "should not send"
event1.message_id = "should not send"
event1.owner = "should not send"
event1.status = "should not send"
event1.master_event_id = "should not send"
event1.original_start_time = "should not send"
event1.save()
request = mocked_responses.calls[0].request
body = json.loads(request.body)
assert event1.id == "cv4ei7syx10uvsxbs21ccsezf"
assert "title" in body
assert "object" not in body
assert "account_id" not in body
assert "job_status_id" not in body
assert "ical_uid" not in body
assert "message_id" not in body
assert "owner" not in body
assert "status" not in body
assert "master_event_id" not in body
assert "original_start_time" not in body

event1.title = "blah"
event1.save()
request = mocked_responses.calls[1].request
body = json.loads(request.body)
assert event1.title == "loaded from JSON"
assert event1.get("ignored") is None
assert "id" not in body


@pytest.mark.usefixtures("mock_event_create_notify_response")
Expand Down

0 comments on commit 93bc034

Please sign in to comment.