from rest_framework.test import APITestCase
from tests.testapp.models import TimeSeries
from wq.io import load_string
import json


class PandasTestCase(APITestCase):
    def setUp(self):
        data = (
            ('2014-01-01', 0.5),
            ('2014-01-02', 0.4),
            ('2014-01-03', 0.6),
            ('2014-01-04', 0.2),
            ('2014-01-05', 0.1),
        )
        for date, value in data:
            TimeSeries.objects.create(date=date, value=value)

    def test_view(self):
        response = self.client.get("/timeseries.csv")
        data = self.load_string(response)
        self.assertEqual(len(data), 5)
        self.assertEqual(data[0].value, '0.5')

    def test_view_json(self):
        response = self.client.get("/timeseries.json")
        self.assertEqual(response.accepted_media_type, "application/json")
        data = json.loads(response.content.decode('utf-8'))
        self.assertEqual(len(data), 5)
        self.assertEqual(data[0]["value"], 0.5)

    def test_viewset(self):
        response = self.client.get("/router/timeseries/.csv")
        data = self.load_string(response)
        self.assertEqual(len(data), 5)
        self.assertEqual(data[0].value, '0.5')

    def test_no_model(self):
        response = self.client.get("/nomodel.csv")
        data = self.load_string(response)
        self.assertEqual(len(data), 4)
        self.assertEqual(data[0].x, '5')

    def load_string(self, response):
        return load_string(response.content.decode('utf-8'))

@@ -123,16 +123,32 @@
 rt json%0A
+import datetime%0A
 %0A%0Aclass 
@@ -999,16 +999,555 @@
 %22%5D, 0.5)
+%0A        self.assertEqual(data%5B0%5D%5B%22date%22%5D, %222014-01-01T00:00:00.000Z%22)%0A%0A    def test_view_json_kwargs(self):%0A        response = self.client.get(%22/timeseries.json?date_format=epoch%22)%0A        self.assertEqual(response.accepted_media_type, %22application/json%22)%0A        data = json.loads(response.content.decode('utf-8'))%0A        self.assertEqual(len(data), 5)%0A        self.assertEqual(data%5B0%5D%5B%22value%22%5D, 0.5)%0A        date = datetime.datetime.utcfromtimestamp(data%5B0%5D%5B%22date%22%5D / 1000)%0A        self.assertEqual(date, datetime.datetime(2014, 1, 1))
 %0A%0A    de

test for new URL kwargs feature