Esempio n. 1
0
func TestGenerateModel_WithTuple(t *testing.T) {
	specDoc, err := spec.Load("../fixtures/codegen/todolist.models.yml")
	if assert.NoError(t, err) {
		definitions := specDoc.Spec().Definitions
		k := "WithTuple"
		schema := definitions[k]
		genModel, err := makeGenDefinition(k, "models", schema, specDoc)
		if assert.NoError(t, err) && assert.NotEmpty(t, genModel.ExtraSchemas) && assert.NotEmpty(t, genModel.Properties) {
			assert.False(t, genModel.IsTuple)
			assert.True(t, genModel.IsComplexObject)
			assert.False(t, genModel.IsArray)
			assert.False(t, genModel.IsAnonymous)

			sch := genModel.ExtraSchemas[0]
			assert.True(t, sch.IsTuple)
			assert.False(t, sch.IsComplexObject)
			assert.False(t, sch.IsArray)
			assert.False(t, sch.IsAnonymous)
			assert.Equal(t, k+"FlagsTuple0", sch.Name)
			assert.False(t, sch.HasAdditionalItems)
			assert.Nil(t, sch.AdditionalItems)

			prop := genModel.Properties[0]
			assert.False(t, genModel.IsTuple)
			assert.True(t, genModel.IsComplexObject)
			assert.False(t, prop.IsArray)
			assert.False(t, prop.IsAnonymous)
			assert.Equal(t, k+"FlagsTuple0", prop.GoType)
			assert.Equal(t, "flags", prop.Name)
			buf := bytes.NewBuffer(nil)
			err := modelTemplate.Execute(buf, genModel)
			if assert.NoError(t, err) {
				ff, err := formatGoFile("with_tuple.go", buf.Bytes())
				if assert.NoError(t, err) {
					res := string(ff)
					assertInCode(t, "swagger:model "+k+"Flags", res)
					assertInCode(t, "type "+k+"FlagsTuple0 struct {", res)
					assertInCode(t, "P0 int64 `json:\"-\"`", res)
					assertInCode(t, "P1 string `json:\"-\"`", res)
					assertInCode(t, k+"FlagsTuple0) UnmarshalJSON", res)
					assertInCode(t, k+"FlagsTuple0) MarshalJSON", res)
					assertInCode(t, "json.Marshal(data)", res)
					assert.NotRegexp(t, regexp.MustCompile("lastIndex"), res)

					for i, p := range sch.Properties {
						r := "m.P" + strconv.Itoa(i)
						if !p.IsNullable {
							r = "&" + r
						}
						assertInCode(t, "json.Unmarshal(stage1["+strconv.Itoa(i)+"], "+r+")", res)
						assertInCode(t, "P"+strconv.Itoa(i)+",", res)
					}
				}
			}
		}
	}
}
Esempio n. 2
0
func TestRegexp(t *testing.T) {
	app := New("test", "")
	flag := app.Flag("reg", "").Regexp()
	_, err := app.Parse([]string{"--reg", "^abc$"})
	assert.NoError(t, err)
	assert.NotNil(t, *flag)
	assert.Equal(t, "^abc$", (*flag).String())
	assert.Regexp(t, *flag, "abc")
	assert.NotRegexp(t, *flag, "abcd")
}
Esempio n. 3
0
func TestGenerateModel_SimpleTuple(t *testing.T) {
	tt := templateTest{t, modelTemplate}
	specDoc, err := spec.Load("../fixtures/codegen/todolist.models.yml")
	if assert.NoError(t, err) {
		definitions := specDoc.Spec().Definitions
		k := "SimpleTuple"
		schema := definitions[k]
		genModel, err := makeGenDefinition(k, "models", schema, specDoc)
		if assert.NoError(t, err) && assert.Empty(t, genModel.ExtraSchemas) {
			assert.True(t, genModel.IsTuple)
			assert.False(t, genModel.IsComplexObject)
			assert.False(t, genModel.IsArray)
			assert.False(t, genModel.IsAnonymous)
			assert.Equal(t, k, genModel.Name)
			assert.Equal(t, k, genModel.GoType)
			assert.Len(t, genModel.Properties, 5)
			buf := bytes.NewBuffer(nil)
			tt.template.Execute(buf, genModel)
			res := buf.String()
			assertInCode(t, "swagger:model "+k, res)
			assertInCode(t, "type "+k+" struct {", res)
			assertInCode(t, "P0 int64 `json:\"-\"`", res)
			assertInCode(t, "P1 string `json:\"-\"`", res)
			assertInCode(t, "P2 strfmt.DateTime `json:\"-\"`", res)
			assertInCode(t, "P3 *Notable `json:\"-\"`", res)
			assertInCode(t, "P4 *Notable `json:\"-\"`", res)
			assertInCode(t, k+") UnmarshalJSON", res)
			assertInCode(t, k+") MarshalJSON", res)
			assertInCode(t, "json.Marshal(data)", res)
			assert.NotRegexp(t, regexp.MustCompile("lastIndex"), res)

			for i, p := range genModel.Properties {
				r := "m.P" + strconv.Itoa(i)
				if !p.IsNullable {
					r = "&" + r
				}
				assertInCode(t, "json.Unmarshal(stage1["+strconv.Itoa(i)+"], "+r+")", res)
				assertInCode(t, "P"+strconv.Itoa(i)+",", res)
			}
		}
	}
}
Esempio n. 4
0
// NotRegexp asserts that a specified regexp does not match a string.
//
//  assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
//  assert.NotRegexp(t, "^start", "it's not starting")
//
// Returns whether the assertion was successful (true) or not (false).
func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) {
	if !assert.NotRegexp(t, rx, str, msgAndArgs...) {
		t.FailNow()
	}
}
func assertNotInCode(t testing.TB, expr, code string) bool {
	return assert.NotRegexp(t, reqm(expr), code)
}
Esempio n. 6
0
// NotRegexp asserts that a specified regexp does not match a string.
//
//  require.NotRegexp(t, regexp.MustCompile("starts"), "it's starting")
//  require.NotRegexp(t, "^start", "it's not starting")
func NotRegexp(t TestingT, rx interface{}, str interface{}) {
	if !assert.NotRegexp(t, rx, str) {
		t.FailNow()
	}
}
Esempio n. 7
0
func Test_Submit(t *testing.T) {
	check := Check{
		Name:     "test_check",
		Bulk:     "true",
		Report:   "true",
		output:   "myoutput\n",
		err_msg:  "myerror\nsecondline",
		rc:       0,
		duration: time.Duration(42 * time.Second),
		latency:  time.Duration(24 * time.Millisecond),
	}
	cfg = &Config{
		Host: "test01.example.com",
	}

	output := check.test_submission(t, false, 1024)
	expect := regexp.MustCompile(fmt.Sprintf("STATE \\d+ test01.example.com:bmad:test_check 0 %s",
		"test_check completed successfully!"))
	assert.Regexp(t, expect, output, "bulk + report returns state")

	check.rc = 2
	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile(fmt.Sprintf("STATE \\d+ test01.example.com:bmad:test_check 2 %s",
		"myerror secondline"))
	assert.Regexp(t, expect, output, "bulk + report with non-ok state gets stderr")

	check.Report = "false"
	check.Bulk = "true"
	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile("STATE")
	assert.NotRegexp(t, expect, output, "bulk + noreport doesn't do state")

	check.Report = "true"
	check.Bulk = "false"
	output = check.test_submission(t, false, 1024)
	assert.NotRegexp(t, expect, output, "nobulk + report doesn't do state")

	output = check.test_submission(t, true, 1024)
	expect = regexp.MustCompile("^myoutput")
	assert.Regexp(t, expect, output, "normal check output is still present")
	expect = regexp.MustCompile("COUNTER \\d+ test01.example.com:bmad:checks")
	assert.Regexp(t, expect, output, "bmad check counter meta-stat is reported")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:latency 0.0240")
	assert.Regexp(t, expect, output, "bmad latency meta-stat is reported")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:exec-time 42.0000")
	assert.Regexp(t, expect, output, "bmad exec time meta-stat is reported")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:test_check:exec-time 42.000")
	assert.Regexp(t, expect, output, "bmad check exec time meta-stat is reported")

	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile("^myoutput")
	assert.Regexp(t, expect, output, "normal check output is still present")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:exec-time 42.0000")
	assert.Regexp(t, expect, output, "bmad exec time meta-stat is reported")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:test_check:exec-time 42.000")
	assert.Regexp(t, expect, output, "bmad check exec time meta-stat is reported")
	expect = regexp.MustCompile("COUNTER \\d+ test01.example.com:bmad:checks")
	assert.NotRegexp(t, expect, output, "bmad check counter meta-stat is not reported")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:latency 0.0240")
	assert.NotRegexp(t, expect, output, "bmad latency meta-stat is not reported")

	check.Bulk = "true"
	check.attempts = 1
	check.Retries = 3
	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile("^myoutput")
	assert.Regexp(t, expect, output, "Bulk check with fewer attempts than retries submits status")

	check.Bulk = "false"
	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile("^myoutput")
	assert.NotRegexp(t, expect, output, "Non-bulk check with fewer attempts than retries doesn't submit status")
	expect = regexp.MustCompile("SAMPLE \\d+ test01.example.com:bmad:exec-time 42.0000")
	assert.Regexp(t, expect, output, "meta-stats are reported despite attempts less than max retries")

	check.attempts = 3
	output = check.test_submission(t, false, 1024)
	expect = regexp.MustCompile("^myoutput")
	assert.Regexp(t, expect, output, "Non-bulk check with more attempts than retries submits status")
}