<s> """<STR_LIT>""" <EOL> import sys <EOL> import os <EOL> import tempfile <EOL> import pytest <EOL> from sqlalchemy import create_engine , MetaData , exc <EOL> from sqlalchemy import Table , Column , Integer , String <EOL> from sqlalchemy import ForeignKeyConstraint , UniqueConstraint , CheckConstraint <EOL> sys . path . insert ( <NUM_LIT:0> , os . path . dirname ( os . path . dirname ( os . path . dirname ( os . path . abspath ( __file__ ) ) ) ) ) <EOL> import gristle . simplesql as simplesql <EOL> class TestSurrogateKeyTable ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . fqdb_name = os . path . join ( self . tempdir , '<STR_LIT>' ) <EOL> self . db = create_engine ( '<STR_LIT>' % self . fqdb_name , <EOL> logging_name = '<STR_LIT>' ) <EOL> self . db . echo = False <EOL> self . conn = self . db . connect ( ) <EOL> self . metadata = MetaData ( self . db ) <EOL> self . person_tools = PersonTools ( self . metadata , self . db ) <EOL> self . person = self . person_tools . table_create ( ) <EOL> self . metadata . create_all ( ) <EOL> def teardown_method ( self , method ) : <EOL> os . remove ( os . path . join ( self . tempdir , '<STR_LIT>' ) ) <EOL> os . rmdir ( self . tempdir ) <EOL> def test_setter_and_lister_happypath ( self ) : <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> people = self . person_tools . lister ( ) <EOL> assert len ( people ) == <NUM_LIT:3> <EOL> for person in people : <EOL> assert len ( person ) == <NUM_LIT:3> <EOL> assert person . id > <NUM_LIT:0> <EOL> assert person . id < <NUM_LIT:4> <EOL> assert person . person_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> assert person . person_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_setter_null_constraint_violation ( self ) : <EOL> with pytest . raises ( exc . IntegrityError ) : <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' ) <EOL> def test_updating_without_id ( self ) : <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:0> <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:1> <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:1> <EOL> for person in self . person_tools . lister ( ) : <EOL> assert person . person_desc == '<STR_LIT>' <EOL> def test_updating_with_id ( self ) : <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:0> <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:1> <EOL> self . person_tools . setter ( id = <NUM_LIT:1> , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:1> <EOL> for person in self . person_tools . lister ( ) : <EOL> assert person . person_name == '<STR_LIT>' <EOL> assert person . person_desc == '<STR_LIT>' <EOL> def test_get_unique_constraints ( self ) : <EOL> constraints = self . person_tools . _get_unique_constraints ( ) <EOL> assert len ( constraints ) == <NUM_LIT:1> <EOL> assert constraints [ <NUM_LIT:0> ] == '<STR_LIT>' <EOL> def test_get_id ( self ) : <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:3> <EOL> assert self . person_tools . get_id ( person_name = '<STR_LIT>' ) == <NUM_LIT:1> <EOL> assert self . person_tools . get_id ( person_name = '<STR_LIT>' ) == <NUM_LIT:2> <EOL> assert self . person_tools . get_id ( person_name = '<STR_LIT>' ) == <NUM_LIT:3> <EOL> def test_getter ( self ) : <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:3> <EOL> assert self . person_tools . getter ( id = <NUM_LIT:1> ) . person_name == '<STR_LIT>' <EOL> assert self . person_tools . getter ( person_name = '<STR_LIT>' ) . id == <NUM_LIT:2> <EOL> with pytest . raises ( KeyError ) : <EOL> self . person_tools . getter ( person_desc = '<STR_LIT>' ) <EOL> def test_deleter ( self ) : <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> self . person_tools . setter ( person_name = '<STR_LIT>' , person_desc = '<STR_LIT>' ) <EOL> assert len ( self . person_tools . lister ( ) ) == <NUM_LIT:3> <EOL> assert self . person_tools . deleter ( person_name = '<STR_LIT>' ) == <NUM_LIT:1> <EOL> assert self . person_tools . deleter ( person_name = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . person_tools . deleter ( pet_desc = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . person_tools . deleter ( person_desc = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . person_tools . deleter ( id = <NUM_LIT:1> ) == <NUM_LIT:1> <EOL> class TestNaturalKeyTable ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . fqdb_name = os . path . join ( self . tempdir , '<STR_LIT>' ) <EOL> self . db = create_engine ( '<STR_LIT>' % self . fqdb_name , <EOL> logging_name = '<STR_LIT>' ) <EOL> self . db . echo = False <EOL> self . conn = self . db . connect ( ) <EOL> self . metadata = MetaData ( self . db ) <EOL> self . pet_tools = PetTools ( self . metadata , self . db ) <EOL> self . pet = self . pet_tools . table_create ( ) <EOL> self . metadata . create_all ( ) <EOL> def teardown_method ( self , method ) : <EOL> os . remove ( os . path . join ( self . tempdir , '<STR_LIT>' ) ) <EOL> os . rmdir ( self . tempdir ) <EOL> def test_setter_and_lister_happypath ( self ) : <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> pets = self . pet_tools . lister ( ) <EOL> assert len ( pets ) == <NUM_LIT:3> <EOL> for pet in pets : <EOL> assert len ( pet ) == <NUM_LIT:2> <EOL> assert pet . pet_name in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def test_updating_without_id ( self ) : <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:0> <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:1> <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:1> <EOL> for pet in self . pet_tools . lister ( ) : <EOL> assert pet . pet_desc == '<STR_LIT>' <EOL> def test_updating_without_pk_or_uk ( self ) : <EOL> with pytest . raises ( KeyError ) : <EOL> self . pet_tools . setter ( pet_desc = '<STR_LIT>' ) <EOL> def test_get_unique_constraints ( self ) : <EOL> constraints = self . pet_tools . _get_unique_constraints ( ) <EOL> assert len ( constraints ) == <NUM_LIT:0> <EOL> def test_get_id ( self ) : <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT:foo>' ) <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:1> <EOL> assert self . pet_tools . get_id ( pet_name = '<STR_LIT>' ) is None <EOL> def test_getter ( self ) : <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:3> <EOL> assert self . pet_tools . getter ( pet_name = '<STR_LIT>' ) . pet_desc == '<STR_LIT>' <EOL> with pytest . raises ( KeyError ) : <EOL> self . pet_tools . getter ( person_desc = '<STR_LIT>' ) <EOL> with pytest . raises ( KeyError ) : <EOL> self . pet_tools . getter ( pet_desc = '<STR_LIT>' ) <EOL> with pytest . raises ( KeyError ) : <EOL> self . pet_tools . getter ( id = <NUM_LIT:1> ) <EOL> def test_deleter ( self ) : <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> self . pet_tools . setter ( pet_name = '<STR_LIT>' , pet_desc = '<STR_LIT>' ) <EOL> assert len ( self . pet_tools . lister ( ) ) == <NUM_LIT:3> <EOL> assert self . pet_tools . deleter ( pet_name = '<STR_LIT>' ) == <NUM_LIT:1> <EOL> assert self . pet_tools . deleter ( pet_name = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . pet_tools . deleter ( person_desc = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . pet_tools . deleter ( pet_desc = '<STR_LIT>' ) == <NUM_LIT:0> <EOL> assert self . pet_tools . deleter ( id = <NUM_LIT:1> ) == <NUM_LIT:0> <EOL> class TestSurrogateKeyCheckConstraintTable ( object ) : <EOL> def setup_method ( self , method ) : <EOL> self . tempdir = tempfile . mkdtemp ( ) <EOL> self . fqdb_name = os . path . join ( self . tempdir , '<STR_LIT>' ) <EOL> self . db = create_engine ( '<STR_LIT>' % self . fqdb_name , <EOL> logging_name = '<STR_LIT>' ) <EOL> self . db . echo = False <EOL> self . conn = self . db . connect ( ) <EOL> self . metadata = MetaData ( self . db ) <EOL> self . animal_tools = AnimalTools ( self . metadata , self . db ) <EOL> self . animal = self . animal_tools . table_create ( ) <EOL> self . metadata . create_all ( ) <EOL> def teardown_method ( self , method ) : <EOL> os . remove ( os . path . join ( self . tempdir , '<STR_LIT>' ) ) <EOL> os . rmdir ( self . tempdir ) <EOL> def test_setter_and_null_constraint ( self ) : <EOL> with pytest . raises ( exc . IntegrityError ) : <EOL> self . animal_tools . setter ( animal_name = '<STR_LIT>' , animal_desc = '<STR_LIT>' ) <EOL> def test_setter_and_check_constraint ( self ) : <EOL> assert self . animal_tools . setter ( animal_name = '<STR_LIT>' , <EOL> animal_desc = '<STR_LIT>' , <EOL> animal_age = <NUM_LIT:9> ) == <NUM_LIT:1> <EOL> with pytest . raises ( exc . IntegrityError ) : <EOL> self . animal_tools . setter ( animal_name = '<STR_LIT>' , animal_desc = '<STR_LIT>' , animal_age = <NUM_LIT> ) <EOL> class PersonTools ( simplesql . TableTools ) : <EOL> """<STR_LIT>""" <EOL> def table_create ( self ) : <EOL> self . _table_name = '<STR_LIT>' <EOL> self . person = Table ( self . _table_name , <EOL> self . metadata , <EOL> Column ( '<STR_LIT:id>' , <EOL> Integer , <EOL> nullable = False , <EOL> primary_key = True ) , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT> ) , <EOL> nullable = False ) , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT:255> ) , <EOL> nullable = False ) , <EOL> UniqueConstraint ( '<STR_LIT>' , name = '<STR_LIT>' ) ) <EOL> self . _table = self . person <EOL> return self . _table <EOL> class PetTools ( simplesql . TableTools ) : <EOL> """<STR_LIT>""" <EOL> def table_create ( self ) : <EOL> self . _table_name = '<STR_LIT>' <EOL> self . pet = Table ( self . _table_name , <EOL> self . metadata , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT> ) , <EOL> nullable = False , <EOL> primary_key = True ) , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT:255> ) , <EOL> nullable = False ) ) <EOL> self . _table = self . pet <EOL> return self . _table <EOL> class AnimalTools ( simplesql . TableTools ) : <EOL> """<STR_LIT>""" <EOL> def table_create ( self ) : <EOL> self . _table_name = '<STR_LIT>' <EOL> self . animal = Table ( self . _table_name , <EOL> self . metadata , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT> ) , <EOL> nullable = False , <EOL> primary_key = True ) , <EOL> Column ( '<STR_LIT>' , <EOL> String ( <NUM_LIT:255> ) , <EOL> nullable = False ) , <EOL> Column ( '<STR_LIT>' , <EOL> Integer , <EOL> nullable = False ) , <EOL> CheckConstraint ( '<STR_LIT>' ) ) <EOL> self . _table = self . animal <EOL> return self . _table </s>
