1
+ from polls .models import Question , Choice
1
2
from django .test import Client , TestCase
2
3
from django .test .utils import override_settings
4
+ from textwrap import dedent
3
5
import pyexcel as pe
4
6
import pyexcel .ext .xls
5
7
import pyexcel .ext .xlsx
11
13
from ordereddict import OrderedDict
12
14
else :
13
15
from collections import OrderedDict
14
-
15
16
if PY2 :
16
17
import pyexcel .ext .ods
17
18
from StringIO import StringIO
@@ -112,6 +113,71 @@ def test_exchange(self):
112
113
os .unlink (tmp_filename )
113
114
114
115
116
+ class DatabaseOperationsTestCase (TestCase ):
117
+ def setUp (self ):
118
+ self .testfile = "sample-data.xls"
119
+ Question .objects .all ().delete ()
120
+ Choice .objects .all ().delete ()
121
+
122
+ def testBook (self ):
123
+ fp = open (self .testfile , "rb" )
124
+ response = self .client .post ('/polls/import/' , data = {"file" : fp })
125
+ assert response .status_code == 200
126
+ response2 = self .client .get ('/polls/export/book' )
127
+ assert response2 .status_code == 200
128
+ book = pe .load_book_from_memory ('xls' , response2 .content )
129
+ content = dedent ("""
130
+ Sheet Name: question
131
+ +----+---------------------------+----------------------------------------------+----------+
132
+ | id | pub_date | question_text | slug |
133
+ +----+---------------------------+----------------------------------------------+----------+
134
+ | 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
135
+ +----+---------------------------+----------------------------------------------+----------+
136
+ | 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
137
+ +----+---------------------------+----------------------------------------------+----------+
138
+ Sheet Name: choice
139
+ +---------------+----+-------------+-------+
140
+ | choice_text | id | question_id | votes |
141
+ +---------------+----+-------------+-------+
142
+ | Java | 1 | 1 | 0 |
143
+ +---------------+----+-------------+-------+
144
+ | C++ | 2 | 1 | 0 |
145
+ +---------------+----+-------------+-------+
146
+ | C | 3 | 1 | 0 |
147
+ +---------------+----+-------------+-------+
148
+ | Eclipse | 4 | 2 | 0 |
149
+ +---------------+----+-------------+-------+
150
+ | Visual Studio | 5 | 2 | 0 |
151
+ +---------------+----+-------------+-------+
152
+ | PyCharm | 6 | 2 | 0 |
153
+ +---------------+----+-------------+-------+
154
+ | IntelliJ | 7 | 2 | 0 |
155
+ +---------------+----+-------------+-------+""" ).strip ('\n ' )
156
+ assert str (book ) == content
157
+
158
+ def testSheet (self ):
159
+ fp = open (self .testfile , "rb" )
160
+ response = self .client .post ('/polls/import/' , data = {"file" : fp })
161
+ assert response .status_code == 200
162
+ response2 = self .client .get ('/polls/export/sheet' )
163
+ assert response2 .status_code == 200
164
+ sheet = pe .load_from_memory ('xls' , response2 .content )
165
+ content = dedent ("""
166
+ Sheet Name: question
167
+ +----+---------------------------+----------------------------------------------+----------+
168
+ | id | pub_date | question_text | slug |
169
+ +----+---------------------------+----------------------------------------------+----------+
170
+ | 1 | 2015-01-28T00:00:00+00:00 | What is your favourite programming language? | language |
171
+ +----+---------------------------+----------------------------------------------+----------+
172
+ | 2 | 2015-01-29T00:00:00+00:00 | What is your favourite IDE? | ide |
173
+ +----+---------------------------+----------------------------------------------+----------+""" ).strip ('\n ' )
174
+ assert str (sheet ) == content
175
+
176
+
115
177
@override_settings (FILE_UPLOAD_MAX_MEMORY_SIZE = 1 )
116
178
class ExcelResponseUsingFileTestCase (ExcelResponseTestCase ):
179
+ pass
180
+
181
+ @override_settings (FILE_UPLOAD_MAX_MEMORY_SIZE = 1 )
182
+ class DatabaseOperationsUsingFileTestCase (DatabaseOperationsTestCase ):
117
183
pass
0 commit comments