DeleteOldTest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 93.94 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 31
loc 33
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_crashes() 16 16 2
A test_feedbacks() 15 15 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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, override_settings
22
from django.utils import timezone
23
from django.core.cache import cache
24
25
from crash.factories import CrashFactory
26
from crash.models import Crash, Symbols
27
from feedback.factories import FeedbackFactory
28
from feedback.models import Feedback
29
from omaha.dynamic_preferences_registry import global_preferences_manager as gpm
30
from omaha.limitation import delete_older_than, delete_size_is_exceeded, delete_duplicate_crashes
31
from omaha.limitation import monitoring_size
32
from omaha.factories import VersionFactory
33
from omaha_server.utils import is_private
34
from sparkle.factories import SparkleVersionFactory
35
36
class DeleteOldTest(TestCase):
37 View Code Duplication
    @is_private()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
38
    def test_crashes(self):
39
        old_date = timezone.now() - timezone.timedelta(days=5)
40
        gpm['Crash__limit_storage_days'] = 2
41
        CrashFactory.create_batch(10, created=old_date)
42
        Crash.objects.update(created=old_date)
43
        self.assertEqual(Crash.objects.all().count(), 10)
44
45
        deleted = list(Crash.objects.values_list('id', 'created', 'signature', 'userid', 'appid'))
46
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
47
                                     userid=x[3], appid=x[4]), deleted)
48
49
        result = delete_older_than('crash', 'Crash')
50
51
        self.assertDictEqual(result, dict(count=10, size=0, elements=deleted))
52
        self.assertEqual(Crash.objects.all().count(), 0)
53
54 View Code Duplication
    @is_private()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
55
    def test_feedbacks(self):
56
        old_date = timezone.now() - timezone.timedelta(days=5)
57
        gpm['Feedback__limit_storage_days'] = 2
58
        FeedbackFactory.create_batch(10, created=old_date)
59
        Feedback.objects.update(created=old_date)
60
        self.assertEqual(Feedback.objects.all().count(), 10)
61
62
        deleted = list(Feedback.objects.values_list('id', 'created'))
63
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p")), deleted)
64
65
        result = delete_older_than('feedback', 'Feedback')
66
67
        self.assertDictEqual(result, dict(count=10, size=0, elements=deleted))
68
        self.assertEqual(Feedback.objects.all().count(), 0)
69
70
71
class SizeExceedTest(TestCase):
72
    maxDiff = None
73
74 View Code Duplication
    @is_private()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
75
    def test_crashes(self):
76
        gpm['Crash__limit_size'] = 1
77
        crash_size = 10*1024*1023
78
        CrashFactory.create_batch(200, archive_size=crash_size, minidump_size=0)
79
        self.assertEqual(Crash.objects.all().count(), 200)
80
81
        del_count = 98
82
        deleted = list(Crash.objects.values_list('id', 'created', 'signature', 'userid', 'appid'))[:del_count]
83
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
84
                                      userid=x[3], appid=x[4]), deleted)
85
86
        result = delete_size_is_exceeded('crash', 'Crash')
87
88
        self.assertDictEqual(result, dict(count=del_count, size=del_count * crash_size, elements=deleted))
89
        self.assertEqual(Crash.objects.all().count(), 102)
90
91 View Code Duplication
    @is_private()
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
92
    def test_feedbacks(self):
93
        gpm['Feedback__limit_size'] = 1
94
        feedback_size = 10*1024*1023
95
        FeedbackFactory.create_batch(200, screenshot_size=feedback_size, system_logs_size=0, attached_file_size=0, blackbox_size=0)
96
        self.assertEqual(Feedback.objects.all().count(), 200)
97
98
        del_count = 98
99
        deleted = list(Feedback.objects.values_list('id', 'created'))
100
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p")), deleted)[:del_count]
101
102
        result = delete_size_is_exceeded('feedback', 'Feedback')
103
        self.assertDictEqual(result, dict(count=del_count, size=del_count * feedback_size, elements=deleted))
104
        self.assertEqual(Feedback.objects.all().count(), 102)
105
106
107
class DeleteDuplicateTest(TestCase):
108
    maxDiff = None
109
110
    @is_private()
111
    def test_crashes(self):
112
        gpm['Crash__duplicate_number'] = 10
113
        CrashFactory.create_batch(25, signature='test1')
114
        self.assertEqual(Crash.objects.filter(signature='test1').count(), 25)
115
        CrashFactory.create_batch(9, signature='test2')
116
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
117
118
        deleted = list(Crash.objects.filter(signature='test1').order_by('created').values_list('id', 'created', 'signature', 'userid', 'appid'))[:15]
119
        deleted = map(lambda x: dict(id=x[0], element_created=x[1].strftime("%d. %B %Y %I:%M%p"), signature=x[2],
120
                                     userid=x[3], appid=x[4]), deleted)
121
122
        result = delete_duplicate_crashes()
123
124
        self.assertDictEqual(result, dict(count=15, size=0, elements=deleted))
125
        self.assertEqual(Crash.objects.filter(signature='test1').count(), gpm['Crash__duplicate_number'])
126
        self.assertEqual(Crash.objects.filter(signature='test2').count(), 9)
127
128
129
@override_settings(CACHES={
130
    'default': {
131
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
132
    }
133
})
134
class MonitoringTest(TestCase):
135
    cache_keys = ['omaha_version_size', 'sparkle_version_size', 'crashes_size', 'feedbacks_size', 'symbols_size']
136
137
    def setUp(self):
138
        for key in self.cache_keys:
139
            cache.delete(key)
140
141
    @is_private()
142
    def test_monitoring(self):
143
        for key in self.cache_keys:
144
            self.assertEqual(cache.get(key, 0), 0)
145
146
        VersionFactory.create(file_size=100)
147
        SparkleVersionFactory.create(file_size=100)
148
        Crash.objects.create(archive_size=80, minidump_size=20)
149
        Symbols.objects.create(file_size=100)
150
        Feedback.objects.create(screenshot_size=25, blackbox_size=25, attached_file_size=25, system_logs_size=25)
151
152
        monitoring_size()
153
154
        for key in self.cache_keys:
155
            self.assertEqual(cache.get(key), 100)
156