Scott Baker | f53bf15 | 2019-05-29 17:50:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Portions copyright 2019-present Open Networking Foundation |
| 3 | * Original copyright 2019-present Ciena Corporation |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | package commands |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "github.com/stretchr/testify/assert" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func TestDecodeOperator(t *testing.T) { |
| 26 | value, operator, invert, err := DecodeOperator("=something") |
| 27 | assert.Equal(t, value, "something") |
| 28 | assert.Equal(t, operator, "EQUAL") |
| 29 | assert.Equal(t, invert, false) |
| 30 | assert.Equal(t, err, nil) |
| 31 | |
| 32 | value, operator, invert, err = DecodeOperator("!=something") |
| 33 | assert.Equal(t, value, "something") |
| 34 | assert.Equal(t, operator, "EQUAL") |
| 35 | assert.Equal(t, invert, true) |
| 36 | assert.Equal(t, err, nil) |
| 37 | |
| 38 | value, operator, invert, err = DecodeOperator(">3") |
| 39 | assert.Equal(t, value, "3") |
| 40 | assert.Equal(t, operator, "GREATER_THAN") |
| 41 | assert.Equal(t, invert, false) |
| 42 | assert.Equal(t, err, nil) |
| 43 | |
| 44 | value, operator, invert, err = DecodeOperator(">=3") |
| 45 | assert.Equal(t, value, "3") |
| 46 | assert.Equal(t, operator, "GREATER_THAN_OR_EQUAL") |
| 47 | assert.Equal(t, invert, false) |
| 48 | assert.Equal(t, err, nil) |
| 49 | |
| 50 | value, operator, invert, err = DecodeOperator("<3") |
| 51 | assert.Equal(t, value, "3") |
| 52 | assert.Equal(t, operator, "LESS_THAN") |
| 53 | assert.Equal(t, invert, false) |
| 54 | assert.Equal(t, err, nil) |
| 55 | |
| 56 | value, operator, invert, err = DecodeOperator("<=3") |
| 57 | assert.Equal(t, value, "3") |
| 58 | assert.Equal(t, operator, "LESS_THAN_OR_EQUAL") |
| 59 | assert.Equal(t, invert, false) |
| 60 | assert.Equal(t, err, nil) |
| 61 | } |
| 62 | |
| 63 | func TestCommaSeparatedQueryStringsToMap(t *testing.T) { |
| 64 | m, err := CommaSeparatedQueryToMap("foo=7,bar!=stuff, x = 5, y= 27", true) |
| 65 | assert.Equal(t, err, nil) |
| 66 | assert.Equal(t, m["foo"], "=7") |
| 67 | assert.Equal(t, m["bar"], "!=stuff") |
| 68 | assert.Equal(t, m["x"], "= 5") |
| 69 | assert.Equal(t, m["y"], "= 27") |
| 70 | } |
| 71 | |
| 72 | func TestTypeConvert(t *testing.T) { |
| 73 | conn, descriptor, err := InitReflectionClient() |
| 74 | assert.Equal(t, err, nil) |
| 75 | defer conn.Close() |
| 76 | |
| 77 | v, err := TypeConvert(descriptor, "Site", "id", "7") |
| 78 | assert.Equal(t, err, nil) |
| 79 | assert.Equal(t, v, int32(7)) |
| 80 | |
| 81 | v, err = TypeConvert(descriptor, "Site", "name", "foo") |
| 82 | assert.Equal(t, err, nil) |
| 83 | assert.Equal(t, v, "foo") |
| 84 | |
| 85 | v, err = TypeConvert(descriptor, "Site", "enacted", "123.4") |
| 86 | assert.Equal(t, err, nil) |
| 87 | assert.Equal(t, v, 123.4) |
| 88 | } |
| 89 | |
| 90 | func TestCheckModelName(t *testing.T) { |
| 91 | conn, descriptor, err := InitReflectionClient() |
| 92 | assert.Equal(t, err, nil) |
| 93 | defer conn.Close() |
| 94 | |
| 95 | err = CheckModelName(descriptor, "Slice") |
| 96 | assert.Equal(t, err, nil) |
| 97 | |
| 98 | err = CheckModelName(descriptor, "DoesNotExist") |
| 99 | assert.Equal(t, err.Error(), "Model DoesNotExist does not exist. Use `cordctl models available` to get a list of available models") |
| 100 | } |
| 101 | |
| 102 | func TestCreateModel(t *testing.T) { |
| 103 | conn, descriptor, err := InitReflectionClient() |
| 104 | assert.Equal(t, err, nil) |
| 105 | defer conn.Close() |
| 106 | |
| 107 | m := make(map[string]interface{}) |
| 108 | m["name"] = "mockslice3" |
| 109 | m["site_id"] = int32(1) |
| 110 | |
| 111 | err = CreateModel(conn, descriptor, "Slice", m) |
| 112 | assert.Equal(t, err, nil) |
| 113 | |
| 114 | assert.Equal(t, m["id"], int32(3)) |
| 115 | } |
| 116 | |
| 117 | func TestUpdateModel(t *testing.T) { |
| 118 | conn, descriptor, err := InitReflectionClient() |
| 119 | assert.Equal(t, err, nil) |
| 120 | defer conn.Close() |
| 121 | |
| 122 | m := make(map[string]interface{}) |
| 123 | m["id"] = int32(1) |
| 124 | m["name"] = "mockslice1_newname" |
| 125 | |
| 126 | err = UpdateModel(conn, descriptor, "Slice", m) |
| 127 | assert.Equal(t, err, nil) |
| 128 | } |
| 129 | |
| 130 | func TestGetModel(t *testing.T) { |
| 131 | conn, descriptor, err := InitReflectionClient() |
| 132 | assert.Equal(t, err, nil) |
| 133 | defer conn.Close() |
| 134 | |
| 135 | m, err := GetModel(context.Background(), conn, descriptor, "Slice", int32(1)) |
| 136 | assert.Equal(t, err, nil) |
| 137 | |
| 138 | assert.Equal(t, m.GetFieldByName("id").(int32), int32(1)) |
| 139 | assert.Equal(t, m.GetFieldByName("name").(string), "mockslice1") |
| 140 | } |
| 141 | |
| 142 | func TestListModels(t *testing.T) { |
| 143 | conn, descriptor, err := InitReflectionClient() |
| 144 | assert.Equal(t, err, nil) |
| 145 | defer conn.Close() |
| 146 | |
| 147 | m, err := ListModels(context.Background(), conn, descriptor, "Slice") |
| 148 | assert.Equal(t, err, nil) |
| 149 | |
| 150 | assert.Equal(t, len(m), 2) |
| 151 | assert.Equal(t, m[0].GetFieldByName("id").(int32), int32(1)) |
| 152 | assert.Equal(t, m[0].GetFieldByName("name").(string), "mockslice1") |
| 153 | assert.Equal(t, m[1].GetFieldByName("id").(int32), int32(2)) |
| 154 | assert.Equal(t, m[1].GetFieldByName("name").(string), "mockslice2") |
| 155 | } |
| 156 | |
| 157 | func TestFilterModels(t *testing.T) { |
| 158 | conn, descriptor, err := InitReflectionClient() |
| 159 | assert.Equal(t, err, nil) |
| 160 | defer conn.Close() |
| 161 | |
| 162 | qm := map[string]string{"id": "=1"} |
| 163 | |
| 164 | m, err := FilterModels(context.Background(), conn, descriptor, "Slice", qm) |
| 165 | assert.Equal(t, err, nil) |
| 166 | |
| 167 | assert.Equal(t, len(m), 1) |
| 168 | assert.Equal(t, m[0].GetFieldByName("id").(int32), int32(1)) |
| 169 | assert.Equal(t, m[0].GetFieldByName("name").(string), "mockslice1") |
| 170 | } |
| 171 | |
| 172 | func TestDeleteModel(t *testing.T) { |
| 173 | conn, descriptor, err := InitReflectionClient() |
| 174 | assert.Equal(t, err, nil) |
| 175 | defer conn.Close() |
| 176 | |
| 177 | err = DeleteModel(conn, descriptor, "Slice", int32(1)) |
| 178 | assert.Equal(t, err, nil) |
| 179 | } |