@@ -714,3 +714,64 @@ values (42.00);
714714 assert .NoError (t , err )
715715 assert .Equal (t , "42.00" , m .String ())
716716}
717+
718+ func TestAnonDispatcherFunc (t * testing.T ) {
719+ qry := `
720+ if OBJECT_ID('dbo.MyUsers', 'U') is not null drop table MyUsers
721+ create table MyUsers (
722+ ID INT IDENTITY(1,1) PRIMARY KEY,
723+ Username NVARCHAR(50)
724+ );
725+ insert into MyUsers (Username) values ('JohnDoe');
726+
727+ -- logging
728+ select _log='info', Y = 'one';
729+
730+ -- dispatcher
731+ select _function='TestFunction', component = 'abc', val=1, time=1.23;
732+
733+ select _function='ReturnAnonFunc', label = 'myLabel', time=1.23;
734+ `
735+
736+ var hook LogHook
737+ logger := logrus .StandardLogger ()
738+ logger .Hooks .Add (& hook )
739+ ctx := querysql .WithLogger (context .Background (), querysql .LogrusMSSQLLogger (logger , logrus .InfoLevel ))
740+ ctx = querysql .WithDispatcher (ctx , querysql .GoMSSQLDispatcher ([]interface {}{
741+ testhelper .TestFunction ,
742+ testhelper .ReturnAnonFunc ("myComponent" ),
743+ }))
744+ testhelper .ResetTestFunctionsCalled ()
745+
746+ _ , err := querysql .ExecContext (ctx , sqldb , qry , "world" )
747+ assert .NoError (t , err )
748+
749+ assert .True (t , testhelper .TestFunctionsCalled ["ReturnAnonFunc.myComponent" ])
750+
751+ // Check that we have exhausted the logging select before we do the call that gets ErrNoMoreSets
752+ assert .Equal (t , []logrus.Fields {
753+ {"Y" : "one" },
754+ }, hook .lines )
755+
756+ assert .True (t , testhelper .TestFunctionsCalled ["TestFunction" ])
757+ }
758+
759+ func TestDispatcherPanicsWithTwoAnonFuncs (t * testing.T ) {
760+ var mustNotBeTrue bool
761+ var hook LogHook
762+ logger := logrus .StandardLogger ()
763+ logger .Hooks .Add (& hook )
764+ defer func () {
765+ r := recover ()
766+ assert .NotNil (t , r ) // nil if a panic didn't happen, not nil if a panic happened
767+ assert .False (t , mustNotBeTrue )
768+ }()
769+
770+ ctx := querysql .WithLogger (context .Background (), querysql .LogrusMSSQLLogger (logger , logrus .InfoLevel ))
771+ ctx = querysql .WithDispatcher (ctx , querysql .GoMSSQLDispatcher ([]interface {}{
772+ testhelper .ReturnAnonFunc ("myComponent" ),
773+ testhelper .ReturnAnonFunc ("myComponent2" ), // This should cause a panic
774+ }))
775+ // Nothing here gets executed because we expect the WithDispatcher to have panicked
776+ mustNotBeTrue = true
777+ }
0 commit comments