Skip to content

Commit

Permalink
✅ test: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
0xE8551CCB committed Oct 11, 2019
1 parent e373252 commit aa801b3
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ func main() {

To learn more about [portal](https://github.com/iFaceless/portal), please read the [User Guide](./USERGUIDE.md)~

# Concurrency Strategy

1. Any fields tagged with `portal:"async"` will be serialized asynchronously.
1. When dumping to multiple schemas, portal will do it concurrently if any fields in the schema are tagged with `protal:"async"`.
1. You can always disable concurrency strategy with option `portal.DisableConcurrency()`.

# Core APIs

```go
Expand Down
6 changes: 6 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ func main() {

更多使用细节,请参考 [使用指南](./USERGUIDE.md)~

# 并发策略控制

1. 当某个 Schema 结构体字段标记了 `portal:"async"` 标签时会异步填充字段值;
1. 当序列化 Schema 列表时,会分析 Schema 中有无标记了 `async` 的字段,如果存在的话,则使用并发填充策略;否则只在当前 goroutine 中完成序列化;
1. 可以在 Dump 时添加 `portal.DisableConcurrency()` 禁用并发序列化的功能。

# 核心 APIs

```go
Expand Down
15 changes: 14 additions & 1 deletion chell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type NotiSchema struct {
type UserSchema struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty" portal:"attr:Fullname"`
Notifications []*NotiSchema `json:"notifications,omitempty" portal:"nested"`
Notifications []*NotiSchema `json:"notifications,omitempty" portal:"nested;async"`
AnotherNotifications []*NotiSchema `json:"another_notifications,omitempty" portal:"nested;attr:Notifications"`
}

Expand Down Expand Up @@ -109,6 +109,13 @@ func TestDumpOneFilterOnlyFields(t *testing.T) {

data, _ = json.Marshal(taskSchema2)
assert.Equal(t, `{"id":"1","user":{"id":"1","notifications":[{"id":"0"}],"another_notifications":[{"title":"title_0"}]},"simple_user":{"name":"user:1"}}`, string(data))

var taskSchema3 TaskSchema
err = Dump(&taskSchema3, &task, Only("title", "simple_user"), FieldAliasMapTagName("json"))
assert.Nil(t, err)

data, _ = json.Marshal(taskSchema)
assert.Equal(t, `{"title":"Finish your jobs.","simple_user":{"name":"user:1"}}`, string(data))
}

func TestDumpOneExcludeFields(t *testing.T) {
Expand Down Expand Up @@ -143,6 +150,12 @@ func TestDumpMany(t *testing.T) {

data, _ := json.Marshal(taskSchemas)
assert.Equal(t, `[{"id":"0","title":"Task #1","user":{"name":"user:100"}},{"id":"1","title":"Task #2","user":{"name":"user:101"}}]`, string(data))

err = Dump(&taskSchemas, &tasks, Only("ID", "Title", "User[Name]"), DisableConcurrency())
assert.Nil(t, err)

data, _ = json.Marshal(taskSchemas)
assert.Equal(t, `[{"id":"0","title":"Task #1","user":{"name":"user:100"}},{"id":"1","title":"Task #2","user":{"name":"user:101"}}]`, string(data))
}

func TestDumpError(t *testing.T) {
Expand Down
18 changes: 9 additions & 9 deletions examples/todo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ func main() {
portal.SetMaxPoolSize(1024)
portal.SetDebug(true)

//task := model.TaskModel{
// ID: 1,
// UserID: 1,
// Title: "Finish your jobs.",
//}
task := model.TaskModel{
ID: 1,
UserID: 1,
Title: "Finish your jobs.",
}

//printFullFields(&task)
//printWithOnlyFields(&task, "Description")
//printWithOnlyFields(&task, "ID", "User[id,Notifications[ID],AnotherNotifications[Title]]", "simple_user[id]")
printFullFields(&task)
printWithOnlyFields(&task, "Description")
printWithOnlyFields(&task, "ID", "User[id,Notifications[ID],AnotherNotifications[Title]]", "simple_user[id]")
printMany()
//printWithExcludeFields(&task, "Description", "ID", "User[Name,Notifications[ID,Content],AnotherNotifications], SimpleUser")
printWithExcludeFields(&task, "Description", "ID", "User[Name,Notifications[ID,Content],AnotherNotifications], SimpleUser")
fmt.Printf("elapsed: %.1f ms\n", time.Since(start).Seconds()*1000)
}

Expand Down
1 change: 1 addition & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func FieldAliasMapTagName(tag string) Option {
}
}

// DisableConcurrency disables concurrency strategy.
func DisableConcurrency() Option {
return func(c *Chell) error {
c.disableConcurrency = true
Expand Down

0 comments on commit aa801b3

Please sign in to comment.