Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added test case for uploading data.

  • Loading branch information...
commit b2ba9b496e05cb7bc6373905d0cc90e230dd62d9 1 parent d339ef8
Alexander Dutton authored January 15, 2012

Showing 1 changed file with 42 additions and 15 deletions. Show diff stats Hide diff stats

  1. 57  openorg_timeseries/tests/admin.py
57  openorg_timeseries/tests/admin.py
@@ -2,6 +2,7 @@
2 2
 import csv
3 3
 import httplib
4 4
 import os
  5
+import urlparse
5 6
 
6 7
 try:
7 8
     import json
@@ -127,7 +128,21 @@ def testCreateAlreadyExisting(self):
127 128
         # Check that we get a conflict response
128 129
         self.assertEqual(response.status_code, httplib.CONFLICT)
129 130
 
130  
-class RESTDetailTestCase(TimeSeriesTestCase):
  131
+class DetailTestCase(TimeSeriesTestCase):
  132
+    readings = {
  133
+        'json': json.dumps({'readings': [{'ts': '1970-01-01T00:30:00+00:00', 'val': 5},
  134
+                                         {'ts': 3600000, 'val': 10},
  135
+                                         {'ts': '1970-01-01 01:30:00Z', 'val': 15},
  136
+                                         ['1970-01-01 03:00+01:00', 20]]}),
  137
+        'csv': '\n'.join(["1970-01-01T00:30:00+00:00,5",
  138
+                    "1970-01-01 01:00:00Z,10",
  139
+                    "1970-01-01 01:30:00Z,15",
  140
+                    "1970-01-01 03:00+01:00,20"]),
  141
+        'expected': [['1970-01-01T01:30:00+01:00', '5.0'],
  142
+                     ['1970-01-01T02:00:00+01:00', '10.0'],
  143
+                     ['1970-01-01T02:30:00+01:00', '15.0'],
  144
+                     ['1970-01-01T03:00:00+01:00', '20.0']]}
  145
+
131 146
     def setUp(self):
132 147
         response = self.client.post('/admin/',
133 148
                                     data=json.dumps(self.real_timeseries),
@@ -135,6 +150,7 @@ def setUp(self):
135 150
                                     REMOTE_USER='withaddperm')
136 151
         self.location = response['Location']
137 152
 
  153
+class RESTDetailTestCase(DetailTestCase):
138 154
     def testGet(self):
139 155
         response = self.client.get(self.location,
140 156
                                    content_type='application/json',
@@ -144,20 +160,6 @@ def testGet(self):
144 160
         self.assertEqual(response['Content-type'], 'application/json')
145 161
         body = json.loads(response._get_content())
146 162
 
147  
-    readings = {
148  
-        'json': json.dumps({'readings': [{'ts': '1970-01-01T00:30:00+00:00', 'val': 5},
149  
-                                         {'ts': 3600000, 'val': 10},
150  
-                                         {'ts': '1970-01-01 01:30:00Z', 'val': 15},
151  
-                                         ['1970-01-01 03:00+01:00', 20]]}),
152  
-        'csv': '\n'.join(["1970-01-01T00:30:00+00:00,5",
153  
-                    "1970-01-01 01:00:00Z,10",
154  
-                    "1970-01-01 01:30:00Z,15",
155  
-                    "1970-01-01 03:00+01:00,20"]),
156  
-        'expected': [['1970-01-01T01:30:00+01:00', '5.0'],
157  
-                     ['1970-01-01T02:00:00+01:00', '10.0'],
158  
-                     ['1970-01-01T02:30:00+01:00', '15.0'],
159  
-                     ['1970-01-01T03:00:00+01:00', '20.0']]}
160  
-
161 163
     def testPostJSON(self):
162 164
         self.postReadings('application/json', 'json')
163 165
 
@@ -244,6 +246,7 @@ def testJSONChangeUnprivileged(self):
244 246
 
245 247
         self.assertEqual(response.status_code, httplib.FORBIDDEN)
246 248
 
  249
+class FormDetailTestCase(DetailTestCase):
247 250
     def testFormChange(self):
248 251
         form_data = {'title': 'new title',
249 252
                      'notes': 'new notes'}
@@ -265,6 +268,30 @@ def testEmptyRequest(self):
265 268
                                     REMOTE_USER='unprivileged')
266 269
         self.assertEqual(response.status_code, httplib.OK)
267 270
 
  271
+    def testCSVUpload(self):
  272
+        filename = os.path.join(os.path.dirname(__file__), 'data', 'test_data.csv')
  273
+        with open(filename) as csv_file:
  274
+            response = self.client.post(self.location,
  275
+                                        data={'readings': csv_file},
  276
+                                        REMOTE_USER='withaddperm',
  277
+                                        HTTP_ACCEPT='text/html',
  278
+                                        follow=False)
  279
+
  280
+        self.assertEqual(response.status_code, httplib.SEE_OTHER)
  281
+
  282
+        # Check that the time-series was updated
  283
+        with open(os.path.join(settings.TIME_SERIES_PATH, 'csv', self.real_timeseries['slug'] + '.csv')) as f:
  284
+            reader = csv.reader(f)
  285
+            self.assertSequenceEqual(list(reader), self.readings['expected'])
  286
+
  287
+        # Check that the result of the upload made it into the query string
  288
+        location = urlparse.urlparse(response['Location'])
  289
+        query = urlparse.parse_qs(location.query)
  290
+        self.assertEqual(query.get('readings.count'), ['4'])
  291
+        self.assertEqual(query.get('readings.appended'), ['4'])
  292
+
  293
+
  294
+
268 295
 class CreateViewTestCase(TimeSeriesTestCase):
269 296
     def testGET(self):
270 297
         response = self.client.get('/admin/create/',

0 notes on commit b2ba9b4

Please sign in to comment.
Something went wrong with that request. Please try again.