File size: 4,175 Bytes
550665c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from unittest import TestCase

from gcsa.attachment import Attachment
from gcsa.serializers.attachment_serializer import AttachmentSerializer

DOC_URL = 'https://bit.ly/3lZo0Cc'


class TestAttachment(TestCase):

    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 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'")


class TestAttachmentSerializer(TestCase):

    def test_to_json(self):
        attachment = Attachment(
            file_url=DOC_URL,
            title='My doc',
            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(
            file_url=DOC_URL,
            title='My doc2',
            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_object(self):
        attachment_json = {
            'title': 'My doc',
            'fileUrl': DOC_URL,
            'mimeType': "application/vnd.google-apps.document"
        }
        attachment = AttachmentSerializer.to_object(attachment_json)

        self.assertEqual(attachment.title, 'My doc')
        self.assertEqual(attachment.file_url, DOC_URL)
        self.assertEqual(attachment.mime_type, "application/vnd.google-apps.document")
        self.assertIsNone(attachment.icon_link)
        self.assertIsNone(attachment.file_id)

        attachment_json = {
            'title': 'My doc2',
            'fileUrl': DOC_URL,
            'mimeType': "application/vnd.google-apps.drawing",
            'iconLink': "https://some_link.com",
            'fileId': 'abc123'
        }
        serializer = AttachmentSerializer(attachment_json)
        attachment = serializer.get_object()

        self.assertEqual(attachment.title, 'My doc2')
        self.assertEqual(attachment.file_url, DOC_URL)
        self.assertEqual(attachment.mime_type, "application/vnd.google-apps.drawing")
        self.assertEqual(attachment.icon_link, "https://some_link.com")
        self.assertEqual(attachment.file_id, 'abc123')

        attachment_json_str = """{

            "title": "My doc3",

            "fileUrl": "%s",

            "mimeType": "application/vnd.google-apps.drawing",

            "iconLink": "https://some_link.com",

            "fileId": "abc123"

        }

        """ % DOC_URL
        attachment = AttachmentSerializer.to_object(attachment_json_str)

        self.assertEqual(attachment.title, 'My doc3')
        self.assertEqual(attachment.file_url, DOC_URL)
        self.assertEqual(attachment.mime_type, "application/vnd.google-apps.drawing")
        self.assertEqual(attachment.icon_link, "https://some_link.com")
        self.assertEqual(attachment.file_id, 'abc123')