go-ozzo / ozzo-dbx

A Go (golang) package that enhances the standard database/sql package by providing powerful data retrieval methods as well as DB-agnostic query building capabilities.
MIT License
638 stars 91 forks source link

Transaction not working in test #67

Open dsvteam opened 6 years ago

dsvteam commented 6 years ago

This is my code for DAO testing

        testdata.ResetDB()
    rs := testdata.GetRequestScopeTest()

        // I have to insert this stuff :\
    _, err := testdata.DB.Begin()
    if err != nil {
        panic(err)
    }
        // Then, start a `real` transaction here
    tx, err := testdata.DB.Begin()
    if err != nil {
        panic(err)
    }
    rs.SetTx(tx)
    defer func() {
        if err := rs.Tx().Commit(); err != nil {
            t.Error(err)
        }
    }()

    // My DB operations come here

I'm trying to seed Postgres DB with ozzo-dbx (since I used it too in my main app). When I write my tests, I began a transaction and start my own DB operations later for DAO testing. But as you can see from my comment above, I have to insert a dumb transaction begin to make it works, if not I will get this error:

label_test.go:29: pq: relation "prescription_state" does not exist
label_test.go:31: Pre []models.PrescriptionState(nil)
label_test.go:36: Actual 0, expected: 6
label_test.go:22: pq: Could not complete operation in a failed transaction

It's kinda weird for me since everything works fine in my main app but these tests. Can someone spot any of my mistakes here or is it a potential bug from package? Thank in advance.

kPshi commented 6 years ago

Seems like you do not have all tables and/or relations in the first place. Do you have some SQL level log available?

akaipham commented 6 years ago

Hi @kPshi , we've already all tables/ relations in a sql file to be executed. So I don't think its missing in sql file or so.

The problem is when we start a fake Tx then a real Tx, it could execute our SQL file. But when we have only 1 started Tx, it yields the logs above. Kinda wierd

kPshi commented 6 years ago

Right, only that error looks like it is raised by the DB server so that something seems to be queried during that first transaction that does not exist. When you got everything in place already, that missing bit may be due to some inner state of your client. So I was hoping for some insight by seeing what it does on DB side, thus the SQL log.

Anyways: talking about that issue gave me one or two ideas of what to dive into. The logs would be great nevertheless. Setting a logger for ozzo should be enough (see LogFunc).

kPshi commented 6 years ago

Failed to reproduce the error. Do you have a minimal buildable example?