<s> from __future__ import absolute_import , unicode_literals <EOL> from collections import namedtuple <EOL> from datetime import date <EOL> from django . db import connection <EOL> from django . test import TestCase <EOL> from django . test . utils import override_settings <EOL> from django_pg import models <EOL> from django_pg . models . fields . composite import CompositeField <EOL> from tests . composite . fields import Monarch , Book , Item <EOL> from tests . composite . models import Monarchy , Author , Character <EOL> class CompositeTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> Monarchy . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> ruler = Monarch ( title = '<STR_LIT>' , name = '<STR_LIT>' , suffix = <NUM_LIT:2> ) , <EOL> ) <EOL> Monarchy . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> ruler = Monarch ( "<STR_LIT>" , '<STR_LIT>' , <NUM_LIT:1> ) , <EOL> ) <EOL> Monarchy . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> ruler = ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> ) , <EOL> ) <EOL> Monarchy . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> ruler = { <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT:2>' , <EOL> } , <EOL> ) <EOL> def test_regular_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( monarchy . ruler . name , '<STR_LIT>' ) <EOL> self . assertEqual ( monarchy . ruler . suffix , <NUM_LIT:2> ) <EOL> def test_composite_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy . objects . get ( ruler = Monarch ( <EOL> title = '<STR_LIT>' , <EOL> name = '<STR_LIT>' , <EOL> suffix = <NUM_LIT:2> , <EOL> ) ) <EOL> self . assertEqual ( monarchy . name , '<STR_LIT>' ) <EOL> def test_composite_lookup_tuple ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy . objects . get ( ruler = ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> ) ) <EOL> self . assertEqual ( monarchy . name , '<STR_LIT>' ) <EOL> def test_composite_lookup_dict ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy . objects . get ( ruler = { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT:title>' : '<STR_LIT>' , <EOL> } ) <EOL> self . assertEqual ( monarchy . name , '<STR_LIT>' ) <EOL> def test_quoted_value ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( monarchy . ruler . title , "<STR_LIT>" ) <EOL> def test_invalid_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> Monarchy . objects . get ( ruler__gt = <NUM_LIT:5> ) <EOL> def test_empty_composite ( self ) : <EOL> """<STR_LIT>""" <EOL> monarchy = Monarchy ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( monarchy . ruler . title , '<STR_LIT>' ) <EOL> self . assertEqual ( monarchy . ruler . suffix , <NUM_LIT:0> ) <EOL> def test_invalid_composite_instance_init ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> monarch = Monarch ( '<STR_LIT>' , title = '<STR_LIT>' , name = '<STR_LIT>' , suffix = <NUM_LIT:2> ) <EOL> def test_invalid_composite_instance_key ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( KeyError ) : <EOL> monarch = Monarch ( title = '<STR_LIT>' , label = '<STR_LIT>' , suffix = <NUM_LIT:2> ) <EOL> def test_related_field_raises ( self ) : <EOL> """<STR_LIT>""" <EOL> with self . assertRaises ( TypeError ) : <EOL> class CitizenField ( models . CompositeField ) : <EOL> first_name = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> last_name = models . CharField ( max_length = <NUM_LIT:30> ) <EOL> resident_of = models . ForeignKey ( Monarchy ) <EOL> with self . assertRaises ( TypeError ) : <EOL> class CitizenField ( models . CompositeField ) : <EOL> first_name = models . CharField ( max_length = <NUM_LIT:20> ) <EOL> last_name = models . CharField ( max_length = <NUM_LIT:30> ) <EOL> authors = models . ManyToManyField ( Author ) <EOL> class ArrayCompositeTestCase ( TestCase ) : <EOL> def setUp ( self ) : <EOL> """<STR_LIT>""" <EOL> Author . objects . create ( <EOL> birthday = date ( <NUM_LIT> , <NUM_LIT:11> , <NUM_LIT> ) , <EOL> books = [ <EOL> Book ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> Book ( title = '<STR_LIT>' , pages = <NUM_LIT> ) , <EOL> ( '<STR_LIT>' , <NUM_LIT> ) , <EOL> { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , <EOL> ] , <EOL> name = '<STR_LIT>' , <EOL> sex = '<STR_LIT>' , <EOL> ) <EOL> Author . objects . create ( <EOL> birthday = date ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:3> ) , <EOL> books = [ <EOL> Book ( title = '<STR_LIT>' , pages = <NUM_LIT> ) , <EOL> Book ( title = '<STR_LIT>' , pages = <NUM_LIT> ) , <EOL> Book ( title = '<STR_LIT>' , pages = <NUM_LIT> ) , <EOL> ] , <EOL> name = '<STR_LIT>' , <EOL> sex = '<STR_LIT>' , <EOL> ) <EOL> def test_regular_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> author = Author . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( author . sex , '<STR_LIT>' ) <EOL> self . assertEqual ( len ( author . books ) , <NUM_LIT:3> ) <EOL> def test_composite_containment_lookup_single ( self ) : <EOL> """<STR_LIT>""" <EOL> book = Book ( '<STR_LIT>' , <NUM_LIT> ) <EOL> author = Author . objects . get ( books__contains = book ) <EOL> self . assertEqual ( author . name , '<STR_LIT>' ) <EOL> def test_composite_containment_lookup_multi ( self ) : <EOL> """<STR_LIT>""" <EOL> books = [ <EOL> Book ( title = '<STR_LIT>' , pages = <NUM_LIT> ) , <EOL> { '<STR_LIT:title>' : '<STR_LIT>' , '<STR_LIT>' : <NUM_LIT> } , <EOL> ] <EOL> author = Author . objects . get ( books__contains = books ) <EOL> self . assertEqual ( author . name , '<STR_LIT>' ) <EOL> class NestedCompositeSuite ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def setUp ( self ) : <EOL> Character . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> items = [ <EOL> Item ( <EOL> name = '<STR_LIT>' , <EOL> acquired_in = Book ( <EOL> title = '<STR_LIT>' , <EOL> pages = <NUM_LIT> , <EOL> ) <EOL> ) <EOL> ] <EOL> ) <EOL> Character . objects . create ( <EOL> name = '<STR_LIT>' , <EOL> items = [ <EOL> Item ( name = '<STR_LIT>' , acquired_in = ( '<STR_LIT>' , <NUM_LIT> ) ) , <EOL> ] <EOL> ) <EOL> def test_nested_access ( self ) : <EOL> """<STR_LIT>""" <EOL> character = Character . objects . get ( name = '<STR_LIT>' ) <EOL> self . assertEqual ( character . items [ <NUM_LIT:0> ] . acquired_in . title , '<STR_LIT>' ) <EOL> def test_nested_lookup ( self ) : <EOL> """<STR_LIT>""" <EOL> character = Character . objects . get ( <EOL> items = [ ( '<STR_LIT>' , ( '<STR_LIT>' , <NUM_LIT> ) ) ] , <EOL> ) <EOL> self . assertEqual ( character . name , '<STR_LIT>' ) <EOL> class CreateTypeSuite ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def assertContains ( self , haystack , needle ) : <EOL> """<STR_LIT>""" <EOL> assert needle in haystack , '<STR_LIT:\n>' . join ( ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> needle , <EOL> '<STR_LIT>' , <EOL> haystack , <EOL> ) ) <EOL> def test_simple_composite_sql ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . db import connection <EOL> field = Monarchy . _meta . get_field ( '<STR_LIT>' ) <EOL> sql = field . create_type_sql ( connection ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> def test_composite_type_exists ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . db import connection <EOL> field = Monarchy . _meta . get_field ( '<STR_LIT>' ) <EOL> self . assertEqual ( field . type_exists ( connection ) , True ) <EOL> def test_array_composite_sql ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . db import connection <EOL> field = Author . _meta . get_field ( '<STR_LIT>' ) <EOL> sql = field . create_type_sql ( connection ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> def test_nested_composite_sql ( self ) : <EOL> """<STR_LIT>""" <EOL> from django . db import connection <EOL> field = Character . _meta . get_field ( '<STR_LIT>' ) <EOL> sql = field . create_type_sql ( connection ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> self . assertContains ( sql , '<STR_LIT>' ) <EOL> class SupportSuite ( TestCase ) : <EOL> """<STR_LIT>""" <EOL> def test_field_names ( self ) : <EOL> """<STR_LIT>""" <EOL> field = Monarchy . _meta . get_field ( '<STR_LIT>' ) <EOL> self . assertEqual ( field . field_names , [ '<STR_LIT:title>' , '<STR_LIT:name>' , '<STR_LIT>' ] ) <EOL> def test_composite_instance_repr ( self ) : <EOL> monarch_tuple_type = namedtuple ( <EOL> typename = '<STR_LIT>' , <EOL> field_names = [ '<STR_LIT:title>' , '<STR_LIT:name>' , '<STR_LIT>' ] , <EOL> ) <EOL> monarch = Monarch ( title = '<STR_LIT>' , name = '<STR_LIT>' , suffix = <NUM_LIT:2> ) <EOL> monarch_tuple = monarch_tuple_type ( '<STR_LIT>' , '<STR_LIT>' , <NUM_LIT:2> ) <EOL> self . assertEqual ( repr ( monarch ) , repr ( monarch_tuple ) ) <EOL> def test_create_type_dummy ( self ) : <EOL> class FakeConnection ( object ) : <EOL> vendor = '<STR_LIT>' <EOL> self . assertEqual ( CompositeField . create_type ( FakeConnection ( ) ) , None ) <EOL> def test_register_composite_dummy ( self ) : <EOL> class FakeConnection ( object ) : <EOL> vendor = '<STR_LIT>' <EOL> self . assertEqual ( CompositeField . register_composite ( FakeConnection ( ) ) , <EOL> None ) </s>
