-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
770 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package framework | ||
|
||
import ( | ||
"github.com/bmizerany/assert" | ||
_ "github.com/go-sql-driver/mysql" | ||
"log" | ||
"testing" | ||
) | ||
|
||
func TestSetup(t *testing.T) { | ||
t.Run("Create instance", func(t *testing.T) { | ||
cron.Setup() | ||
assert.NotEqual(t, nil, cron.scheduler) | ||
assert.NotEqual(t, nil, scheduler) | ||
}) | ||
} | ||
|
||
func TestAddEverySecond(t *testing.T) { | ||
t.Run("Add on global", func(t *testing.T) { | ||
cron.Setup() | ||
cron.scheduler = nil | ||
cron.AddEverySecond(10, func() { log.Println("hello") }) | ||
assert.NotEqual(t, nil, scheduler) | ||
assert.Equal(t, 1, scheduler.Len()) | ||
}) | ||
t.Run("Add on local", func(t *testing.T) { | ||
cron.Setup() | ||
cron.AddEverySecond(10, func() { log.Println("hello") }) | ||
assert.NotEqual(t, nil, cron.scheduler) | ||
assert.Equal(t, 1, cron.scheduler.Len()) | ||
}) | ||
} | ||
|
||
func TestAddEveryDay(t *testing.T) { | ||
t.Run("Add on global", func(t *testing.T) { | ||
cron.Setup() | ||
cron.scheduler = nil | ||
cron.AddEveryDay("1:00", func() { log.Println("hello") }) | ||
assert.NotEqual(t, nil, scheduler) | ||
assert.Equal(t, 1, scheduler.Len()) | ||
}) | ||
t.Run("Add on local", func(t *testing.T) { | ||
cron.Setup() | ||
cron.AddEveryDay("1:00", func() { log.Println("hello") }) | ||
assert.NotEqual(t, nil, cron.scheduler) | ||
assert.Equal(t, 1, cron.scheduler.Len()) | ||
}) | ||
} | ||
|
||
func TestStart(t *testing.T) { | ||
t.Run("Start on global", func(t *testing.T) { | ||
cron.Setup() | ||
cron.scheduler = nil | ||
cron.Start() | ||
assert.NotEqual(t, nil, scheduler) | ||
|
||
}) | ||
t.Run("Start on local", func(t *testing.T) { | ||
cron.Setup() | ||
cron.Start() | ||
assert.NotEqual(t, nil, cron.scheduler) | ||
}) | ||
} | ||
|
||
func TestStop(t *testing.T) { | ||
t.Run("Start on global", func(t *testing.T) { | ||
cron.Setup() | ||
cron.scheduler = nil | ||
cron.Stop() | ||
assert.NotEqual(t, nil, scheduler) | ||
}) | ||
t.Run("Start on local", func(t *testing.T) { | ||
cron.Setup() | ||
cron.Stop() | ||
assert.NotEqual(t, nil, cron.scheduler) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package framework | ||
|
||
import ( | ||
"database/sql" | ||
"github.com/DATA-DOG/go-sqlmock" | ||
"github.com/bmizerany/assert" | ||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/source/iofs" | ||
"testing" | ||
"testing/fstest" | ||
) | ||
|
||
func TestConnectMysql(t *testing.T) { | ||
db, _, _ := sqlmock.New() | ||
defer db.Close() | ||
|
||
t.Run("Connect to mock", func(t *testing.T) { | ||
dbMysql.connectSuccess(db) | ||
assert.NotEqual(t, nil, dbMysql.client) | ||
}) | ||
t.Run("Connect mysql", func(t *testing.T) { | ||
dbMysql.Host = ":33306" | ||
dbMysql.Username = "" | ||
dbMysql.Password = "" | ||
dbMysql.Database = "" | ||
assert.Panic(t, nil, func() { | ||
dbMysql.Connect() | ||
}) | ||
}) | ||
} | ||
|
||
func TestCheckClientMysql(t *testing.T) { | ||
db, _, _ := sqlmock.New() | ||
defer db.Close() | ||
|
||
t.Run("Error no connection", func(t *testing.T) { | ||
dbMysql.client = nil | ||
DatabaseMysql = nil | ||
assert.Equal(t, (*sql.DB)(nil), dbMysql.CheckClient()) | ||
}) | ||
t.Run("With global connection", func(t *testing.T) { | ||
dbMysql.client = nil | ||
DatabaseMysql = db | ||
assert.NotEqual(t, (*sql.DB)(nil), dbMysql.CheckClient()) | ||
}) | ||
t.Run("With local connection", func(t *testing.T) { | ||
dbMysql.client = db | ||
DatabaseMysql = nil | ||
assert.NotEqual(t, (*sql.DB)(nil), dbMysql.CheckClient()) | ||
}) | ||
} | ||
|
||
func TestMigrateDatabase(t *testing.T) { | ||
fsSample := fstest.MapFS{ | ||
"migration/1_test.up.sql": { | ||
Data: []byte("SELECT 1;"), | ||
}, | ||
"migration/1_test.down.sql": { | ||
Data: []byte("SELECT 1;"), | ||
}, | ||
} | ||
d, _ := iofs.New(fsSample, "migration") | ||
|
||
t.Run("Database connection fail", func(t *testing.T) { | ||
dbMysql.Host = "localhost:33306" | ||
dbMysql.Username = "test" | ||
dbMysql.Password = "test" | ||
dbMysql.Database = "test" | ||
assert.Panic(t, "dial tcp [::1]:33306: connectex: No connection could be made because the target machine actively refused it.", func() { | ||
dbMysql.Connect() | ||
dbMysql.MigrateDatabase(d) | ||
}) | ||
}) | ||
t.Run("Migrate success", func(t *testing.T) { | ||
assert.Panic(t, nil, func() { | ||
dbMysql.migrateHandling(nil) | ||
}) | ||
}) | ||
t.Run("Migrate no change", func(t *testing.T) { | ||
assert.Panic(t, nil, func() { | ||
dbMysql.migrateHandling(migrate.ErrNoChange) | ||
}) | ||
}) | ||
t.Run("Migrate Fail", func(t *testing.T) { | ||
assert.Panic(t, "database locked", func() { | ||
dbMysql.migrateHandling(migrate.ErrLocked) | ||
}) | ||
}) | ||
} | ||
|
||
func TestTableCheck(t *testing.T) { | ||
db, mock, _ := sqlmock.New() | ||
defer db.Close() | ||
|
||
rows := mock.NewRows([]string{"Tables_in"}). | ||
AddRow("one") | ||
|
||
mock.ExpectQuery("SHOW TABLES LIKE 'test'").WillReturnRows(rows) | ||
|
||
t.Run("Failure", func(t *testing.T) { | ||
dbMysql.client = nil | ||
DatabaseMysql = db | ||
dbMysql.TableCheck("test") | ||
assert.Equal(t, false, dbMysql.TableCheck("test")) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
package framework | ||
|
||
import ( | ||
"github.com/alicebob/miniredis" | ||
"github.com/bmizerany/assert" | ||
"github.com/go-redis/redis/v8" | ||
"log" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestConnectRedis(t *testing.T) { | ||
mr, err := miniredis.Run() | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
|
||
t.Run("Error no connection", func(t *testing.T) { | ||
redisConf := RedisDatabase{Host: mr.Addr(), Password: "hello", Database: 10} | ||
assert.Equal(t, (*redis.Client)(nil), redisConf.Connect()) | ||
}) | ||
t.Run("With connection", func(t *testing.T) { | ||
redisConf := RedisDatabase{Host: mr.Addr()} | ||
assert.NotEqual(t, (*redis.Client)(nil), redisConf.Connect()) | ||
}) | ||
} | ||
|
||
func TestCheckClientRedis(t *testing.T) { | ||
mr, err := miniredis.Run() | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
redisMock := RedisDatabase{Host: mr.Addr()} | ||
|
||
t.Run("Error no connection", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
assert.Equal(t, (*redis.Client)(nil), redisMock.CheckClient()) | ||
}) | ||
t.Run("With global connection", func(t *testing.T) { | ||
redisMock.Connect() | ||
redisMock.client = nil | ||
assert.NotEqual(t, (*redis.Client)(nil), redisMock.CheckClient()) | ||
}) | ||
t.Run("With local connection", func(t *testing.T) { | ||
redisMock.Connect() | ||
RedisDB = nil | ||
assert.NotEqual(t, (*redis.Client)(nil), redisMock.CheckClient()) | ||
}) | ||
} | ||
|
||
func TestCheckPrefix(t *testing.T) { | ||
redisMock := RedisDatabase{} | ||
|
||
t.Run("Error no connection", func(t *testing.T) { | ||
redisMock.Prefix = "" | ||
RedisPrefix = "" | ||
assert.Equal(t, "", redisMock.CheckPrefix()) | ||
}) | ||
t.Run("With global connection", func(t *testing.T) { | ||
target := "hello-world" | ||
redisMock.Prefix = "" | ||
RedisPrefix = target | ||
assert.Equal(t, target, redisMock.CheckPrefix()) | ||
}) | ||
t.Run("With local connection", func(t *testing.T) { | ||
target := "hello-world" | ||
redisMock.Prefix = target | ||
RedisPrefix = "" | ||
assert.Equal(t, target, redisMock.CheckPrefix()) | ||
}) | ||
} | ||
|
||
func TestSetCompress(t *testing.T) { | ||
mr, err := miniredis.Run() | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
redisMock := RedisDatabase{Host: mr.Addr()} | ||
redisMock.Connect() | ||
redisMock.client.WithTimeout(1 * time.Second) | ||
|
||
t.Run("Valid data", func(t *testing.T) { | ||
output := redisMock.SetCompress("test", "1", 1, "data") | ||
assert.Equal(t, true, output) | ||
}) | ||
t.Run("Long data", func(t *testing.T) { | ||
long := "" | ||
for i := 0; i < 100000; i++ { | ||
long += "a" | ||
} | ||
output := redisMock.SetCompress("test", "2", 1, long) | ||
assert.Equal(t, true, output) | ||
}) | ||
t.Run("Error disconnect", func(t *testing.T) { | ||
mr.Close() | ||
output := redisMock.Set("test", "1", 1, "data") | ||
assert.Equal(t, false, output) | ||
}) | ||
t.Run("Error no connection compress", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
output := redisMock.SetCompress("test", "1", 1, "data") | ||
assert.Equal(t, false, output) | ||
}) | ||
t.Run("Error no connection", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
output := redisMock.Set("test", "1", 1, "data") | ||
assert.Equal(t, false, output) | ||
}) | ||
} | ||
|
||
func TestGetCompress(t *testing.T) { | ||
mr, err := miniredis.Run() | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
redisMock := RedisDatabase{Host: mr.Addr()} | ||
redisMock.Connect() | ||
redisMock.client.WithTimeout(1 * time.Second) | ||
redisMock.SetCompress("test", "1", 1, "data") | ||
|
||
long := "" | ||
for i := 0; i < 100000; i++ { | ||
long += "a" | ||
} | ||
redisMock.SetCompress("long", "1", 1, long) | ||
|
||
t.Run("Valid data", func(t *testing.T) { | ||
isOkay, data := redisMock.GetCompress("test", "1") | ||
assert.Equal(t, true, isOkay) | ||
assert.Equal(t, "data", data) | ||
}) | ||
t.Run("Valid data decompress", func(t *testing.T) { | ||
isOkay, data := redisMock.GetCompress("long", "1") | ||
assert.Equal(t, true, isOkay) | ||
assert.Equal(t, long, data) | ||
}) | ||
t.Run("No data", func(t *testing.T) { | ||
isOkay, data := redisMock.GetCompress("test", "2") | ||
assert.Equal(t, false, isOkay) | ||
assert.Equal(t, "", data) | ||
}) | ||
t.Run("Error disconnect decompress", func(t *testing.T) { | ||
mr.Close() | ||
isOkay, _ := redisMock.GetCompress("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
t.Run("Error disconnect", func(t *testing.T) { | ||
mr.Close() | ||
isOkay, _ := redisMock.Get("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
t.Run("Error no client decompress", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
isOkay, _ := redisMock.GetCompress("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
t.Run("Error no client", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
isOkay, _ := redisMock.Get("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
} | ||
|
||
func TestRemove(t *testing.T) { | ||
mr, err := miniredis.Run() | ||
if err != nil { | ||
log.Fatalf("an error '%s' was not expected when opening a stub database connection", err) | ||
} | ||
redisMock := RedisDatabase{Host: mr.Addr()} | ||
redisMock.Connect() | ||
redisMock.client.WithTimeout(1 * time.Second) | ||
redisMock.SetCompress("test", "1", 1, "data") | ||
redisMock.SetCompress("test", "2", 1, "data") | ||
redisMock.SetCompress("test", "3", 1, "data") | ||
redisMock.SetCompress("test", "4", 1, "data") | ||
|
||
t.Run("Remove data", func(t *testing.T) { | ||
isOkay := redisMock.Remove("test", "1") | ||
assert.Equal(t, true, isOkay) | ||
}) | ||
t.Run("Remove manual", func(t *testing.T) { | ||
hash := redisMock.CheckPrefix() + utils.SeedName("test") + "|" + "2" | ||
err := redisMock.removeData(redisMock.client, hash) | ||
assert.Equal(t, nil, err) | ||
}) | ||
t.Run("Remove data all", func(t *testing.T) { | ||
isOkay := redisMock.Remove("test", "") | ||
assert.Equal(t, true, isOkay) | ||
}) | ||
t.Run("Error disconnect delete", func(t *testing.T) { | ||
redisMock.Connect() | ||
mr.Close() | ||
hash := redisMock.CheckPrefix() + utils.SeedName("test") + "|" + "2" | ||
err := redisMock.removeData(redisMock.client, hash) | ||
assert.NotEqual(t, nil, err) | ||
}) | ||
t.Run("Error disconnect", func(t *testing.T) { | ||
mr.Close() | ||
isOkay := redisMock.Remove("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
t.Run("Error no client", func(t *testing.T) { | ||
redisMock.client = nil | ||
RedisDB = nil | ||
isOkay := redisMock.Remove("test", "1") | ||
assert.Equal(t, false, isOkay) | ||
}) | ||
} |
Oops, something went wrong.