2014-02-07 13:10:01 +03:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/pkg/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestGetCommit(t *testing.T) {
|
|
|
|
Setup()
|
|
|
|
defer Teardown()
|
|
|
|
|
|
|
|
commit, err := database.GetCommit(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if commit.ID != 1 {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected ID %d, got %d", 1, commit.ID)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Status != "Success" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Branch != "master" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Author != "brad.rydzewski@gmail.com" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Author %s, got %s", "master", commit.Author)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Message != "commit message" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Message %s, got %s", "master", commit.Message)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetCommitHash(t *testing.T) {
|
|
|
|
Setup()
|
|
|
|
defer Teardown()
|
|
|
|
|
|
|
|
commit, err := database.GetCommitHash("4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", 1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if commit.ID != 1 {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected ID %d, got %d", 1, commit.ID)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Status != "Success" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSaveCommit(t *testing.T) {
|
|
|
|
Setup()
|
|
|
|
defer Teardown()
|
|
|
|
|
|
|
|
// get the commit we plan to update
|
|
|
|
commit, err := database.GetCommit(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// update fields
|
|
|
|
commit.Status = "Failing"
|
|
|
|
|
|
|
|
// update the database
|
|
|
|
if err := database.SaveCommit(commit); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the updated commit
|
|
|
|
updatedCommit, err := database.GetCommit(1)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Hash != updatedCommit.Hash {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Hash %s, got %s", updatedCommit.Hash, commit.Hash)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Status != "Failing" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Status %s, got %s", updatedCommit.Status, commit.Status)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteCommit(t *testing.T) {
|
|
|
|
Setup()
|
|
|
|
defer Teardown()
|
|
|
|
|
|
|
|
if err := database.DeleteCommit(1); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try to get the deleted row
|
|
|
|
_, err := database.GetCommit(1)
|
|
|
|
if err == nil {
|
|
|
|
t.Fail()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestListCommits(t *testing.T) {
|
|
|
|
Setup()
|
|
|
|
defer Teardown()
|
|
|
|
|
|
|
|
// commits for repo_id = 1
|
|
|
|
commits, err := database.ListCommits(1, "master")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify commit count
|
|
|
|
if len(commits) != 2 {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected %d commits in database, got %d", 2, len(commits))
|
2014-02-07 13:10:01 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the first user in the list and verify
|
|
|
|
// fields are being populated correctly
|
|
|
|
commit := commits[1] // TODO something strange is happening with ordering here
|
|
|
|
|
|
|
|
if commit.ID != 1 {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected ID %d, got %d", 1, commit.ID)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Status != "Success" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Branch != "master" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Author != "brad.rydzewski@gmail.com" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Author %s, got %s", "master", commit.Author)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Message != "commit message" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Message %s, got %s", "master", commit.Message)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
|
2014-03-14 14:35:38 +03:00
|
|
|
t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
|
2014-02-07 13:10:01 +03:00
|
|
|
}
|
|
|
|
}
|