driftprogramming / pgxpoolmock

pgx postgresql pool
MIT License
38 stars 16 forks source link

Not understanding the sample code #1

Closed khanzf closed 2 years ago

khanzf commented 2 years ago

Hi, I read the example and looked up other use cases on github, but I do not understand what is happening after this line:

    pgxRows := pgxpoolmock.NewRows(columns).AddRow(100, 100000.9).ToPgxRows()

It seems to me that the code is defining the table, then populating it with 2 values (the ID and integer). But what happens afterwards? What does this line do:

    mockPool.EXPECT().Query(gomock.Any(), gomock.Any(), gomock.Any()).Return(pgxRows, nil)

I see 3 arguments of Query(), but I do not understand that. Afterwards, I see this line:

    orderDao := testdata.OrderDAO{
        Pool: mockPool,
    }

What is orderDao and why is it populated this way?

Ultimately, my objective is to figure out how to unit-test a Query() that uses pgxpool. Thank you!

khanzf commented 2 years ago

Figured it out by looking at the source. For anyone else reading this: The NewMockPgxPool section sets up the pool, while the NewRows defines a row that could be returned. The EXCEPT/Query() line defines what a query would look like and what should be returned. The orderDao lines are setting up and running the actual test, while the final GetORderByID is the execution of the test.