Skip to content

Commit

Permalink
Added test case for uploading data.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdutton committed Jan 15, 2012
1 parent d339ef8 commit b2ba9b4
Showing 1 changed file with 42 additions and 15 deletions.
57 changes: 42 additions & 15 deletions openorg_timeseries/tests/admin.py
Expand Up @@ -2,6 +2,7 @@
import csv import csv
import httplib import httplib
import os import os
import urlparse


try: try:
import json import json
Expand Down Expand Up @@ -127,14 +128,29 @@ def testCreateAlreadyExisting(self):
# Check that we get a conflict response # Check that we get a conflict response
self.assertEqual(response.status_code, httplib.CONFLICT) self.assertEqual(response.status_code, httplib.CONFLICT)


class RESTDetailTestCase(TimeSeriesTestCase): class DetailTestCase(TimeSeriesTestCase):
readings = {
'json': json.dumps({'readings': [{'ts': '1970-01-01T00:30:00+00:00', 'val': 5},
{'ts': 3600000, 'val': 10},
{'ts': '1970-01-01 01:30:00Z', 'val': 15},
['1970-01-01 03:00+01:00', 20]]}),
'csv': '\n'.join(["1970-01-01T00:30:00+00:00,5",
"1970-01-01 01:00:00Z,10",
"1970-01-01 01:30:00Z,15",
"1970-01-01 03:00+01:00,20"]),
'expected': [['1970-01-01T01:30:00+01:00', '5.0'],
['1970-01-01T02:00:00+01:00', '10.0'],
['1970-01-01T02:30:00+01:00', '15.0'],
['1970-01-01T03:00:00+01:00', '20.0']]}

def setUp(self): def setUp(self):
response = self.client.post('/admin/', response = self.client.post('/admin/',
data=json.dumps(self.real_timeseries), data=json.dumps(self.real_timeseries),
content_type='application/json', content_type='application/json',
REMOTE_USER='withaddperm') REMOTE_USER='withaddperm')
self.location = response['Location'] self.location = response['Location']


class RESTDetailTestCase(DetailTestCase):
def testGet(self): def testGet(self):
response = self.client.get(self.location, response = self.client.get(self.location,
content_type='application/json', content_type='application/json',
Expand All @@ -144,20 +160,6 @@ def testGet(self):
self.assertEqual(response['Content-type'], 'application/json') self.assertEqual(response['Content-type'], 'application/json')
body = json.loads(response._get_content()) body = json.loads(response._get_content())


readings = {
'json': json.dumps({'readings': [{'ts': '1970-01-01T00:30:00+00:00', 'val': 5},
{'ts': 3600000, 'val': 10},
{'ts': '1970-01-01 01:30:00Z', 'val': 15},
['1970-01-01 03:00+01:00', 20]]}),
'csv': '\n'.join(["1970-01-01T00:30:00+00:00,5",
"1970-01-01 01:00:00Z,10",
"1970-01-01 01:30:00Z,15",
"1970-01-01 03:00+01:00,20"]),
'expected': [['1970-01-01T01:30:00+01:00', '5.0'],
['1970-01-01T02:00:00+01:00', '10.0'],
['1970-01-01T02:30:00+01:00', '15.0'],
['1970-01-01T03:00:00+01:00', '20.0']]}

def testPostJSON(self): def testPostJSON(self):
self.postReadings('application/json', 'json') self.postReadings('application/json', 'json')


Expand Down Expand Up @@ -244,6 +246,7 @@ def testJSONChangeUnprivileged(self):


self.assertEqual(response.status_code, httplib.FORBIDDEN) self.assertEqual(response.status_code, httplib.FORBIDDEN)


class FormDetailTestCase(DetailTestCase):
def testFormChange(self): def testFormChange(self):
form_data = {'title': 'new title', form_data = {'title': 'new title',
'notes': 'new notes'} 'notes': 'new notes'}
Expand All @@ -265,6 +268,30 @@ def testEmptyRequest(self):
REMOTE_USER='unprivileged') REMOTE_USER='unprivileged')
self.assertEqual(response.status_code, httplib.OK) self.assertEqual(response.status_code, httplib.OK)


def testCSVUpload(self):
filename = os.path.join(os.path.dirname(__file__), 'data', 'test_data.csv')
with open(filename) as csv_file:
response = self.client.post(self.location,
data={'readings': csv_file},
REMOTE_USER='withaddperm',
HTTP_ACCEPT='text/html',
follow=False)

self.assertEqual(response.status_code, httplib.SEE_OTHER)

# Check that the time-series was updated
with open(os.path.join(settings.TIME_SERIES_PATH, 'csv', self.real_timeseries['slug'] + '.csv')) as f:
reader = csv.reader(f)
self.assertSequenceEqual(list(reader), self.readings['expected'])

# Check that the result of the upload made it into the query string
location = urlparse.urlparse(response['Location'])
query = urlparse.parse_qs(location.query)
self.assertEqual(query.get('readings.count'), ['4'])
self.assertEqual(query.get('readings.appended'), ['4'])



class CreateViewTestCase(TimeSeriesTestCase): class CreateViewTestCase(TimeSeriesTestCase):
def testGET(self): def testGET(self):
response = self.client.get('/admin/create/', response = self.client.get('/admin/create/',
Expand Down

0 comments on commit b2ba9b4

Please sign in to comment.