Coder Social home page Coder Social logo

Issues with new NamedArgs mocks about pgxmock HOT 7 CLOSED

FryDay avatar FryDay commented on August 22, 2024
Issues with new NamedArgs mocks

from pgxmock.

Comments (7)

FryDay avatar FryDay commented on August 22, 2024 1
type AnyTime struct{}

// Match satisfies pgxmock.Argument interface
func (a AnyTime) Match(v interface{}) bool {
	_, ok := v.(time.Time)
	return ok
}

from pgxmock.

FryDay avatar FryDay commented on August 22, 2024

If it helps, the sql seems to get rewritten by argsMatches here. This seems like where the issue is coming from, but I'm not sure what the best way to fix it would be.

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

What is the definition of postgresql.AnyTime{}?

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

OK, I found the issue. In short: pgxmock always expects WithRewrittenSQL() for QueryRewriter arguments. Which is way too strict, I must agree. This works for me:

func TestFail(t *testing.T) {
	t.Parallel()
	mock, err := NewConn(QueryMatcherOption(QueryMatcherEqual))
	a := assert.New(t)
	a.NoError(err)

	update := `
UPDATE "user"
SET email = @email,
    password = @password,
	pin = @pin,
	name = @name,
	software_admin = @software_admin,
	refresh_token = @refresh_token,
	updated_utc = @updated_utc
WHERE id = @id`

	mock.ExpectExec(update).WithRewrittenSQL(`
UPDATE "user"
SET email = $1,
    password = $2,
	pin = $3,
	name = $4,
	software_admin = $5,
	refresh_token = $6,
	updated_utc = $7
WHERE id = $8`).WithArgs(pgx.NamedArgs{
		"id":             "mockUser.ID",
		"email":          "mockUser.Email",
		"password":       "mockUser.Password",
		"pin":            "mockUser.Pin",
		"name":           "mockUser.Name",
		"software_admin": "mockUser.SoftwareAdmin",
		"refresh_token":  "mockUser.RefreshToken",
		"updated_utc":    AnyArg(),
	}).WillReturnError(errPanic)

	_, err = mock.Exec(context.Background(), update, pgx.NamedArgs{
		"id":             "mockUser.ID",
		"email":          "mockUser.Email",
		"password":       "mockUser.Password",
		"pin":            "mockUser.Pin",
		"name":           "mockUser.Name",
		"software_admin": "mockUser.SoftwareAdmin",
		"refresh_token":  "mockUser.RefreshToken",
		"updated_utc":    time.Now().UTC(),
	})
	a.Error(err)
	a.NoError(mock.ExpectationsWereMet())
}

As you can see I specified both queries to expect: initial and rewritten. Let me try to fix it. I think it's not that complicated

from pgxmock.

FryDay avatar FryDay commented on August 22, 2024

I see, yeah, it almost feels backwards. If you manage to change it around, I'll be very happy!

from pgxmock.

pashagolub avatar pashagolub commented on August 22, 2024

Would you please try a fix in 167-fix-queryrewriter branch? This should work go get -u github.com/pashagolub/pgxmock/v3@167-fix-queryrewriter

from pgxmock.

FryDay avatar FryDay commented on August 22, 2024

Yes, that works perfectly! I tested it with both my Execs and my Queries.

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.