-
Notifications
You must be signed in to change notification settings - Fork 3
/
id_test.go
107 lines (102 loc) · 2.11 KB
/
id_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package dbw_test
import (
"strings"
"testing"
"github.com/hashicorp/go-dbw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewId(t *testing.T) {
type args struct {
prefix string
}
tests := []struct {
name string
args args
wantErr bool
wantLen int
}{
{
name: "valid",
args: args{
prefix: "id",
},
wantErr: false,
wantLen: 10 + len("id_"),
},
{
name: "bad-prefix",
args: args{
prefix: "",
},
wantErr: true,
wantLen: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := dbw.NewId(tt.args.prefix)
if (err != nil) != tt.wantErr {
t.Errorf("NewPublicId() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && !strings.HasPrefix(got, tt.args.prefix+"_") {
t.Errorf("NewPublicId() = %v, wanted it to start with %v", got, tt.args.prefix)
}
if len(got) != tt.wantLen {
t.Errorf("NewPublicId() = %v, with len of %d and wanted len of %v", got, len(got), tt.wantLen)
}
})
}
}
func TestPseudoRandomId(t *testing.T) {
type args struct {
prngValues []string
}
tests := []struct {
name string
args args
sameAsPrev bool
}{
{
name: "valid first",
args: args{},
},
{
name: "valid second",
args: args{},
},
{
name: "first prng",
args: args{prngValues: []string{"foo", "bar"}},
},
{
name: "first prng verify",
args: args{prngValues: []string{"foo", "bar"}},
sameAsPrev: true,
},
{
name: "second prng",
args: args{prngValues: []string{"bar", "foo"}},
},
{
name: "second prng verify",
args: args{prngValues: []string{"bar", "foo"}},
sameAsPrev: true,
},
}
var prevTestValue string
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := dbw.NewId("id", dbw.WithPrngValues(tt.args.prngValues))
require.NoError(err)
if tt.sameAsPrev {
assert.Equal(prevTestValue, got)
}
prevTestValue = got
})
}
}