from os import unlink, rmdir
from os.path import join
from glob import glob
import unittest

from django.test import TestCase
from django.conf import settings
from django.contrib.auth.models import User
from django.core.files.base import ContentFile
from test_app.models import MyModel
from jsonate import jsonate
import json

def destroy_media_folder(folder):
    path = join(settings.MEDIA_ROOT, folder)
    [unlink(f) for f in glob(join(path, "*"))]
    try: rmdir(path)
    except: pass
            
class JsonateTests(TestCase):        
    maxDiff = 10**4
    def setUp(self):
        destroy_media_folder("files")
        destroy_media_folder("images")
        
        self.user = User.objects.create_user("asdf", 'asdf@example.com', "password")
        self.model = MyModel(
                             foreign_key=self.user
                             )
        self.model.file_field.save("text_file.txt", ContentFile("Any Old Content"))
        self.model.image_field.save("image_file.wbm", ContentFile('\x00\x00\x01\x01\x80'))
        self.model.save()
    
    def tearDown(self):
        destroy_media_folder("files")
        destroy_media_folder("images")
    
    def assertJsonEqual(self, obj1, obj2, *args, **kwargs):
        obj1 = json.loads(obj1)
        
        if isinstance(obj2, basestring):
            obj2 = json.loads(obj2)
        
        self.assertEqual(obj1, obj2, *args, **kwargs)
    
    def test_basic_serialization(self):
        mymodel_data = {
            u"float_field": 32.25,
            u"normal_field1": u"field1",
            u"normal_field2": u"field2",
            u"boolean_field": True,
            u"null_field": None,
            u"decimal_field": 32.25,
            u"foreign_key": 1,
            u"datetime_field": u"2011-01-11T11:11:11",
            u"image_field": u"images/image_file.wbm",
            u"date_field": u"2011-01-11",
            u"id": 1,
            u"file_field": u"files/text_file.txt"
        }
        self.assertJsonEqual(jsonate(self.model), mymodel_data)
        self.assertJsonEqual(jsonate(MyModel.objects.all()), [mymodel_data])
        
        user_data = [{
            "username": 'asdf',
            "password": self.user.password
        }]
        self.assertJsonEqual(jsonate(User.objects.values("username", "password")), user_data)

from django.core import management

if __name__ == "__main__":
    management.call_command("migrate")

    unittest.main()
@@ -2140,14 +2140,21 @@
 data
+_values
  = %5B%7B%0A
-
     
@@ -2256,32 +2256,45 @@
 assertJsonEqual(
+%0A            
 jsonate(User.obj
@@ -2330,26 +2330,427 @@
 word%22)),
- user_data
+%0A            user_data_values%0A        )%0A%0A        user_data_values_list = %5B%5B'asdf'%5D%5D%0A        self.assertJsonEqual(%0A            jsonate(User.objects.values_list(%22username%22)),%0A            user_data_values_list%0A        )%0A%0A        user_data_values_list_flat = %5B'asdf'%5D%0A        self.assertJsonEqual(%0A            jsonate(User.objects.values_list(%22username%22, flat=True)),%0A            user_data_values_list_flat%0A        
 )%0A%0Afrom 

test values_list and values_list(..., flat=True)