How to use CatchFatal method of test Package

Best Go-testdeep code snippet using test.CatchFatal

t_struct_test.go

Source:t_struct_test.go Github

copy

Full Screen

...64 })65 //66 // Bad usages67 ttb := test.NewTestingTB("usage params")68 ttb.CatchFatal(func() {69 td.NewT(ttb, td.ContextConfig{}, td.ContextConfig{})70 })71 test.IsTrue(tt, ttb.IsFatal)72 test.IsTrue(tt, strings.Contains(ttb.Messages[0], "usage: NewT("))73 test.CheckPanic(tt, func() { td.NewT(nil) }, "usage: NewT")74}75func TestTCmp(tt *testing.T) {76 ttt := test.NewTestingTB(tt.Name())77 t := td.NewT(ttt)78 test.IsTrue(tt, t.Cmp(1, 1))79 test.IsFalse(tt, ttt.Failed())80 ttt = test.NewTestingTB(tt.Name())81 t = td.NewT(ttt)82 test.IsFalse(tt, t.Cmp(1, 2))83 test.IsTrue(tt, ttt.Failed())84}85func TestTCmpDeeply(tt *testing.T) {86 ttt := test.NewTestingTB(tt.Name())87 t := td.NewT(ttt)88 test.IsTrue(tt, t.CmpDeeply(1, 1))89 test.IsFalse(tt, ttt.Failed())90 ttt = test.NewTestingTB(tt.Name())91 t = td.NewT(ttt)92 test.IsFalse(tt, t.CmpDeeply(1, 2))93 test.IsTrue(tt, ttt.Failed())94}95func TestParallel(t *testing.T) {96 t.Run("without Parallel", func(tt *testing.T) {97 ttt := test.NewTestingTB(tt.Name())98 t := td.NewT(ttt)99 t.Parallel()100 // has no effect101 })102 t.Run("with Parallel", func(tt *testing.T) {103 ttt := test.NewParallelTestingTB(tt.Name())104 t := td.NewT(ttt)105 t.Parallel()106 test.IsTrue(tt, ttt.IsParallel)107 })108 t.Run("Run with Parallel", func(tt *testing.T) {109 // This test verifies that subtests with t.Parallel() are run110 // in parallel. We use a WaitGroup to make both subtests block111 // until they're both ready. This test will block forever if112 // the tests are not run together.113 var ready sync.WaitGroup114 ready.Add(2)115 t := td.NewT(tt)116 t.Run("level 1", func(t *td.T) {117 t.Parallel()118 ready.Done() // I'm ready.119 ready.Wait() // Are you?120 })121 t.Run("level 2", func(t *td.T) {122 t.Parallel()123 ready.Done() // I'm ready.124 ready.Wait() // Are you?125 })126 })127}128func TestRun(t *testing.T) {129 t.Run("test.TB with Run", func(tt *testing.T) {130 t := td.NewT(tt)131 runPassed := false132 nestedFailureIsFatal := false133 ok := t.Run("Test level1",134 func(t *td.T) {135 ok := t.FailureIsFatal().Run("Test level2",136 func(t *td.T) {137 runPassed = t.True(true) // test succeeds!138 // Check we inherit config from caller139 nestedFailureIsFatal = t.Config.FailureIsFatal140 })141 t.True(ok)142 })143 test.IsTrue(tt, ok)144 test.IsTrue(tt, runPassed)145 test.IsTrue(tt, nestedFailureIsFatal)146 })147 t.Run("test.TB without Run", func(tt *testing.T) {148 t := td.NewT(test.NewTestingTB("gg"))149 runPassed := false150 ok := t.Run("Test level1",151 func(t *td.T) {152 ok := t.Run("Test level2",153 func(t *td.T) {154 runPassed = t.True(true) // test succeeds!155 })156 t.True(ok)157 })158 t.True(ok)159 t.True(runPassed)160 })161}162func TestRunAssertRequire(t *testing.T) {163 t.Run("test.TB with Run", func(tt *testing.T) {164 t := td.NewT(tt)165 runPassed := false166 assertIsFatal := true167 requireIsFatal := false168 ok := t.RunAssertRequire("Test level1",169 func(assert, require *td.T) {170 assertIsFatal = assert.Config.FailureIsFatal171 requireIsFatal = require.Config.FailureIsFatal172 ok := assert.RunAssertRequire("Test level2",173 func(assert, require *td.T) {174 runPassed = assert.True(true) // test succeeds!175 runPassed = runPassed && require.True(true) // test succeeds!176 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal177 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal178 })179 assert.True(ok)180 require.True(ok)181 ok = require.RunAssertRequire("Test level2",182 func(assert, require *td.T) {183 runPassed = runPassed && assert.True(true) // test succeeds!184 runPassed = runPassed && require.True(true) // test succeeds!185 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal186 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal187 })188 assert.True(ok)189 require.True(ok)190 })191 test.IsTrue(tt, ok)192 test.IsTrue(tt, runPassed)193 test.IsFalse(tt, assertIsFatal)194 test.IsTrue(tt, requireIsFatal)195 })196 t.Run("test.TB without Run", func(tt *testing.T) {197 t := td.NewT(test.NewTestingTB("gg"))198 runPassed := false199 assertIsFatal := true200 requireIsFatal := false201 ok := t.RunAssertRequire("Test level1",202 func(assert, require *td.T) {203 assertIsFatal = assert.Config.FailureIsFatal204 requireIsFatal = require.Config.FailureIsFatal205 ok := assert.RunAssertRequire("Test level2",206 func(assert, require *td.T) {207 runPassed = assert.True(true) // test succeeds!208 runPassed = runPassed && require.True(true) // test succeeds!209 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal210 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal211 })212 assert.True(ok)213 require.True(ok)214 ok = require.RunAssertRequire("Test level2",215 func(assert, require *td.T) {216 runPassed = runPassed && assert.True(true) // test succeeds!217 runPassed = runPassed && require.True(true) // test succeeds!218 assertIsFatal = assertIsFatal || assert.Config.FailureIsFatal219 requireIsFatal = requireIsFatal && require.Config.FailureIsFatal220 })221 assert.True(ok)222 require.True(ok)223 })224 test.IsTrue(tt, ok)225 test.IsTrue(tt, runPassed)226 test.IsFalse(tt, assertIsFatal)227 test.IsTrue(tt, requireIsFatal)228 })229}230// Deprecated RunT.231func TestRunT(t *testing.T) {232 t.Run("test.TB with Run", func(tt *testing.T) {233 t := td.NewT(tt)234 runPassed := false235 ok := t.RunT("Test level1", //nolint: staticcheck236 func(t *td.T) {237 ok := t.RunT("Test level2", //nolint: staticcheck238 func(t *td.T) {239 runPassed = t.True(true) // test succeeds!240 })241 t.True(ok)242 })243 test.IsTrue(tt, ok)244 test.IsTrue(tt, runPassed)245 })246 t.Run("test.TB without Run", func(tt *testing.T) {247 t := td.NewT(test.NewTestingTB("gg"))248 runPassed := false249 ok := t.RunT("Test level1", //nolint: staticcheck250 func(t *td.T) {251 ok := t.RunT("Test level2", //nolint: staticcheck252 func(t *td.T) {253 runPassed = t.True(true) // test succeeds!254 })255 t.True(ok)256 })257 test.IsTrue(tt, ok)258 test.IsTrue(tt, runPassed)259 })260}261func TestFailureIsFatal(tt *testing.T) {262 // All t.True(false) tests of course fail263 // Using default config264 ttt := test.NewTestingTB(tt.Name())265 t := td.NewT(ttt)266 t.True(false) // failure267 test.IsTrue(tt, ttt.LastMessage() != "")268 test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal")269 // Using specific config270 ttt = test.NewTestingTB(tt.Name())271 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true})272 ttt.CatchFatal(func() { t.True(false) }) // failure273 test.IsTrue(tt, ttt.LastMessage() != "")274 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")275 // Using FailureIsFatal()276 ttt = test.NewTestingTB(tt.Name())277 t = td.NewT(ttt).FailureIsFatal()278 ttt.CatchFatal(func() { t.True(false) }) // failure279 test.IsTrue(tt, ttt.LastMessage() != "")280 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")281 // Using FailureIsFatal(true)282 ttt = test.NewTestingTB(tt.Name())283 t = td.NewT(ttt).FailureIsFatal(true)284 ttt.CatchFatal(func() { t.True(false) }) // failure285 test.IsTrue(tt, ttt.LastMessage() != "")286 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")287 // Using T.Assert()288 ttt = test.NewTestingTB(tt.Name())289 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: true}).Assert()290 t.True(false) // failure291 test.IsTrue(tt, ttt.LastMessage() != "")292 test.IsFalse(tt, ttt.IsFatal, "by default it is not fatal")293 // Using T.Require()294 ttt = test.NewTestingTB(tt.Name())295 t = td.NewT(ttt).Require()296 ttt.CatchFatal(func() { t.True(false) }) // failure297 test.IsTrue(tt, ttt.LastMessage() != "")298 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")299 // Using Require()300 ttt = test.NewTestingTB(tt.Name())301 t = td.Require(ttt)302 ttt.CatchFatal(func() { t.True(false) }) // failure303 test.IsTrue(tt, ttt.LastMessage() != "")304 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")305 // Using Require() with specific config (cannot override FailureIsFatal)306 ttt = test.NewTestingTB(tt.Name())307 t = td.Require(ttt, td.ContextConfig{FailureIsFatal: false})308 ttt.CatchFatal(func() { t.True(false) }) // failure309 test.IsTrue(tt, ttt.LastMessage() != "")310 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")311 // Canceling specific config312 ttt = test.NewTestingTB(tt.Name())313 t = td.NewT(ttt, td.ContextConfig{FailureIsFatal: false}).314 FailureIsFatal(false)315 t.True(false) // failure316 test.IsTrue(tt, ttt.LastMessage() != "")317 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")318 // Using Assert()319 ttt = test.NewTestingTB(tt.Name())320 t = td.Assert(ttt)321 t.True(false) // failure322 test.IsTrue(tt, ttt.LastMessage() != "")323 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")324 // Using Assert() with specific config (cannot override FailureIsFatal)325 ttt = test.NewTestingTB(tt.Name())326 t = td.Assert(ttt, td.ContextConfig{FailureIsFatal: true})327 t.True(false) // failure328 test.IsTrue(tt, ttt.LastMessage() != "")329 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")330 // AssertRequire() / assert331 ttt = test.NewTestingTB(tt.Name())332 t, _ = td.AssertRequire(ttt)333 t.True(false) // failure334 test.IsTrue(tt, ttt.LastMessage() != "")335 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")336 // Using AssertRequire() / assert with specific config (cannot337 // override FailureIsFatal)338 ttt = test.NewTestingTB(tt.Name())339 t, _ = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true})340 t.True(false) // failure341 test.IsTrue(tt, ttt.LastMessage() != "")342 test.IsFalse(tt, ttt.IsFatal, "it must be not fatal")343 // AssertRequire() / require344 ttt = test.NewTestingTB(tt.Name())345 _, t = td.AssertRequire(ttt)346 ttt.CatchFatal(func() { t.True(false) }) // failure347 test.IsTrue(tt, ttt.LastMessage() != "")348 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")349 // Using AssertRequire() / require with specific config (cannot350 // override FailureIsFatal)351 ttt = test.NewTestingTB(tt.Name())352 _, t = td.AssertRequire(ttt, td.ContextConfig{FailureIsFatal: true})353 ttt.CatchFatal(func() { t.True(false) }) // failure354 test.IsTrue(tt, ttt.LastMessage() != "")355 test.IsTrue(tt, ttt.IsFatal, "it must be fatal")356}357func TestUseEqual(tt *testing.T) {358 ttt := test.NewTestingTB(tt.Name())359 var time1, time2 time.Time360 for {361 time1 = time.Now()362 time2 = time1.Truncate(0)363 if !time1.Equal(time2) {364 tt.Fatal("time.Equal() does not work as expected")365 }366 if time1 != time2 { // to avoid the bad luck case where time1.wall=0367 break368 }369 }370 // Using default config371 t := td.NewT(ttt)372 test.IsFalse(tt, t.Cmp(time1, time2))373 // UseEqual374 t = td.NewT(ttt).UseEqual() // enable globally375 test.IsTrue(tt, t.Cmp(time1, time2))376 t = td.NewT(ttt).UseEqual(true) // enable globally377 test.IsTrue(tt, t.Cmp(time1, time2))378 t = td.NewT(ttt).UseEqual(false) // disable globally379 test.IsFalse(tt, t.Cmp(time1, time2))380 t = td.NewT(ttt).UseEqual(time.Time{}) // enable only for time.Time381 test.IsTrue(tt, t.Cmp(time1, time2))382 t = t.UseEqual().UseEqual(false) // enable then disable globally383 test.IsTrue(tt, t.Cmp(time1, time2)) // Equal() still used384 test.EqualStr(tt,385 ttt.CatchFatal(func() { td.NewT(ttt).UseEqual(42) }),386 "UseEqual expects type int owns an Equal method (@0)")387}388func TestBeLax(tt *testing.T) {389 ttt := test.NewTestingTB(tt.Name())390 // Using default config391 t := td.NewT(ttt)392 test.IsFalse(tt, t.Cmp(int64(123), 123))393 // BeLax394 t = td.NewT(ttt).BeLax()395 test.IsTrue(tt, t.Cmp(int64(123), 123))396 t = td.NewT(ttt).BeLax(true)397 test.IsTrue(tt, t.Cmp(int64(123), 123))398 t = td.NewT(ttt).BeLax(false)399 test.IsFalse(tt, t.Cmp(int64(123), 123))400}401func TestIgnoreUnexported(tt *testing.T) {402 ttt := test.NewTestingTB(tt.Name())403 type SType1 struct {404 Public int405 private string406 }407 a1, b1 := SType1{Public: 42, private: "test"}, SType1{Public: 42}408 type SType2 struct {409 Public int410 private string411 }412 a2, b2 := SType2{Public: 42, private: "test"}, SType2{Public: 42}413 // Using default config414 t := td.NewT(ttt)415 test.IsFalse(tt, t.Cmp(a1, b1))416 // IgnoreUnexported417 t = td.NewT(ttt).IgnoreUnexported() // ignore unexported globally418 test.IsTrue(tt, t.Cmp(a1, b1))419 test.IsTrue(tt, t.Cmp(a2, b2))420 t = td.NewT(ttt).IgnoreUnexported(true) // ignore unexported globally421 test.IsTrue(tt, t.Cmp(a1, b1))422 test.IsTrue(tt, t.Cmp(a2, b2))423 t = td.NewT(ttt).IgnoreUnexported(false) // handle unexported globally424 test.IsFalse(tt, t.Cmp(a1, b1))425 test.IsFalse(tt, t.Cmp(a2, b2))426 t = td.NewT(ttt).IgnoreUnexported(SType1{}) // ignore only for SType1427 test.IsTrue(tt, t.Cmp(a1, b1))428 test.IsFalse(tt, t.Cmp(a2, b2))429 t = t.UseEqual().UseEqual(false) // enable then disable globally430 test.IsTrue(tt, t.Cmp(a1, b1))431 test.IsFalse(tt, t.Cmp(a2, b2))432 t = td.NewT(ttt).IgnoreUnexported(SType1{}, SType2{}) // enable for both433 test.IsTrue(tt, t.Cmp(a1, b1))434 test.IsTrue(tt, t.Cmp(a2, b2))435 test.EqualStr(tt,436 ttt.CatchFatal(func() { td.NewT(ttt).IgnoreUnexported(42) }),437 "IgnoreUnexported expects type int be a struct, not a int (@0)")438}439func TestLogTrace(tt *testing.T) {440 ttt := test.NewTestingTB(tt.Name())441 t := td.NewT(ttt)442//line /t_struct_test.go:100443 t.LogTrace()444 test.EqualStr(tt, ttt.LastMessage(), `Stack trace:445 TestLogTrace() /t_struct_test.go:100`)446 test.IsFalse(tt, ttt.HasFailed)447 test.IsFalse(tt, ttt.IsFatal)448 ttt.ResetMessages()449//line /t_struct_test.go:110450 t.LogTrace("This is the %s:", "stack")451 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:452 TestLogTrace() /t_struct_test.go:110`)453 ttt.ResetMessages()454//line /t_struct_test.go:120455 t.LogTrace("This is the %s:\n", "stack")456 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:457 TestLogTrace() /t_struct_test.go:120`)458 ttt.ResetMessages()459//line /t_struct_test.go:130460 t.LogTrace("This is the ", "stack")461 test.EqualStr(tt, ttt.LastMessage(), `This is the stack462 TestLogTrace() /t_struct_test.go:130`)463 ttt.ResetMessages()464 trace.IgnorePackage()465 defer trace.UnignorePackage()466//line /t_struct_test.go:140467 t.LogTrace("Stack:\n")468 test.EqualStr(tt, ttt.LastMessage(), `Stack:469 Empty stack trace`)470}471func TestErrorTrace(tt *testing.T) {472 ttt := test.NewTestingTB(tt.Name())473 t := td.NewT(ttt)474//line /t_struct_test.go:200475 t.ErrorTrace()476 test.EqualStr(tt, ttt.LastMessage(), `Stack trace:477 TestErrorTrace() /t_struct_test.go:200`)478 test.IsTrue(tt, ttt.HasFailed)479 test.IsFalse(tt, ttt.IsFatal)480 ttt.ResetMessages()481//line /t_struct_test.go:210482 t.ErrorTrace("This is the %s:", "stack")483 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:484 TestErrorTrace() /t_struct_test.go:210`)485 ttt.ResetMessages()486//line /t_struct_test.go:220487 t.ErrorTrace("This is the %s:\n", "stack")488 test.EqualStr(tt, ttt.LastMessage(), `This is the stack:489 TestErrorTrace() /t_struct_test.go:220`)490 ttt.ResetMessages()491//line /t_struct_test.go:230492 t.ErrorTrace("This is the ", "stack")493 test.EqualStr(tt, ttt.LastMessage(), `This is the stack494 TestErrorTrace() /t_struct_test.go:230`)495 ttt.ResetMessages()496 trace.IgnorePackage()497 defer trace.UnignorePackage()498//line /t_struct_test.go:240499 t.ErrorTrace("Stack:\n")500 test.EqualStr(tt, ttt.LastMessage(), `Stack:501 Empty stack trace`)502}503func TestFatalTrace(tt *testing.T) {504 ttt := test.NewTestingTB(tt.Name())505 t := td.NewT(ttt)506 match := func(got, expectedRe string) {507 tt.Helper()508 re := regexp.MustCompile(expectedRe)509 if !re.MatchString(got) {510 test.EqualErrorMessage(tt, got, expectedRe)511 }512 }513//line /t_struct_test.go:300514 match(ttt.CatchFatal(func() { t.FatalTrace() }), `Stack trace:515 TestFatalTrace\.func\d\(\) /t_struct_test\.go:300516 \(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+517 TestFatalTrace\(\) /t_struct_test\.go:300`)518 test.IsTrue(tt, ttt.HasFailed)519 test.IsTrue(tt, ttt.IsFatal)520 ttt.ResetMessages()521//line /t_struct_test.go:310522 match(ttt.CatchFatal(func() { t.FatalTrace("This is the %s:", "stack") }),523 `This is the stack:524 TestFatalTrace\.func\d\(\) /t_struct_test\.go:310525 \(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+526 TestFatalTrace\(\) /t_struct_test\.go:310`)527 ttt.ResetMessages()528//line /t_struct_test.go:320529 match(ttt.CatchFatal(func() { t.FatalTrace("This is the %s:\n", "stack") }),530 `This is the stack:531 TestFatalTrace\.func\d\(\) /t_struct_test\.go:320532 \(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+533 TestFatalTrace\(\) /t_struct_test\.go:320`)534 ttt.ResetMessages()535//line /t_struct_test.go:330536 match(ttt.CatchFatal(func() { t.FatalTrace("This is the ", "stack") }),537 `This is the stack538 TestFatalTrace\.func\d\(\) /t_struct_test\.go:330539 \(\*TestingT\)\.CatchFatal\(\) internal/test/types\.go:\d+540 TestFatalTrace\(\) /t_struct_test\.go:330`)541 ttt.ResetMessages()542 trace.IgnorePackage()543 defer trace.UnignorePackage()544//line /t_struct_test.go:340545 test.EqualStr(tt, ttt.CatchFatal(func() { t.FatalTrace("Stack:\n") }),546 `Stack:547 Empty stack trace`)548}...

Full Screen

Full Screen

root.go

Source:root.go Github

copy

Full Screen

...27func getConnectedDatabase(con TConnectionConfig) (*sql.DB) {28 db, err := sql.Open(29 "sqlserver",30 mfa.Format("url", "sqlserver://{{.User}}:{{.Password}}@{{.Url}}?sendStringParametersAsUnicode={{.SendStringParametersAsUnicode}}&prepareSQL={{.PrepareSQL}}&log={{.Log}}&database={{.Database}}", con))31 mfa.CatchFatal(err)32 mfa.CatchFatal(db.Ping())33 mfa.CatchFatal(err)34 var dbname string35 mfa.CatchFatal(db.QueryRow("select DB_NAME()").Scan(&dbname))36 if SelectedConnectionConfig.Name != dbname {37 log.Print("Actual database is " + dbname + " trying use...")38 _, err = db.Exec("use " + con.Name)39 mfa.CatchFatal(err)40 }41 err = db.QueryRow(`42 select 43 [edition] = SERVERPROPERTY('Edition'),44 [version] = SERVERPROPERTY ('productversion'),45 [level] = SERVERPROPERTY('ProductLevel')46 `).Scan(&SqlServerVersion.edition, &SqlServerVersion.versionStr, &SqlServerVersion.level)47 mfa.CatchFatal(err)48 _, err = fmt.Sscanf(49 SqlServerVersion.versionStr, 50 "%d.%d.%d.%d",51 &(SqlServerVersion.version)[0],52 &(SqlServerVersion.version)[1],53 &(SqlServerVersion.version)[2],54 &(SqlServerVersion.version)[3])55 fmt.Printf("Connected to %s\n%s %s\n\n", con.Url, SqlServerVersion.edition, SqlServerVersion.versionStr)56 mfa.CatchFatal(err)57 58 return db59}60/*61func schemaLocallyAvailable(schema *TSchema) bool {62 var schemaRoot string63 64 switch {65 case len(schema.localDir) > 0:66 schemaRoot = schema.localDir67 case schema.Getter == "" || schema.Getter == "file":68 switch {69 case len(schema.Url) > 0:70 schemaRoot = filepath.Join(WorkingDirectory, "schemas", schema.Url)71 case len(schema.Name) > 0:72 schemaRoot = filepath.Join(WorkingDirectory, "schemas", schema.Name)73 }74 }75 76 if _, err := os.Stat(schemaRoot); err == nil {77 return true78 } else if os.IsNotExist(err) {79 return false80 } else {81 // Schrodinger: file may or may not exist. See err for details.82 // Therefore, do *NOT* use !os.IsNotExist(err) to test for file existence83 errors.New("Failed to determine if '" + schemaRoot + "' exists.")84 }85 return false86}87*/88// Find schema locally or download it89/*90func Get(schema *TSchema) {91 if schemaLocallyAvailable(schema) {92 return93 }94 switch schema.Getter {95 case "git":96 // Create temporary directory to clone the repository (will be moved)97 tmpPath := filepath.Join(WorkingDirectory, "schemas")98 mfa.CatchFatal(os.MkdirAll(tmpPath, os.ModePerm))99 tmpDir, err := ioutil.TempDir(tmpPath, "getting")100 mfa.CatchFatal(err)101 _, err = git.PlainClone(tmpDir, false, &git.CloneOptions{102 URL: schema.Url,103 })104 mfa.CatchFatal(err)105 // Read schema definition from downloaded files106 yamlFile, err := ioutil.ReadFile(filepath.Join(tmpDir, "schema.yaml"))107 mfa.CatchFatal(err)108 err = yaml.Unmarshal(yamlFile, &schema)109 mfa.CatchFatal(err)110 111 // Move file to schemas/{name}112 schema.localDir = filepath.Join(WorkingDirectory, "schemas", schema.Name)113 if schemaLocallyAvailable(schema) {114 defer os.RemoveAll(tmpDir)115 } else {116 mfa.CatchFatal(os.Rename(tmpDir, schema.localDir))117 }118 case "sqlserver":119 // TODO [mfa] read schema contents from database120 default:121 // Treat as local directory122 schema.localDir = schema.Url123 yamlFile, err := ioutil.ReadFile(filepath.Join(schema.localDir, "schema.yaml"))124 mfa.CatchFatal(err)125 err = yaml.Unmarshal(yamlFile, &schema)126 mfa.CatchFatal(err)127 }128 fmt.Println("[pulled] " + schema.Name + " at " + schema.localDir)129}130*/131//func Uninstall(schema *TSchema) {132 // Get(schema)133 // Scan(schema)134 /*135 rows, err := DB.Query("select [name], [type] from sys.objects where schema_id = SCHEMA_ID('" + "') and [type] in ('P', 'FN')")136 mfa.CatchFatal(err)137 defer rows.Close()138 var (139 name string140 objectType string)141 for rows.Next() {142 err := rows.Scan(&name, &objectType)143 switch objectType {144 case "P":145 mfa.CatchFatal(DB.Exec("DROP PROCEDURE " + name))146 case "FN":147 mfa.CatchFatal(DB.Exec("DROP FUNCTION " + name))148 }149 }150 // SELECT * FROM sys.objects WHERE schema_id = SCHEMA_ID('...') where type in ('FN', 'P')151 // column name: , ...152 // column type: { FN (SQL_SCALAR_FUNCTION), U (USER_TABLE), PK (PRIMARY_KEY_CONSTRAINT), P (SQL_STORED_PROCEDURE) }153 */154 // Remove subpackages155 // for i := 0; i < len(schema.Packages); i++ {156 // Uninstall(&schema.Packages[i])157 // fmt.Println(schema.Packages[i].Name, schema.Packages[i].localDir)158 // }159 /*160 // Remove package161 for _, script := range schema.UninstallScripts {162 fmt.Print("[running] ", script, " ...")163 err := execBatchesFromFile(filepath.Join(schema.localDir, script))164 if err != nil {165 color.Yellow.Println("[ERROR] ", err)166 }167 fmt.Println("done")168 }169 */170 // Remove schema-info from database171 // DB.Exec(fmt.Sprintf("DROP FUNCTION [%s].[SCHEMA_INFO]()", schema.Name))172 // DB.Exec(fmt.Sprintf("DROP SCHEMA [%s]", schema.Name))173//}174type SQLError interface {175 SQLErrorNumber() int32176 SQLErrorState() uint8177 SQLErrorClass() uint8178 SQLErrorMessage() string179 SQLErrorServerName() string180 SQLErrorProcName() string181 SQLErrorLineNo() int32182}183var batchSeparator = regexp.MustCompile(`(\s+|^)GO\s+`)184func execBatchesFromFile(path string) (error, string, SQLError) {185 content, err := ioutil.ReadFile(path)186 mfa.CatchFatal(err)187 var readLines int = 0188 189 for _, batch := range batchSeparator.Split(string(content), -1) {190 Lint([]byte(batch), readLines)191 _, err = DB.Exec(batch)192 if err != nil {193 if sqlError, ok := err.(SQLError); ok {194 return err, batch, sqlError195 } else {196 return err, batch, nil197 }198 }199 readLines += strings.Count(batch, "\n") +1200 }201 return nil, "", nil202}203/*204func Pull() {205 for _, schema := range schemas {206 err = DB.QueryRow("select [" + schema.Name + "].[SCHEMA_INFO]()").Scan(&schema.Version)207 if err != nil {208 } else {209 result = append(result, schema)210 fmt.Println("-", schema.Name, "@", schema.Version)211 }212 }213 exec sp_helptext 'dbo.proc_akquisestamm_detail'214 SELECT * FROM sys.all_objects215WHERE ([type] = 'P' OR [type] = 'X' OR [type] = 'PC')216ORDER BY [name];217go218for each object function219 EXEC sp_helptext N'..'220}221*/222func Connect() {223 DB = getConnectedDatabase(SelectedConnectionConfig)224}225// func init() {226 // Disable TLS certificate checks for 227 /*228 customClient := &http.Client {229 Transport: &http.Transport {230 TLSClientConfig: &tls.Config{ InsecureSkipVerify: true },231 },232 }233 client.InstallProtocol("https", githttp.NewClient(customClient))234 */235 236 // var version string237 // mfa.CatchFatal(DB.QueryRow("select ").Scan(&version))238 // log.Print("version", version)239// }240/*241pull schema from database242# assuming state243C CHECK_CONSTRAINT244F FOREIGN_KEY_CONSTRAINT245U USER_TABLE246IT INTERNAL_TABLE247S SYSTEM_TABLE248D DEFAULT_CONSTRAINT249PK PRIMARY_KEY_CONSTRAINT250TF SQL_TABLE_VALUED_FUNCTION251TR SQL_TRIGGER...

Full Screen

Full Screen

logger_test.go

Source:logger_test.go Github

copy

Full Screen

1package mock2import "testing"3type commandType int4const (5 Log commandType = iota6 Logf7 Error8 Errorf9 Debug10 Debugf11 Fatal12 Fatalf13 LoggedIs14 Clear15)16type command struct {17 cmd commandType18 args []interface{}19}20func newCommand(cmd commandType, args ...interface{}) *command {21 return &command{22 cmd: cmd,23 args: args,24 }25}26//returning false stop execution27func (c *command) testExecute(t *testing.T, b *Logger) bool {28 switch c.cmd {29 case Log:30 b.Log(c.args...)31 case Logf:32 b.Logf(c.args[0].(string), c.args[1:]...)33 case Error:34 b.Error(c.args...)35 case Errorf:36 b.Errorf(c.args[0].(string), c.args[1:]...)37 case Debug:38 b.Debug(c.args...)39 case Debugf:40 b.Debugf(c.args[0].(string), c.args[1:]...)41 case Fatal:42 if !catchFatal(func() { b.Fatal(c.args...) }) {43 t.Errorf("fatal didn't panic :(")44 }45 case Fatalf:46 if !catchFatal(func() { b.Fatalf(c.args[0].(string), c.args[1:]...) }) {47 t.Errorf("fatal didn't panic :(")48 }49 case LoggedIs:50 logged := b.Logged()51 expectedLength, actualLength := len(c.args), len(logged)52 if expectedLength != actualLength {53 t.Errorf("Different lengths of expected (%d) and actual (%d) logged messages.", expectedLength, actualLength)54 return false55 }56 for index, expected := range c.args {57 actual := logged[index]58 if expected != actual {59 t.Errorf("Log has wrong contents on index %d expected `%s` but got `%s`", index, expected, actual)60 return false61 }62 }63 case Clear:64 b.Clear()65 default:66 return false67 }68 return true69}70func catchFatal(f func()) (result bool) {71 defer func() {72 if p := recover(); p != "" {73 result = true74 }75 }()76 result = false77 f()78 return79}80func TestLogging(t *testing.T) {81 t.Parallel()82 var testMat = []*command{83 newCommand(LoggedIs),84 newCommand(Log, "arg1"),85 newCommand(Logf, `arg%d%s`, 2, "3"),86 newCommand(LoggedIs, `Log:arg1`, `Log:arg23`),87 newCommand(Error, "err1"),88 newCommand(Debugf, "debug1"),89 newCommand(LoggedIs, `Log:arg1`, `Log:arg23`, `Error:err1`, `Debug:debug1`),90 newCommand(Fatalf, "%s%s%s", "fa", "ta", "l"),91 newCommand(LoggedIs, `Log:arg1`, `Log:arg23`, `Error:err1`, `Debug:debug1`, `Fatal:fatal`),92 newCommand(Clear),93 newCommand(LoggedIs),94 }95 logger := NewLogger()96 for index, test := range testMat {97 if !test.testExecute(t, logger) {98 t.Errorf("Command number %d made the test stop", index)99 return100 }101 }102}...

Full Screen

Full Screen

CatchFatal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 test.CatchFatal(func() {4 fmt.Println("Hello")5 panic("Error")6 fmt.Println("World")7 })8}

Full Screen

Full Screen

CatchFatal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 logger.Log("Starting test...")4 t := test.NewTest("Test 2")5 t.Assert("1 + 1 = 2", 1+1 == 2)6 t.Assert("1 + 1 = 3", 1+1 == 3)7 t.Assert("1 + 1 = 3", 1+1 == 3)8 t.Assert("1 + 1 = 3", 1+1 == 3)9 t.Assert("1 + 1 = 3", 1+1 == 3)10 t.Assert("1 + 1 = 3", 1+1 == 3)11 t.Assert("1 + 1 = 3", 1+1 == 3)12 t.Assert("1 + 1 = 3", 1+1 == 3)13 t.Assert("1 + 1 = 3", 1+1 == 3)14 t.Assert("1 + 1 = 3", 1+1 == 3)15 t.Assert("1 + 1 = 3", 1+1 == 3)16 t.Assert("1 + 1 = 4", 1+1 == 4)17 t.Assert("1 + 1 = 5", 1+1 == 5)18 t.Assert("1 + 1 = 6", 1+1 == 6)19 t.Assert("1 + 1 = 7", 1+1 == 7)20 t.Assert("1 + 1 = 8", 1+1 == 8)21 t.Assert("1 + 1 = 9", 1+1 == 9)22 t.Assert("1 + 1 = 10", 1+1 == 10)23 t.Assert("1 + 1 = 11", 1+1 == 11)24 t.Assert("1 + 1 = 12", 1+1 == 12)25 t.Assert("1 + 1 = 13", 1+1 == 13)26 t.Assert("1 + 1 = 14", 1+1 == 14)27 t.Assert("1 + 1 = 15", 1+1 ==

Full Screen

Full Screen

CatchFatal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 goltest.CatchFatal(func() {4 fmt.Println("Hello")5 })6}7import (8func main() {9 goltest.CatchFatal(func() {10 fmt.Println("Hello")11 }, "Hello")12}13import (14func main() {15 goltest.CatchFatal(func() {16 fmt.Println("Hello")17 }, "Hello2")18}19import (20func main() {21 goltest.CatchFatal(func() {22 fmt.Println("Hello")23 }, "Hello2")24}25import (26func main() {27 goltest.CatchFatal(func() {28 fmt.Println("Hello")29 }, "Hello2")30}31import (32func main() {33 goltest.CatchFatal(func() {34 fmt.Println("Hello")35 }, "Hello2")36}37import (38func main() {39 goltest.CatchFatal(func() {40 fmt.Println("Hello

Full Screen

Full Screen

CatchFatal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 test.CatchFatal(func() {4 fmt.Println("I am going to panic")5 panic("panic")6 })7}

Full Screen

Full Screen

CatchFatal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Welcome to catch fatal example")4 gotest.CatchFatal("This is a fatal error")5 fmt.Println("This will not be printed")6}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful