CrashManualCleanupFormTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 24
loc 24
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A test_negative_fields() 6 6 1
A test_form() 16 16 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 collections import OrderedDict
22
23
from django.test import TestCase
24
25
from omaha.forms import ApplicationAdminForm, ManualCleanupForm, CrashManualCleanupForm
26
27
import mock
28
29
30
class ApplicationAdminFormTest(TestCase):
31
    def test_form(self):
32
        app_id = '{5fad27d4-6bfa-4daa-a1b3-5a1f821fee0f}'
33
        form_data = dict(id=app_id, name='test')
34
        form = ApplicationAdminForm(data=form_data)
35
        self.assertTrue(form.is_valid())
36
        self.assertEqual(form.cleaned_data['id'], app_id.upper())
37
        self.assertEqual(form.cleaned_data['name'], 'test')
38
39
40
41 View Code Duplication
class ManualCleanupFormTest(TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
42
    def test_form(self):
43
        data = dict(limit_days=10, limit_size=10)
44
        form = ManualCleanupForm(data=data, initial=dict(type='feedback__Feedback'))
45
        self.assertTrue(form.is_valid())
46
        self.assertEqual(form.cleaned_data['limit_days'], 10)
47
        self.assertEqual(form.cleaned_data['limit_size'], 10)
48
        self.assertEqual(type(form.fields), OrderedDict)
49
        self.assertEqual(form.fields.keys(), ['limit_days', 'limit_size'])
50
51
        with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked:
52
            form.cleanup()
53
        mock_args, mock_kwargs = mocked.call_args
54
55
        self.assertTrue(mocked.called)
56
        self.assertDictEqual(mock_args[1], data)
57
58
    def test_negative_fields(self):
59
        data = dict(limit_days=-1, limit_size=-1)
60
        form = ManualCleanupForm(data=data, initial=dict(type='feedback__Feedback'))
61
62
        self.assertFalse(form.is_valid())
63
        self.assertItemsEqual(form.errors.keys(), ['limit_size', 'limit_days'])
64
65
66 View Code Duplication
class CrashManualCleanupFormTest(TestCase):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
    def test_form(self):
68
        data = dict(limit_days=10, limit_size=10, limit_duplicated=10)
69
        form = CrashManualCleanupForm(data=data, initial=dict(type='crash__Crash'))
70
        self.assertTrue(form.is_valid())
71
        self.assertEqual(form.cleaned_data['limit_days'], 10)
72
        self.assertEqual(form.cleaned_data['limit_size'], 10)
73
        self.assertEqual(form.cleaned_data['limit_duplicated'], 10)
74
        self.assertEqual(type(form.fields), OrderedDict)
75
        self.assertEqual(form.fields.keys(), ['limit_duplicated', 'limit_days', 'limit_size'])
76
77
        with mock.patch('omaha.tasks.deferred_manual_cleanup.apply_async') as mocked:
78
            form.cleanup()
79
        mock_args, mock_kwargs = mocked.call_args
80
81
        self.assertTrue(mocked.called)
82
        self.assertDictEqual(mock_args[1], data)
83
84
    def test_negative_fields(self):
85
        data = dict(limit_days=-1, limit_size=-1, limit_duplicated=-1)
86
        form = CrashManualCleanupForm(data=data, initial=dict(type='crash__Crash'))
87
88
        self.assertFalse(form.is_valid())
89
        self.assertItemsEqual(form.errors.keys(), ['limit_duplicated', 'limit_days', 'limit_size'])
90