github.com/kaisawind/go-swagger@v0.19.0/fixtures/bugs/1548/base64Thing_test.go (about)

     1  //+build integration
     2  
     3  package main
     4  
     5  import (
     6  	"encoding/json"
     7  	"io/ioutil"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/go-openapi/strfmt"
    14  	"github.com/go-swagger/go-swagger/fixtures/bugs/1548/gen-fixture-1548/models"
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func Test_Base64Thing(t *testing.T) {
    19  	base := "base64Thing-data"
    20  	cwd, _ := os.Getwd()
    21  	// read schema
    22  	filepath.Walk(cwd, func(path string, info os.FileInfo, err error) error {
    23  		fixture := info.Name()
    24  		if !info.IsDir() && strings.HasPrefix(fixture, base) {
    25  			// read fixture
    26  			buf, _ := ioutil.ReadFile(fixture)
    27  
    28  			t.Logf("INFO:Fixture: %s: %s", fixture, string(buf))
    29  			var input interface{}
    30  			erm := json.Unmarshal(buf, &input)
    31  			if !assert.NoError(t, erm, "ERROR:Error unmarshaling fixture: %v", erm) {
    32  				t.FailNow()
    33  				return erm
    34  			}
    35  
    36  			var erv error
    37  			model := models.Base64Model{}
    38  			eru := json.Unmarshal(buf, &model)
    39  			if fixture != base+"-3.json" {
    40  				if assert.NoErrorf(t, eru, "ERROR:Error unmarshaling struct: %v", eru) {
    41  					// run model validation
    42  					erv = model.Validate(strfmt.Default)
    43  					if erv == nil {
    44  						t.Logf("INFO:Validation for %s returned: valid", fixture)
    45  
    46  					} else {
    47  						t.Logf("INFO:Validation for %s returned: invalid, with: %v", fixture, erv)
    48  					}
    49  				} else {
    50  					t.FailNow()
    51  					return eru
    52  				}
    53  			} else {
    54  				t.Logf("INFO: expected error= invalid base 64 string")
    55  			}
    56  			// marshall the model back to json
    57  			bbb, erm := json.Marshal(model)
    58  			if assert.NoErrorf(t, erm, "ERROR:Error marshaling: %v", erm) {
    59  				t.Logf("INFO:Data internal representation: %s", string(model.Prop1))
    60  				t.Logf("INFO:Data marshalled as: %s", string(bbb))
    61  			}
    62  		}
    63  		return nil
    64  	})
    65  
    66  }