|
| 1 | +package models |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/stretchr/testify/require" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func TestExtractValuesFromTemplate(t *testing.T) { |
| 9 | + type testCase struct { |
| 10 | + name string |
| 11 | + template string |
| 12 | + expected []string |
| 13 | + } |
| 14 | + |
| 15 | + testCases := []testCase{ |
| 16 | + { |
| 17 | + name: "Empty template", |
| 18 | + template: "", |
| 19 | + expected: nil, |
| 20 | + }, |
| 21 | + { |
| 22 | + name: "Valid template", |
| 23 | + template: "{{ foo }}.{{boo}}", |
| 24 | + expected: []string{"foo", "boo"}, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "Template with filters", |
| 28 | + template: "{{ foo | upper | lower }}", |
| 29 | + expected: []string{"foo"}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Template with functions", |
| 33 | + template: "{{ upper('foo') | lower }}@{{ boo }}", |
| 34 | + expected: []string{"boo"}, |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "Invalid template", |
| 38 | + template: "{_{ foo }}", |
| 39 | + expected: nil, |
| 40 | + }, |
| 41 | + } |
| 42 | + |
| 43 | + testFunc := func(t *testing.T, tc testCase) { |
| 44 | + t.Helper() |
| 45 | + |
| 46 | + actual := extractValuesFromTemplate(tc.template) |
| 47 | + require.Equal(t, tc.expected, actual) |
| 48 | + } |
| 49 | + |
| 50 | + for _, tc := range testCases { |
| 51 | + t.Run(tc.name, func(t *testing.T) { testFunc(t, tc) }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestTopologicalSort(t *testing.T) { |
| 56 | + type testCase struct { |
| 57 | + name string |
| 58 | + columns []*Column |
| 59 | + wantErr bool |
| 60 | + expected []string |
| 61 | + } |
| 62 | + |
| 63 | + testCases := []testCase{ |
| 64 | + { |
| 65 | + name: "Empty columns", |
| 66 | + columns: []*Column{}, |
| 67 | + wantErr: false, |
| 68 | + expected: []string{}, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "Columns with dependencies", |
| 72 | + columns: []*Column{ |
| 73 | + { |
| 74 | + Name: "1", |
| 75 | + Type: "string", |
| 76 | + Ranges: []*Params{ |
| 77 | + { |
| 78 | + StringParams: &ColumnStringParams{ |
| 79 | + Template: "{{ 3 }}", |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }, |
| 84 | + { |
| 85 | + Name: "2", |
| 86 | + Type: "string", |
| 87 | + Ranges: []*Params{ |
| 88 | + { |
| 89 | + StringParams: &ColumnStringParams{ |
| 90 | + Template: "{{ 4 }}", |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + { |
| 96 | + Name: "3", |
| 97 | + Type: "string", |
| 98 | + Ranges: []*Params{ |
| 99 | + { |
| 100 | + StringParams: &ColumnStringParams{ |
| 101 | + Template: "{{ 2 }}", |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + }, |
| 106 | + { |
| 107 | + Name: "4", |
| 108 | + Type: "string", |
| 109 | + Ranges: []*Params{ |
| 110 | + { |
| 111 | + StringParams: &ColumnStringParams{ |
| 112 | + Template: "", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + wantErr: false, |
| 119 | + expected: []string{"4", "2", "3", "1"}, |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "Columns with cycle dependencies", |
| 123 | + columns: []*Column{ |
| 124 | + { |
| 125 | + Name: "1", |
| 126 | + Type: "string", |
| 127 | + Ranges: []*Params{ |
| 128 | + { |
| 129 | + StringParams: &ColumnStringParams{ |
| 130 | + Template: "{{ 2 }}", |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + { |
| 136 | + Name: "2", |
| 137 | + Type: "string", |
| 138 | + Ranges: []*Params{ |
| 139 | + { |
| 140 | + StringParams: &ColumnStringParams{ |
| 141 | + Template: "{{ 1 }}", |
| 142 | + }, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + wantErr: true, |
| 148 | + expected: nil, |
| 149 | + }, |
| 150 | + } |
| 151 | + |
| 152 | + testFunc := func(t *testing.T, tc testCase) { |
| 153 | + t.Helper() |
| 154 | + |
| 155 | + actual, err := TopologicalSort(tc.columns) |
| 156 | + require.Equal(t, tc.wantErr, err != nil) |
| 157 | + require.Equal(t, tc.expected, actual) |
| 158 | + } |
| 159 | + |
| 160 | + for _, tc := range testCases { |
| 161 | + t.Run(tc.name, func(t *testing.T) { testFunc(t, tc) }) |
| 162 | + } |
| 163 | +} |
0 commit comments