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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
package mapper
import (
"encoding/json"
"fmt"
"testing"
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/stretchr/testify/assert"
)
var sampleTable = []byte(`{
"columns": {
"aString": {
"type": "string"
},
"aInteger": {
"type": "integer"
},
"aSet": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0
}
},
"aMap": {
"type": {
"key": "string",
"value": "string"
}
}
}
}`)
func TestNewMapperInfo(t *testing.T) {
type test struct {
name string
table []byte
obj interface{}
expectedCols []string
err bool
}
tests := []test{
{
name: "no_orm",
table: sampleTable,
obj: &struct {
foo string
bar int
}{},
err: false,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("NewMapper_%s", tt.name), func(t *testing.T) {
var table ovsdb.TableSchema
err := json.Unmarshal(tt.table, &table)
assert.Nil(t, err)
info, err := NewInfo("Test", &table, tt.obj)
if tt.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
for _, col := range tt.expectedCols {
assert.Truef(t, info.hasColumn(col), "Expected column should be present in Mapper Info")
}
assert.Equal(t, "Test", info.Metadata.TableName)
})
}
}
func TestMapperInfoSet(t *testing.T) {
type obj struct {
Ostring string `ovsdb:"aString"`
Oint int `ovsdb:"aInteger"`
Oset []string `ovsdb:"aSet"`
Omap map[string]string `ovsdb:"aMap"`
}
type test struct {
name string
table []byte
obj interface{}
field interface{}
column string
err bool
}
tests := []test{
{
name: "string",
table: sampleTable,
obj: &obj{},
field: "foo",
column: "aString",
err: false,
},
{
name: "set",
table: sampleTable,
obj: &obj{},
field: []string{"foo", "bar"},
column: "aSet",
err: false,
},
{
name: "map",
table: sampleTable,
obj: &obj{},
field: map[string]string{"foo": "bar"},
column: "aMap",
err: false,
},
{
name: "nonempty",
table: sampleTable,
obj: &obj{
Omap: map[string]string{"original": "stuff"},
Oint: 1,
Ostring: "foo",
Oset: []string{"foo"},
},
field: map[string]string{"foo": "bar"},
column: "aMap",
err: false,
},
{
name: "un-assignable",
table: sampleTable,
obj: &obj{},
field: []string{"foo"},
column: "aMap",
err: true,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("SetField_%s", tt.name), func(t *testing.T) {
var table ovsdb.TableSchema
err := json.Unmarshal(tt.table, &table)
assert.Nil(t, err)
info, err := NewInfo("Test", &table, tt.obj)
assert.Nil(t, err)
err = info.SetField(tt.column, tt.field)
if tt.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
readBack, err := info.FieldByColumn(tt.column)
assert.Nil(t, err)
assert.Equalf(t, tt.field, readBack, "Set field should match original")
}
})
}
}
func TestMapperInfoColByPtr(t *testing.T) {
type obj struct {
ostring string `ovsdb:"aString"`
oint int `ovsdb:"aInteger"`
oset []string `ovsdb:"aSet"`
omap map[string]string `ovsdb:"aMap"`
}
obj1 := obj{}
type test struct {
name string
table []byte
obj interface{}
field interface{}
column string
err bool
}
tests := []test{
{
name: "first",
table: sampleTable,
obj: &obj1,
field: &obj1.ostring,
column: "aString",
err: false,
},
{
name: "middle",
table: sampleTable,
obj: &obj1,
field: &obj1.oint,
column: "aInteger",
err: false,
},
{
name: "middle2",
table: sampleTable,
obj: &obj1,
field: &obj1.oset,
column: "aSet",
err: false,
},
{
name: "last",
table: sampleTable,
obj: &obj1,
field: &obj1.omap,
column: "aMap",
err: false,
},
{
name: "external",
table: sampleTable,
obj: &obj1,
field: &obj{},
err: true,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("GetFieldByPtr_%s", tt.name), func(t *testing.T) {
var table ovsdb.TableSchema
err := json.Unmarshal(tt.table, &table)
assert.Nil(t, err)
info, err := NewInfo("Test", &table, tt.obj)
assert.Nil(t, err)
col, err := info.ColumnByPtr(tt.field)
if tt.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.Equalf(t, tt.column, col, "Column name extracted should match")
}
})
}
}
func TestOrmGetIndex(t *testing.T) {
tableSchema := []byte(`{
"indexes": [["name"],["composed_1","composed_2"]],
"columns": {
"name": {
"type": "string"
},
"composed_1": {
"type": {
"key": "string"
}
},
"composed_2": {
"type": {
"key": "string"
}
},
"config": {
"type": {
"key": "string",
"max": "unlimited",
"min": 0,
"value": "string"
}
}
}
}`)
var table ovsdb.TableSchema
err := json.Unmarshal(tableSchema, &table)
assert.Nil(t, err)
type obj struct {
ID string `ovsdb:"_uuid"`
MyName string `ovsdb:"name"`
Config map[string]string `ovsdb:"config"`
Comp1 string `ovsdb:"composed_1"`
Comp2 string `ovsdb:"composed_2"`
}
type test struct {
name string
obj interface{}
expected [][]string
err bool
}
tests := []test{
{
name: "empty",
obj: &obj{},
expected: [][]string{},
err: false,
},
{
name: "UUID",
obj: &obj{
ID: aUUID0,
},
expected: [][]string{{"_uuid"}},
err: false,
},
{
name: "simple",
obj: &obj{
MyName: "foo",
},
expected: [][]string{{"name"}},
err: false,
},
{
name: "additional index",
obj: &obj{
ID: aUUID0,
MyName: "foo",
},
expected: [][]string{{"_uuid"}, {"name"}},
err: false,
},
{
name: "complex index",
obj: &obj{
Comp1: "foo",
Comp2: "bar",
},
expected: [][]string{{"composed_1", "composed_2"}},
err: false,
},
{
name: "multiple index",
obj: &obj{
MyName: "something",
Comp1: "foo",
Comp2: "bar",
},
expected: [][]string{{"name"}, {"composed_1", "composed_2"}},
err: false,
},
{
name: "all ",
obj: &obj{
ID: aUUID0,
MyName: "something",
Comp1: "foo",
Comp2: "bar",
},
expected: [][]string{{"_uuid"}, {"name"}, {"composed_1", "composed_2"}},
err: false,
},
{
name: "Error: None",
obj: &obj{
Config: map[string]string{"foo": "bar"},
},
expected: [][]string{},
err: false,
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("GetValidIndexes_%s", tt.name), func(t *testing.T) {
info, err := NewInfo("Test", &table, tt.obj)
assert.Nil(t, err)
indexes, err := info.getValidIndexes()
if tt.err {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
assert.ElementsMatchf(t, tt.expected, indexes, "Indexes must match")
}
})
}
}
|