-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
101 lines (75 loc) · 4.43 KB
/
tests.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 coding=utf-8
from django.test import TestCase
from django.db import models
from django.contrib.auth.models import User
from django.db.models import exceptions
from code_generator import *
class CodeGeneratorTests(TestCase):
def test_increment_base_10(self):
self.assertEqual(increment_base_10('100'), '101')
self.assertEqual(increment_base_10('10'), '011')
self.assertEqual(increment_base_10('1', min_length=5), '00002')
self.assertEqual(increment_base_10('999', min_length=1), '1000')
self.assertEqual(increment_base_10('t22', prefix='t'), 't023')
self.assertEqual(increment_base_10('bla00001', prefix='bla'), 'bla002')
self.assertEqual(increment_base_10('010001%', suffix='%', min_length=4),
'10002%')
self.assertEqual(increment_base_10('010099atchoum', suffix='atchoum',
min_length=8, inc=2),
'00010101atchoum')
self.assertEqual(increment_base_10('A099L', prefix='A',suffix='L',
min_length=8, inc=2, pad_with='X'),
'AXXXXX101L')
def test_get_code_from_model(self):
self.assertEqual(get_code_from_model(User, 'username', default='0'),
'0')
self.assertEqual(get_code_from_model(User, 'username'), '0')
User.objects.create(username='toto')
try:
get_code_from_model(User, 'unexistant_field')
self.fail()
except exceptions.FieldError:
pass
# ordering is on the code field by default
self.assertEqual(get_code_from_model(User, 'username'), 'toto')
User.objects.create(username='bobo')
self.assertEqual(get_code_from_model(User, 'username'), 'toto')
self.assertEqual(get_code_from_model(User, 'username', order_by='id'),
'bobo')
qs = User.objects.exclude(username='bobo')
self.assertEqual(get_code_from_model(User, 'username', qs=qs), 'toto')
def test_generate_code(self):
def start(code, **kwargs):
return code
self.assertEqual(generate_code(start, code='100'), '101')
self.assertEqual(generate_code(start, code='10'), '011')
self.assertEqual(generate_code(start, code='1', min_length=5), '00002')
self.assertEqual(generate_code(start, code='999', min_length=1), '1000')
self.assertEqual(generate_code(start, code='t22', prefix='t'), 't023')
self.assertEqual(generate_code(start, code='bla00001', prefix='bla'),
'bla002')
self.assertEqual(generate_code(start, code='010001%',
suffix='%', min_length=4),
'10002%')
self.assertEqual(generate_code(start, code='010099atchoum',
suffix='atchoum', min_length=8, inc=2),
'00010101atchoum')
self.assertEqual(generate_code(start, code='A099L', prefix='A',
suffix='L', min_length=8, inc=2,
pad_with='X'),
'AXXXXX101L')
self.assertEqual(generate_code(get_code_from_model, prefix='L',
default='0', min_length=3,
field='username', model=User), 'L001')
User.objects.create(username='L001')
self.assertEqual(generate_code(get_code_from_model, prefix='L',
default='0', min_length=3,
field='username', model=User), 'L002')
User.objects.all().delete()
User.objects.create(username='2a2')
self.assertEqual(generate_code(get_code_from_model,
generate_tracking_tag,
field='username', model=User),
'3a2')