File: test_randomchar_field.py

package info (click to toggle)
python-django-extensions 4.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,812 kB
  • sloc: python: 18,601; javascript: 7,354; makefile: 108; xml: 17
file content (111 lines) | stat: -rw-r--r-- 3,647 bytes parent folder | download | duplicates (2)
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
# -*- coding: utf-8 -*-
import string
import pytest

from django.test import TestCase

from .testapp.models import (
    RandomCharTestModel,
    RandomCharTestModelUnique,
    RandomCharTestModelLowercase,
)
from .testapp.models import (
    RandomCharTestModelUppercase,
    RandomCharTestModelAlpha,
    RandomCharTestModelDigits,
)
from .testapp.models import (
    RandomCharTestModelPunctuation,
    RandomCharTestModelLowercaseAlphaDigits,
    RandomCharTestModelUppercaseAlphaDigits,
)
from .testapp.models import RandomCharTestModelUniqueTogether

from unittest import mock


class RandomCharFieldTest(TestCase):
    def testRandomCharField(self):
        m = RandomCharTestModel()
        m.save()
        assert len(m.random_char_field) == 8, m.random_char_field

    def testRandomCharFieldUnique(self):
        m = RandomCharTestModelUnique()
        m.save()
        assert len(m.random_char_field) == 8, m.random_char_field

    def testRandomCharFieldLowercase(self):
        m = RandomCharTestModelLowercase()
        m.save()
        for c in m.random_char_field:
            assert c.islower(), m.random_char_field

    def testRandomCharFieldUppercase(self):
        m = RandomCharTestModelUppercase()
        m.save()
        for c in m.random_char_field:
            assert c.isupper(), m.random_char_field

    def testRandomCharFieldAlpha(self):
        m = RandomCharTestModelAlpha()
        m.save()
        for c in m.random_char_field:
            assert c.isalpha(), m.random_char_field

    def testRandomCharFieldDigits(self):
        m = RandomCharTestModelDigits()
        m.save()
        for c in m.random_char_field:
            assert c.isdigit(), m.random_char_field

    def testRandomCharFieldPunctuation(self):
        m = RandomCharTestModelPunctuation()
        m.save()
        for c in m.random_char_field:
            assert c in string.punctuation, m.random_char_field

    def testRandomCharTestModelLowercaseAlphaDigits(self):
        m = RandomCharTestModelLowercaseAlphaDigits()
        m.save()
        for c in m.random_char_field:
            assert c.isdigit() or (c.isalpha() and c.islower()), m.random_char_field

    def testRandomCharTestModelUppercaseAlphaDigits(self):
        m = RandomCharTestModelUppercaseAlphaDigits()
        m.save()
        for c in m.random_char_field:
            assert c.isdigit() or (c.isalpha() and c.isupper()), m.random_char_field

    def testRandomCharTestModelDuplicate(self):
        m = RandomCharTestModelUnique()
        m.save()
        with mock.patch(
            "django_extensions.db.fields.RandomCharField.random_char_generator"
        ) as func:
            func.return_value = iter([m.random_char_field, "aaa"])
            m = RandomCharTestModelUnique()
            m.save()
        assert m.random_char_field == "aaa"

    def testRandomCharTestModelAsserts(self):
        with mock.patch("django_extensions.db.fields.get_random_string") as mock_sample:
            mock_sample.return_value = "aaa"
            m = RandomCharTestModelUnique()
            m.save()

            m = RandomCharTestModelUnique()
            with pytest.raises(RuntimeError):
                m.save()

    def testRandomCharTestModelUniqueTogether(self):
        with mock.patch("django_extensions.db.fields.get_random_string") as mock_sample:
            mock_sample.return_value = "aaa"
            m = RandomCharTestModelUniqueTogether()
            m.common_field = "bbb"
            m.save()

            m = RandomCharTestModelUniqueTogether()
            m.common_field = "bbb"
            with pytest.raises(RuntimeError):
                m.save()