<s> import datetime <EOL> from django . conf import settings <EOL> from django . core . exceptions import ValidationError <EOL> from django . db import connection <EOL> from django . test import TestCase <EOL> from django . utils import unittest <EOL> from models import ( CustomPKModel , UniqueTogetherModel , UniqueFieldsModel , <EOL> UniqueForDateModel , ModelToValidate , Post , FlexibleDatePost ) <EOL> class GetUniqueCheckTests ( unittest . TestCase ) : <EOL> def test_unique_fields_get_collected ( self ) : <EOL> m = UniqueFieldsModel ( ) <EOL> self . assertEqual ( <EOL> ( [ ( UniqueFieldsModel , ( '<STR_LIT:id>' , ) ) , <EOL> ( UniqueFieldsModel , ( '<STR_LIT>' , ) ) , <EOL> ( UniqueFieldsModel , ( '<STR_LIT>' , ) ) ] , <EOL> [ ] ) , <EOL> m . _get_unique_checks ( ) <EOL> ) <EOL> def test_unique_together_gets_picked_up_and_converted_to_tuple ( self ) : <EOL> m = UniqueTogetherModel ( ) <EOL> self . assertEqual ( <EOL> ( [ ( UniqueTogetherModel , ( '<STR_LIT>' , '<STR_LIT>' , ) ) , <EOL> ( UniqueTogetherModel , ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> ( UniqueTogetherModel , ( '<STR_LIT:id>' , ) ) , ] , <EOL> [ ] ) , <EOL> m . _get_unique_checks ( ) <EOL> ) <EOL> def test_primary_key_is_considered_unique ( self ) : <EOL> m = CustomPKModel ( ) <EOL> self . assertEqual ( ( [ ( CustomPKModel , ( '<STR_LIT>' , ) ) ] , [ ] ) , m . _get_unique_checks ( ) ) <EOL> def test_unique_for_date_gets_picked_up ( self ) : <EOL> m = UniqueForDateModel ( ) <EOL> self . assertEqual ( ( <EOL> [ ( UniqueForDateModel , ( '<STR_LIT:id>' , ) ) ] , <EOL> [ ( UniqueForDateModel , '<STR_LIT:date>' , '<STR_LIT:count>' , '<STR_LIT>' ) , <EOL> ( UniqueForDateModel , '<STR_LIT>' , '<STR_LIT:count>' , '<STR_LIT>' ) , <EOL> ( UniqueForDateModel , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> ) , m . _get_unique_checks ( ) <EOL> ) <EOL> def test_unique_for_date_exclusion ( self ) : <EOL> m = UniqueForDateModel ( ) <EOL> self . assertEqual ( ( <EOL> [ ( UniqueForDateModel , ( '<STR_LIT:id>' , ) ) ] , <EOL> [ ( UniqueForDateModel , '<STR_LIT>' , '<STR_LIT:count>' , '<STR_LIT>' ) , <EOL> ( UniqueForDateModel , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ] <EOL> ) , m . _get_unique_checks ( exclude = '<STR_LIT>' ) <EOL> ) <EOL> class PerformUniqueChecksTest ( TestCase ) : <EOL> def test_primary_key_unique_check_not_performed_when_adding_and_pk_not_specified ( self ) : <EOL> def test ( ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' ) <EOL> setattr ( mtv , '<STR_LIT>' , True ) <EOL> mtv . full_clean ( ) <EOL> self . assertNumQueries ( <NUM_LIT:0> , test ) <EOL> def test_primary_key_unique_check_performed_when_adding_and_pk_specified ( self ) : <EOL> def test ( ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' , id = <NUM_LIT> ) <EOL> setattr ( mtv , '<STR_LIT>' , True ) <EOL> mtv . full_clean ( ) <EOL> self . assertNumQueries ( <NUM_LIT:1> , test ) <EOL> def test_primary_key_unique_check_not_performed_when_not_adding ( self ) : <EOL> def test ( ) : <EOL> mtv = ModelToValidate ( number = <NUM_LIT:10> , name = '<STR_LIT>' ) <EOL> mtv . full_clean ( ) <EOL> self . assertNumQueries ( <NUM_LIT:0> , test ) <EOL> def test_unique_for_date ( self ) : <EOL> p1 = Post . objects . create ( title = "<STR_LIT>" , <EOL> slug = "<STR_LIT>" , subtitle = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> p = Post ( title = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . assertEqual ( e . message_dict , { '<STR_LIT:title>' : [ u'<STR_LIT>' ] } ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> p = Post ( title = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> p . full_clean ( ) <EOL> p = Post ( title = "<STR_LIT>" , posted = datetime . datetime ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:4> ) ) <EOL> p . full_clean ( ) <EOL> p = Post ( slug = "<STR_LIT>" , posted = datetime . datetime ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . assertEqual ( e . message_dict , { '<STR_LIT>' : [ u'<STR_LIT>' ] } ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> p = Post ( subtitle = "<STR_LIT>" , posted = datetime . datetime ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:30> ) ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . assertEqual ( e . message_dict , { '<STR_LIT>' : [ u'<STR_LIT>' ] } ) <EOL> else : <EOL> self . fail ( '<STR_LIT>' ) <EOL> p = Post ( title = "<STR_LIT>" ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . assertEqual ( e . message_dict , { '<STR_LIT>' : [ u'<STR_LIT>' ] } ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> def test_unique_for_date_with_nullable_date ( self ) : <EOL> p1 = FlexibleDatePost . objects . create ( title = "<STR_LIT>" , <EOL> slug = "<STR_LIT>" , subtitle = "<STR_LIT>" , posted = datetime . date ( <NUM_LIT> , <NUM_LIT:9> , <NUM_LIT:3> ) ) <EOL> p = FlexibleDatePost ( title = "<STR_LIT>" ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . fail ( "<STR_LIT>" ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> p = FlexibleDatePost ( slug = "<STR_LIT>" ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . fail ( "<STR_LIT>" ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) <EOL> p = FlexibleDatePost ( subtitle = "<STR_LIT>" ) <EOL> try : <EOL> p . full_clean ( ) <EOL> except ValidationError , e : <EOL> self . fail ( "<STR_LIT>" ) <EOL> except : <EOL> self . fail ( "<STR_LIT>" ) </s>
