github.com/weaviate/weaviate@v1.24.6/entities/schema/crossref/crossref_test.go (about) 1 // _ _ 2 // __ _____ __ ___ ___ __ _| |_ ___ 3 // \ \ /\ / / _ \/ _` \ \ / / |/ _` | __/ _ \ 4 // \ V V / __/ (_| |\ V /| | (_| | || __/ 5 // \_/\_/ \___|\__,_| \_/ |_|\__,_|\__\___| 6 // 7 // Copyright © 2016 - 2024 Weaviate B.V. All rights reserved. 8 // 9 // CONTACT: hello@weaviate.io 10 // 11 12 package crossref 13 14 import ( 15 "testing" 16 17 "github.com/go-openapi/strfmt" 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 "github.com/weaviate/weaviate/entities/models" 21 ) 22 23 func TestParseCrossReference(t *testing.T) { 24 ref := Ref{ 25 Local: true, 26 PeerName: _LocalHost, 27 TargetID: "c2cd3f91-0160-477e-869a-8da8829e0a4d", 28 Class: "class", 29 } 30 tests := []struct { 31 beacon string 32 ref Ref 33 ok bool 34 }{ 35 { 36 beacon: "weaviate://localhost/class/c2cd3f91-0160-477e-869a-8da8829e0a4d", 37 ref: ref, 38 ok: true, 39 }, 40 { 41 beacon: "weaviate://remote/class/c2cd3f91-0160-477e-869a-8da8829e0a4d", 42 ref: Ref{false, "remote", ref.TargetID, "class"}, 43 ok: true, 44 }, 45 { 46 beacon: "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d", 47 ref: Ref{true, _LocalHost, ref.TargetID, ""}, 48 ok: true, 49 }, 50 { 51 beacon: "weaviate://remote/c2cd3f91-0160-477e-869a-8da8829e0a4d", 52 ref: Ref{false, "remote", ref.TargetID, ""}, 53 ok: true, 54 }, 55 { 56 beacon: "weaviate://localhost/class/c2cd3f91-0160-477e-869a-8da8829e0a4d/i-shouldnt-be-here", 57 }, 58 { 59 beacon: "weaviate://localhost/class/invalid-id", 60 }, 61 { 62 beacon: "weaviate://localhost/class", 63 }, 64 { 65 beacon: "weaviate://localhost", 66 }, 67 { 68 beacon: "i:am:not:a:url", 69 }, 70 } 71 for i, tc := range tests { 72 got, err := Parse(tc.beacon) 73 if (err == nil) != tc.ok { 74 t.Errorf("%d - Parse(%s) error %v error expected: %t", i, tc.beacon, err, tc.ok) 75 continue 76 } 77 if err != nil { 78 continue 79 } 80 if *got != tc.ref { 81 t.Errorf("%d - Parse(%s) got %v want %v", i, tc.beacon, *got, tc.ref) 82 } 83 if beacon := got.String(); beacon != tc.beacon { 84 t.Errorf("beacon expected: %v want %v", tc.beacon, beacon) 85 } 86 } 87 } 88 89 func TestSingleRef(t *testing.T) { 90 ref := NewLocalhost("class", "c2cd3f91-0160-477e-869a-8da8829e0a4d") 91 expected := &models.SingleRef{ 92 Beacon: strfmt.URI("weaviate://localhost/class/c2cd3f91-0160-477e-869a-8da8829e0a4d"), 93 } 94 sref := ref.SingleRef() 95 assert.Equal(t, expected, sref, "should create a singleRef") 96 xref, _ := ParseSingleRef(sref) 97 assert.Equal(t, ref, xref) 98 } 99 100 func Test_ParsingFromStringDeprecated(t *testing.T) { 101 t.Run("from a local object ref that is well-formed", func(t *testing.T) { 102 uri := "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d" 103 ref, err := Parse(uri) 104 105 require.Nil(t, err, "should not error") 106 107 t.Run("is a local ref", func(t *testing.T) { 108 assert.Equal(t, ref.Local, true) 109 }) 110 111 t.Run("peerName points to localhost", func(t *testing.T) { 112 assert.Equal(t, ref.PeerName, "localhost") 113 }) 114 115 t.Run("id points correctly", func(t *testing.T) { 116 assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d")) 117 }) 118 }) 119 120 t.Run("from a local action ref that is well-formed", func(t *testing.T) { 121 uri := "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d" 122 ref, err := Parse(uri) 123 124 require.Nil(t, err, "should not error") 125 126 t.Run("is a local ref", func(t *testing.T) { 127 assert.Equal(t, ref.Local, true) 128 }) 129 130 t.Run("peerName points to localhost", func(t *testing.T) { 131 assert.Equal(t, ref.PeerName, "localhost") 132 }) 133 134 t.Run("id points correctly", func(t *testing.T) { 135 assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d")) 136 }) 137 }) 138 139 t.Run("from a network action ref that is well-formed", func(t *testing.T) { 140 uri := "weaviate://another-weaviate/c2cd3f91-0160-477e-869a-8da8829e0a4d" 141 ref, err := Parse(uri) 142 143 require.Nil(t, err, "should not error") 144 145 t.Run("is a local ref", func(t *testing.T) { 146 assert.Equal(t, ref.Local, false) 147 }) 148 149 t.Run("peerName points to localhost", func(t *testing.T) { 150 assert.Equal(t, ref.PeerName, "another-weaviate") 151 }) 152 153 t.Run("id points correctly", func(t *testing.T) { 154 assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d")) 155 }) 156 }) 157 158 t.Run("with formatting errors", func(t *testing.T) { 159 type testCaseError struct { 160 name string 161 uri string 162 } 163 164 tests := []testCaseError{ 165 { 166 name: "with an invalid URL", 167 uri: "i:am:not:a:url", 168 }, 169 { 170 name: "with too few path segments", 171 uri: "weaviate://localhost", 172 }, 173 { 174 name: "with too many path segments", 175 uri: "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d/i-shouldnt-be-here", 176 }, 177 { 178 name: "with an invalid uuid", 179 uri: "weaviate://localhost/c2cd3f91-iSneakedInHere-477e-869a-8da8829e0a4d", 180 }, 181 } 182 183 for _, test := range tests { 184 t.Run(test.name, func(t *testing.T) { 185 _, err := Parse(test.uri) 186 assert.NotNil(t, err, test.name) 187 }) 188 } 189 }) 190 } 191 192 func Test_ParsingFromSingleRefDeprecated(t *testing.T) { 193 t.Run("from a local object ref that is well-formed", func(t *testing.T) { 194 uri := strfmt.URI("weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d") 195 singleRef := &models.SingleRef{ 196 Beacon: uri, 197 } 198 ref, err := ParseSingleRef(singleRef) 199 200 require.Nil(t, err, "should not error") 201 202 t.Run("is a local ref", func(t *testing.T) { 203 assert.Equal(t, ref.Local, true) 204 }) 205 206 t.Run("peerName points to localhost", func(t *testing.T) { 207 assert.Equal(t, ref.PeerName, "localhost") 208 }) 209 210 t.Run("id points correctly", func(t *testing.T) { 211 assert.Equal(t, ref.TargetID, strfmt.UUID("c2cd3f91-0160-477e-869a-8da8829e0a4d")) 212 }) 213 }) 214 } 215 216 func Test_GenerateStringDeprecated(t *testing.T) { 217 uri := "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d" 218 ref, err := Parse(uri) 219 220 require.Nil(t, err, "should not error") 221 assert.Equal(t, uri, ref.String(), "should be the same as the input string") 222 } 223 224 func Test_DeprecatedSingleRef(t *testing.T) { 225 uri := "weaviate://localhost/c2cd3f91-0160-477e-869a-8da8829e0a4d" 226 ref, err := Parse(uri) 227 expectedResult := &models.SingleRef{ 228 Beacon: strfmt.URI(uri), 229 } 230 231 require.Nil(t, err, "should not error") 232 assert.Equal(t, expectedResult, ref.SingleRef(), "should create a singleRef (api construct)") 233 }