def test_to_json(self): attachment = Attachment('My doc', file_url=DOC_URL, mime_type="application/vnd.google-apps.document") attachment_json = { 'title': 'My doc', 'fileUrl': DOC_URL, 'mimeType': "application/vnd.google-apps.document" } self.assertDictEqual(AttachmentSerializer.to_json(attachment), attachment_json) attachment = Attachment('My doc2', file_url=DOC_URL, mime_type="application/vnd.google-apps.drawing", icon_link="https://some_link.com", file_id='abc123') attachment_json = { 'title': 'My doc2', 'fileUrl': DOC_URL, 'mimeType': "application/vnd.google-apps.drawing", 'iconLink': "https://some_link.com", 'fileId': 'abc123' } serializer = AttachmentSerializer(attachment) self.assertDictEqual(serializer.get_json(), attachment_json)
def test_to_json_attachments(self): e = Event('Good day', start=(1 / Jan / 2019)[11:22:33], timezone=TEST_TIMEZONE, attachments=[ Attachment('My file1', 'https://file.url1', "application/vnd.google-apps.document"), Attachment('My file2', 'https://file.url2', "application/vnd.google-apps.document") ]) event_json = { 'summary': 'Good day', 'start': {'dateTime': '2019-01-01T11:22:33+13:00', 'timeZone': TEST_TIMEZONE}, 'end': {'dateTime': '2019-01-01T12:22:33+13:00', 'timeZone': TEST_TIMEZONE}, 'recurrence': [], 'visibility': 'default', 'reminders': {'useDefault': False}, 'attachments': [ { 'title': 'My file1', 'fileUrl': 'https://file.url1', 'mimeType': 'application/vnd.google-apps.document' }, { 'title': 'My file2', 'fileUrl': 'https://file.url2', 'mimeType': 'application/vnd.google-apps.document' } ] } self.assertDictEqual(EventSerializer.to_json(e), event_json)
def test_repr_str(self): attachment = Attachment( file_url=DOC_URL, title='My doc', mime_type="application/vnd.google-apps.document") self.assertEqual(attachment.__repr__(), "<Attachment 'My doc' - 'https://bit.ly/3lZo0Cc'>") self.assertEqual(attachment.__str__(), "'My doc' - 'https://bit.ly/3lZo0Cc'")
def test_create(self): attachment = Attachment('My doc', file_url=DOC_URL, mime_type="application/vnd.google-apps.document") self.assertEqual(attachment.title, 'My doc') with self.assertRaises(ValueError): Attachment('My doc', file_url=DOC_URL, mime_type="application/vnd.google-apps.something")
def test_create(self): attachment = Attachment( file_url=DOC_URL, title='My doc', mime_type="application/vnd.google-apps.document") self.assertEqual(attachment.title, 'My doc') attachment = Attachment( file_url=DOC_URL, title='My doc', mime_type="application/vnd.google-apps.something") self.assertTrue(attachment.unsupported_mime_type)
def to_object(json_attachment): BaseSerializer.assure_dict(json_attachment) return Attachment(title=json_attachment['title'], file_url=json_attachment['fileUrl'], mime_type=json_attachment['mimeType'], icon_link=json_attachment.get('iconLink', None), file_id=json_attachment.get('fileId', None))
def test_equal(self): dp = {'summary': 'Breakfast', 'start': (1 / Feb / 2019)[9:00]} gadget_dp = { "type_": Gadget.ICON, "link": 'https://gadget.com', "icon_link": 'https://icon.com' } attachments_dp = { "file_url": 'https://file.com', "mime_type": "application/vnd.google-apps.map" } event1 = Event(**dp, event_id='123', end=(31 / Dec / 2019)[23:59], timezone=TEST_TIMEZONE, description='Everyday breakfast', location='Home', recurrence=Recurrence.rule(freq=DAILY), color='#254433', visibility=Visibility.PRIVATE, attendees='*****@*****.**', gadget=Gadget('Gadget', **gadget_dp), attachments=Attachment('My doc', **attachments_dp), minutes_before_popup_reminder=15, other={"key": "value"}) self.assertEqual(event1, event1) self.assertNotEqual(Event(**dp), Event('Breakfast', start=(22 / Jun / 2020)[22:22])) self.assertNotEqual(Event(**dp, event_id='123'), Event(**dp, event_id='abc')) self.assertNotEqual(Event(**dp, description='Desc1'), Event(**dp, description='Desc2')) self.assertNotEqual(Event(**dp, location='Home'), Event(**dp, location='Work')) self.assertNotEqual( Event(**dp, recurrence=Recurrence.rule(freq=DAILY)), Event(**dp, recurrence=Recurrence.rule(freq=WEEKLY))) self.assertNotEqual(Event(**dp, color='#254433'), Event(**dp, color='#ffffff')) self.assertNotEqual(Event(**dp, visibility=Visibility.PRIVATE), Event(**dp, visibility=Visibility.PUBLIC)) self.assertNotEqual(Event(**dp, attendees='*****@*****.**'), Event(**dp, attendees='*****@*****.**')) self.assertNotEqual(Event(**dp, gadget=Gadget('Gadget1', **gadget_dp)), Event(**dp, gadget=Gadget('Gadget2', **gadget_dp))) self.assertNotEqual( Event(**dp, attachments=Attachment('Attachment1', **attachments_dp)), Event(**dp, attachments=Attachment('Attachment2', **attachments_dp))) self.assertNotEqual(Event(**dp, minutes_before_email_reminder=10), Event(**dp, minutes_before_popup_reminder=10)) self.assertNotEqual(Event(**dp, other={"key1": "value1"}), Event(**dp, other={"key2": "value2"}))
def _to_object(json_attachment): return Attachment(file_url=json_attachment['fileUrl'], title=json_attachment.get('title', None), mime_type=json_attachment.get('mimeType', None), _icon_link=json_attachment.get('iconLink', None), _file_id=json_attachment.get('fileId', None))