Coder Social home page Coder Social logo

API closer to gocql about gockle HOT 13 CLOSED

clawconduce avatar clawconduce commented on July 20, 2024 1
API closer to gocql

from gockle.

Comments (13)

willfaught avatar willfaught commented on July 20, 2024

Thanks for sharing. I omitted a query type that would match gocql.Query because it's one fewer type you have to mock, and it seemed to work having all the query stuff in Session. I'm curious: why did you find having a separate query type was better?

from gockle.

clawconduce avatar clawconduce commented on July 20, 2024

It was for 2 reasons, first, it made it super easy to port code - it was just :%s/*gocql.Session/gockle.Session, and 2nd it kept the results & arguments more separated which I thought was easier to read. So for things like Scan, I thought it was clearer for the non-mocked code to have &results separated from the cql statement and arguments.

from gockle.

willfaught avatar willfaught commented on July 20, 2024

Ah, that makes sense. I designed it from the perspective of starting fresh, whereas you're coming at it from already using gocql, which seems more likely to me. Perhaps there's more value in just mimicking the gocql interfaces exactly. Unfortunately, gocql method names are (IMO) inconsistent, so I hate to adopt them, but the ability to search/replace is appealing. Do you favor keeping the same method names as well?

from gockle.

willfaught avatar willfaught commented on July 20, 2024

Edit: Do you favor keeping the same gocql method names as well, or do you like the gockle names enough to make the extra search/replaces for them worth it?

from gockle.

clawconduce avatar clawconduce commented on July 20, 2024

Even with inconsistent names, I would favor the gocql names. It's really appealing to only have to replace the gocql.Session with a gockle.Session. Then the rest of the code is untouched, and also, everything else can be figured out by referring to the gocql docs, mailing lists, stackoverflow questions, etc.

from gockle.

willfaught avatar willfaught commented on July 20, 2024

That makes sense. I suppose I could always have separate types that simplify the original API. I'll make the change. Thanks for your thoughts!

from gockle.

clawconduce avatar clawconduce commented on July 20, 2024

No problem!

from gockle.

soldiershen avatar soldiershen commented on July 20, 2024

@clawconduce
Hi Claw,
I use your zip code(does not rely on willfaught's code right?)
and find a Mock call missing error.Could you help me to explain it?
I copy you readme code.
log:

panic: Mock call missing for Query(

"select id from atlas.mock_test where id = ?",

int(1),

)
func TestGetId(t *testing.T){
myRet := 42
mockQuery := common.MockQuery{}
//Scan should set a variable and return an error - nil in this case
mockQuery.When("Scan", mock.Any).ReturnToArgument(0, &myRet).Return(nil)
session := &common.SessionMock{}
session.When("Query", mock.Any).Return(mockQuery) //error
result,error:=GetId(session)
println(result)
assert.True(t,error==nil)
assert.True(t,result==1)
}

func GetId(session common.Session) (int,error) {
var id int
if err := session.Query(select id from atlas.mock_test where id = ?,
1).Scan(&id); err != nil {
return id,err
}
return id,nil
}

from gockle.

willfaught avatar willfaught commented on July 20, 2024

Sorry about the delay doing this. Lost steam when I started another thing. Not sure when I'll be able to get to it.

from gockle.

clawconduce avatar clawconduce commented on July 20, 2024

Hey, I have not looked at this much since posting it (though I do have tests that use it). I think the issue here is that the query expects 1 argument, but is getting 2 arguments. So the easiest fix is to change the line with the //error to session.When("Query", mock.Any, mock.Any).Return(mockQuery)

If this is the case, the README should say something like:

...
session.When("Query", mock.Any).Return(mockQuery)

The number of arguments in the query should match the number of arguments passed to query in your code. Bear in mind that the query itself is an argument. For the example above, the call in the code being tested should look something like: session.Query("SELECT value from my_keyspace.my_table").

Here is an example with 1 argument passed to the cassandra:

session.When("Query", mock.Any, int64(1)).Return(mockQuery)

With the code being tested looking like:

session.Query("SELECT email from ks.users", int64(userID))

from gockle.

soldiershen avatar soldiershen commented on July 20, 2024

@clawconduce yes,I correct it by doing
session.When("Query", "select id from atlas.mock_test where id = ?",mock.Any).Return(mockQuery)

Thanks

from gockle.

soldiershen avatar soldiershen commented on July 20, 2024

@willfaught @clawconduce a suggestion is that use regex to match sqls like https://github.com/DATA-DOG/go-sqlmock

from gockle.

willfaught avatar willfaught commented on July 20, 2024

Sorry if any issues are unresolved. I haven't touched or used this code in a couple years and don't remember much about it, or plan to add to it at this point. Please feel free to fork the code under the license if needed.

from gockle.

Related Issues (5)

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.