github.com/josephspurrier/go-swagger@v0.2.1-0.20221129144919-1f672a142a00/fixtures/bugs/1571/tupleThing_test.go (about) 1 //go:build ignore 2 // +build ignore 3 4 package main 5 6 import ( 7 "encoding/json" 8 "os" 9 "path/filepath" 10 "strings" 11 "testing" 12 13 "github.com/go-openapi/spec" 14 "github.com/go-openapi/strfmt" 15 "github.com/go-openapi/validate" 16 "github.com/go-swagger/go-swagger/fixtures/bugs/1571/gen-fixture-simple-tuple-minimal/models" 17 "github.com/stretchr/testify/assert" 18 ) 19 20 func Test_TupleThing(t *testing.T) { 21 base := "tupleThing-data" 22 cwd, _ := os.Getwd() 23 cwd = filepath.Join(cwd, "json-data") 24 schemaSource := filepath.Join(cwd, "tupleThing.json") 25 // read schema 26 jsonSchema, _ := os.ReadFile(schemaSource) 27 schema := new(spec.Schema) 28 err := json.Unmarshal(jsonSchema, schema) 29 if !assert.NoError(t, err) { 30 t.FailNow() 31 return 32 } 33 filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error { 34 fixture := info.Name() 35 //t.Logf("Found: %s", fixture) 36 if !info.IsDir() && strings.HasPrefix(fixture, base) { 37 // read fixture 38 buf, _ := os.ReadFile(filepath.Join("json-data", fixture)) 39 40 t.Logf("INFO:Fixture: %s: %s", fixture, string(buf)) 41 input := []interface{}{} 42 erm := json.Unmarshal(buf, &input) 43 if !assert.NoErrorf(t, erm, "ERROR:Error unmarshaling fixture: %v", erm) { 44 t.FailNow() 45 return erm 46 } 47 48 // run validate.AgainstSchema 49 //bb, _ := json.MarshalIndent(schema, "", " ") 50 //t.Log(string(bb)) 51 erj := validate.AgainstSchema(schema, input, strfmt.Default) 52 if erj == nil { 53 t.Logf("INFO:Validation AgainstSchema for %s returned: valid", fixture) 54 } else { 55 t.Logf("INFO AgainstSchema for %s returned: invalid, with %v", fixture, erj) 56 } 57 //bb, _ = json.MarshalIndent(schema, "", " ") 58 //t.Log(string(bb)) 59 // unmarshall into model 60 var erv error 61 model := models.TupleThing{} 62 eru := model.UnmarshalJSON(buf) 63 if assert.NoErrorf(t, eru, "ERROR:Error unmarshaling struct: %v", eru) { 64 // run model validation 65 erv = model.Validate(strfmt.Default) 66 if erv == nil { 67 t.Logf("INFO:Validation for %s returned: valid", fixture) 68 69 } else { 70 t.Logf("INFO:Validation for %s returned: invalid, with: %v", fixture, erv) 71 } 72 } else { 73 t.FailNow() 74 return eru 75 } 76 // marshall the model back to json 77 bbb, erm := model.MarshalJSON() 78 if assert.NoErrorf(t, erm, "ERROR:Error marshaling: %v", erm) { 79 t.Logf("INFO:Data marshalled as: %s", string(bbb)) 80 } 81 // compare validation methods 82 if erv != nil && erj == nil || erv == nil && erj != nil { 83 t.Logf("ERROR:Our validators returned different results for: %s", fixture) 84 if fixture == strings.Join([]string{base, "2"}, "-")+".json" { 85 t.Logf("WARNING: expected failure - see issue #1486") 86 } else { 87 t.Fail() 88 } 89 } 90 } 91 return nil 92 }) 93 94 }