1
1
from contextlib import contextmanager
2
2
from datetime import datetime
3
3
4
- from six .moves import cStringIO as StringIO
5
- from django .test import TestCase
6
4
from django .core import management
5
+ from django .test import TestCase
6
+ from six .moves import cStringIO as StringIO
7
7
8
8
from simple_history import models as sh_models
9
9
from simple_history .management .commands import populate_history
10
-
11
- from .. import models
10
+ from ..models import Book , Poll , Restaurant
12
11
13
12
14
13
@contextmanager
@@ -47,60 +46,60 @@ def test_bad_args(self):
47
46
self .assertIn (msg , out .getvalue ())
48
47
49
48
def test_auto_populate (self ):
50
- models . Poll .objects .create (question = "Will this populate?" ,
51
- pub_date = datetime .now ())
52
- models . Poll .history .all ().delete ()
49
+ Poll .objects .create (question = "Will this populate?" ,
50
+ pub_date = datetime .now ())
51
+ Poll .history .all ().delete ()
53
52
management .call_command (self .command_name , auto = True ,
54
53
stdout = StringIO (), stderr = StringIO ())
55
- self .assertEqual (models . Poll .history .all ().count (), 1 )
54
+ self .assertEqual (Poll .history .all ().count (), 1 )
56
55
57
56
def test_populate_with_custom_batch_size (self ):
58
- models . Poll .objects .create (question = "Will this populate?" ,
59
- pub_date = datetime .now ())
60
- models . Poll .history .all ().delete ()
57
+ Poll .objects .create (question = "Will this populate?" ,
58
+ pub_date = datetime .now ())
59
+ Poll .history .all ().delete ()
61
60
management .call_command (self .command_name , auto = True , batchsize = 500 ,
62
61
stdout = StringIO (), stderr = StringIO ())
63
- self .assertEqual (models . Poll .history .all ().count (), 1 )
62
+ self .assertEqual (Poll .history .all ().count (), 1 )
64
63
65
64
def test_specific_populate (self ):
66
- models . Poll .objects .create (question = "Will this populate?" ,
67
- pub_date = datetime .now ())
68
- models . Poll .history .all ().delete ()
69
- models . Book .objects .create (isbn = "9780007117116" )
70
- models . Book .history .all ().delete ()
65
+ Poll .objects .create (question = "Will this populate?" ,
66
+ pub_date = datetime .now ())
67
+ Poll .history .all ().delete ()
68
+ Book .objects .create (isbn = "9780007117116" )
69
+ Book .history .all ().delete ()
71
70
management .call_command (self .command_name , "tests.book" ,
72
71
stdout = StringIO (), stderr = StringIO ())
73
- self .assertEqual (models . Book .history .all ().count (), 1 )
74
- self .assertEqual (models . Poll .history .all ().count (), 0 )
72
+ self .assertEqual (Book .history .all ().count (), 1 )
73
+ self .assertEqual (Poll .history .all ().count (), 0 )
75
74
76
75
def test_failing_wont_save (self ):
77
- models . Poll .objects .create (question = "Will this populate?" ,
78
- pub_date = datetime .now ())
79
- models . Poll .history .all ().delete ()
76
+ Poll .objects .create (question = "Will this populate?" ,
77
+ pub_date = datetime .now ())
78
+ Poll .history .all ().delete ()
80
79
self .assertRaises (self .command_error ,
81
80
management .call_command , self .command_name ,
82
81
"tests.poll" , "tests.invalid_model" ,
83
82
stdout = StringIO (), stderr = StringIO ())
84
- self .assertEqual (models . Poll .history .all ().count (), 0 )
83
+ self .assertEqual (Poll .history .all ().count (), 0 )
85
84
86
85
def test_multi_table (self ):
87
86
data = {'rating' : 5 , 'name' : "Tea 'N More" }
88
- models . Restaurant .objects .create (** data )
89
- models . Restaurant .updates .all ().delete ()
87
+ Restaurant .objects .create (** data )
88
+ Restaurant .updates .all ().delete ()
90
89
management .call_command (self .command_name , 'tests.restaurant' ,
91
90
stdout = StringIO (), stderr = StringIO ())
92
- update_record = models . Restaurant .updates .all ()[0 ]
91
+ update_record = Restaurant .updates .all ()[0 ]
93
92
for attr , value in data .items ():
94
93
self .assertEqual (getattr (update_record , attr ), value )
95
94
96
95
def test_existing_objects (self ):
97
96
data = {'rating' : 5 , 'name' : "Tea 'N More" }
98
97
out = StringIO ()
99
- models . Restaurant .objects .create (** data )
100
- pre_call_count = models . Restaurant .updates .count ()
98
+ Restaurant .objects .create (** data )
99
+ pre_call_count = Restaurant .updates .count ()
101
100
management .call_command (self .command_name , 'tests.restaurant' ,
102
101
stdout = StringIO (), stderr = out )
103
- self .assertEqual (models . Restaurant .updates .count (), pre_call_count )
102
+ self .assertEqual (Restaurant .updates .count (), pre_call_count )
104
103
self .assertIn (populate_history .Command .EXISTING_HISTORY_FOUND ,
105
104
out .getvalue ())
106
105
@@ -111,3 +110,35 @@ def test_no_historical(self):
111
110
stdout = out )
112
111
self .assertIn (populate_history .Command .NO_REGISTERED_MODELS ,
113
112
out .getvalue ())
113
+
114
+ def test_batch_processing_with_batch_size_less_than_total (self ):
115
+ data = [
116
+ Poll (id = 1 , question = 'Question 1' , pub_date = datetime .now ()),
117
+ Poll (id = 2 , question = 'Question 2' , pub_date = datetime .now ()),
118
+ Poll (id = 3 , question = 'Question 3' , pub_date = datetime .now ()),
119
+ Poll (id = 4 , question = 'Question 4' , pub_date = datetime .now ()),
120
+ ]
121
+ Poll .objects .bulk_create (data )
122
+
123
+ management .call_command (self .command_name , auto = True , batchsize = 3 ,
124
+ stdout = StringIO (), stderr = StringIO ())
125
+
126
+ self .assertEqual (Poll .history .count (), 4 )
127
+
128
+ def test_stdout_not_printed_when_verbosity_is_0 (self ):
129
+ out = StringIO ()
130
+ Poll .objects .create (question = 'Question 1' , pub_date = datetime .now ())
131
+
132
+ management .call_command (self .command_name , auto = True , batchsize = 3 ,
133
+ stdout = out , stderr = StringIO (), verbosity = 0 )
134
+
135
+ self .assertEqual (out .getvalue (), '' )
136
+
137
+ def test_stdout_printed_when_verbosity_is_not_specified (self ):
138
+ out = StringIO ()
139
+ Poll .objects .create (question = 'Question 1' , pub_date = datetime .now ())
140
+
141
+ management .call_command (self .command_name , auto = True , batchsize = 3 ,
142
+ stdout = out , stderr = StringIO ())
143
+
144
+ self .assertNotEqual (out .getvalue (), '' )
0 commit comments