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
 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 test_unhandled_exception(self):
     """Testing the exception from engine that can't be handled"""
     conn = get_test_conn()
     set_context(conn)
     class Spam(Object):
         eggs = BrokenField()
     try:
         Spam.create_table()
     except ProgrammingError as err:
         conn.rollback()
         self.assertEqual(err.pgcode, UNDEFINED_OBJECT)
 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):
     set_context(get_test_conn())
     start_session()
 def test_invalid_context(self):
     """Test setting context with invalid value raises exc"""
     self.assertRaises(TypeError, lambda: set_context(1))
 def test_set_with_cursor(self):
     """Test we can use cursor to set ctx."""
     set_context(get_test_conn().cursor())
     get_cursor() #Passes when no error