Skip to content

Commit 8dae771

Browse files
committed
Minor improvement
1 parent 5d5b35c commit 8dae771

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

common/testing/testvars/test_vars.go

+20-8
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,22 @@ func (tv *TestVars) stringNSetter(v string, n int) string {
8585
return fmt.Sprintf("%s_%d", v, n)
8686
}
8787

88-
// Use this setter for entities that don't support setting n (like uuid).
88+
// Generate new uuid for every new n.
89+
func (tv *TestVars) uuidNSetter(_ string, _ int) string {
90+
return tv.uuidString("")
91+
}
92+
93+
func (tv *TestVars) intNSetter(v int, n int) int {
94+
return n*1000 + v
95+
}
96+
97+
// Use this setter for entities that don't support setting n.
8998
func unsupportedNSetter[T any](v T, _ int) T {
9099
panic(fmt.Sprintf("setting n on type %T is not supported", v))
91100
}
92101

93102
func (tv *TestVars) uniqueString(key string) string {
94-
return tv.testName + "_" + key
103+
return fmt.Sprintf("%s_%s", tv.testName, key)
95104
}
96105

97106
func (tv *TestVars) uuidString(_ string) string {
@@ -140,10 +149,13 @@ func (tv *TestVars) WithEntityN(n int) *TestVars {
140149
*/
141150

142151
func (tv *TestVars) NamespaceID() namespace.ID {
143-
return getOrCreate(tv, "namespace_id", func(key string) namespace.ID {
144-
return namespace.ID(tv.uuidString(key))
145-
},
146-
unsupportedNSetter,
152+
return getOrCreate(tv, "namespace_id",
153+
func(key string) namespace.ID {
154+
return namespace.ID(tv.uuidString(key))
155+
},
156+
func(val namespace.ID, n int) namespace.ID {
157+
return namespace.ID(tv.uuidNSetter(val.String(), n))
158+
},
147159
)
148160
}
149161

@@ -193,7 +205,7 @@ func (tv *TestVars) WithWorkflowIDN(n int) *TestVars {
193205
}
194206

195207
func (tv *TestVars) RunID() string {
196-
return getOrCreate(tv, "run_id", tv.uuidString, unsupportedNSetter)
208+
return getOrCreate(tv, "run_id", tv.uuidString, tv.uuidNSetter)
197209
}
198210

199211
func (tv *TestVars) WithRunID(runID string) *TestVars {
@@ -208,7 +220,7 @@ func (tv *TestVars) WorkflowExecution() *commonpb.WorkflowExecution {
208220
}
209221

210222
func (tv *TestVars) RequestID() string {
211-
return getOrCreate(tv, "request_id", tv.uuidString, unsupportedNSetter)
223+
return getOrCreate(tv, "request_id", tv.uuidString, tv.uuidNSetter)
212224
}
213225

214226
func (tv *TestVars) BuildID() string {

docs/development/testing.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ assert.Equal(t, tv.WorkflowID(), startedWorkflow.WorkflowId)
3535
If you don't care about specific value, you can use `Any()` method to generate a random value.
3636
It indicates that value doesn't matter for this test and will never be asserted on (but required for API, for example).
3737

38-
If you need more than one value of the same type in the same test you can use `WithEntityN()` methods.
38+
If you need more than one value of the same type in the same test, you can use `WithEntityN()` method to
39+
get a new instance of `testvars` with a different value. `N` stands for a number of the value.
3940

4041
```go
4142
func TestFoo(t *testing.T) {

0 commit comments

Comments
 (0)