Skip to content

Commit

Permalink
test: 新增 GlobalDataFileStorage 和 IndexDataFileStorage 的测试用例
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jul 19, 2023
1 parent c447c8a commit 4378aa0
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"CreateAt":"2023-07-19T14:49:35.7235348+08:00","TotalCount":10}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ID":"INDEX_001","Value":10}
19 changes: 19 additions & 0 deletions utils/storage/storages/global_data_file_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package storages_test

import (
"fmt"
"github.com/kercylan98/minotaur/utils/storage/storages"
"time"
)

func ExampleNewGlobalDataFileStorage() {
storage := storages.NewGlobalDataFileStorage[*GlobalData](".", func(name string) *GlobalData {
return &GlobalData{
CreateAt: time.Now(),
}
})

fmt.Println(storage != nil)
// Output:
// true
}
42 changes: 42 additions & 0 deletions utils/storage/storages/global_data_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package storages_test

import (
"github.com/kercylan98/minotaur/utils/storage"
"github.com/kercylan98/minotaur/utils/storage/storages"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)

type GlobalData struct {
CreateAt time.Time
TotalCount int
}

func TestGlobalDataFileStorage_Save(t *testing.T) {
Convey("TestGlobalDataFileStorage_Save", t, func() {
data := storage.NewGlobalData[*GlobalData]("global_data_file_test", storages.NewGlobalDataFileStorage[*GlobalData]("./example-data", func(name string) *GlobalData {
return &GlobalData{
CreateAt: time.Now(),
}
}))
data.Handle(func(name string, data *GlobalData) {
data.TotalCount = 10
})
if err := data.SaveData(); err != nil {
t.Fatal(err)
}
So(data.GetData().TotalCount, ShouldEqual, 10)
})
}

func TestGlobalDataFileStorage_Load(t *testing.T) {
Convey("TestGlobalDataFileStorage_Load", t, func() {
data := storage.NewGlobalData[*GlobalData]("global_data_file_test", storages.NewGlobalDataFileStorage[*GlobalData]("./example-data", func(name string) *GlobalData {
return &GlobalData{
CreateAt: time.Now(),
}
}))
So(data.GetData().TotalCount, ShouldEqual, 10)
})
}
18 changes: 18 additions & 0 deletions utils/storage/storages/index_data_file_example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package storages_test

import (
"fmt"
"github.com/kercylan98/minotaur/utils/storage/storages"
)

func ExampleNewIndexDataFileStorage() {
storage := storages.NewIndexDataFileStorage[string, *IndexData[string]]("./example-data", func(name string, index string) *IndexData[string] {
return &IndexData[string]{ID: index}
}, func(name string) *IndexData[string] {
return new(IndexData[string])
})

fmt.Println(storage != nil)
// Output:
// true
}
45 changes: 45 additions & 0 deletions utils/storage/storages/index_data_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package storages_test

import (
"github.com/kercylan98/minotaur/utils/storage"
"github.com/kercylan98/minotaur/utils/storage/storages"
. "github.com/smartystreets/goconvey/convey"
"testing"
)

type IndexData[I string] struct {
ID I
Value int
}

func (slf *IndexData[I]) GetIndex() I {
return slf.ID
}

func TestIndexDataFileStorage_Save(t *testing.T) {
Convey("TestIndexDataFileStorage_Save", t, func() {
data := storage.NewIndexData[string, *IndexData[string]]("index_data_file_test", storages.NewIndexDataFileStorage[string, *IndexData[string]]("./example-data", func(name string, index string) *IndexData[string] {
return &IndexData[string]{ID: index}
}, func(name string) *IndexData[string] {
return new(IndexData[string])
}))
data.Handle("INDEX_001", func(name string, index string, data *IndexData[string]) {
data.Value = 10
})
if err := data.SaveData("INDEX_001"); err != nil {
t.Fatal(err)
}
So(data.GetData("INDEX_001").Value, ShouldEqual, 10)
})
}

func TestIndexDataFileStorage_Load(t *testing.T) {
Convey("TestIndexDataFileStorage_Load", t, func() {
data := storage.NewIndexData[string, *IndexData[string]]("index_data_file_test", storages.NewIndexDataFileStorage[string, *IndexData[string]]("./example-data", func(name string, index string) *IndexData[string] {
return &IndexData[string]{ID: index}
}, func(name string) *IndexData[string] {
return new(IndexData[string])
}))
So(data.GetData("INDEX_001").Value, ShouldEqual, 10)
})
}

0 comments on commit 4378aa0

Please sign in to comment.