<s> import datetime <EOL> import json <EOL> from django import forms <EOL> from django . core import exceptions , serializers <EOL> from django . db import models <EOL> from django . test import SimpleTestCase , TestCase <EOL> from . models import DurationModel , NullDurationModel <EOL> class TestSaveLoad ( TestCase ) : <EOL> def test_simple_roundtrip ( self ) : <EOL> duration = datetime . timedelta ( days = <NUM_LIT> , seconds = <NUM_LIT> , microseconds = <NUM_LIT> ) <EOL> DurationModel . objects . create ( field = duration ) <EOL> loaded = DurationModel . objects . get ( ) <EOL> self . assertEqual ( loaded . field , duration ) <EOL> def test_create_empty ( self ) : <EOL> NullDurationModel . objects . create ( ) <EOL> loaded = NullDurationModel . objects . get ( ) <EOL> self . assertEqual ( loaded . field , None ) <EOL> def test_fractional_seconds ( self ) : <EOL> value = datetime . timedelta ( seconds = <NUM_LIT> ) <EOL> d = DurationModel . objects . create ( field = value ) <EOL> d . refresh_from_db ( ) <EOL> self . assertEqual ( d . field , value ) <EOL> class TestQuerying ( TestCase ) : <EOL> @ classmethod <EOL> def setUpTestData ( cls ) : <EOL> cls . objs = [ <EOL> DurationModel . objects . create ( field = datetime . timedelta ( days = <NUM_LIT:1> ) ) , <EOL> DurationModel . objects . create ( field = datetime . timedelta ( seconds = <NUM_LIT:1> ) ) , <EOL> DurationModel . objects . create ( field = datetime . timedelta ( seconds = - <NUM_LIT:1> ) ) , <EOL> ] <EOL> def test_exact ( self ) : <EOL> self . assertSequenceEqual ( <EOL> DurationModel . objects . filter ( field = datetime . timedelta ( days = <NUM_LIT:1> ) ) , <EOL> [ self . objs [ <NUM_LIT:0> ] ] <EOL> ) <EOL> def test_gt ( self ) : <EOL> self . assertSequenceEqual ( <EOL> DurationModel . objects . filter ( field__gt = datetime . timedelta ( days = <NUM_LIT:0> ) ) , <EOL> [ self . objs [ <NUM_LIT:0> ] , self . objs [ <NUM_LIT:1> ] ] <EOL> ) <EOL> class TestSerialization ( SimpleTestCase ) : <EOL> test_data = '<STR_LIT>' <EOL> def test_dumping ( self ) : <EOL> instance = DurationModel ( field = datetime . timedelta ( days = <NUM_LIT:1> , hours = <NUM_LIT:1> ) ) <EOL> data = serializers . serialize ( '<STR_LIT>' , [ instance ] ) <EOL> self . assertEqual ( json . loads ( data ) , json . loads ( self . test_data ) ) <EOL> def test_loading ( self ) : <EOL> instance = list ( serializers . deserialize ( '<STR_LIT>' , self . test_data ) ) [ <NUM_LIT:0> ] . object <EOL> self . assertEqual ( instance . field , datetime . timedelta ( days = <NUM_LIT:1> , hours = <NUM_LIT:1> ) ) <EOL> class TestValidation ( SimpleTestCase ) : <EOL> def test_invalid_string ( self ) : <EOL> field = models . DurationField ( ) <EOL> with self . assertRaises ( exceptions . ValidationError ) as cm : <EOL> field . clean ( '<STR_LIT>' , None ) <EOL> self . assertEqual ( cm . exception . code , '<STR_LIT>' ) <EOL> self . assertEqual ( <EOL> cm . exception . message % cm . exception . params , <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> class TestFormField ( SimpleTestCase ) : <EOL> def test_formfield ( self ) : <EOL> field = models . DurationField ( ) <EOL> self . assertIsInstance ( field . formfield ( ) , forms . DurationField ) </s>
