github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/configs/configschema/filter_test.go (about) 1 package configschema 2 3 import ( 4 "testing" 5 6 "github.com/zclconf/go-cty/cty" 7 8 "github.com/google/go-cmp/cmp" 9 "github.com/google/go-cmp/cmp/cmpopts" 10 ) 11 12 func TestFilter(t *testing.T) { 13 testCases := map[string]struct { 14 schema *Block 15 filterAttribute FilterT[*Attribute] 16 filterBlock FilterT[*NestedBlock] 17 want *Block 18 }{ 19 "empty": { 20 schema: &Block{}, 21 filterAttribute: FilterDeprecatedAttribute, 22 filterBlock: FilterDeprecatedBlock, 23 want: &Block{}, 24 }, 25 "noop": { 26 schema: &Block{ 27 Attributes: map[string]*Attribute{ 28 "string": { 29 Type: cty.String, 30 Required: true, 31 }, 32 }, 33 BlockTypes: map[string]*NestedBlock{ 34 "list": { 35 Nesting: NestingList, 36 Block: Block{ 37 Attributes: map[string]*Attribute{ 38 "string": { 39 Type: cty.String, 40 Required: true, 41 }, 42 }, 43 }, 44 }, 45 }, 46 }, 47 filterAttribute: nil, 48 filterBlock: nil, 49 want: &Block{ 50 Attributes: map[string]*Attribute{ 51 "string": { 52 Type: cty.String, 53 Required: true, 54 }, 55 }, 56 BlockTypes: map[string]*NestedBlock{ 57 "list": { 58 Nesting: NestingList, 59 Block: Block{ 60 Attributes: map[string]*Attribute{ 61 "string": { 62 Type: cty.String, 63 Required: true, 64 }, 65 }, 66 }, 67 }, 68 }, 69 }, 70 }, 71 "filter_deprecated": { 72 schema: &Block{ 73 Attributes: map[string]*Attribute{ 74 "string": { 75 Type: cty.String, 76 Optional: true, 77 }, 78 "deprecated_string": { 79 Type: cty.String, 80 Deprecated: true, 81 }, 82 "nested": { 83 NestedType: &Object{ 84 Attributes: map[string]*Attribute{ 85 "string": { 86 Type: cty.String, 87 }, 88 "deprecated_string": { 89 Type: cty.String, 90 Deprecated: true, 91 }, 92 }, 93 Nesting: NestingList, 94 }, 95 }, 96 }, 97 98 BlockTypes: map[string]*NestedBlock{ 99 "list": { 100 Nesting: NestingList, 101 Block: Block{ 102 Attributes: map[string]*Attribute{ 103 "string": { 104 Type: cty.String, 105 Optional: true, 106 }, 107 }, 108 Deprecated: true, 109 }, 110 }, 111 }, 112 }, 113 filterAttribute: FilterDeprecatedAttribute, 114 filterBlock: FilterDeprecatedBlock, 115 want: &Block{ 116 Attributes: map[string]*Attribute{ 117 "string": { 118 Type: cty.String, 119 Optional: true, 120 }, 121 "nested": { 122 NestedType: &Object{ 123 Attributes: map[string]*Attribute{ 124 "string": { 125 Type: cty.String, 126 }, 127 }, 128 Nesting: NestingList, 129 }, 130 }, 131 }, 132 }, 133 }, 134 "filter_read_only": { 135 schema: &Block{ 136 Attributes: map[string]*Attribute{ 137 "string": { 138 Type: cty.String, 139 Optional: true, 140 }, 141 "read_only_string": { 142 Type: cty.String, 143 Computed: true, 144 }, 145 "nested": { 146 NestedType: &Object{ 147 Attributes: map[string]*Attribute{ 148 "string": { 149 Type: cty.String, 150 Optional: true, 151 }, 152 "read_only_string": { 153 Type: cty.String, 154 Computed: true, 155 }, 156 "deeply_nested": { 157 NestedType: &Object{ 158 Attributes: map[string]*Attribute{ 159 "number": { 160 Type: cty.Number, 161 Required: true, 162 }, 163 "read_only_number": { 164 Type: cty.Number, 165 Computed: true, 166 }, 167 }, 168 Nesting: NestingList, 169 }, 170 }, 171 }, 172 Nesting: NestingList, 173 }, 174 }, 175 }, 176 177 BlockTypes: map[string]*NestedBlock{ 178 "list": { 179 Nesting: NestingList, 180 Block: Block{ 181 Attributes: map[string]*Attribute{ 182 "string": { 183 Type: cty.String, 184 Optional: true, 185 }, 186 "read_only_string": { 187 Type: cty.String, 188 Computed: true, 189 }, 190 }, 191 }, 192 }, 193 }, 194 }, 195 filterAttribute: FilterReadOnlyAttribute, 196 filterBlock: nil, 197 want: &Block{ 198 Attributes: map[string]*Attribute{ 199 "string": { 200 Type: cty.String, 201 Optional: true, 202 }, 203 "nested": { 204 NestedType: &Object{ 205 Attributes: map[string]*Attribute{ 206 "string": { 207 Type: cty.String, 208 Optional: true, 209 }, 210 "deeply_nested": { 211 NestedType: &Object{ 212 Attributes: map[string]*Attribute{ 213 "number": { 214 Type: cty.Number, 215 Required: true, 216 }, 217 }, 218 Nesting: NestingList, 219 }, 220 }, 221 }, 222 Nesting: NestingList, 223 }, 224 }, 225 }, 226 BlockTypes: map[string]*NestedBlock{ 227 "list": { 228 Nesting: NestingList, 229 Block: Block{ 230 Attributes: map[string]*Attribute{ 231 "string": { 232 Type: cty.String, 233 Optional: true, 234 }, 235 }, 236 }, 237 }, 238 }, 239 }, 240 }, 241 "filter_optional_computed_id": { 242 schema: &Block{ 243 Attributes: map[string]*Attribute{ 244 "id": { 245 Type: cty.String, 246 Optional: true, 247 Computed: true, 248 }, 249 "string": { 250 Type: cty.String, 251 Optional: true, 252 Computed: true, 253 }, 254 }, 255 }, 256 filterAttribute: FilterHelperSchemaIdAttribute, 257 filterBlock: nil, 258 want: &Block{ 259 Attributes: map[string]*Attribute{ 260 "string": { 261 Type: cty.String, 262 Optional: true, 263 Computed: true, 264 }, 265 }, 266 }, 267 }, 268 } 269 270 for name, tc := range testCases { 271 t.Run(name, func(t *testing.T) { 272 got := tc.schema.Filter(tc.filterAttribute, tc.filterBlock) 273 if !cmp.Equal(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty()) { 274 t.Fatal(cmp.Diff(got, tc.want, cmp.Comparer(cty.Type.Equals), cmpopts.EquateEmpty())) 275 } 276 }) 277 } 278 }