Coder Social home page Coder Social logo

Comments (7)

OiCMudkips avatar OiCMudkips commented on August 22, 2024

Wait I think I'm doing something wrong, let me look at this more.

from pgxmock.

aaranmcguire avatar aaranmcguire commented on August 22, 2024

Did you look at fixing this, as I think this is still an issue. Or can you confirm what you were doing wrong?

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

@aaranmcguire show us your code and we will try to investigate together

from pgxmock.

aaranmcguire avatar aaranmcguire commented on August 22, 2024

Thanks @pashagolub. I think I understand the issue again. It's wanting the exact type when doing the add row. So AddRow( nil) will work correctly, Providing a pointer of the same type to AddRow() will also allow the test to pass.

This again is unlike go-sqlmock making it more work to switch over. Do you have any thoughts on how to make it easier to swap over so types are more easily converted like PGX does?

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

I don't think I understand your last statement? Can you please provide code samples with pgx showing the issue?

from pgxmock.

aaranmcguire avatar aaranmcguire commented on August 22, 2024

With DATA-DOG/go-sqlmock you're able to AddRow() and it won't mind the type conversion when running Scan(). See below of what I would expect to work.

	mock.ExpectQuery("SELECT .+ FROM test WHERE .+").
		WithArgs(1).
		WillReturnRows(db.NewRows(
			[]string{"id", "value"}).
			AddRow(uint64(1), "hello"),
		)
	rows := mock.QueryRow(context.Background(), "SELECT id, value FROM test WHERE id = $1", 1)

	var id uint64
	var value *string
	err = rows.Scan(&id, &value)
	if err != nil {
		t.Error(err)
	}

In your mock software you require the exact match, so you have to modify it to:

 
+       mockedValue := "hello"
        mock.ExpectQuery("SELECT .+ FROM test WHERE .+").
                WithArgs(1).
                WillReturnRows(db.NewRows(
                        []string{"id", "value"}).
-                       AddRow(uint64(1), "hello"),
+                       AddRow(uint64(1), &mockedValue),
                )
        rows := mock.QueryRow(context.Background(), "SELECT id, value FROM test WHERE id = $1", 1)

Which makes it harder to migrate from go-sqlmock, and doesn't seem to match exactly what pgx does as it coverts using this function: https://github.com/jackc/pgx/blob/master/values.go#L27

go-sqlmock also converts during AddRow into something the database would give too, so that on Scan it sees what a database may give: https://github.com/DATA-DOG/go-sqlmock/blob/master/rows.go#L173-L176

While I could update all the code to give the exact types, it's a bit of a pain, and makes it harder for others to convert over to pgx.

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

Thanks for the detailed explanation. I'll try to take a look.

Of course, PRs are more than welcome!

from pgxmock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.