github.com/mmrath/gobase@v0.0.1/client/test/app_suite_test.go (about)

     1  package test
     2  
     3  import (
     4  	"log"
     5  	"github.com/mmrath/gobase/client/app"
     6  	"net/http/httptest"
     7  	"path/filepath"
     8  	"runtime"
     9  	"time"
    10  
    11  	"github.com/stretchr/testify/require"
    12  	"github.com/stretchr/testify/suite"
    13  	"github.com/mmrath/gobase/model"
    14  )
    15  
    16  type TestSuite struct {
    17  	suite.Suite
    18  	db     *model.DB
    19  	mailer *MockMailer
    20  	server *httptest.Server
    21  	cfg    app.Config
    22  }
    23  
    24  // SetupSuite setup at the beginning of test
    25  func (s *TestSuite) SetupSuite() {
    26  
    27  	_, file, _, _ := runtime.Caller(0)
    28  
    29  	root := filepath.Dir(filepath.Dir(file))
    30  	configRoot := filepath.Join(root, "resources")
    31  
    32  	cfg := app.LoadConfig(configRoot, "test")
    33  
    34  	mailer, err := NewMockMailer()
    35  	require.NoError(s.T(), err)
    36  	s.mailer = mailer
    37  	db, err := model.DBConn(cfg.DB)
    38  	require.NoError(s.T(), err)
    39  
    40  	s.db = db
    41  	s.cfg = cfg
    42  
    43  	server, err := app.BuildServer(cfg, mailer)
    44  	require.NoError(s.T(), err)
    45  	s.server = httptest.NewServer(server.Handler)
    46  }
    47  
    48  // TearDownSuite teardown at the end of test
    49  func (s *TestSuite) TearDownSuite() {
    50  	defer s.server.Close()
    51  }
    52  
    53  func (s *TestSuite) SetupTest() {
    54  	cleanDB(s.db)
    55  	createTestUser(s.db)
    56  }
    57  
    58  func cleanDB(db *model.DB) {
    59  	defer timeTrack(time.Now(), "truncate tables")
    60  	stmts := []string{
    61  		"TRUNCATE TABLE role_permission CASCADE",
    62  		"TRUNCATE TABLE user_group_role CASCADE",
    63  		"TRUNCATE TABLE user_group_user CASCADE",
    64  		"TRUNCATE TABLE user_group CASCADE",
    65  		"TRUNCATE TABLE user_role CASCADE",
    66  		"TRUNCATE TABLE role CASCADE",
    67  		"TRUNCATE TABLE user_credential CASCADE",
    68  		"TRUNCATE TABLE permission CASCADE",
    69  		"TRUNCATE TABLE user_account CASCADE",
    70  		"TRUNCATE TABLE country CASCADE",
    71  		"TRUNCATE TABLE currency CASCADE",
    72  		"TRUNCATE TABLE timezone CASCADE",
    73  		"TRUNCATE TABLE datetime_format CASCADE",
    74  		"TRUNCATE TABLE date_format CASCADE",
    75  		"TRUNCATE TABLE language CASCADE",
    76  		"TRUNCATE TABLE notification CASCADE",
    77  		"TRUNCATE TABLE notification_recipient CASCADE",
    78  		"TRUNCATE TABLE notification_attachment CASCADE",
    79  	}
    80  	executeStmts(db, stmts)
    81  }
    82  
    83  func createTestUser(db *model.DB) {
    84  	defer timeTrack(time.Now(), "create test user")
    85  	stmts := []string{
    86  		`INSERT INTO public.user_account(
    87  	id, first_name, last_name, email, phone_number, active, created_at, created_by, updated_at, updated_by, version)
    88  	VALUES (1, 'Test', 'Test', 'testuser@localhost', NULL, true, current_timestamp, 'test', current_timestamp, 'test', 1)`,
    89  	}
    90  	executeStmts(db, stmts)
    91  }
    92  
    93  func executeStmts(db *model.DB, stmts []string) {
    94  	for _, stmt := range stmts {
    95  		_, err := db.Exec(stmt)
    96  		if err != nil {
    97  			log.Printf("Error executing statement %s", stmt)
    98  			panic(err)
    99  		}
   100  	}
   101  }
   102  
   103  func timeTrack(start time.Time, name string) {
   104  	elapsed := time.Since(start)
   105  	log.Printf("%s took %s", name, elapsed)
   106  }