github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/quickstarts/model_test.go (about) 1 // +build unit 2 3 package quickstarts_test 4 5 import ( 6 "testing" 7 8 "github.com/blang/semver" 9 "github.com/olli-ai/jx/v2/pkg/quickstarts" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestQuickstartModelFilterText(t *testing.T) { 15 t.Parallel() 16 17 quickstart1 := &quickstarts.Quickstart{ 18 ID: "jenkins-x-quickstarts/node-http", 19 Name: "node-http", 20 } 21 quickstart2 := &quickstarts.Quickstart{ 22 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 23 Name: "node-http-watch-pipeline-activity", 24 } 25 quickstart3 := &quickstarts.Quickstart{ 26 ID: "jenkins-x-quickstarts/ruby", 27 Name: "ruby", 28 } 29 30 qstarts := make(map[string]*quickstarts.Quickstart) 31 qstarts["node-http"] = quickstart1 32 qstarts["node-http-watch-pipeline-activity"] = quickstart2 33 qstarts["ruby"] = quickstart3 34 35 quickstartModel := &quickstarts.QuickstartModel{ 36 Quickstarts: qstarts, 37 } 38 39 quickstartFilter := &quickstarts.QuickstartFilter{ 40 Text: "ruby", 41 } 42 43 results := quickstartModel.Filter(quickstartFilter) 44 45 assert.Equal(t, 1, len(results)) 46 assert.Contains(t, results, quickstart3) 47 } 48 49 func TestQuickstartModelFilterTextMatchesMoreThanOne(t *testing.T) { 50 t.Parallel() 51 52 quickstart1 := &quickstarts.Quickstart{ 53 ID: "jenkins-x-quickstarts/node-http", 54 Name: "node-http", 55 } 56 quickstart2 := &quickstarts.Quickstart{ 57 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 58 Name: "node-http-watch-pipeline-activity", 59 } 60 quickstart3 := &quickstarts.Quickstart{ 61 ID: "jenkins-x-quickstarts/ruby", 62 Name: "ruby", 63 } 64 65 qstarts := make(map[string]*quickstarts.Quickstart) 66 qstarts["node-http"] = quickstart1 67 qstarts["node-http-watch-pipeline-activity"] = quickstart2 68 qstarts["ruby"] = quickstart3 69 70 quickstartModel := &quickstarts.QuickstartModel{ 71 Quickstarts: qstarts, 72 } 73 74 quickstartFilter := &quickstarts.QuickstartFilter{ 75 Text: "node-htt", 76 } 77 78 results := quickstartModel.Filter(quickstartFilter) 79 80 assert.Equal(t, 2, len(results)) 81 assert.Contains(t, results, quickstart1) 82 assert.Contains(t, results, quickstart2) 83 } 84 85 func TestQuickstartModelFilterTextMatchesOneExactly(t *testing.T) { 86 t.Parallel() 87 88 quickstart1 := &quickstarts.Quickstart{ 89 ID: "jenkins-x-quickstarts/node-http", 90 Name: "node-http", 91 } 92 quickstart2 := &quickstarts.Quickstart{ 93 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 94 Name: "node-http-watch-pipeline-activity", 95 } 96 quickstart3 := &quickstarts.Quickstart{ 97 ID: "jenkins-x-quickstarts/ruby", 98 Name: "ruby", 99 } 100 101 qstarts := make(map[string]*quickstarts.Quickstart) 102 qstarts["node-http"] = quickstart1 103 qstarts["node-http-watch-pipeline-activity"] = quickstart2 104 qstarts["ruby"] = quickstart3 105 106 quickstartModel := &quickstarts.QuickstartModel{ 107 Quickstarts: qstarts, 108 } 109 110 quickstartFilter := &quickstarts.QuickstartFilter{ 111 Text: "node-http", 112 } 113 114 results := quickstartModel.Filter(quickstartFilter) 115 116 assert.Equal(t, 1, len(results)) 117 assert.Contains(t, results, quickstart1) 118 } 119 120 func TestQuickstartModelFilterExcludesMachineLearning(t *testing.T) { 121 t.Parallel() 122 123 quickstart1 := &quickstarts.Quickstart{ 124 ID: "jenkins-x-quickstarts/node-http", 125 Name: "node-http", 126 } 127 quickstart2 := &quickstarts.Quickstart{ 128 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 129 Name: "node-http-watch-pipeline-activity", 130 } 131 quickstart3 := &quickstarts.Quickstart{ 132 ID: "machine-learning-quickstarts/ML-is-a-machine-learning-quickstart", 133 Name: "ML-is-a-machine-learning-quickstart", 134 } 135 136 qstarts := make(map[string]*quickstarts.Quickstart) 137 qstarts["node-http"] = quickstart1 138 qstarts["node-http-watch-pipeline-activity"] = quickstart2 139 qstarts["ML-is-a-machine-learning-quickstart"] = quickstart3 140 141 quickstartModel := &quickstarts.QuickstartModel{ 142 Quickstarts: qstarts, 143 } 144 145 quickstartFilter := &quickstarts.QuickstartFilter{ 146 AllowML: false, 147 } 148 149 results := quickstartModel.Filter(quickstartFilter) 150 151 assert.Equal(t, 2, len(results)) 152 assert.Contains(t, results, quickstart1) 153 assert.Contains(t, results, quickstart2) 154 assert.NotContains(t, results, quickstart3) 155 } 156 157 func TestQuickstartModelFilterIncludesMachineLearning(t *testing.T) { 158 t.Parallel() 159 160 quickstart1 := &quickstarts.Quickstart{ 161 ID: "jenkins-x-quickstarts/node-http", 162 Name: "node-http", 163 } 164 quickstart2 := &quickstarts.Quickstart{ 165 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 166 Name: "node-http-watch-pipeline-activity", 167 } 168 quickstart3 := &quickstarts.Quickstart{ 169 ID: "machine-learning-quickstarts/ML-is-a-machine-learning-quickstart", 170 Name: "ML-is-a-machine-learning-quickstart", 171 } 172 173 qstarts := make(map[string]*quickstarts.Quickstart) 174 qstarts["node-http"] = quickstart1 175 qstarts["node-http-watch-pipeline-activity"] = quickstart2 176 qstarts["ML-is-a-machine-learning-quickstart"] = quickstart3 177 178 quickstartModel := &quickstarts.QuickstartModel{ 179 Quickstarts: qstarts, 180 } 181 182 quickstartFilter := &quickstarts.QuickstartFilter{ 183 AllowML: true, 184 } 185 186 results := quickstartModel.Filter(quickstartFilter) 187 188 assert.Equal(t, 3, len(results)) 189 assert.Contains(t, results, quickstart1) 190 assert.Contains(t, results, quickstart2) 191 assert.Contains(t, results, quickstart3) 192 } 193 194 func TestQuickstartModelFilterDefaultsToNoMachineLearning(t *testing.T) { 195 t.Parallel() 196 197 quickstart1 := &quickstarts.Quickstart{ 198 ID: "jenkins-x-quickstarts/node-http", 199 Name: "node-http", 200 } 201 quickstart2 := &quickstarts.Quickstart{ 202 ID: "jenkins-x-quickstarts/node-http-watch-pipeline-activity", 203 Name: "node-http-watch-pipeline-activity", 204 } 205 quickstart3 := &quickstarts.Quickstart{ 206 ID: "machine-learning-quickstarts/ML-is-a-machine-learning-quickstart", 207 Name: "ML-is-a-machine-learning-quickstart", 208 } 209 210 qstarts := make(map[string]*quickstarts.Quickstart) 211 qstarts["node-http"] = quickstart1 212 qstarts["node-http-watch-pipeline-activity"] = quickstart2 213 qstarts["ML-is-a-machine-learning-quickstart"] = quickstart3 214 215 quickstartModel := &quickstarts.QuickstartModel{ 216 Quickstarts: qstarts, 217 } 218 219 quickstartFilter := &quickstarts.QuickstartFilter{ 220 Text: "", 221 } 222 223 results := quickstartModel.Filter(quickstartFilter) 224 225 assert.Equal(t, 2, len(results)) 226 assert.Contains(t, results, quickstart1) 227 assert.Contains(t, results, quickstart2) 228 assert.NotContains(t, results, quickstart3) 229 } 230 231 func TestQuickstartCreateVersion(t *testing.T) { 232 t.Parallel() 233 234 sha := "d9e925718" 235 v := quickstarts.QuickStartVersion(sha) 236 sv, err := semver.Parse(v) 237 require.NoError(t, err, "failed to parse semantic version %s for quickstart", v) 238 t.Logf("parsed semantic version %s for quickstart", sv.String()) 239 }