info_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package storage
  2. import (
  3. "log"
  4. "testing"
  5. )
  6. type void struct{}
  7. type voidSet map[string]void
  8. var purgeList voidSet
  9. /*
  10. setUp() initializes a test database.
  11. - tablesToTruncate is a slice of names of tables to truncate during initialization.
  12. If any of these tables are not present in the database, the function will fatal out.
  13. Use defer tearDown(t) to clean the database at the end of a test.
  14. Implementation note:
  15. In the current implementation, truncation is performed using SQL DELETE, not
  16. TRUNCATE, because MySQL 5.x cannot TRUNCATE the master table in a master/child
  17. referential integrity constraint, even when the child table is empty. This could
  18. smarter.
  19. // FIXME stop using constant credentials
  20. */
  21. func setUp(t *testing.T, entitiesToPurge []StorageController) {
  22. var err error
  23. var DSN = "goroot:gopass@tcp(localhost:3306)/go_kurz_test"
  24. var name string = ""
  25. var voidNil void = void{}
  26. var tableNames voidSet = make(voidSet)
  27. Service.SetDSN(DSN)
  28. err = Service.Open()
  29. if err != nil {
  30. t.Fatalf("Failed opening the test database: %+v", err)
  31. }
  32. sql := "SHOW TABLES"
  33. rows, err := Service.DB.Query(sql)
  34. if err != nil {
  35. log.Fatalf("Failed listing tables in the database: %+v\n", err)
  36. }
  37. defer rows.Close()
  38. purgeList = make(voidSet)
  39. for rows.Next() {
  40. if err = rows.Scan(&name); err != nil {
  41. log.Fatalf("Failed scanning table name: %+v\n", err)
  42. }
  43. tableNames[name] = voidNil
  44. }
  45. log.Print("Tables:", tableNames)
  46. for _, entityName := range entitiesToPurge {
  47. tableName := entityName.Table()
  48. log.Printf("Checking %s\n", tableName)
  49. if _, ok := tableNames[tableName]; !ok {
  50. log.Fatalf("Table %s can not be found in the database. Aborting\n", tableName)
  51. }
  52. purgeList[tableName] = voidNil
  53. Service.Truncate(name)
  54. }
  55. }
  56. func tearDown(t *testing.T) {
  57. if purgeList == nil {
  58. log.Fatalf("Truncate list may not be nil : it has been initialized in initTermStorage. Aborting")
  59. }
  60. for name, _ := range purgeList {
  61. Service.Truncate(name)
  62. }
  63. Service.Close()
  64. }
  65. func TestSave(t *testing.T) {
  66. setUp(t, []StorageController{EventInfoService})
  67. defer tearDown(t)
  68. }
  69. func TestLoad(t *testing.T) {
  70. setUp(t, []StorageController{EventInfoService})
  71. defer tearDown(t)
  72. }