import datetime as dt
import json
from decimal import Decimal

from django.test import TestCase
from django.utils import timezone
from osf.models import BaseFileNode
from tests.test_websitefiles import TestFileNode

from osf.utils.datetime_aware_jsonfield import (DateTimeAwareJSONEncoder,
                                                decode_datetime_objects)


class DateTimeAwareJSONFieldTests(TestCase):
    @classmethod
    def setUpTestData(cls):
        cls.json_dict_data = dict(
            sample_date=dt.date.today(),
            nested_data=dict(
                sample_date=dt.date.today(),
                sample_datetime=timezone.now(),
                sample_decimal=Decimal('10.259')
            ),
            sample_datetime=timezone.now(),
            sample_decimal=Decimal('10.259'),
            sample_text='wut wut',
            list_of_things=[
                dict(
                    sample_date=dt.date.today(),
                    sample_datetime=timezone.now(),
                    sample_decimal=Decimal('10.259')
                ),
                dict(
                    sample_date=dt.date.today(),
                    sample_datetime=timezone.now(),
                    sample_decimal=Decimal('10.259')
                ),
                [
                    dict(
                        sample_date=dt.date.today(),
                        sample_datetime=timezone.now(),
                        sample_decimal=Decimal('10.259')
                    ),
                    dict(
                        sample_date=dt.date.today(),
                        sample_datetime=timezone.now(),
                        sample_decimal=Decimal('10.259')
                    ),
                ]
            ]
        )
        cls.json_list_data = [
            dict(
                sample_date=dt.date.today(),
                sample_datetime=timezone.now(),
                sample_decimal=Decimal('10.259')
            ),
            dict(
                sample_date=dt.date.today(),
                sample_datetime=timezone.now(),
                sample_decimal=Decimal('10.259')
            ),
        ]

    def test_dict(self):
        json_string = json.dumps(self.json_dict_data, cls=DateTimeAwareJSONEncoder)
        json_data = decode_datetime_objects(json.loads(json_string))
        assert json_data == self.json_dict_data, 'Nope'

    def test_list(self):
        json_string = json.dumps(self.json_list_data, cls=DateTimeAwareJSONEncoder)
        json_data = decode_datetime_objects(json.loads(json_string))
        assert json_data == self.json_list_data, 'Nope'

    def test_list_field(self):
        l = TestFileNode.objects.create(history=self.json_list_data)
        iden = l.id
        assert BaseFileNode.objects.get(id=iden).history == self.json_list_data

    def test_dict_field(self):
        d = TestFileNode.objects.create(history=self.json_dict_data)
        iden = d.id
        assert BaseFileNode.objects.get(id=iden).history == self.json_dict_data

@@ -159,16 +159,63 @@
 ileNode%0A
+from osf_tests.factories import ProjectFactory%0A
 from tes
@@ -495,16 +495,55 @@
 a(cls):%0A
+        cls.project = ProjectFactory()%0A
         
@@ -2787,24 +2787,45 @@
 on_list_data
+, target=self.project
 )%0A        id
@@ -3013,16 +3013,37 @@
 ict_data
+, target=self.project
 )%0A      

make sure test basefilenodes have targets