Skip to content

Commit

Permalink
Updating and simplifying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfava committed Oct 31, 2024
1 parent ed01238 commit 5fa5229
Showing 1 changed file with 5 additions and 17 deletions.
22 changes: 5 additions & 17 deletions querysql/querysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func isClosed(r *sql.Rows) bool {
return err != nil && err.Error() == "sql: Rows are closed"
}

// TODO(dsf): Expand to test std logger
func TestMultipleRowsetsResultsHappyDay(t *testing.T) {
qry := `
-- single scalar
Expand Down Expand Up @@ -144,6 +145,7 @@ select _function='OtherTestFunction', time=42, money=convert(money, 12345.67);

}

// TODO(dsf): Also test the std logger
func TestInvalidLogLevel(t *testing.T) {
qry := `
-- log something
Expand Down Expand Up @@ -218,16 +220,13 @@ select 2;

func TestDispatcherSetupError(t *testing.T) {
var mustNotBeTrue bool
var hook LogHook
logger := logrus.StandardLogger()
logger.Hooks.Add(&hook)
defer func() {
r := recover()
assert.NotNil(t, r) // nil if a panic didn't happen, not nil if a panic happened
assert.False(t, mustNotBeTrue)
}()

ctx := querysql.WithLogger(context.Background(), querysql.LogrusMSSQLLogger(logger, logrus.InfoLevel))
ctx := context.Background()
ctx = querysql.WithDispatcher(ctx, querysql.GoMSSQLDispatcher([]interface{}{
"SomethingThatIsNotAFunctionPointer", // This should cause a panic
}))
Expand Down Expand Up @@ -724,19 +723,13 @@ create table MyUsers (
);
insert into MyUsers (Username) values ('JohnDoe');
-- logging
select _log='info', Y = 'one';
-- dispatcher
select _function='TestFunction', component = 'abc', val=1, time=1.23;
select _function='ReturnAnonFunc', label = 'myLabel', time=1.23;
`

var hook LogHook
logger := logrus.StandardLogger()
logger.Hooks.Add(&hook)
ctx := querysql.WithLogger(context.Background(), querysql.LogrusMSSQLLogger(logger, logrus.InfoLevel))
ctx := context.Background()
ctx = querysql.WithDispatcher(ctx, querysql.GoMSSQLDispatcher([]interface{}{
testhelper.TestFunction,
testhelper.ReturnAnonFunc("myComponent"),
Expand All @@ -748,11 +741,6 @@ select _function='ReturnAnonFunc', label = 'myLabel', time=1.23;

assert.True(t, testhelper.TestFunctionsCalled["ReturnAnonFunc.myComponent"])

// Check that we have exhausted the logging select before we do the call that gets ErrNoMoreSets
assert.Equal(t, []logrus.Fields{
{"Y": "one"},
}, hook.lines)

assert.True(t, testhelper.TestFunctionsCalled["TestFunction"])
}

Expand All @@ -767,7 +755,7 @@ func TestDispatcherPanicsWithTwoAnonFuncs(t *testing.T) {
assert.False(t, mustNotBeTrue)
}()

ctx := querysql.WithLogger(context.Background(), querysql.LogrusMSSQLLogger(logger, logrus.InfoLevel))
ctx := context.Background()
ctx = querysql.WithDispatcher(ctx, querysql.GoMSSQLDispatcher([]interface{}{
testhelper.ReturnAnonFunc("myComponent"),
testhelper.ReturnAnonFunc("myComponent2"), // This should cause a panic
Expand Down

0 comments on commit 5fa5229

Please sign in to comment.