Completed
Push — master ( 6a4ed1...7ae2ee )
by
unknown
44s
created

PartialUpdateSerializerTest   A

Complexity

Total Complexity 1

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 10
c 0
b 0
f 0
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
B test_serializer() 0 24 1
1
# coding: utf8
2
3
"""
4
This software is licensed under the Apache 2 license, quoted below.
5
6
Copyright 2014 Crystalnix Limited
7
8
Licensed under the Apache License, Version 2.0 (the "License"); you may not
9
use this file except in compliance with the License. You may obtain a copy of
10
the License at
11
12
    http://www.apache.org/licenses/LICENSE-2.0
13
14
Unless required by applicable law or agreed to in writing, software
15
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17
License for the specific language governing permissions and limitations under
18
the License.
19
"""
20
21
from django.test import TestCase
22
from django.core.files.uploadedfile import SimpleUploadedFile
23
24
from omaha.tests.utils import temporary_media_root
25
from omaha.models import Version
26
from omaha.factories import ApplicationFactory, DataFactory, PlatformFactory, ChannelFactory, VersionFactory, ActionFactory, PartialUpdateFactory
27
from omaha.serializers import AppSerializer, DataSerializer, PlatformSerializer, ChannelSerializer, VersionSerializer, ActionSerializer, PartialUpdateSerializer
28
from datetime import date
29
30
31
class PartialUpdateSerializerTest(TestCase):
32
    def test_serializer(self):
33
        today = date.today()
34
        version = VersionFactory()
35
        data = dict(
36
            is_enabled=True,
37
            exclude_new_users=True,
38
            version=version,
39
            end_date=str(date(today.year, today.month, today.day)),
40
            percent=51.0,
41
            start_date=str(date(today.year, today.month, today.day)),
42
            active_users=1,
43
        )
44
        partial = PartialUpdateFactory(**data)
45
        self.assertDictEqual(
46
            PartialUpdateSerializer(partial).data,
47
            dict(
48
                id=partial.pk,
49
                is_enabled=partial.is_enabled,
50
                exclude_new_users=partial.exclude_new_users,
51
                version=partial.version.pk,
52
                end_date=partial.end_date,
53
                percent=partial.percent,
54
                start_date=partial.start_date,
55
                active_users=partial.active_users,
56
            )
57
        )
58
59
60
class AppSerializerTest(TestCase):
61
    def test_serializer(self):
62
        data = dict(id='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
63
                    name='chrome2')
64
        app = ApplicationFactory(**data)
65
        data.update(dict(data_set=[]))
66
        self.assertDictEqual(AppSerializer(app).data, data)
67
68
69
class PlatformSerializerTest(TestCase):
70
    def test_serializer(self):
71
        data = dict(name='win')
72
        platform = PlatformFactory(**data)
73
        self.assertDictEqual(PlatformSerializer(platform).data, dict(id=platform.pk, **data))
74
75
76
class ChannelSerializerTest(TestCase):
77
    def test_serializer(self):
78
        data = dict(name='stable')
79
        channel = ChannelFactory(**data)
80
        self.assertDictEqual(ChannelSerializer(channel).data, dict(id=channel.pk, **data))
81 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
82
83
class ActionSerializerTest(TestCase):
84
    def test_serializer(self):
85
        data = dict(terminateallbrowsers=True)
86
        action = ActionFactory(**data)
87
        self.assertDictEqual(ActionSerializer(action).data,
88
                             dict(id=action.pk,
89
                                  successsaction=action.successsaction,
90
                                  run=action.run,
91
                                  event=action.event,
92
                                  other=action.other,
93
                                  version=action.version.pk,
94
                                  successurl=action.successurl,
95
                                  arguments=action.arguments,
96
                                  **data))
97
98
99
class DataSerializerTest(TestCase):
100
    def test_serializer(self):
101
        _data = DataFactory()
102
        self.assertDictEqual(DataSerializer(_data).data,
103
                             dict(id=_data.pk,
104
                                  app=_data.app.pk,
105
                                  name=_data.name,
106
                                  index=_data.index,
107
                                  value=_data.value,
108
                                  ))
109
110
111
class VersionSerializerTest(TestCase):
112
    maxDiff = None
113
114
    @temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/')
115
    def test_serializer(self):
116
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', False))
117
        self.assertDictEqual(VersionSerializer(version).data, dict(
118
            id=version.id,
119
            is_enabled=version.is_enabled,
120
            is_critical=version.is_critical,
121
            app=version.app.id,
122
            platform=version.platform.id,
123
            channel=version.channel.id,
124
            version=str(version.version),
125
            release_notes=version.release_notes,
126
            file=version.file.url,
127
            file_hash=version.file_hash,
128
            file_size=version.file_size,
129
            created=version.created.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
130
            modified=version.modified.strftime('%Y-%m-%dT%H:%M:%S.%fZ'),
131
        ))
132
133
    @temporary_media_root(MEDIA_URL='http://cache.pack.google.com/edgedl/chrome/install/782.112/')
134
    def test_auto_fill_file_size(self):
135
        version = VersionFactory.create(file=SimpleUploadedFile('./chrome_installer.exe', b' ' * 10))
136
        data = dict(
137
            app=version.app.id,
138
            platform=version.platform.id,
139
            channel=version.channel.id,
140
            version='4.3.2.1',
141
            release_notes=version.release_notes,
142
            file=version.file,
143
            file_hash=version.file_hash,
144
        )
145
146
        new_version = VersionSerializer(data=data)
147
        self.assertTrue(new_version.is_valid())
148
        new_version_instance = new_version.save()
149
        self.assertEqual(new_version_instance.file_size, 10)
150