1
0
mirror of https://github.com/ManyakRus/starter.git synced 2025-11-26 23:10:42 +02:00
Files
starter/postgres_gorm/postgres_gorm_test.go

178 lines
3.5 KiB
Go
Raw Normal View History

2023-03-17 11:20:09 +03:00
package postgres_gorm
import (
"errors"
"testing"
2024-03-04 18:00:03 +03:00
"time"
2023-03-17 11:20:09 +03:00
//log "github.com/sirupsen/logrus"
2023-12-26 16:42:53 +03:00
"github.com/ManyakRus/starter/config_main"
2023-05-02 09:51:50 +03:00
"github.com/ManyakRus/starter/contextmain"
"github.com/ManyakRus/starter/micro"
2023-03-17 11:20:09 +03:00
2023-05-02 09:51:50 +03:00
// logger "github.com/ManyakRus/starter/common/v0/logger"
"github.com/ManyakRus/starter/stopapp"
2023-03-17 11:20:09 +03:00
)
func TestConnect_err(t *testing.T) {
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
err := Connect_err()
if err != nil {
t.Error("TestConnect error: ", err)
}
err = CloseConnection_err()
if err != nil {
t.Error("TestConnect() error: ", err)
}
}
func TestIsClosed(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
err := Connect_err()
if err != nil {
t.Error("TestIsClosed Connect() error: ", err)
}
isClosed := IsClosed()
if isClosed == true {
t.Error("TestIsClosed() isClosed = true ")
}
err = CloseConnection_err()
if err != nil {
t.Error("TestIsClosed() CloseConnection() error: ", err)
}
}
func TestReconnect(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
err := Connect_err()
if err != nil {
t.Error("TestIsClosed Connect() error: ", err)
}
//ctx := context.Background()
Reconnect(errors.New(""))
err = CloseConnection_err()
if err != nil {
t.Error("TestReconnect() CloseConnection() error: ", err)
}
}
func TestWaitStop(t *testing.T) {
stopapp.StartWaitStop()
stopapp.GetWaitGroup_Main().Add(1)
go WaitStop()
micro.Pause(10)
//stopapp.SignalInterrupt <- syscall.SIGINT
contextmain.CancelContext()
}
func TestStartDB(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
StartDB()
err := CloseConnection_err()
if err != nil {
t.Error("db_test.TestStartDB() CloseConnection() error: ", err)
}
}
func TestConnect(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
Connect()
CloseConnection()
}
func TestGetConnection(t *testing.T) {
//ProgramDir := micro.ProgramDir_Common()
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-03-17 11:20:09 +03:00
GetConnection()
CloseConnection()
}
func TestConnect_WithApplicationName_err(t *testing.T) {
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
err := Connect_WithApplicationName_err("starter test")
if err != nil {
t.Error("TestConnect_WithApplicationName_err error: ", err)
}
//micro.Pause(60 * 1000)
err = CloseConnection_err()
if err != nil {
t.Error("TestConnect_WithApplicationName_err() error: ", err)
}
}
2023-12-21 13:28:59 +03:00
func TestRawMultipleSQL(t *testing.T) {
2023-12-26 16:42:53 +03:00
config_main.LoadEnv()
2023-12-21 13:28:59 +03:00
GetConnection()
defer CloseConnection()
2024-03-04 18:00:03 +03:00
TimeStart := time.Now()
TextSQL := `
drop table if exists temp_TestRawMultipleSQL2;
CREATE TEMPORARY TABLE temp_TestRawMultipleSQL2 (id int);
insert into temp_TestRawMultipleSQL2
select 1;
SELECT * FROM temp_TestRawMultipleSQL2
`
//TextSQL := "SELECT 1; SELECT 2"
tx := RawMultipleSQL(Conn, TextSQL)
err := tx.Error
if err != nil {
t.Error("TestRawMultipleSQL() error: ", err)
}
t.Log("Прошло время: ", time.Since(TimeStart))
}
func TestRawMultipleSQL2(t *testing.T) {
config_main.LoadEnv()
GetConnection()
defer CloseConnection()
TimeStart := time.Now()
TextSQL := `
drop table if exists temp_TestRawMultipleSQL2;
CREATE TEMPORARY TABLE temp_TestRawMultipleSQL2 (id int);
insert into temp_TestRawMultipleSQL2
select 1;
SELECT * FROM temp_TestRawMultipleSQL2
`
2023-12-21 13:30:30 +03:00
tx := RawMultipleSQL(Conn, TextSQL)
err := tx.Error
2023-12-21 13:28:59 +03:00
if err != nil {
t.Error("TestRawMultipleSQL() error: ", err)
}
2024-03-04 18:00:03 +03:00
if tx.RowsAffected != 1 {
t.Error("TestRawMultipleSQL() RowsAffected = ", tx.RowsAffected)
}
t.Log("Прошло время: ", time.Since(TimeStart))
2023-12-21 13:28:59 +03:00
}