-
Notifications
You must be signed in to change notification settings - Fork 669
/
test_aliases.go
106 lines (81 loc) · 2.9 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
99
100
101
102
103
104
105
106
// Copyright (C) 2019-2022, 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, w AliaserWriter) {
_, err := r.Lookup("Batman")
require.Error(err, "expected an error due to missing alias")
}
func AliaserLookupTest(require *require.Assertions, r AliaserReader, w AliaserWriter) {
id := ID{'K', 'a', 't', 'e', ' ', 'K', 'a', 'n', 'e'}
err := w.Alias(id, "Batwoman")
require.NoError(err)
res, err := r.Lookup("Batwoman")
require.NoError(err)
require.Equal(id, res)
}
func AliaserAliasesEmptyTest(require *require.Assertions, r AliaserReader, w 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'}
err := w.Alias(id, "Batman")
require.NoError(err)
err = w.Alias(id, "Dark Knight")
require.NoError(err)
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'}
err := w.Alias(id2, "Batman")
require.NoError(err)
err = w.Alias(id2, "Dark Knight")
require.NoError(err)
_, err = r.PrimaryAlias(id1)
require.Error(err)
expected := "Batman"
res, err := r.PrimaryAlias(id2)
require.NoError(err)
require.Equal(expected, res)
}
func AliaserAliasClashTest(require *require.Assertions, r 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'}
err := w.Alias(id1, "Batman")
require.NoError(err)
err = w.Alias(id2, "Batman")
require.Error(err)
}
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'}
err := w.Alias(id1, "Batman")
require.NoError(err)
err = w.Alias(id1, "Dark Knight")
require.NoError(err)
w.RemoveAliases(id1)
_, err = r.PrimaryAlias(id1)
require.Error(err)
err = w.Alias(id2, "Batman")
require.NoError(err)
err = w.Alias(id2, "Dark Knight")
require.NoError(err)
err = w.Alias(id1, "Dark Night Rises")
require.NoError(err)
}