Пример #1
0
func TestGetCreateUser(t *testing.T) {
	mdb := db.GetSetMock()
	login := "******"
	sqlmock.ExpectQuery(`SELECT \* from "user" WHERE login.*`).
		WithArgs(login).
		WillReturnError(fmt.Errorf("user not found"))
	sqlmock.ExpectExec(`INSERT INTO "user".*`).
		WithArgs(login).
		WillReturnResult(sqlmock.NewResult(0, 1)) // 1 affected row.
	sqlmock.ExpectQuery(`SELECT \* from "user" WHERE login.*`).
		WithArgs(login).
		WillReturnRows(
			sqlmock.NewRows([]string{"uid", "login"}).
				AddRow(1, login))
	u, err := GetCreateUser(login)
	if err != nil {
		t.Error(err)
	}
	if u.Login != login {
		t.Errorf("Got user login %s, wanted %s", u.Login, login)
	}
	if err := mdb.Close(); err != nil {
		t.Error(err)
	}
}
Пример #2
0
func TestCreateUser(t *testing.T) {
	mdb := db.GetSetMock()
	login := "******"
	sqlmock.ExpectExec(`INSERT INTO "user".*`).
		WithArgs(login).
		WillReturnResult(sqlmock.NewResult(0, 1)) // 1 affected row.
	if err := CreateUser(login); err != nil {
		t.Error(err)
	}
	if err := mdb.Close(); err != nil {
		t.Error(err)
	}
}
Пример #3
0
func TestSetEmail(t *testing.T) {
	mdb := db.GetSetMock()
	email := "*****@*****.**"
	uid := 1
	sqlmock.ExpectExec(`UPDATE "user" SET email = .* WHERE uid = .*`).
		WithArgs(email, uid).
		WillReturnResult(sqlmock.NewResult(0, 1)) // 1 affected row.
	if err := SetEmail(User{UID: 1}, email); err != nil {
		t.Error(err)
	}
	if err := mdb.Close(); err != nil {
		t.Error(err)
	}
}
Пример #4
0
func TestCreateCommit(t *testing.T) {
	mdb := db.GetSetMock()
	u := User{UID: 1, Login: "******"}
	c := GetGitHubCommitRepoForTest(u.Login)
	sqlmock.ExpectBegin()
	sqlmock.ExpectExec("INSERT INTO commit.*").
		WithArgs(*c.SHA, u.UID, *c.Commit.Author.Date, c.RepoName,
								*c.Commit.Message, *c.Stats.Additions, *c.Stats.Deletions).
		WillReturnResult(sqlmock.NewResult(0, 1)) // 1 affected row.
	f := c.Files[0]
	sqlmock.ExpectExec("INSERT INTO commit_file.*").
		WithArgs(*c.SHA, *f.Filename, *f.Status, *f.Additions, *f.Deletions, *f.Patch).
		WillReturnResult(sqlmock.NewResult(0, 1)) // 1 affected row.
	sqlmock.ExpectCommit()
	if err := CreateCommit(u, c); err != nil {
		t.Error(err)
	}
	if err := mdb.Close(); err != nil {
		t.Error(err)
	}
}