def insert_p(state):
     p = ProductsTable(
         name="Test transactional",
         description="Testing 1, 2, 3!",
         created_at=nowtz(),
     )
     db.session.add(p)
def product_2():
    product = ProductsTable(name="Product 2",
                            description="Product 2 description",
                            created_at=nowtz())
    db.session.add(product)
    db.session.commit()
    return str(product.id)
 def insert_p_error(state):
     p = ProductsTable(
         name="Test transactional [ERROR]",
         description="Testing 1, 2, 3! BOOM!",
         created_at=nowtz(),
     )
     db.session.add(p)
     raise Exception("Let's wreck some havoc!")
    def insert_p(state):
        p = ProductsTable(
            name="Test transactional should not be committed",
            description="Testing 1, 2, 3!",
            created_at=nowtz(),
        )
        db.session.add(p)
        db.session.commit()

        raise Exception("Lets rollback")
    def insert_p(state):
        p = ProductsTable(
            name="Test transactional should not be committed",
            description="Testing 1, 2, 3!",
            created_at=nowtz(),
        )
        db.session.add(p)
        db.session.commit()

        # Create new database session to simulate another workflow/api handler running at the same time
        # This is also a workaround for our disable commit wrapper but it should be reasonable obvious that
        # someone is f*****g around if you see `with db.database_scope():` in actual production code

        with db.database_scope():
            p2 = ProductsTable(
                name="Test transactional should be committed",
                description="Testing 1, 2, 3!",
                created_at=nowtz(),
            )
            db.session.add(p2)
            db.session.commit()

        raise Exception("Lets rollback")
def test_autouse_fixture_rolls_back_bbb():
    # We want to test whether a change committed to the database in one test is visible to other tests (as in really
    # persisted to the database). Of course such a change should not be visible if our `flask_app` and `database`
    # autouse fixtures work as advertised.
    #
    # However, tests should be independent of each other and we cannot assume one test runs before the other. Hence
    # this test comes in two versions: one with the `_aaa` postfix and one with the `_bbb` postfix. Both will test
    # for the presence of a change the other test thinks it has committed to the database. If one of the tests (the
    # one that runs after the other) finds the change the other has committed our fixtures don't work properly.

    # Using ResourceTypeTable as it's a simple model than doesn't require foreign keys.
    p = ProductsTable(name="bbb", description="bbb", created_at=nowtz())
    db.session.add(p)
    db.session.commit()

    with pytest.raises(NoResultFound):
        ProductsTable.query.filter(ProductsTable.name == "aaa").one()
def test_str_method():
    assert str(ProductsTable()) == "ProductsTable(id=None, name=None, description=None, created_at=None)"