11package domain
22
33import (
4- "os"
54 "testing"
65
76 "github.com/stretchr/testify/assert"
87 "github.com/stretchr/testify/require"
98)
109
1110func TestIsValidNetworkAccess (t * testing.T ) {
11+ t .Parallel ()
12+
1213 tests := []struct {
1314 name string
1415 access string
@@ -38,12 +39,16 @@ func TestIsValidNetworkAccess(t *testing.T) {
3839
3940 for _ , tt := range tests {
4041 t .Run (tt .name , func (t * testing.T ) {
42+ t .Parallel ()
43+
4144 assert .Equal (t , tt .expected , isValidNetworkAccess (tt .access ))
4245 })
4346 }
4447}
4548
4649func TestEnvironment_Validate (t * testing.T ) {
50+ t .Parallel ()
51+
4752 tests := []struct {
4853 name string
4954 environment Environment
@@ -107,6 +112,8 @@ func TestEnvironment_Validate(t *testing.T) {
107112
108113 for _ , tt := range tests {
109114 t .Run (tt .name , func (t * testing.T ) {
115+ t .Parallel ()
116+
110117 err := tt .environment .Validate ()
111118 if tt .wantErr {
112119 require .Error (t , err )
@@ -119,20 +126,21 @@ func TestEnvironment_Validate(t *testing.T) {
119126}
120127
121128func TestLoad (t * testing.T ) {
129+ t .Parallel ()
130+
122131 tests := []struct {
123- name string
124- useFixtureFile bool
125- filePath string
126- yamlContent string
127- wantErr bool
128- validate func (t * testing.T , config * DomainConfig )
132+ name string
133+ filePath string
134+ wantErr bool
135+ validate func (t * testing.T , config * DomainConfig )
129136 }{
130137 {
131- name : "load valid yaml fixture file" ,
132- useFixtureFile : true ,
133- filePath : "testdata/valid.yaml" ,
134- wantErr : false ,
138+ name : "load valid yaml fixture file" ,
139+ filePath : "testdata/valid.yaml" ,
140+ wantErr : false ,
135141 validate : func (t * testing.T , config * DomainConfig ) {
142+ t .Helper ()
143+
136144 require .NotNil (t , config )
137145
138146 // Check that all expected environments are loaded
@@ -167,33 +175,33 @@ func TestLoad(t *testing.T) {
167175 },
168176 },
169177 {
170- name : "error when file not found" ,
171- useFixtureFile : true ,
172- filePath : "/tmp/nonexistent_file.yml" ,
173- wantErr : true ,
178+ name : "error when file not found" ,
179+ filePath : "/tmp/nonexistent_file.yml" ,
180+ wantErr : true ,
174181 },
175182 {
176- name : "invalid yaml content" ,
177- useFixtureFile : true ,
178- filePath : "testdata/invalid.yaml" ,
179- wantErr : true ,
183+ name : "invalid yaml content" ,
184+ filePath : "testdata/invalid.yaml" ,
185+ wantErr : true ,
180186 },
181187 {
182- name : "empty environments" ,
183- useFixtureFile : true ,
184- filePath : "testdata/empty_environments.yaml" ,
185- wantErr : false ,
188+ name : "empty environments" ,
189+ filePath : "testdata/empty_environments.yaml" ,
190+ wantErr : false ,
186191 validate : func (t * testing.T , config * DomainConfig ) {
192+ t .Helper ()
193+
187194 require .NotNil (t , config )
188- assert .Len (t , config .Environments , 0 )
195+ assert .Empty (t , config .Environments )
189196 },
190197 },
191198 {
192- name : "multiple environments with mixed access" ,
193- useFixtureFile : true ,
194- filePath : "testdata/mixed_access.yaml" ,
195- wantErr : false ,
199+ name : "multiple environments with mixed access" ,
200+ filePath : "testdata/mixed_access.yaml" ,
201+ wantErr : false ,
196202 validate : func (t * testing.T , config * DomainConfig ) {
203+ t .Helper ()
204+
197205 require .NotNil (t , config )
198206 assert .Len (t , config .Environments , 3 )
199207
@@ -211,24 +219,9 @@ func TestLoad(t *testing.T) {
211219
212220 for _ , tt := range tests {
213221 t .Run (tt .name , func (t * testing.T ) {
214- var filePath string
222+ t . Parallel ()
215223
216- if tt .useFixtureFile {
217- filePath = tt .filePath
218- } else {
219- // Create temporary file with YAML content
220- tmpFile , err := os .CreateTemp ("" , "domain_test_*.yml" )
221- require .NoError (t , err )
222- defer os .Remove (tmpFile .Name ())
223-
224- _ , err = tmpFile .WriteString (tt .yamlContent )
225- require .NoError (t , err )
226- require .NoError (t , tmpFile .Close ())
227-
228- filePath = tmpFile .Name ()
229- }
230-
231- config , err := Load (filePath )
224+ config , err := Load (tt .filePath )
232225
233226 if tt .wantErr {
234227 require .Error (t , err )
@@ -243,195 +236,3 @@ func TestLoad(t *testing.T) {
243236 })
244237 }
245238}
246-
247- func TestDomainConfig_Validation (t * testing.T ) {
248- tests := []struct {
249- name string
250- yamlContent string
251- loadShouldFail bool
252- envToValidate string
253- validationErr bool
254- errContains string
255- validate func (t * testing.T , config * DomainConfig )
256- }{
257- {
258- name : "environments with invalid network access should be loadable but fail validation" ,
259- yamlContent : `
260- environments:
261- development:
262- networkAccess:
263- - invalid_access
264- ` ,
265- loadShouldFail : false ,
266- envToValidate : "development" ,
267- validationErr : true ,
268- errContains : "invalid networkAccess value: invalid_access" ,
269- },
270- {
271- name : "environments with duplicate network access should fail validation" ,
272- yamlContent : `
273- environments:
274- development:
275- networkAccess:
276- - testnet
277- - testnet
278- ` ,
279- loadShouldFail : false ,
280- envToValidate : "development" ,
281- validationErr : true ,
282- errContains : "duplicate networkAccess value: testnet" ,
283- },
284- {
285- name : "environments with duplicate mainnet should fail validation" ,
286- yamlContent : `
287- environments:
288- production:
289- networkAccess:
290- - mainnet
291- - mainnet
292- ` ,
293- loadShouldFail : false ,
294- envToValidate : "production" ,
295- validationErr : true ,
296- errContains : "duplicate networkAccess value: mainnet" ,
297- },
298- {
299- name : "environments with empty network access should fail validation" ,
300- yamlContent : `
301- environments:
302- development:
303- networkAccess: []
304- ` ,
305- loadShouldFail : false ,
306- envToValidate : "development" ,
307- validationErr : true ,
308- errContains : "networkAccess is required and cannot be empty" ,
309- },
310- {
311- name : "environments with mixed invalid and valid access should fail validation" ,
312- yamlContent : `
313- environments:
314- staging:
315- networkAccess:
316- - testnet
317- - invalid_network
318- - mainnet
319- ` ,
320- loadShouldFail : false ,
321- envToValidate : "staging" ,
322- validationErr : true ,
323- errContains : "invalid networkAccess value: invalid_network" ,
324- },
325- {
326- name : "empty environments map should load successfully" ,
327- yamlContent : `
328- environments: {}
329- ` ,
330- loadShouldFail : false ,
331- validate : func (t * testing.T , config * DomainConfig ) {
332- require .NotNil (t , config )
333- assert .Len (t , config .Environments , 0 )
334- },
335- },
336- {
337- name : "valid environments should pass validation" ,
338- yamlContent : `
339- environments:
340- development:
341- networkAccess:
342- - testnet
343- staging:
344- networkAccess:
345- - testnet
346- - mainnet
347- production:
348- networkAccess:
349- - mainnet
350- ` ,
351- loadShouldFail : false ,
352- validate : func (t * testing.T , config * DomainConfig ) {
353- require .NotNil (t , config )
354- assert .Len (t , config .Environments , 3 )
355-
356- // Validate each environment
357- for envName , env := range config .Environments {
358- err := env .Validate ()
359- assert .NoError (t , err , "Environment %s should be valid" , envName )
360- }
361- },
362- },
363- {
364- name : "multiple environments with some invalid should allow selective validation" ,
365- yamlContent : `
366- environments:
367- valid_env:
368- networkAccess:
369- - testnet
370- invalid_env:
371- networkAccess:
372- - invalid_access
373- ` ,
374- loadShouldFail : false ,
375- validate : func (t * testing.T , config * DomainConfig ) {
376- require .NotNil (t , config )
377- assert .Len (t , config .Environments , 2 )
378-
379- // Valid environment should pass validation
380- validEnv := config .Environments ["valid_env" ]
381- err := validEnv .Validate ()
382- assert .NoError (t , err )
383-
384- // Invalid environment should fail validation
385- invalidEnv := config .Environments ["invalid_env" ]
386- err = invalidEnv .Validate ()
387- assert .Error (t , err )
388- assert .Contains (t , err .Error (), "invalid networkAccess value: invalid_access" )
389- },
390- },
391- }
392-
393- for _ , tt := range tests {
394- t .Run (tt .name , func (t * testing.T ) {
395- // Create temporary file with YAML content
396- tmpFile , err := os .CreateTemp ("" , "domain_test_*.yml" )
397- require .NoError (t , err )
398- defer os .Remove (tmpFile .Name ())
399-
400- _ , err = tmpFile .WriteString (tt .yamlContent )
401- require .NoError (t , err )
402- require .NoError (t , tmpFile .Close ())
403-
404- config , err := Load (tmpFile .Name ())
405-
406- if tt .loadShouldFail {
407- require .Error (t , err )
408- assert .Nil (t , config )
409- return
410- }
411-
412- require .NoError (t , err )
413- require .NotNil (t , config )
414-
415- // If we need to validate a specific environment
416- if tt .envToValidate != "" {
417- env , exists := config .Environments [tt .envToValidate ]
418- require .True (t , exists , "Environment %s should exist" , tt .envToValidate )
419-
420- err = env .Validate ()
421- if tt .validationErr {
422- require .Error (t , err )
423- if tt .errContains != "" {
424- assert .Contains (t , err .Error (), tt .errContains )
425- }
426- } else {
427- require .NoError (t , err )
428- }
429- }
430-
431- // Run custom validation if provided
432- if tt .validate != nil {
433- tt .validate (t , config )
434- }
435- })
436- }
437- }
0 commit comments