github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/database/testing/commits_test.go (about)

     1  package database
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/drone/drone/pkg/database"
     7  )
     8  
     9  func TestGetCommit(t *testing.T) {
    10  	Setup()
    11  	defer Teardown()
    12  
    13  	commit, err := database.GetCommit(1)
    14  	if err != nil {
    15  		t.Error(err)
    16  	}
    17  
    18  	if commit.ID != 1 {
    19  		t.Errorf("Expected ID %d, got %d", 1, commit.ID)
    20  	}
    21  
    22  	if commit.Status != "Success" {
    23  		t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
    24  	}
    25  
    26  	if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
    27  		t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
    28  	}
    29  
    30  	if commit.Branch != "master" {
    31  		t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
    32  	}
    33  
    34  	if commit.Author != "brad.rydzewski@gmail.com" {
    35  		t.Errorf("Expected Author %s, got %s", "master", commit.Author)
    36  	}
    37  
    38  	if commit.Message != "commit message" {
    39  		t.Errorf("Expected Message %s, got %s", "master", commit.Message)
    40  	}
    41  
    42  	if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
    43  		t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
    44  	}
    45  }
    46  
    47  func TestGetCommitBranchHash(t *testing.T) {
    48  	Setup()
    49  	defer Teardown()
    50  
    51  	commit, err := database.GetCommitBranchHash("develop", "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", 2)
    52  	if err != nil {
    53  		t.Error(err)
    54  	}
    55  
    56  	if commit.ID != 5 {
    57  		t.Errorf("Exepected ID %d, got %d", 5, commit.ID)
    58  	}
    59  
    60  	if commit.Branch != "develop" {
    61  		t.Errorf("Exepected Branch %s, got %s", "develop", commit.Branch)
    62  	}
    63  
    64  	if commit.Hash != "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9" {
    65  		t.Errorf("Exepected Hash %s, got %s", "5f32ec7b08dfe3a097c1a5316de5b5069fb35ff9", commit.Hash)
    66  	}
    67  
    68  	if commit.Status != "Success" {
    69  		t.Errorf("Exepected Status %s, got %s", "Success", commit.Status)
    70  	}
    71  }
    72  
    73  func TestGetCommitHash(t *testing.T) {
    74  	Setup()
    75  	defer Teardown()
    76  
    77  	commit, err := database.GetCommitHash("4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", 1)
    78  	if err != nil {
    79  		t.Error(err)
    80  	}
    81  
    82  	if commit.ID != 1 {
    83  		t.Errorf("Expected ID %d, got %d", 1, commit.ID)
    84  	}
    85  
    86  	if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
    87  		t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
    88  	}
    89  
    90  	if commit.Status != "Success" {
    91  		t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
    92  	}
    93  }
    94  
    95  func TestSaveCommit(t *testing.T) {
    96  	Setup()
    97  	defer Teardown()
    98  
    99  	// get the commit we plan to update
   100  	commit, err := database.GetCommit(1)
   101  	if err != nil {
   102  		t.Error(err)
   103  	}
   104  
   105  	// update fields
   106  	commit.Status = "Failing"
   107  
   108  	// update the database
   109  	if err := database.SaveCommit(commit); err != nil {
   110  		t.Error(err)
   111  	}
   112  
   113  	// get the updated commit
   114  	updatedCommit, err := database.GetCommit(1)
   115  	if err != nil {
   116  		t.Error(err)
   117  	}
   118  
   119  	if commit.Hash != updatedCommit.Hash {
   120  		t.Errorf("Expected Hash %s, got %s", updatedCommit.Hash, commit.Hash)
   121  	}
   122  
   123  	if commit.Status != "Failing" {
   124  		t.Errorf("Expected Status %s, got %s", updatedCommit.Status, commit.Status)
   125  	}
   126  }
   127  
   128  func TestDeleteCommit(t *testing.T) {
   129  	Setup()
   130  	defer Teardown()
   131  
   132  	if err := database.DeleteCommit(1); err != nil {
   133  		t.Error(err)
   134  	}
   135  
   136  	// try to get the deleted row
   137  	_, err := database.GetCommit(1)
   138  	if err == nil {
   139  		t.Fail()
   140  	}
   141  }
   142  
   143  func TestListCommits(t *testing.T) {
   144  	Setup()
   145  	defer Teardown()
   146  
   147  	// commits for repo_id = 1
   148  	commits, err := database.ListCommits(1, "master")
   149  	if err != nil {
   150  		t.Error(err)
   151  	}
   152  
   153  	// verify commit count
   154  	if len(commits) != 2 {
   155  		t.Errorf("Expected %d commits in database, got %d", 2, len(commits))
   156  		return
   157  	}
   158  
   159  	// get the first user in the list and verify
   160  	// fields are being populated correctly
   161  	commit := commits[1] // TODO something strange is happening with ordering here
   162  
   163  	if commit.ID != 1 {
   164  		t.Errorf("Expected ID %d, got %d", 1, commit.ID)
   165  	}
   166  
   167  	if commit.Status != "Success" {
   168  		t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
   169  	}
   170  
   171  	if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
   172  		t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
   173  	}
   174  
   175  	if commit.Branch != "master" {
   176  		t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
   177  	}
   178  
   179  	if commit.Author != "brad.rydzewski@gmail.com" {
   180  		t.Errorf("Expected Author %s, got %s", "master", commit.Author)
   181  	}
   182  
   183  	if commit.Message != "commit message" {
   184  		t.Errorf("Expected Message %s, got %s", "master", commit.Message)
   185  	}
   186  
   187  	if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
   188  		t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
   189  	}
   190  }