mysql_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package infrastructure
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "code.osinet.fr/fgm/kurz/domain"
  8. "github.com/pressly/goose"
  9. "github.com/spf13/viper"
  10. )
  11. func TestMySQLEmptyRepo(test *testing.T) {
  12. db := mySQLTestSetup(test)
  13. defer db.Close()
  14. test.Run("empty repo", func(subTest *testing.T) {
  15. _, err := domain.GetTargetURL("whatever")
  16. if err == nil {
  17. subTest.Error("Empty repository should not find a target for any URL")
  18. }
  19. s1, isNew, err := domain.GetShortURL(exampleValidHTTPURL)
  20. if err != nil {
  21. subTest.Error("Creating a short URL for a valid URL should not fail")
  22. }
  23. if !isNew {
  24. subTest.Error("The first short URL in an empty repository should be new")
  25. }
  26. s2, isNew, err := domain.GetShortURL(exampleValidHTTPURL)
  27. if err != nil {
  28. subTest.Error("Creating a short URL for a valid URL should not fail")
  29. }
  30. if isNew {
  31. subTest.Error("The second short URL for an already shortened URL should not be new")
  32. }
  33. if s1 != s2 {
  34. subTest.Error("The second short URL for an already shortened URL should be the same as the first one")
  35. }
  36. t, err := domain.GetTargetURL(s1)
  37. if err != nil {
  38. subTest.Error("Repository should find a target for an existing short URL")
  39. }
  40. if t != exampleValidHTTPURL {
  41. subTest.Error("Target URL for a short URL should match the target it was created for")
  42. }
  43. })
  44. mySQLTestTeardown(test, db)
  45. }
  46. func TestMySQLSad(test *testing.T) {
  47. db := mySQLTestSetup(test)
  48. defer db.Close()
  49. test.Run("Sad", func(subTest *testing.T) {
  50. _, _, err := domain.GetShortURL("")
  51. if err == nil {
  52. test.Error("Empty target URL has no valid short URL")
  53. }
  54. })
  55. mySQLTestTeardown(test, db)
  56. }
  57. /**
  58. mySQLTestSetup opens the database and initialized it from the production database schema.
  59. */
  60. func mySQLTestSetup(t *testing.T) *sql.DB {
  61. db, err := DbDial(ParseTestDbCred())
  62. if err != nil {
  63. panic(err)
  64. }
  65. // Ensure current schema for test DB.
  66. cwd, _ := os.Getwd()
  67. current, err := goose.GetDBVersion(db)
  68. if err != nil {
  69. t.Errorf("setup failed to obtain the test DB version: %s", err)
  70. t.FailNow()
  71. }
  72. versions, err := goose.CollectMigrations(cwd, current, int64((1<<63)-1))
  73. if err != nil {
  74. t.Errorf("setup failed to obtain the test DB migrations: %s", err)
  75. t.FailNow()
  76. }
  77. if versions != nil {
  78. err = goose.Up(db, cwd)
  79. if err != nil {
  80. t.Errorf("setup failed to upgrade the test DB schema: %s", err)
  81. t.FailNow()
  82. }
  83. }
  84. // Ensure empty test DB.
  85. _, err = db.Exec("DELETE FROM map")
  86. if err != nil {
  87. t.Error("setup failed to clean test database")
  88. }
  89. sr := MySQLShortURLRepository{db}
  90. tr := MySQLTargetURLRepository{db}
  91. domain.RegisterRepositories(sr, tr)
  92. return db
  93. }
  94. func mySQLTestTeardown(t *testing.T, db *sql.DB) {
  95. _, err := db.Exec("DELETE FROM map")
  96. if err != nil {
  97. t.Error("teardown failed to clean test database")
  98. }
  99. }
  100. func TestMain(m *testing.M) {
  101. viper.SetConfigName("config")
  102. viper.AddConfigPath(".")
  103. viper.AddConfigPath("$HOME/.kurz")
  104. err := viper.ReadInConfig()
  105. if err != nil {
  106. fmt.Fprintln(os.Stderr, err)
  107. os.Exit(2)
  108. }
  109. exitCode := m.Run()
  110. os.Exit(exitCode)
  111. }