-
Notifications
You must be signed in to change notification settings - Fork 669
/
test_aliases.go
98 lines (74 loc) · 3.15 KB
/
test_aliases.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package ids
import "github.com/stretchr/testify/require"
var AliasTests = []func(require *require.Assertions, r AliaserReader, w AliaserWriter){
AliaserLookupErrorTest,
AliaserLookupTest,
AliaserAliasesEmptyTest,
AliaserAliasesTest,
AliaserPrimaryAliasTest,
AliaserAliasClashTest,
AliaserRemoveAliasTest,
}
func AliaserLookupErrorTest(require *require.Assertions, r AliaserReader, _ AliaserWriter) {
_, err := r.Lookup("Batman")
// TODO: require error to be errNoIDWithAlias
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}
func AliaserLookupTest(require *require.Assertions, r AliaserReader, w AliaserWriter) {
id := ID{'K', 'a', 't', 'e', ' ', 'K', 'a', 'n', 'e'}
require.NoError(w.Alias(id, "Batwoman"))
res, err := r.Lookup("Batwoman")
require.NoError(err)
require.Equal(id, res)
}
func AliaserAliasesEmptyTest(require *require.Assertions, r AliaserReader, _ AliaserWriter) {
id := ID{'J', 'a', 'm', 'e', 's', ' ', 'G', 'o', 'r', 'd', 'o', 'n'}
aliases, err := r.Aliases(id)
require.NoError(err)
require.Empty(aliases)
}
func AliaserAliasesTest(require *require.Assertions, r AliaserReader, w AliaserWriter) {
id := ID{'B', 'r', 'u', 'c', 'e', ' ', 'W', 'a', 'y', 'n', 'e'}
require.NoError(w.Alias(id, "Batman"))
require.NoError(w.Alias(id, "Dark Knight"))
aliases, err := r.Aliases(id)
require.NoError(err)
expected := []string{"Batman", "Dark Knight"}
require.Equal(expected, aliases)
}
func AliaserPrimaryAliasTest(require *require.Assertions, r AliaserReader, w AliaserWriter) {
id1 := ID{'J', 'a', 'm', 'e', 's', ' ', 'G', 'o', 'r', 'd', 'o', 'n'}
id2 := ID{'B', 'r', 'u', 'c', 'e', ' ', 'W', 'a', 'y', 'n', 'e'}
require.NoError(w.Alias(id2, "Batman"))
require.NoError(w.Alias(id2, "Dark Knight"))
_, err := r.PrimaryAlias(id1)
// TODO: require error to be errNoAliasForID
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
expected := "Batman"
res, err := r.PrimaryAlias(id2)
require.NoError(err)
require.Equal(expected, res)
}
func AliaserAliasClashTest(require *require.Assertions, _ AliaserReader, w AliaserWriter) {
id1 := ID{'B', 'r', 'u', 'c', 'e', ' ', 'W', 'a', 'y', 'n', 'e'}
id2 := ID{'D', 'i', 'c', 'k', ' ', 'G', 'r', 'a', 'y', 's', 'o', 'n'}
require.NoError(w.Alias(id1, "Batman"))
err := w.Alias(id2, "Batman")
// TODO: require error to be errAliasAlreadyMapped
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
}
func AliaserRemoveAliasTest(require *require.Assertions, r AliaserReader, w AliaserWriter) {
id1 := ID{'B', 'r', 'u', 'c', 'e', ' ', 'W', 'a', 'y', 'n', 'e'}
id2 := ID{'J', 'a', 'm', 'e', 's', ' ', 'G', 'o', 'r', 'd', 'o', 'n'}
require.NoError(w.Alias(id1, "Batman"))
require.NoError(w.Alias(id1, "Dark Knight"))
w.RemoveAliases(id1)
_, err := r.PrimaryAlias(id1)
// TODO: require error to be errNoAliasForID
require.Error(err) //nolint:forbidigo // currently returns grpc errors too
require.NoError(w.Alias(id2, "Batman"))
require.NoError(w.Alias(id2, "Dark Knight"))
require.NoError(w.Alias(id1, "Dark Night Rises"))
}