daydreamer
daydreamer

Reputation: 91959

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 with 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). My 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 is 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()
        for tbl in reversed(db.metadata.sorted_tables):
            db.engine.execute(tbl.delete())

It should fail when I try to insert the new category with same name and parent because of UniqueConstraint in database, but it doesn't

Also, when I assert at last line(omitted in code above) how many entries I have in the 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 it overwrites the existing entry?

What is going wrong here?

UPDATE

As per @sr2222 answer, I resolved the bug in my following method

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

Upvotes: 1

Views: 130

Answers (1)

Silas Ray
Silas Ray

Reputation: 26150

Your session_commit function has a catchall except block around the DB commit. Unless the rollback fails, this will always pass. As to your assert, your commit is silently failing since you are squelching the error, so the single row you find should be the original, unedited entry. If you want to conditionally handle the exception state in your validation, you need to catch it and handle it appropriately, not just throw it away.

Upvotes: 1

Related Questions