def setUp(self): class Knight(Object): title = f.CharacterVarying( length = 5, null = False, default = 'Sir' ) name = f.CharacterVarying(length = 20, null = False) nickname = f.CharacterVarying(length = 20, null = True) score = f.Integer() self.Knight = Knight set_context(get_test_conn()) start_session() # Knight.create_table() for ktup in [ ('Sir', 'Galahad', 'The Pure', 10), ('King', 'Arthur', None, 20), ('Sir', 'Robin', 'The Brave', 30), ('Sir', 'Lancelot', None, 40), ]: title, name, nickname, score = ktup k = Knight( title = title, name = name, nickname = nickname, score = score ) add(k) commit() start_session()
def setUp(self): class Knight(Object): set_context(get_test_conn()) start_session() title = f.CharacterVarying( length = 5, null = False, default = 'Sir' ) name = f.CharacterVarying(length = 20, null = False) nickname = f.CharacterVarying(length = 20, null = True) score = f.Integer() self.Knight = Knight self.root = Root() Traverser(class_=self.Knight, field='name', session=get_session()).mount(self.root, 'knights') for ktup in [ ('Sir', 'Galahad', 'The Pure', 10), ('King', 'Arthur', None, 20), ('Sir', 'Robin', 'The Brave', 30), ('Sir', 'Lancelot', None, 40), ]: title, name, nickname, score = ktup k = Knight( title = title, name = name, nickname = nickname, score = score ) add(k) commit() start_session()
def test_deft(self): """Test of default value""" k = self.Knight(name = 'Doris', nickname = 'The Hamster', score = 999) add(k) commit() start_session() k_got = self.Knight.get(self.Knight.name == 'Doris') self.assertEqual(k_got.title, 'Sir')
def test_custom_name(self): """Creating a table with custom table_name""" class Spam(Object): __table_name__ = 'eggs' meal = f.CharacterVarying(length = 10) add(Spam(meal = 'bacon')) commit() c = get_connection() cur = c.cursor() cur.execute('SELECT id, meal FROM eggs WHERE 1 = 1;') self.assertEqual(cur.fetchone(), (1, 'bacon'))
def test_no_connection(self): """Test trying to use nonexistent ctx raises exc""" self.assertRaises(CtxError, lambda: get_connection()) self.assertRaises(CtxError, lambda: get_cursor()) self.assertRaises(CtxError, lambda: get_session()) self.assertRaises(CtxError, lambda: commit()) self.assertRaises(CtxError, lambda: add())
def test_unhandled_exception_getting(self): """Testing the exception from engine that can't be handled (while get()ting)""" conn = get_test_conn() set_context(conn) start_session() class Spam(Object): eggs = CharacterVarying(20) add(Spam(eggs='abc')) commit() start_session() class Spam(Object): bacon = CharacterVarying(20) try: spam = Spam.get(1) except ProgrammingError as err: self.assertEqual(err.pgcode, UNDEFINED_COLUMN)
def test_table_mismatch(self): """Testing unhandled errors when flushing""" conn = get_test_conn() cur = conn.cursor() cur.execute( """CREATE TABLE spams ( id integer, eggs character varying(10), PRIMARY KEY (id) );""") conn.commit() set_context(conn) start_session() class Spam(Object): bacon = CharacterVarying(length = 10) add(Spam(bacon = 'sausage')) self.assertRaises(ProgrammingError, commit)
def setUp(self): class Knight(Object): name = f.CharacterVarying(length = 20, null = False) self.Knight = Knight set_context(get_test_conn()) start_session() for knight_name in [ 'Agravain', 'Bagdemagus', 'Bedivere', 'Bors', 'Breunor', 'Calogrenant', 'Caradoc', 'Dagonet', 'Dinadan', 'Gaheris', 'Galahad', 'Gareth', 'Gawain', 'Geraint', 'Griflet', 'Hector de Maris', 'Kay', 'Lamorak', 'Lancelot', 'Leodegrance', 'Lionel', 'Lucan', 'Maleagant', 'Marhaus', 'Palamedes', 'Pelleas', 'Pellinore', 'Percival', 'Safir', 'Sagramore', 'Segwarides', 'Tor', ]: # There are 32 knights knight = Knight(name=knight_name) add(knight) commit()
def setUp(self): class Movie(Object): title = CharacterVarying(30) class Character(Object): name = CharacterVarying(30) movie = ManyToOne(Movie) self.connection = get_test_conn() set_context(self.connection) start_session() life = Movie(title = 'Life of Brian') grail = Movie(title = 'MP & the Holy Grail') add(life) add(grail) for c in [ ('Brian', life), ('Patsy', grail), ('Arthur', grail), ]: name, movie = c char = Character(name = name, movie = movie) add(char) commit() self.Character = Character self.Movie = Movie