-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathunique_test.go
118 lines (102 loc) · 3.53 KB
/
unique_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package faker_test
import (
"errors"
"fmt"
"testing"
"github.com/pioz/faker"
"github.com/stretchr/testify/assert"
)
func ExampleUniq() {
faker.SetSeed(403)
generator := func() (interface{}, error) { return faker.IntInRange(0, 1), nil }
value1, _ := faker.Uniq("test", 0, generator)
fmt.Println(value1)
value2, _ := faker.Uniq("test", 0, generator)
fmt.Println(value2)
faker.ClearUniqCache("test")
value3, _ := faker.Uniq("test", 0, generator)
fmt.Println(value3)
// Output:
// 1
// 0
// 0
}
func TestUniq(t *testing.T) {
faker.SetSeed(400)
values := make([]int, 0)
for i := 0; i < 11; i++ {
value, err := faker.Uniq("test", 0, func() (interface{}, error) { return faker.IntInRange(0, 10), nil })
assert.Nil(t, err)
values = append(values, value.(int))
}
assert.Equal(t, 11, len(values))
faker.ClearUniqCache("notexistingkey")
_, err := faker.Uniq("test", 0, func() (interface{}, error) { return faker.IntInRange(0, 10), nil })
assert.NotNil(t, err)
assert.Equal(t, "failed to generate a unique value for group 'test'", err.Error())
faker.ClearUniqCache("test")
_, err = faker.Uniq("test", 0, func() (interface{}, error) { return faker.IntInRange(0, 0), nil })
assert.Nil(t, err)
_, err = faker.Uniq("test", 0, func() (interface{}, error) { return faker.IntInRange(0, 0), nil })
assert.NotNil(t, err)
assert.Equal(t, "failed to generate a unique value for group 'test'", err.Error())
faker.ClearAllUniqCache()
_, err = faker.Uniq("test", 0, func() (interface{}, error) { return faker.IntInRange(0, 0), nil })
assert.Nil(t, err)
}
func TestUniqNoErrorIfValueIsString(t *testing.T) {
faker.SetSeed(405)
valuesRound1 := make([]string, 0)
for i := 0; i < 15; i++ {
value, err := faker.Uniq("test", 0, func() (interface{}, error) { return faker.NamePrefix(), nil })
assert.Nil(t, err)
valuesRound1 = append(valuesRound1, value.(string))
}
assert.Equal(t, 15, len(valuesRound1))
valuesRound2 := make([]string, 0)
for i := 0; i < 15; i++ {
value, err := faker.Uniq("test", 0, func() (interface{}, error) { return faker.NamePrefix(), nil })
assert.Nil(t, err)
valuesRound2 = append(valuesRound2, value.(string))
}
assert.Equal(t, 15, len(valuesRound2))
valuesRound3 := make([]string, 0)
for i := 0; i < 15; i++ {
value, err := faker.Uniq("test", 0, func() (interface{}, error) { return faker.NamePrefix(), nil })
assert.Nil(t, err)
valuesRound3 = append(valuesRound3, value.(string))
}
assert.Equal(t, 15, len(valuesRound3))
checkUniquenes := map[string]struct{}{}
for _, s := range append(append(valuesRound1, valuesRound2...), valuesRound3...) {
_, found := checkUniquenes[s]
assert.False(t, found)
checkUniquenes[s] = struct{}{}
}
}
func TestUniqError(t *testing.T) {
_, err := faker.Uniq("test", 0, func() (interface{}, error) { return nil, errors.New("this is an error") })
assert.NotNil(t, err)
assert.Equal(t, "this is an error", err.Error())
}
func TestUniqSlice(t *testing.T) {
faker.SetSeed(401)
randIntSlice, err := faker.UniqSlice(100, "testSlice", 0, func() (interface{}, error) {
return faker.IntInRange(1, 100), nil
})
t.Log(randIntSlice)
assert.Nil(t, err)
assert.Equal(t, 100, len(randIntSlice))
for _, randInt := range randIntSlice {
assert.NotEmpty(t, randInt)
}
}
func TestUniqSliceError(t *testing.T) {
faker.SetSeed(402)
randIntSlice, err := faker.UniqSlice(10, "testSlice", 0, func() (interface{}, error) {
return faker.IntInRange(1, 9), nil
})
t.Log(randIntSlice)
assert.NotNil(t, err)
assert.Equal(t, "failed to generate a unique value for group 'testSlice'", err.Error())
}