|
1 | 1 | package gosnowflake
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "database/sql/driver"
|
5 | 6 | "fmt"
|
6 | 7 | "reflect"
|
| 8 | + "strconv" |
| 9 | + "strings" |
7 | 10 | "testing"
|
8 | 11 | "time"
|
9 | 12 | )
|
@@ -409,3 +412,165 @@ func TestHybridTablesE2E(t *testing.T) {
|
409 | 412 | })
|
410 | 413 | })
|
411 | 414 | }
|
| 415 | + |
| 416 | +func TestHTAPOptimizations(t *testing.T) { |
| 417 | + if runningOnGithubAction() { |
| 418 | + t.Skip("insufficient permissions") |
| 419 | + } |
| 420 | + for _, useHtapOptimizations := range []bool{true, false} { |
| 421 | + runSnowflakeConnTest(t, func(sct *SCTest) { |
| 422 | + t.Run("useHtapOptimizations="+strconv.FormatBool(useHtapOptimizations), func(t *testing.T) { |
| 423 | + if useHtapOptimizations { |
| 424 | + sct.mustExec("ALTER SESSION SET ENABLE_SNOW_654741_FOR_TESTING = true", nil) |
| 425 | + } |
| 426 | + runID := time.Now().UnixMilli() |
| 427 | + t.Run("Schema", func(t *testing.T) { |
| 428 | + newSchema := fmt.Sprintf("test_schema_%v", runID) |
| 429 | + if strings.EqualFold(sct.sc.cfg.Schema, newSchema) { |
| 430 | + t.Errorf("schema should not be switched") |
| 431 | + } |
| 432 | + |
| 433 | + sct.mustExec(fmt.Sprintf("CREATE SCHEMA %v", newSchema), nil) |
| 434 | + defer sct.mustExec(fmt.Sprintf("DROP SCHEMA %v", newSchema), nil) |
| 435 | + |
| 436 | + if !strings.EqualFold(sct.sc.cfg.Schema, newSchema) { |
| 437 | + t.Errorf("schema should be switched, expected %v, got %v", newSchema, sct.sc.cfg.Schema) |
| 438 | + } |
| 439 | + |
| 440 | + query := sct.mustQuery("SELECT 1", nil) |
| 441 | + query.Close() |
| 442 | + |
| 443 | + if !strings.EqualFold(sct.sc.cfg.Schema, newSchema) { |
| 444 | + t.Errorf("schema should be switched, expected %v, got %v", newSchema, sct.sc.cfg.Schema) |
| 445 | + } |
| 446 | + }) |
| 447 | + t.Run("Database", func(t *testing.T) { |
| 448 | + newDatabase := fmt.Sprintf("test_database_%v", runID) |
| 449 | + if strings.EqualFold(sct.sc.cfg.Database, newDatabase) { |
| 450 | + t.Errorf("database should not be switched") |
| 451 | + } |
| 452 | + |
| 453 | + sct.mustExec(fmt.Sprintf("CREATE DATABASE %v", newDatabase), nil) |
| 454 | + defer sct.mustExec(fmt.Sprintf("DROP DATABASE %v", newDatabase), nil) |
| 455 | + |
| 456 | + if !strings.EqualFold(sct.sc.cfg.Database, newDatabase) { |
| 457 | + t.Errorf("database should be switched, expected %v, got %v", newDatabase, sct.sc.cfg.Database) |
| 458 | + } |
| 459 | + |
| 460 | + query := sct.mustQuery("SELECT 1", nil) |
| 461 | + query.Close() |
| 462 | + |
| 463 | + if !strings.EqualFold(sct.sc.cfg.Database, newDatabase) { |
| 464 | + t.Errorf("database should be switched, expected %v, got %v", newDatabase, sct.sc.cfg.Database) |
| 465 | + } |
| 466 | + }) |
| 467 | + t.Run("Warehouse", func(t *testing.T) { |
| 468 | + newWarehouse := fmt.Sprintf("test_warehouse_%v", runID) |
| 469 | + if strings.EqualFold(sct.sc.cfg.Warehouse, newWarehouse) { |
| 470 | + t.Errorf("warehouse should not be switched") |
| 471 | + } |
| 472 | + |
| 473 | + sct.mustExec(fmt.Sprintf("CREATE WAREHOUSE %v", newWarehouse), nil) |
| 474 | + defer sct.mustExec(fmt.Sprintf("DROP WAREHOUSE %v", newWarehouse), nil) |
| 475 | + |
| 476 | + if !strings.EqualFold(sct.sc.cfg.Warehouse, newWarehouse) { |
| 477 | + t.Errorf("warehouse should be switched, expected %v, got %v", newWarehouse, sct.sc.cfg.Warehouse) |
| 478 | + } |
| 479 | + |
| 480 | + query := sct.mustQuery("SELECT 1", nil) |
| 481 | + query.Close() |
| 482 | + |
| 483 | + if !strings.EqualFold(sct.sc.cfg.Warehouse, newWarehouse) { |
| 484 | + t.Errorf("warehouse should be switched, expected %v, got %v", newWarehouse, sct.sc.cfg.Warehouse) |
| 485 | + } |
| 486 | + }) |
| 487 | + t.Run("Role", func(t *testing.T) { |
| 488 | + if strings.EqualFold(sct.sc.cfg.Role, "PUBLIC") { |
| 489 | + t.Errorf("role should not be public for this test") |
| 490 | + } |
| 491 | + |
| 492 | + sct.mustExec("USE ROLE public", nil) |
| 493 | + |
| 494 | + if !strings.EqualFold(sct.sc.cfg.Role, "PUBLIC") { |
| 495 | + t.Errorf("role should be switched, expected public, got %v", sct.sc.cfg.Role) |
| 496 | + } |
| 497 | + |
| 498 | + query := sct.mustQuery("SELECT 1", nil) |
| 499 | + query.Close() |
| 500 | + |
| 501 | + if !strings.EqualFold(sct.sc.cfg.Role, "PUBLIC") { |
| 502 | + t.Errorf("role should be switched, expected public, got %v", sct.sc.cfg.Role) |
| 503 | + } |
| 504 | + }) |
| 505 | + t.Run("Session param - DATE_OUTPUT_FORMAT", func(t *testing.T) { |
| 506 | + if !strings.EqualFold(*sct.sc.cfg.Params["date_output_format"], "YYYY-MM-DD") { |
| 507 | + t.Errorf("should use default date_output_format, but got: %v", *sct.sc.cfg.Params["date_output_format"]) |
| 508 | + } |
| 509 | + |
| 510 | + sct.mustExec("ALTER SESSION SET DATE_OUTPUT_FORMAT = 'DD-MM-YYYY'", nil) |
| 511 | + defer sct.mustExec("ALTER SESSION SET DATE_OUTPUT_FORMAT = 'YYYY-MM-DD'", nil) |
| 512 | + |
| 513 | + if !strings.EqualFold(*sct.sc.cfg.Params["date_output_format"], "DD-MM-YYYY") { |
| 514 | + t.Errorf("date output format should be switched, expected DD-MM-YYYY, got %v", sct.sc.cfg.Params["date_output_format"]) |
| 515 | + } |
| 516 | + |
| 517 | + query := sct.mustQuery("SELECT 1", nil) |
| 518 | + query.Close() |
| 519 | + |
| 520 | + if !strings.EqualFold(*sct.sc.cfg.Params["date_output_format"], "DD-MM-YYYY") { |
| 521 | + t.Errorf("date output format should be switched, expected DD-MM-YYYY, got %v", sct.sc.cfg.Params["date_output_format"]) |
| 522 | + } |
| 523 | + }) |
| 524 | + }) |
| 525 | + }) |
| 526 | + } |
| 527 | +} |
| 528 | + |
| 529 | +func TestConnIsCleanAfterClose(t *testing.T) { |
| 530 | + // We create a new db here to not use the default pool as we can leave it in dirty state. |
| 531 | + t.Skip("Fails, because connection is returned to a pool dirty") |
| 532 | + ctx := context.Background() |
| 533 | + runID := time.Now().UnixMilli() |
| 534 | + |
| 535 | + db := openDB(t) |
| 536 | + defer db.Close() |
| 537 | + db.SetMaxOpenConns(1) |
| 538 | + |
| 539 | + conn, err := db.Conn(ctx) |
| 540 | + if err != nil { |
| 541 | + t.Fatal(err) |
| 542 | + } |
| 543 | + defer conn.Close() |
| 544 | + |
| 545 | + dbt := DBTest{t, conn} |
| 546 | + |
| 547 | + dbt.mustExec(forceJSON) |
| 548 | + |
| 549 | + var dbName string |
| 550 | + rows1 := dbt.mustQuery("SELECT CURRENT_DATABASE()") |
| 551 | + rows1.Next() |
| 552 | + rows1.Scan(&dbName) |
| 553 | + |
| 554 | + newDbName := fmt.Sprintf("test_database_%v", runID) |
| 555 | + dbt.mustExec("CREATE DATABASE " + newDbName) |
| 556 | + |
| 557 | + rows1.Close() |
| 558 | + conn.Close() |
| 559 | + |
| 560 | + conn2, err := db.Conn(ctx) |
| 561 | + if err != nil { |
| 562 | + t.Fatal(err) |
| 563 | + } |
| 564 | + |
| 565 | + dbt2 := DBTest{t, conn2} |
| 566 | + |
| 567 | + var dbName2 string |
| 568 | + rows2 := dbt2.mustQuery("SELECT CURRENT_DATABASE()") |
| 569 | + defer rows2.Close() |
| 570 | + rows2.Next() |
| 571 | + rows2.Scan(&dbName2) |
| 572 | + |
| 573 | + if !strings.EqualFold(dbName, dbName2) { |
| 574 | + t.Errorf("fresh connection from pool should have original database") |
| 575 | + } |
| 576 | +} |
0 commit comments