Beispiel #1
0
func doTestQueryMap(t *testing.T, mg *Migration, q *Qbs) {
	defer closeMigrationAndQbs(mg, q)
	assert := assrt.NewAssert(t)
	type types struct {
		Id      int64
		Name    string `qbs:"size:64"`
		Created time.Time
	}
	tp := new(types)
	mg.dropTableIfExists(tp)
	mg.CreateTableIfNotExists(tp)
	result, err := q.QueryMap("SELECT * FROM types")
	assert.Nil(result)
	assert.Equal(sql.ErrNoRows, err)
	for i := 0; i < 3; i++ {
		tp.Id = 0
		tp.Name = "abc"
		q.Save(tp)
	}
	result, err = q.QueryMap("SELECT * FROM types")
	assert.NotNil(result)
	assert.Equal(1, result["id"])
	assert.Equal("abc", result["name"])
	if _, sql3 := q.Dialect.(*sqlite3); !sql3 {
		_, ok := result["created"].(time.Time)
		assert.True(ok)
	} else {
		_, ok := result["created"].(string)
		assert.True(ok)
	}
	results, err := q.QueryMapSlice("SELECT * FROM types")
	assert.Equal(3, len(results))
}
Beispiel #2
0
func TestInterfaceToModel(t *testing.T) {
	assert := assrt.NewAssert(t)
	now := time.Now()
	table1 := &indexedTable{
		ColPrimary: 6,
		ColVarChar: "orange",
		ColTime:    now,
	}
	m := structPtrToModel(table1, true)
	assert.Equal("col_primary", m.Pk.Name)
	assert.Equal(4, len(m.Fields))
	assert.Equal(2, len(m.Indexes))
	assert.Equal("col_primary_col_time", m.Indexes[0].Name)
	assert.True(!m.Indexes[0].Unique)
	assert.Equal("col_var_char_col_time", m.Indexes[1].Name)
	assert.True(m.Indexes[1].Unique)

	f := m.Fields[0]
	id, _ := f.Value.(Id)
	assert.Equal(6, id)
	assert.True(f.PrimaryKey())

	f = m.Fields[1]
	assert.Equal("'banana'", f.Default())

	f = m.Fields[2]
	str, _ := f.Value.(string)
	assert.Equal("orange", str)
	assert.Equal(64, f.Size())

	f = m.Fields[3]
	tm, _ := f.Value.(time.Time)
	assert.Equal(now, tm)
}
Beispiel #3
0
func TestGrep(t *testing.T) {
	assert := assrt.NewAssert(t)

	cmd := exec.Command("grep", "--color=auto", "bar")
	host := siphon.NewHost(cmd, siphon.NewInternalAddr())
	host.Start()

	go func() {
		stdin := host.StdinPipe()
		stdin.Write([]byte("foo\nbar\nbaz\n"))
		stdin.Write([]byte{4}) // EOT
	}()

	outBuffer := new(bytes.Buffer)
	io.Copy(outBuffer, host.StdoutPipe())
	out := string(outBuffer.Bytes())

	expected := // I have no idea where the CR characters come from.
		"foo\r\n" +
			"bar\r\n" +
			"baz\r\n" +
			"bar\r\n"

	assert.Equal(
		expected,
		out,
	)
}
Beispiel #4
0
func doTestCreateIndexSQL(t *testing.T, info dialectSyntax) {
	assert := assrt.NewAssert(t)
	sql := info.dialect.createIndexSql("iname", "itable", true, "a", "b", "c")
	assert.Equal(info.createUniqueIndexSql, sql)
	sql = info.dialect.createIndexSql("iname2", "itable2", false, "d", "e")
	assert.Equal(info.createIndexSql, sql)
}
Beispiel #5
0
func TestGoshDoesNotReportSigStopOrContinueAsExit(t *testing.T) {
	assert := assrt.NewAssert(t)

	cmdr := NewRunningCommand(
		exec.Command("bash", "-c", "sleep 1; exit 4;"),
	)
	cmdr.Start()
	NewRunningCommand(exec.Command("kill", "-SIGSTOP", Itoa(cmdr.Pid()))).Start().Wait()

	// the command shouldn't be able to return while stopped, regardless of how short the sleep call is.
	assert.Equal(
		false,
		cmdr.WaitSoon(1500*time.Millisecond),
	)

	NewRunningCommand(exec.Command("kill", "-SIGCONT", Itoa(cmdr.Pid()))).Start().Wait()

	assert.Equal(
		4,
		cmdr.GetExitCode(),
	)
	assert.Equal(
		nil,
		cmdr.err,
	)
	assert.Equal(
		FINISHED,
		cmdr.State(),
	)
}
Beispiel #6
0
/**
Expect:
 - all of the input to come back out, because terminals default to echo mode.
 - then the grep'd string should come out, because the command recieved it and matched.
 - the grep'd string should come out surrounded by the escape codes for color, since grep's auto mode should detect that we're in a terminal.
*/
func TestPtySanity(t *testing.T) {
	assert := assrt.NewAssert(t)

	c := exec.Command("grep", "--color=auto", "bar")
	f, err := pty.Start(c)
	if err != nil {
		t.Fatal(err)
	}

	go func() {
		f.Write([]byte("foo\nbar\nbaz\n"))
		/*
			All of the input must be written in a single call to prevent this test from occationally failing nondeterministically, because:
			grep operates stream-wise and will start printing output before it has all of its input,
			and 3/4ths of the output lines are actually from the terminal operating in echo mode on the same channel.
			So there's actually a race between the terminal itself (somewhere down in kernel land I suppose?) and the output of grep.
		*/
		f.Write([]byte{4}) // EOT
	}()

	outBuffer := new(bytes.Buffer)
	io.Copy(outBuffer, f)
	out := string(outBuffer.Bytes())

	expected := // I have no idea where the CR characters come from.
		"foo\r\n" +
			"bar\r\n" +
			"baz\r\n" +
			"bar\r\n"

	assert.Equal(
		expected,
		out,
	)
}
Beispiel #7
0
func Template(t *testing.T) {
	assert := assrt.NewAssert(t)
	assert.Equal(
		"yes",
		"no",
	)
}
Beispiel #8
0
func TestIntegration_ShStreamingInputAndOutputWithStringChan(t *testing.T) {
	assert := assrt.NewAssert(t)

	msg1 := "bees\n"
	msg2 := "knees\n"
	in := make(chan string, 1)
	out := make(chan string, 1)
	catCmd := Sh("cat")("-")(Opts{In: in, Out: out}).Start()

	in <- msg1
	assert.Equal(
		msg1,
		<-out,
	)
	in <- msg2
	assert.Equal(
		msg2,
		<-out,
	)
	close(in)
	assert.Equal(
		0,
		catCmd.GetExitCode(),
	)
}
Beispiel #9
0
func doTestTransaction(t *testing.T, mg *Migration, q *Qbs) {
	defer closeMigrationAndQbs(mg, q)
	assert := assrt.NewAssert(t)
	type txModel struct {
		Id int64
		A  string
	}
	table := txModel{
		A: "A",
	}
	mg.dropTableIfExists(&table)
	mg.CreateTableIfNotExists(&table)
	q.Begin()
	assert.NotNil(q.Tx)
	_, err := q.Save(&table)
	assert.Nil(err)
	err = q.Rollback()
	assert.Nil(err)
	out := new(txModel)
	err = q.Find(out)
	assert.Equal(sql.ErrNoRows, err)
	q.Begin()
	table.Id = 0
	_, err = q.Save(&table)
	assert.Nil(err)
	err = q.Commit()
	assert.Nil(err)
	out.Id = table.Id
	err = q.Find(out)
	assert.Nil(err)
	assert.Equal("A", out.A)
}
Beispiel #10
0
func TestInterfaceToModel(t *testing.T) {
	assert := assrt.NewAssert(t)
	now := time.Now()
	table1 := &indexedTable{
		ColPrimary: 6,
		ColVarChar: "orange",
		ColTime:    now,
	}
	m := structPtrToModel(table1, true, nil)
	assert.Equal("col_primary", m.pk.name)
	assert.Equal(4, len(m.fields))
	assert.Equal(2, len(m.indexes))
	assert.Equal("col_primary_col_time", m.indexes[0].name)
	assert.True(!m.indexes[0].unique)
	assert.Equal("col_var_char_col_time", m.indexes[1].name)
	assert.True(m.indexes[1].unique)

	f := m.fields[0]
	assert.Equal(6, f.value)
	assert.True(f.pk)

	f = m.fields[1]
	assert.Equal("'banana'", f.dfault())

	f = m.fields[2]
	str, _ := f.value.(string)
	assert.Equal("orange", str)
	assert.Equal(64, f.size())

	f = m.fields[3]
	tm, _ := f.value.(time.Time)
	assert.Equal(now, tm)
}
func TestPublishNewOrphanLineage(t *testing.T) {
	do(func() {
		assert := assrt.NewAssert(t)

		g := NewGraph(".")
		lineage := "line"
		ancestor := ""

		g.Publish(
			lineage,
			ancestor,
			&GraphStoreRequest_Tar{
				Tarstream: fsSetA(),
			},
		)

		assert.Equal(
			3,
			strings.Count(
				g.cmd("ls-tree", git_branch_ref_prefix+hroot_image_ref_prefix+lineage).Output(),
				"\n",
			),
		)

		assert.Equal(
			`{"Name":"a","Type":"F","Mode":644,"ModTime":"1970-01-12T13:48:20Z"}`+"\n"+
				`{"Name":"b","Type":"F","Mode":640}`+"\n",
			g.cmd("show", git_branch_ref_prefix+hroot_image_ref_prefix+lineage+":"+".guitar").Output(),
		)
	})
}
Beispiel #12
0
func doTestUpdate(t *testing.T, mg *Migration, q *Qbs) {
	defer closeMigrationAndQbs(mg, q)
	assert := assrt.NewAssert(t)
	mg.dropTableIfExists(&basic{})
	mg.CreateTableIfNotExists(&basic{})
	_, err := q.Save(&basic{Name: "a", State: 1})
	_, err = q.Save(&basic{Name: "b", State: 1})
	_, err = q.Save(&basic{Name: "c", State: 0})
	assert.MustNil(err)
	{
		// define a temporary struct in a block to update partial columns of a table
		// as the type is in a block, so it will not conflict with other types with the same name in the same method
		type basic struct {
			Name string
		}
		affected, err := q.WhereEqual("state", 1).Update(&basic{Name: "d"})
		assert.MustNil(err)
		assert.Equal(2, affected)

		var datas []*basic
		q.WhereEqual("state", 1).FindAll(&datas)
		assert.MustEqual(2, len(datas))
		assert.Equal("d", datas[0].Name)
		assert.Equal("d", datas[1].Name)
	}

	// if choose basic table type to update, all zero value in the struct will be updated too.
	// this may be cause problems, so define a temporary struct to update table is the recommended way.
	affected, err := q.Where("state = ?", 1).Update(&basic{Name: "e"})
	assert.MustNil(err)
	assert.Equal(2, affected)
	var datas []*basic
	q.WhereEqual("state", 1).FindAll(&datas)
	assert.MustEqual(0, len(datas))
}
Beispiel #13
0
func TestExtract(t *testing.T) {
	setUpOnce.Do(setUp)
	defer tearDownOnce.Do(tearDown)
	assert = assrt.NewAssert(t)
	c := NewClient("")

	mockRequest(200, "response")
	response, err := c.ExtractOne("http://www.theonion.com/articles/fasttalking-computer-hacker-just-has-to-break-thro,32000/", Options{})
	assert.MustNil(err)
	assert.Equal("Fast-Talking Computer Hacker Just Has To Break Through Encryption Shield Before Uploading Nano-Virus", response.Title)
	assert.Equal(TypeHTML, response.Type)

	mockRequest(200, "giphy")
	response, err = c.ExtractOne("http://giphy.com/gifs/XYyT3ZRNzaflK", Options{})
	assert.MustNil(err)
	assert.Equal("Jim Carrey Animated GIF", response.Title)
	assert.Equal(TypeHTML, response.Type)

	mockRequest(200, "responses5")
	urls := []string{
		"http://google.com",
		"http://yahoo.com",
		"http://bing.com",
		"http://cnn.com",
		"http://bbc.com",
	}
	links, err := c.Extract(urls, Options{})
	assert.MustNil(err)
	assert.Equal(5, len(links))

	mockRequest(200, "responses10")
	urls = []string{
		"http://google.com",
		"http://yahoo.com",
		"http://bing.com",
		"http://cnn.com",
		"http://bbc.com",
		"http://google.com",
		"http://yahoo.com",
		"http://bing.com",
		"http://cnn.com",
		"http://bbc.com",
		"http://google.com",
		"http://yahoo.com",
		"http://bing.com",
		"http://cnn.com",
		"http://bbc.com",
	}
	links, err = c.Extract(urls, Options{})
	assert.MustNil(err)
	assert.Equal(15, len(links))
	for i, link := range links {
		assert.True(len(link.Title) > 0, strconv.Itoa(i)+"th link with empty title")
	}

	mockRequest(500, "error_response")
	_, err = c.ExtractOne("nope", Options{})
	assert.MustNotNil(err)
}
Beispiel #14
0
func TestHelloWorld(t *testing.T) {
	assert := assrt.NewAssert(t)
	req, _ := http.NewRequest("GET", "http://localhost/hello", nil)
	router := NewRouter(new(Hello))
	recorder := httptest.NewRecorder()
	router.ServeHTTP(recorder, req)
	assert.Equal(`{"data":"hello world","error":null}`, string(recorder.Body.Bytes()))
}
func TestNewGraphInitNewDir(t *testing.T) {
	do(func() {
		assertLegitGraph(
			assrt.NewAssert(t),
			NewGraph("deep"),
		)
	})
}
Beispiel #16
0
func TestJsonp(t *testing.T) {
	assert := assrt.NewAssert(t)
	router := NewRouter(new(Jsonp))
	req := NewGetRequest("", "jsonp", "callback", "dosomething")
	recorder := httptest.NewRecorder()
	router.ServeHTTP(recorder, req)
	assert.Equal(`dosomething({"data":"jsonp","error":null});`, recorder.Body.String())
}
Beispiel #17
0
func TestIntegration_CommandProvidesExitCode(t *testing.T) {
	assert := assrt.NewAssert(t)

	exitingShellCmd := Sh("bash")("-c")("exit 14").Start()
	assert.Equal(
		14,
		exitingShellCmd.GetExitCode(),
	)
}
func TestLoadGraphEmpty(t *testing.T) {
	do(func() {
		assert := assrt.NewAssert(t)

		NewGraph(".")

		assertLegitGraph(assert, LoadGraph("."))
	})
}
func TestPublishNewDerivedLineage(t *testing.T) {
	do(func() {
		assert := assrt.NewAssert(t)

		g := NewGraph(".")
		lineage := "ferk"
		ancestor := "line"

		g.Publish(
			ancestor,
			"",
			&GraphStoreRequest_Tar{
				Tarstream: fsSetA(),
			},
		)

		g.Publish(
			lineage,
			ancestor,
			&GraphStoreRequest_Tar{
				Tarstream: fsSetB(),
			},
		)

		println(g.cmd("ls-tree", git_branch_ref_prefix+hroot_image_ref_prefix+lineage).Output())
		assert.Equal(
			4,
			strings.Count(
				g.cmd("ls-tree", git_branch_ref_prefix+hroot_image_ref_prefix+lineage).Output(),
				"\n",
			),
		)

		assert.Equal(
			1, // shows a tree
			strings.Count(
				g.cmd("ls-tree", git_branch_ref_prefix+hroot_image_ref_prefix+lineage, "d/d").Output(),
				"\n",
			),
		)

		assert.Equal(
			1, // shows the file
			strings.Count(
				g.cmd("ls-tree", git_branch_ref_prefix+hroot_image_ref_prefix+lineage, "d/d/z").Output(),
				"\n",
			),
		)

		assert.Equal(
			`{"Name":"a","Type":"F","Mode":644,"ModTime":"1970-01-12T13:48:20Z"}`+"\n"+
				`{"Name":"d/d/z","Type":"F","Mode":644}`+"\n"+
				`{"Name":"e","Type":"F","Mode":755}`+"\n",
			g.cmd("show", git_branch_ref_prefix+hroot_image_ref_prefix+lineage+":"+".guitar").Output(),
		)
	})
}
Beispiel #20
0
func doTestDeleteSQL(t *testing.T, info dialectSyntax) {
	assert := assrt.NewAssert(t)
	model := structPtrToModel(sqlGenSampleData, true, nil)
	criteria := &criteria{model: model}
	criteria.mergePkCondition(info.dialect)
	sql, _ := info.dialect.deleteSql(criteria)
	sql = info.dialect.substituteMarkers(sql)
	assert.Equal(info.deleteSql, sql)
}
func TestLoadGraphAbsentIsNil(t *testing.T) {
	do(func() {
		assert := assrt.NewAssert(t)

		assert.Nil(LoadGraph("."))

		assert.Nil(LoadGraph("notadir"))
	})
}
Beispiel #22
0
func TestFieldOmit(t *testing.T) {
	assert := assrt.NewAssert(t)
	type Schema struct {
		A string `sql:"-"`
		B string
	}
	m := structPtrToModel(&Schema{}, true)
	assert.OneLen(m.Fields)
}
Beispiel #23
0
func TestIntegration_ShCombinedOutput(t *testing.T) {
	assert := assrt.NewAssert(t)

	cmd := Sh("sh")("-c", "echo out ; echo err 1>&2 ;")

	assert.Equal(
		"out\nerr\n",
		cmd.CombinedOutput(),
	)
}
Beispiel #24
0
func TestFieldOmit(t *testing.T) {
	assert := assrt.NewAssert(t)
	type Schema struct {
		A string `qbs:"-"`
		B string
		C string
	}
	m := structPtrToModel(&Schema{}, true, []string{"C"})
	assert.OneLen(m.fields)
}
Beispiel #25
0
func TestIntegration_ShOutputWithStringChan(t *testing.T) {
	assert := assrt.NewAssert(t)

	out := make(chan string, 1)
	Sh("echo")("wat")(Opts{Out: out})()
	assert.Equal(
		"wat\n",
		<-out,
	)
}
Beispiel #26
0
func TestShBakeArgsMagic(t *testing.T) {
	assert := assrt.NewAssert(t)

	echo := Sh("echo")("a", "b")("c")

	assert.Equal(
		[]string{"a", "b", "c"},
		echo.expose().args,
	)
}
Beispiel #27
0
func TestShConstruction(t *testing.T) {
	assert := assrt.NewAssert(t)

	echo := Sh("echo")

	assert.Equal(
		"echo",
		echo.expose().cmd,
	)
}
Beispiel #28
0
func TestIntegration_ShOutputWithByteSliceChan(t *testing.T) {
	assert := assrt.NewAssert(t)

	out := make(chan []byte, 1)
	Sh("echo")("wat")(Opts{Out: out})()
	assert.Equal(
		[]byte("wat\n"),
		<-out,
	)
}
Beispiel #29
0
func doTestForeignKey(t *testing.T) {
	assert := assrt.NewAssert(t)
	type User struct {
		Id   int64
		Name string
	}
	type Post struct {
		Id       int64
		Title    string
		AuthorId int64
		Author   *User
	}
	aUser := &User{
		Name: "john",
	}
	aPost := &Post{
		Title: "A Title",
	}
	WithMigration(func(mg *Migration) error {
		mg.dropTableIfExists(aPost)
		mg.dropTableIfExists(aUser)
		mg.CreateTableIfNotExists(aUser)
		mg.CreateTableIfNotExists(aPost)
		return nil
	})
	WithQbs(func(q *Qbs) error {
		affected, err := q.Save(aUser)
		assert.Nil(err)
		aPost.AuthorId = int64(aUser.Id)
		affected, err = q.Save(aPost)
		assert.Equal(1, affected)
		pst := new(Post)
		pst.Id = aPost.Id
		err = q.Find(pst)
		assert.MustNil(err)
		assert.Equal(aPost.Id, pst.Id)
		assert.Equal("john", pst.Author.Name)

		pst.Author = nil
		err = q.OmitFields("Author").Find(pst)
		assert.MustNil(err)
		assert.MustNil(pst.Author)

		err = q.OmitJoin().Find(pst)
		assert.MustNil(err)
		assert.MustNil(pst.Author)

		var psts []*Post
		err = q.FindAll(&psts)
		assert.MustNil(err)
		assert.OneLen(psts)
		assert.Equal("john", psts[0].Author.Name)
		return nil
	})
}
Beispiel #30
0
func TestParseTags(t *testing.T) {
	assert := assrt.NewAssert(t)
	m := parseTags(`fk`)
	_, ok := m["fk"]
	assert.True(ok)
	m = parseTags(`notnull,default:'banana'`)
	_, ok = m["notnull"]
	assert.True(ok)
	x, _ := m["default"]
	assert.Equal("'banana'", x)
}