Completed
Pull Request — master (#207)
by Kirill
01:34
created

CrashFormTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 40.98 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 25
loc 61
rs 10
c 2
b 1
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A test_form() 13 14 1
A test_form_tar_file() 10 16 2
B test_invalid_data() 0 28 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
import os
22
import string
23
import random
24
25
from django.test import TestCase
26
from django.core.files.uploadedfile import SimpleUploadedFile
27
28
from crash.forms import SymbolsAdminForm, CrashFrom
29
30
31
BASE_DIR = os.path.dirname(__file__)
32
TEST_DATA_DIR = os.path.join(BASE_DIR, 'testdata')
33
SYM_FILE = os.path.join(TEST_DATA_DIR, 'BreakpadTestApp.sym')
34
TAR_FILE = os.path.join(TEST_DATA_DIR, 'foo.tar')
35
36
37
def string_generator(size, chars=string.ascii_uppercase + string.ascii_lowercase + string.digits):
38
    return ''.join(random.choice(chars) for _ in range(size))
39
40
41
class SymbolsAdminFormTest(TestCase):
42
    def test_form(self):
43
        form_data = {}
44
45
        with open(SYM_FILE, 'rb') as f:
46
            form_file_data = {'file': SimpleUploadedFile('BreakpadTestApp.sym', f.read())}
47
48
        form = SymbolsAdminForm(form_data, form_file_data)
49
50
        self.assertTrue(form.is_valid())
51 View Code Duplication
        self.assertEqual(form.cleaned_data['debug_id'], 'C1C0FA629EAA4B4D9DD2ADE270A231CC1')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
52
        self.assertEqual(form.cleaned_data['debug_file'], 'BreakpadTestApp.pdb')
53
        self.assertEqual(form.cleaned_data['file_size'], 68149)
54
55
56
class CrashFormTest(TestCase):
57
    def test_form(self):
58
        form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
59
            "7b05e196-7e23-416b-bd13-99287924e214.dmp", b"content"))
60
        form_data = dict(
61
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
62
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
63
        )
64
65
        form = CrashFrom(form_data, form_file_data)
66 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
67
        self.assertTrue(form.is_valid())
68
        self.assertEqual(form.cleaned_data['upload_file_minidump'].name, '7b05e196-7e23-416b-bd13-99287924e214.dmp')
69
        self.assertEqual(form.cleaned_data['archive_size'], 0)
70
        self.assertEqual(form.cleaned_data['minidump_size'], 7)
71
72
    def test_form_tar_file(self):
73
        with open(TAR_FILE, 'rb') as f:
74
            form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
75
                "foo.tar", f.read()))
76
        form_data = dict(
77
            appid='{D0AB2EBC-931B-4013-9FEB-C9C4C2225C8C}',
78
            userid='{2882CF9B-D9C2-4edb-9AAF-8ED5FCF366F7}',
79
        )
80
81
        form = CrashFrom(form_data, form_file_data)
82
83
        self.assertTrue(form.is_valid())
84
        self.assertEqual(form.cleaned_data['upload_file_minidump'].name, '7b05e196-7e23-416b-bd13-99287924e214.dmp')
85
        self.assertEqual(form.cleaned_data['archive'].name, 'foo.tar')
86
        self.assertEqual(form.cleaned_data['archive_size'], 85504)
87
        self.assertEqual(form.cleaned_data['minidump_size'], 14606)
88
89
    def test_invalid_data(self):
90
        with open(TAR_FILE, 'rb') as f:
91
            form_file_data = dict(upload_file_minidump=SimpleUploadedFile(
92
                "foo.tar", f.read(100)))
93
        form_data = dict(
94
            appid=string_generator(40),
95
            userid=string_generator(40),
96
            meta=string_generator(40),
97
            stacktrace=string_generator(40),
98
            stacktrace_json=string_generator(40),
99
            signature=string_generator(256),
100
            ip=string_generator(40),
101
            groupid=string_generator(40),
102
            eventid=string_generator(40),
103
        )
104
105
        form = CrashFrom(form_data, form_file_data)
106
        self.assertFalse(form.is_valid())
107
        self.assertIn('upload_file_minidump', form.errors)
108
        self.assertIn('appid', form.errors)
109
        self.assertIn('userid', form.errors)
110
        self.assertIn('meta', form.errors)
111
        self.assertNotIn('stacktrace', form.errors)
112
        self.assertIn('stacktrace_json', form.errors)
113
        self.assertIn('signature', form.errors)
114
        self.assertIn('ip', form.errors)
115
        self.assertIn('groupid', form.errors)
116
        self.assertIn('eventid', form.errors)
117