unit testing - python, test not failing when inserting same row twice -


my postgresql table looks like

             table "public.categories"    column   |           type           | modifiers ------------+--------------------------+-----------  uuid       | uuid                     | not null  name       | character varying        | not null  parent     | character varying        | not null  created_on | timestamp time zone | not null indexes:     "categories_pkey" primary key, btree (uuid)     "categories_name_parent_key" unique constraint, btree (name, parent) referenced by:     table "transactions" constraint "transactions_category_id_fkey" foreign key (category_id) references categories(uuid) 

having unique(name, parent). test is

  def setup(self):         db.create_all()      def session_commit(self):         # noinspection pybroadexception         try:             db.session.commit()         except:             db.session.rollback()         finally:             pass    def test_insert_same_category_twice(self):         """          category, parent relationship unique         """         db.session.add(category('test_insert_same_category_twice', 'parent1'))         self.session_commit()         self.assertequals(1, len(db.session.query(category).all()))         db.session.add(category('test_insert_same_category_twice', 'parent1')) # should fail         self.session_commit()      def teardown(self):         db.session.close()         tbl in reversed(db.metadata.sorted_tables):             db.engine.execute(tbl.delete()) 

it should fail when try insert new category same name , parent because of uniqueconstraint in database, doesn't

also, when assert @ last line(omitted in code above) how many entries have in table

self.assertequals(2, len(db.session.query(category).all())) 

it fails

    self.assertequals(2, len(db.session.query(category).all())) assertionerror: 2 != 1 

that means overwrites existing entry?

what going wrong here?

update

as per @sr2222 answer, resolved bug in following method

def session_commit(self):     # noinspection pybroadexception     try:         db.session.commit()     except:         db.session.rollback()         raise # added error propagate     finally:         pass 

your session_commit function has catchall except block around db commit. unless rollback fails, pass. assert, commit silently failing since squelching error, single row find should original, unedited entry. if want conditionally handle exception state in validation, need catch , handle appropriately, not throw away.


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -