github.com/thetreep/go-swagger@v0.0.0-20240223100711-35af64f14f01/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/stretchr/testify/assert" 17 "github.com/thetreep/go-swagger/fixtures/bugs/1571/gen-fixture-simple-tuple-minimal/models" 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( 34 cwd, func(path string, info os.FileInfo, err error) error { 35 fixture := info.Name() 36 // t.Logf("Found: %s", fixture) 37 if !info.IsDir() && strings.HasPrefix(fixture, base) { 38 // read fixture 39 buf, _ := os.ReadFile(filepath.Join("json-data", fixture)) 40 41 t.Logf("INFO:Fixture: %s: %s", fixture, string(buf)) 42 input := []interface{}{} 43 erm := json.Unmarshal(buf, &input) 44 if !assert.NoErrorf(t, erm, "ERROR:Error unmarshaling fixture: %v", erm) { 45 t.FailNow() 46 return erm 47 } 48 49 // run validate.AgainstSchema 50 // bb, _ := json.MarshalIndent(schema, "", " ") 51 // t.Log(string(bb)) 52 erj := validate.AgainstSchema(schema, input, strfmt.Default) 53 if erj == nil { 54 t.Logf("INFO:Validation AgainstSchema for %s returned: valid", fixture) 55 } else { 56 t.Logf("INFO AgainstSchema for %s returned: invalid, with %v", fixture, erj) 57 } 58 // bb, _ = json.MarshalIndent(schema, "", " ") 59 // t.Log(string(bb)) 60 // unmarshall into model 61 var erv error 62 model := models.TupleThing{} 63 eru := model.UnmarshalJSON(buf) 64 if assert.NoErrorf(t, eru, "ERROR:Error unmarshaling struct: %v", eru) { 65 // run model validation 66 erv = model.Validate(strfmt.Default) 67 if erv == nil { 68 t.Logf("INFO:Validation for %s returned: valid", fixture) 69 70 } else { 71 t.Logf("INFO:Validation for %s returned: invalid, with: %v", fixture, erv) 72 } 73 } else { 74 t.FailNow() 75 return eru 76 } 77 // marshall the model back to json 78 bbb, erm := model.MarshalJSON() 79 if assert.NoErrorf(t, erm, "ERROR:Error marshaling: %v", erm) { 80 t.Logf("INFO:Data marshalled as: %s", string(bbb)) 81 } 82 // compare validation methods 83 if erv != nil && erj == nil || erv == nil && erj != nil { 84 t.Logf("ERROR:Our validators returned different results for: %s", fixture) 85 if fixture == strings.Join([]string{base, "2"}, "-")+".json" { 86 t.Logf("WARNING: expected failure - see issue #1486") 87 } else { 88 t.Fail() 89 } 90 } 91 } 92 return nil 93 }, 94 ) 95 96 }