import json

from tastypie.test import ResourceTestCase
from django.contrib.auth.models import User
from setup_helpers import TestSetupHelpers
from pandas import read_csv, notnull, to_datetime
from rhizome.models import *

class DatapointEntryResourceTest(ResourceTestCase):
    def setUp(self):
        super(DatapointEntryResourceTest, self).setUp()

        self.ts = TestSetupHelpers()
        self.lt = self.ts.create_arbitrary_location_type()
        self.o = self.ts.create_arbitrary_office()

        self.top_lvl_location = self.ts.create_arbitrary_location(self.lt.id, self.o.id)

        LocationPermission.objects.create(user_id = self.ts.user.id,\
            top_lvl_location_id = self.top_lvl_location.id)
        self.ct = CampaignType.objects.create(name='NID')
        self.it = IndicatorTag.objects.create(tag_name='Polio')

        self.c = self.ts.create_arbitrary_campaign(self.o.id, self.ct.id, self.top_lvl_location.id, self.it.id)
        self.ind = self.ts.create_arbitrary_indicator()
        self.doc = self.ts.create_arbitrary_document()
        self.ss = self.ts.create_arbitrary_ss(self.doc.id)

    def test_get(self):
        dp = DataPointEntry.objects.create(
            location_id = self.top_lvl_location.id,
            data_date = '2016-01-01',
            indicator_id = self.ind.id,
            value = 1234,
            cache_job_id = -1,
            source_submission_id = self.ss.id
        )
        data = {'campaign__in': self.ct.id, 'indicator__in': self.ind.id}
        resp = self.ts.get(self, '/api/v1/datapointentry/', data)
        self.assertHttpOK(resp)
        resp_data = self.deserialize(resp)
        self.assertEqual(len(resp_data['objects']), 1)


    def test_get_invalid_request(self):
        data = {'campaign__in': 123, 'indicator__in': 456}
        resp = self.ts.get(self, '/api/v1/datapointentry/', data)
        self.assertHttpApplicationError(resp)

    # def test_post_new_dp(self):
    #     data={
    #     'campaign_id': self.top_lvl_location.id,
    #     'indicator_id': self.ind.id,
    #     'value': 4567
    #     }

    #     resp = self.ts.post(self, '/api/v1/datapointentry/', data)
    #     print(resp)



@@ -1119,20 +1119,44 @@
 doc.id)%0A
+    # @martha plz fix.
 %0A   
+ #
  def tes
@@ -1167,24 +1167,26 @@
 t(self):%0A   
+ #
      dp = Da
@@ -1213,24 +1213,26 @@
 .create(%0A   
+ #
          loc
@@ -1263,32 +1263,34 @@
 location.id,%0A   
+ #
          data_da
@@ -1307,24 +1307,26 @@
 -01-01',%0A   
+ #
          ind
@@ -1354,24 +1354,25 @@
 id,%0A    
+#
         
 value = 
@@ -1363,16 +1363,17 @@
         
+ 
 value = 
@@ -1377,24 +1377,26 @@
  = 1234,%0A   
+ #
          cac
@@ -1410,24 +1410,26 @@
 id = -1,%0A   
+ #
          sou
@@ -1467,17 +1467,21 @@
     
+#
     
+ 
 )%0A   
+ #
     
@@ -1546,24 +1546,26 @@
 .ind.id%7D%0A   
+ #
      resp = 
@@ -1611,36 +1611,38 @@
 ry/', data)%0A    
+#
     
+ 
 self.assertHttpO
@@ -1648,24 +1648,26 @@
 OK(resp)%0A   
+ #
      resp_da
@@ -1693,24 +1693,26 @@
 ze(resp)%0A   
+ #
      self.as
@@ -1752,17 +1752,16 @@
 %5D), 1)%0A%0A
-%0A
     def 
@@ -2203,32 +2203,32 @@
 tentry/', data)%0A
+
     #     print(
@@ -2233,10 +2233,8 @@
 t(resp)%0A
-%0A%0A

fix CI