123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package storage
- import (
- "log"
- "testing"
- )
- type void struct{}
- type voidSet map[string]void
- var purgeList voidSet
- /*
- setUp() initializes a test database.
- - tablesToTruncate is a slice of names of tables to truncate during initialization.
- If any of these tables are not present in the database, the function will fatal out.
- Use defer tearDown(t) to clean the database at the end of a test.
- Implementation note:
- In the current implementation, truncation is performed using SQL DELETE, not
- TRUNCATE, because MySQL 5.x cannot TRUNCATE the master table in a master/child
- referential integrity constraint, even when the child table is empty. This could
- smarter.
- // FIXME stop using constant credentials
- */
- func setUp(t *testing.T, entitiesToPurge []StorageController) {
- var err error
- var DSN = "goroot:gopass@tcp(localhost:3306)/go_kurz_test"
- var name string = ""
- var voidNil void = void{}
- var tableNames voidSet = make(voidSet)
- Service.SetDSN(DSN)
- err = Service.Open()
- if err != nil {
- t.Fatalf("Failed opening the test database: %+v", err)
- }
- sql := "SHOW TABLES"
- rows, err := Service.DB.Query(sql)
- if err != nil {
- log.Fatalf("Failed listing tables in the database: %+v\n", err)
- }
- defer rows.Close()
- purgeList = make(voidSet)
- for rows.Next() {
- if err = rows.Scan(&name); err != nil {
- log.Fatalf("Failed scanning table name: %+v\n", err)
- }
- tableNames[name] = voidNil
- }
- log.Print("Tables:", tableNames)
- for _, entityName := range entitiesToPurge {
- tableName := entityName.Table()
- log.Printf("Checking %s\n", tableName)
- if _, ok := tableNames[tableName]; !ok {
- log.Fatalf("Table %s can not be found in the database. Aborting\n", tableName)
- }
- purgeList[tableName] = voidNil
- Service.Truncate(name)
- }
- }
- func tearDown(t *testing.T) {
- if purgeList == nil {
- log.Fatalf("Truncate list may not be nil : it has been initialized in initTermStorage. Aborting")
- }
- for name, _ := range purgeList {
- Service.Truncate(name)
- }
- Service.Close()
- }
- func TestSave(t *testing.T) {
- setUp(t, []StorageController{EventInfoService})
- defer tearDown(t)
- }
- func TestLoad(t *testing.T) {
- setUp(t, []StorageController{EventInfoService})
- defer tearDown(t)
- }
|