<s> from __future__ import unicode_literals , division <EOL> import inspect <EOL> from json_field . fields import JSON_DECODE_ERROR <EOL> from test_project . app . models import Test <EOL> from test_project . app . forms import TestForm , OptionalForm , EvalForm , ModelForm <EOL> from django . test import TestCase <EOL> from django . db . utils import IntegrityError <EOL> import json <EOL> import datetime <EOL> from decimal import Decimal <EOL> try : <EOL> from django . utils import unittest <EOL> except ImportError : <EOL> import unittest <EOL> class JSONFieldTest ( TestCase ) : <EOL> def test_simple ( self ) : <EOL> t1 = Test . objects . create ( json = <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> t3 = Test . objects . create ( json = [ <NUM_LIT> ] ) <EOL> self . assertEqual ( [ <NUM_LIT> ] , Test . objects . get ( pk = t3 . pk ) . json ) <EOL> t4 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( [ <NUM_LIT> ] , Test . objects . get ( pk = t4 . pk ) . json ) <EOL> t5 = Test . objects . create ( json = { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , Test . objects . get ( pk = t5 . pk ) . json ) <EOL> t6 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , Test . objects . get ( pk = t6 . pk ) . json ) <EOL> t7 = Test . objects . create ( json = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> t7 . json = { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , t7 . json ) <EOL> t8 = Test . objects . get ( pk = t7 . pk ) <EOL> t8 . json = { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , t8 . json ) <EOL> def test_eager ( self ) : <EOL> t1 = Test . objects . create ( json_eager = <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , Test . objects . get ( pk = t1 . pk ) . json_eager ) <EOL> t2 = Test . objects . create ( json_eager = '<STR_LIT>' ) <EOL> self . assertEqual ( <NUM_LIT> , Test . objects . get ( pk = t2 . pk ) . json_eager ) <EOL> t3 = Test . objects . create ( json_eager = [ <NUM_LIT> ] ) <EOL> self . assertEqual ( [ <NUM_LIT> ] , Test . objects . get ( pk = t3 . pk ) . json_eager ) <EOL> t4 = Test . objects . create ( json_eager = '<STR_LIT>' ) <EOL> self . assertEqual ( [ <NUM_LIT> ] , Test . objects . get ( pk = t4 . pk ) . json_eager ) <EOL> t5 = Test . objects . create ( json_eager = { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , Test . objects . get ( pk = t5 . pk ) . json_eager ) <EOL> t6 = Test . objects . create ( json_eager = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] } , Test . objects . get ( pk = t6 . pk ) . json_eager ) <EOL> t7 = Test . objects . create ( json_eager = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> t7 . json_eager = { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , t7 . json_eager ) <EOL> t8 = Test . objects . get ( pk = t7 . pk ) <EOL> t8 . json_eager = { '<STR_LIT>' : <NUM_LIT> } <EOL> self . assertEqual ( { '<STR_LIT>' : <NUM_LIT> } , t8 . json_eager ) <EOL> def test_null ( self ) : <EOL> t1 = Test . objects . create ( json = None ) <EOL> self . assertEqual ( None , t1 . json ) <EOL> self . assertEqual ( '<STR_LIT:null>' , t1 . get_json_json ( ) ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , t2 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t2 . get_json_json ( ) ) <EOL> t3 = Test . objects . create ( json_null = None ) <EOL> self . assertEqual ( None , t3 . json_null ) <EOL> self . assertEqual ( '<STR_LIT:null>' , t3 . get_json_null_json ( ) ) <EOL> t4 = Test . objects . create ( json_null = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , t4 . json_null ) <EOL> self . assertEqual ( '<STR_LIT>' , t4 . get_json_null_json ( ) ) <EOL> def test_decimal ( self ) : <EOL> t1 = Test . objects . create ( json = <NUM_LIT> ) <EOL> self . assertEqual ( Decimal ( '<STR_LIT>' ) , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = Decimal ( <NUM_LIT> ) ) <EOL> self . assertEqual ( str ( Decimal ( <NUM_LIT> ) ) , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> t3 = Test . objects . create ( json = { '<STR_LIT:test>' : [ { '<STR_LIT:test>' : Decimal ( <NUM_LIT> ) } ] } ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ { '<STR_LIT:test>' : str ( Decimal ( <NUM_LIT> ) ) } ] } , Test . objects . get ( pk = t3 . pk ) . json ) <EOL> def test_time ( self ) : <EOL> now = datetime . datetime . now ( ) . time ( ) <EOL> t1 = Test . objects . create ( json = now ) <EOL> now_rounded = now . replace ( microsecond = ( int ( now . microsecond ) // <NUM_LIT:1000> ) * <NUM_LIT:1000> ) <EOL> self . assertEqual ( now_rounded , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = { '<STR_LIT:time>' : [ now ] } ) <EOL> self . assertEqual ( { '<STR_LIT:time>' : [ now_rounded ] } , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> def test_date ( self ) : <EOL> today = datetime . date . today ( ) <EOL> t1 = Test . objects . create ( json = today ) <EOL> self . assertEqual ( today , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = { '<STR_LIT>' : today } ) <EOL> self . assertEqual ( { '<STR_LIT>' : today } , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> def test_datetime ( self ) : <EOL> now = datetime . datetime . now ( ) <EOL> t1 = Test . objects . create ( json = now ) <EOL> now_rounded = now . replace ( microsecond = ( int ( now . microsecond ) // <NUM_LIT:1000> ) * <NUM_LIT:1000> ) <EOL> self . assertEqual ( now_rounded , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = { '<STR_LIT:test>' : [ { '<STR_LIT:test>' : now } ] } ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : [ { '<STR_LIT:test>' : now_rounded } ] } , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> def test_numerical_strings ( self ) : <EOL> t1 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> def test_datelike_strings ( self ) : <EOL> t1 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:title>' : '<STR_LIT>' } , Test . objects . get ( pk = t1 . pk ) . json ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:title>' : '<STR_LIT>' } , Test . objects . get ( pk = t2 . pk ) . json ) <EOL> t3 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:title>' : '<STR_LIT>' } , Test . objects . get ( pk = t3 . pk ) . json ) <EOL> t4 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( { '<STR_LIT:title>' : '<STR_LIT>' } , Test . objects . get ( pk = t4 . pk ) . json ) <EOL> def test_get_set_json ( self ) : <EOL> t1 = Test . objects . create ( json = { '<STR_LIT:test>' : <NUM_LIT> } ) <EOL> self . assertEqual ( { '<STR_LIT:test>' : <NUM_LIT> } , t1 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t1 . get_json_json ( ) ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , t2 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t2 . get_json_json ( ) ) <EOL> self . assertEqual ( None , t2 . json_null ) <EOL> self . assertEqual ( '<STR_LIT:null>' , t2 . get_json_null_json ( ) ) <EOL> t3 = Test . objects . create ( json = [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] ) <EOL> self . assertEqual ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> ] , t3 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t3 . get_json_json ( ) ) <EOL> t3 . set_json_json ( '<STR_LIT>' ) <EOL> self . assertEqual ( [ <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> , <NUM_LIT:5> ] , t3 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t3 . get_json_json ( ) ) <EOL> t3 . set_json_json ( <NUM_LIT> ) <EOL> self . assertEqual ( <NUM_LIT> , t3 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t3 . get_json_json ( ) ) <EOL> def test_strings ( self ) : <EOL> t1 = Test . objects . create ( json = '<STR_LIT:a>' ) <EOL> self . assertEqual ( '<STR_LIT:a>' , t1 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t1 . get_json_json ( ) ) <EOL> t2 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT:a>' , t2 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t2 . get_json_json ( ) ) <EOL> t3 = Test . objects . create ( json_null = '<STR_LIT:a>' ) <EOL> self . assertEqual ( '<STR_LIT:a>' , t3 . json_null ) <EOL> self . assertEqual ( '<STR_LIT>' , t3 . get_json_null_json ( ) ) <EOL> t4 = Test . objects . create ( json = '<STR_LIT>' ) <EOL> self . assertEqual ( '<STR_LIT>' , t4 . json ) <EOL> self . assertEqual ( '<STR_LIT>' , t4 . get_json_json ( ) ) <EOL> def test_formfield ( self ) : <EOL> data = { '<STR_LIT>' : '<STR_LIT>' } <EOL> f1 = TestForm ( data ) <EOL> self . assertTrue ( f1 . is_valid ( ) ) <EOL> self . assertEqual ( f1 . cleaned_data , { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> } } ) <EOL> f2 = TestForm ( { } ) <EOL> self . assertFalse ( f2 . is_valid ( ) ) <EOL> f3 = OptionalForm ( { } ) <EOL> self . assertTrue ( f3 . is_valid ( ) ) <EOL> self . assertEqual ( f3 . cleaned_data , { '<STR_LIT>' : None } ) <EOL> f4 = TestForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( f4 . is_valid ( ) ) <EOL> f5 = EvalForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertTrue ( f5 . is_valid ( ) ) <EOL> f6 = ModelForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( f6 . is_valid ( ) ) <EOL> f7 = ModelForm ( { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> self . assertFalse ( f7 . is_valid ( ) ) <EOL> def test_creator_plays_nice_with_module_inspect ( self ) : <EOL> """<STR_LIT>""" <EOL> data = dict ( inspect . getmembers ( Test ) ) <EOL> self . assertIn ( '<STR_LIT>' , data ) </s>
