|
package storage_test
import (
"bytes"
"context"
"fmt"
"math"
"testing"
"time"
"github.com/lib/pq"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/stretchr/testify/assert"
"github.com/RedHatInsights/insights-results-aggregator/storage"
"github.com/RedHatInsights/insights-results-aggregator/tests/helpers"
)
func TestInitSQLDriverWithLogs ( t * testing . T ) {
zerolog . SetGlobalLevel ( zerolog . DebugLevel )
driverName := storage . InitSQLDriverWithLogs (
& pq . Driver { } ,
"postgres" ,
)
assert . Equal ( t , "postgresWithHooks" , driverName )
}
|
TestInitSQLDriverWithLogsMultipleCalls tests if InitSQLDriverWithLogs
does not panic on multiple calls and is idempotent
|
func TestInitSQLDriverWithLogsMultipleCalls ( t * testing . T ) {
zerolog . SetGlobalLevel ( zerolog . DebugLevel )
for i := 0 ; i < 10 ; i ++ {
driverName := storage . InitSQLDriverWithLogs (
& pq . Driver { } ,
"postgres" ,
)
assert . Equal ( t , "postgresWithHooks" , driverName )
}
}
func TestSQLHooksLoggingArgsJSON ( t * testing . T ) {
zerolog . SetGlobalLevel ( zerolog . DebugLevel )
const query = "SELECT 1"
params := make ( [ ] interface { } , 0 )
buf := new ( bytes . Buffer )
logger := zerolog . New ( buf ) . With ( ) . Str ( "type" , "SQL" ) . Logger ( )
log . Logger = logger
hooks := storage . SQLHooks { }
_ , err := hooks . Before ( context . Background ( ) , query , params ... )
helpers . FailOnError ( t , err )
assert . Contains (
t ,
buf . String ( ) ,
fmt . Sprintf ( storage . LogFormatterString , query , params ) ,
)
_ , err = hooks . After (
context . WithValue ( context . Background ( ) , storage . SQLHooksKeyQueryBeginTime , time . Now ( ) ) ,
query ,
params ... ,
)
helpers . FailOnError ( t , err )
assert . Contains (
t ,
buf . String ( ) ,
fmt . Sprintf ( storage . LogFormatterString , query , params ) + " took" ,
)
}
func TestSQLHooksLoggingArgsNotJSON ( t * testing . T ) {
zerolog . SetGlobalLevel ( zerolog . DebugLevel )
|
just for better test coverage :)
|
const query = "SELECT 1"
var params = [ ] interface { } { math . Inf ( 1 ) }
buf := new ( bytes . Buffer )
logger := zerolog . New ( buf ) . With ( ) . Str ( "type" , "SQL" ) . Logger ( )
log . Logger = logger
hooks := storage . SQLHooks { }
_ , err := hooks . Before ( context . Background ( ) , query , params ... )
helpers . FailOnError ( t , err )
assert . Contains (
t ,
buf . String ( ) ,
fmt . Sprintf ( storage . LogFormatterString , query , params ) ,
)
_ , err = hooks . After (
context . WithValue ( context . Background ( ) , storage . SQLHooksKeyQueryBeginTime , time . Now ( ) ) ,
query ,
params ... ,
)
helpers . FailOnError ( t , err )
assert . Contains (
t ,
buf . String ( ) ,
fmt . Sprintf ( storage . LogFormatterString , query , params ) + " took" ,
)
}
|