示例#1
0
func TestSelectDocRow(t *testing.T) {
	assert := assert.New(t)
	type Post struct {
		ID    int
		Title string
	}

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var person Person
	err := testDB.
		SelectDoc("id", "name").
		Many("posts", `SELECT id, title FROM posts WHERE user_id = people.id`).
		From("people").
		Where("id = $1", 1).
		QueryStruct(&person)

	assert.NoError(err)
	assert.Equal("Mario", person.Name)
	assert.Equal(1, person.ID)

	assert.Equal(2, len(person.Posts))
	assert.Equal("Day 1", person.Posts[0].Title)
	assert.Equal("Day 2", person.Posts[1].Title)
}
func TestParseHeaderList(t *testing.T) {
	assert := assert.New(t)

	assert.Equal(
		[]string{"From", "To", "Subject"},
		ParseHeaderList("	from,to , subject"))
}
示例#3
0
func TestSelectDocNested(t *testing.T) {
	assert := assert.New(t)

	var obj jo.Object

	posts := dat.SelectDoc("id", "title").
		Many("comments", `SELECT * FROM comments WHERE comments.id = posts.id`).
		From("posts").
		Where("user_id = people.id")

	err := testDB.
		SelectDoc("id", "name").
		Many("posts", posts).
		From("people").
		Where("id = $1", 1).
		SetIsInterpolated(true).
		QueryStruct(&obj)

	assert.NoError(err)
	assert.Equal("Mario", obj.AsString("name"))
	assert.Equal(1, obj.AsInt("id"))

	assert.Equal("A very good day", obj.AsString("posts[0].comments[0].comment"))
	assert.Equal("Yum. Apple pie.", obj.AsString("posts[1].comments[0].comment"))
}
func TestCapitalize(t *testing.T) {
	assert := assert.New(t)

	assert.Equal("Hello", Capitalize("hello"))
	assert.Equal("Hello", Capitalize("Hello"))
	assert.Equal("Hello world", Capitalize("hello world"))
	assert.Equal("Èllo", Capitalize("èllo"))
}
示例#5
0
func TestEmptyConfig(t *testing.T) {
	a := assert.New(t)

	cfg, err := ReadConfig(strings.NewReader(""))
	require.Nil(t, err)

	a.Equal(0, len(cfg.Checks))
	a.Equal(0, len(cfg.Publishers))
}
示例#6
0
func TestRegularConfig(t *testing.T) {
	a := assert.New(t)
	r := require.New(t)

	cfg, err := ReadConfig(bytes.NewReader(defaultConfig))
	r.Nil(err)

	a.Equal(5, len(cfg.Checks))
	a.Equal(1, len(cfg.Publishers))
}
func TestPublisherInit(t *testing.T) {
	a := assert.New(t)
	r := require.New(t)

	runner, err := NewRunnerFromFile("testdata/config.yml")
	r.Nil(err)
	r.Equal(1, len(runner.publishers))

	expectType := reflect.TypeOf(&MemoryPublisher{})
	realType := reflect.TypeOf(runner.publishers["memory"])
	a.Equal(expectType, realType)
}
示例#8
0
func TestInsertMultipleRecords(t *testing.T) {
	assert := assert.New(t)

	s := beginTxWithFixtures()
	defer s.AutoRollback()

	res, err := s.
		InsertInto("people").
		Columns("name", "email").
		Values("apple", "*****@*****.**").
		Values("orange", "*****@*****.**").
		Values("pear", "*****@*****.**").
		Exec()
	assert.NoError(err)
	assert.EqualValues(res.RowsAffected, 3)

	person1 := Person{Name: "john_timr"}
	person2 := Person{Name: "jane_timr"}

	res, err = s.InsertInto("people").
		Columns("name", "email").
		Record(&person1).
		Record(&person2).
		Exec()
	assert.NoError(err)
	n := res.RowsAffected
	assert.EqualValues(n, 2)

	people := []Person{}
	err = s.
		Select("name").
		From("people").
		Where("name like $1", "%_timr").
		QueryStructs(&people)
	assert.NoError(err)
	assert.Equal(len(people), 2)

	n = 0
	for _, person := range people {
		if person.Name == "john_timr" {
			n++
		}
		if person.Name == "jane_timr" {
			n++
		}
	}
	assert.EqualValues(n, 2)
}
示例#9
0
func TestTaskArgs(t *testing.T) {
	assert := assert.New(t)
	result := ""
	tasks := func(p *Project) {
		p.Task1("foo", func(c *Context) {
			name := c.Args.MustString("name")
			result = name
		})
	}

	execCLI(tasks, []string{"foo", "--", "--name=gopher"}, nil)
	assert.Equal("gopher", result)
	assert.Panics(func() {
		runTask(tasks, "foo")
	})
}
示例#10
0
func TestExtractHeadersFromMail(t *testing.T) {
	assert := assert.New(t)

	email, err := os.Open("test.eml")
	if err != nil {
		t.Fatal(err)
	}

	expected := make(map[string]string)
	expected["From"] = "Fictious sender <*****@*****.**>"
	expected["To"] = "\"Fictious recipient\" <*****@*****.**>"
	expected["Subject"] = "Example subject"
	expected["No_such_header"] = ""

	headerlist := ParseHeaderList("from,to,subject,no_such_header")
	headers, err := extractHeadersFromMail(email, headerlist)
	assert.Nil(err)
	assert.Equal(expected, headers)
}
示例#11
0
func TestSelectDoc(t *testing.T) {
	assert := assert.New(t)

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var person Person
	err := testDB.
		SelectDoc("id", "name").
		From("people").
		Where("id = $1", 1).
		QueryStruct(&person)

	assert.NoError(err)
	assert.Equal("Mario", person.Name)
	assert.Equal(1, person.ID)
}
示例#12
0
func TestConstructMessageBody(t *testing.T) {
	assert := assert.New(t)

	headerlist := []string{"From", "To", "Subject"}

	headers := make(map[string]string)
	headers["From"] = "Fictious sender <*****@*****.**>"
	headers["To"] = "\"Fictious recipient\" <*****@*****.**>"
	headers["Subject"] = "Example subject"

	expected_body := "From: Fictious sender <*****@*****.**>\n" +
		"To: \"Fictious recipient\" <*****@*****.**>\n" +
		"Subject: Example subject"

	assert.Equal(expected_body, constructMessageBody(headers, headerlist))

	assert.Panics(func() {
		constructMessageBody(headers, []string{"Missing key"})
	})
}
示例#13
0
func TestSelectDocRows(t *testing.T) {
	assert := assert.New(t)
	type Post struct {
		ID    int
		Title string
	}

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var people []*Person
	err := testDB.
		SelectDoc("id", "name").
		Many("posts", `SELECT id, title FROM posts WHERE user_id = people.id`).
		From("people").
		Where("id in $1", []int{1, 2}).
		SetIsInterpolated(true).
		QueryStructs(&people)

	assert.NoError(err)

	person := people[0]
	assert.Equal("Mario", person.Name)
	assert.Equal(1, person.ID)

	assert.Equal(2, len(person.Posts))
	assert.Equal("Day 1", person.Posts[0].Title)
	assert.Equal("Day 2", person.Posts[1].Title)

	person = people[1]
	assert.Equal("John", person.Name)
	assert.Equal(2, person.ID)

	assert.Equal(2, len(person.Posts))
	assert.Equal("Apple", person.Posts[0].Title)
	assert.Equal("Orange", person.Posts[1].Title)
}
示例#14
0
func TestSelectDocNil(t *testing.T) {
	assert := assert.New(t)
	type Post struct {
		ID    int
		Title string
	}

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var person Person
	err := testDB.
		SelectDoc("id", "name").
		Many("posts", `SELECT id, title FROM posts WHERE user_id = people.id`).
		From("people").
		Where("id = $1", 1000).
		QueryStruct(&person)
	assert.Equal(sql.ErrNoRows, err)
}
示例#15
0
func TestSelectDocRowsNil(t *testing.T) {
	assert := assert.New(t)
	type Post struct {
		ID    int
		Title string
	}

	type Person struct {
		ID    int
		Name  string
		Posts []*Post
	}

	var people []*Person
	err := testDB.
		SelectDoc("id", "name").
		Many("posts", `SELECT id, title FROM posts WHERE user_id = people.id`).
		From("people").
		Where("id in $1", []int{2000, 2001}).
		SetIsInterpolated(true).
		QueryStructs(&people)
	assert.Equal(sql.ErrNoRows, err)
}
示例#16
0
func TestChecks(t *testing.T) {
	a := assert.New(t)
	r := require.New(t)

	_, err := ReadConfig(strings.NewReader("checks: { valid: {command: foo} }"))
	a.Nil(err)
	_, err = ReadConfig(strings.NewReader("checks: { invalid: {} }"))
	a.NotNil(err)
	if errt, ok := err.(Error); !ok || errt.Code != ErrInvalidConfig {
		t.Error("Expected error to be ErrInvalidConfig")
	}

	cfg, err := ReadConfig(bytes.NewReader(defaultConfig))
	r.Nil(err)

	check, ok := cfg.Checks["Dummy OK"]
	if !ok {
		t.Fatal("Expected 'Dummy OK' check not found")
	}

	a.Equal("Dummy OK", check.Name)
	a.Equal("/usr/lib/nagios/plugins/check_dummy 0", check.Command)
	a.Equal([]string{"/usr/lib/nagios/plugins/check_dummy", "0"}, check.Args)
	a.Equal(60, check.Interval)
	a.Equal(60, check.Retry)
	a.Equal(10, check.Timeout)

	check, ok = cfg.Checks["Custom"]
	if !ok {
		t.Fatal("Expected 'Dummy OK' check not found")
	}
	a.Equal("Custom check", check.Name)
	a.Equal(10, check.Interval)
	a.Equal(3, check.Retry)
	a.Equal(3, check.Timeout)
}
示例#17
0
func TestRunCheck(t *testing.T) {
	a := assert.New(t)

	check := Check{
		Name:     "Dummy",
		Command:  "",
		Args:     []string{"testdata/check_dummy", "0", "OK"},
		Interval: 1,
		Retry:    1,
		Timeout:  5,
	}
	result := runCheck(check)
	a.Equal(StateOk, result.Returncode)

	check.Args = []string{"testdata/check_dummy", "2", "CRITICAL!"}
	result = runCheck(check)
	a.Equal(StateCritical, result.Returncode)
	a.Equal(
		bytes.TrimSpace([]byte("CRITICAL!")),
		bytes.TrimSpace(result.Output),
	)

	check.Args = []string{"/no/such/file/or/directory"}
	result = runCheck(check)
	a.Equal(StateCritical, result.Returncode)

	// Note: Leave this test last or set timeout back
	check.Args = []string{"/bin/sleep", "2"}
	check.Timeout = 1
	result = runCheck(check)
	a.Equal(StateCritical, result.Returncode)
	a.Equal(
		bytes.TrimSpace([]byte("Process /bin/sleep was killed after 1 second timeout")),
		bytes.TrimSpace(result.Output),
	)
}