-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: 新增 GlobalDataFileStorage 和 IndexDataFileStorage 的测试用例
- Loading branch information
1 parent
c447c8a
commit 4378aa0
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
utils/storage/storages/example-data/global_data_file_test.stock
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 @@ | ||
{"CreateAt":"2023-07-19T14:49:35.7235348+08:00","TotalCount":10} |
1 change: 1 addition & 0 deletions
1
utils/storage/storages/example-data/index_data_file_test.INDEX_001.stock
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 @@ | ||
{"ID":"INDEX_001","Value":10} |
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,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 | ||
} |
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,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) | ||
}) | ||
} |
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,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 | ||
} |
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,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) | ||
}) | ||
} |