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

     1  package database
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/drone/drone/pkg/database"
     7  )
     8  
     9  // TODO unit test to verify unique constraint on Member.UserID and Member.TeamID
    10  
    11  // TestGetRepo tests the ability to retrieve a Repo
    12  // from the database by Unique ID.
    13  func TestGetRepo(t *testing.T) {
    14  	Setup()
    15  	defer Teardown()
    16  
    17  	repo, err := database.GetRepo(1)
    18  	if err != nil {
    19  		t.Error(err)
    20  	}
    21  
    22  	if repo.ID != 1 {
    23  		t.Errorf("Exepected ID %d, got %d", 1, repo.ID)
    24  	}
    25  
    26  	if repo.Slug != "github.com/drone/drone" {
    27  		t.Errorf("Exepected Slug %s, got %s", "github.com/drone/drone", repo.Slug)
    28  	}
    29  
    30  	if repo.Host != "github.com" {
    31  		t.Errorf("Exepected Host %s, got %s", "github.com", repo.Host)
    32  	}
    33  
    34  	if repo.Owner != "drone" {
    35  		t.Errorf("Exepected Owner %s, got %s", "drone", repo.Owner)
    36  	}
    37  
    38  	if repo.Name != "drone" {
    39  		t.Errorf("Exepected Name %s, got %s", "drone", repo.Name)
    40  	}
    41  
    42  	if repo.Private != true {
    43  		t.Errorf("Exepected Private %v, got %v", true, repo.Private)
    44  	}
    45  
    46  	if repo.Disabled != false {
    47  		t.Errorf("Exepected Private %v, got %v", false, repo.Disabled)
    48  	}
    49  
    50  	if repo.SCM != "git" {
    51  		t.Errorf("Exepected Type %s, got %s", "git", repo.SCM)
    52  	}
    53  
    54  	if repo.URL != "git@github.com:drone/drone.git" {
    55  		t.Errorf("Exepected URL %s, got %s", "git@github.com:drone/drone.git", repo.URL)
    56  	}
    57  
    58  	if repo.Username != "no username" {
    59  		t.Errorf("Exepected Username %s, got %s", "no username", repo.Username)
    60  	}
    61  
    62  	if repo.Password != "no password" {
    63  		t.Errorf("Exepected Password %s, got %s", "no password", repo.Password)
    64  	}
    65  
    66  	if repo.PublicKey != pubkey {
    67  		t.Errorf("Exepected PublicKey %s, got %s", "public key", repo.PublicKey)
    68  	}
    69  
    70  	if repo.PrivateKey != privkey {
    71  		t.Errorf("Exepected PrivateKey %s, got %s", "private key", repo.PrivateKey)
    72  	}
    73  
    74  	if repo.UserID != 1 {
    75  		t.Errorf("Exepected ID %d, got %d", 1, repo.UserID)
    76  	}
    77  
    78  	if repo.TeamID != 1 {
    79  		t.Errorf("Exepected ID %d, got %d", 1, repo.TeamID)
    80  	}
    81  }
    82  
    83  // TestGetRepoSlug tests the ability to retrieve a Repo
    84  // from the database by it's Canonical Name.
    85  func TestGetRepoSlug(t *testing.T) {
    86  	Setup()
    87  	defer Teardown()
    88  
    89  	repo, err := database.GetRepoSlug("github.com/drone/drone")
    90  	if err != nil {
    91  		t.Error(err)
    92  	}
    93  
    94  	if repo.ID != 1 {
    95  		t.Errorf("Exepected ID %d, got %d", 1, repo.ID)
    96  	}
    97  
    98  	if repo.Slug != "github.com/drone/drone" {
    99  		t.Errorf("Exepected Slug %s, got %s", "github.com/drone/drone", repo.Slug)
   100  	}
   101  
   102  	if repo.Host != "github.com" {
   103  		t.Errorf("Exepected Host %s, got %s", "github.com", repo.Host)
   104  	}
   105  
   106  	if repo.Owner != "drone" {
   107  		t.Errorf("Exepected Owner %s, got %s", "drone", repo.Owner)
   108  	}
   109  
   110  	if repo.Name != "drone" {
   111  		t.Errorf("Exepected Name %s, got %s", "drone", repo.Name)
   112  	}
   113  
   114  	if repo.Private != true {
   115  		t.Errorf("Exepected Private %v, got %v", true, repo.Private)
   116  	}
   117  
   118  	if repo.Disabled != false {
   119  		t.Errorf("Exepected Private %v, got %v", false, repo.Disabled)
   120  	}
   121  
   122  	if repo.SCM != "git" {
   123  		t.Errorf("Exepected Type %s, got %s", "git", repo.SCM)
   124  	}
   125  
   126  	if repo.URL != "git@github.com:drone/drone.git" {
   127  		t.Errorf("Exepected URL %s, got %s", "git@github.com:drone/drone.git", repo.URL)
   128  	}
   129  
   130  	if repo.Username != "no username" {
   131  		t.Errorf("Exepected Username %s, got %s", "no username", repo.Username)
   132  	}
   133  
   134  	if repo.Password != "no password" {
   135  		t.Errorf("Exepected Password %s, got %s", "no password", repo.Password)
   136  	}
   137  
   138  	if repo.PublicKey != pubkey {
   139  		t.Errorf("Exepected PublicKey %s, got %s", "public key", repo.PublicKey)
   140  	}
   141  
   142  	if repo.PrivateKey != privkey {
   143  		t.Errorf("Exepected PrivateKey %s, got %s", "private key", repo.PrivateKey)
   144  	}
   145  
   146  	if repo.UserID != 1 {
   147  		t.Errorf("Exepected ID %d, got %d", 1, repo.UserID)
   148  	}
   149  
   150  	if repo.TeamID != 1 {
   151  		t.Errorf("Exepected ID %d, got %d", 1, repo.TeamID)
   152  	}
   153  }
   154  
   155  func TestSaveRepo(t *testing.T) {
   156  	Setup()
   157  	defer Teardown()
   158  
   159  	// get the repo we plan to update
   160  	repo, err := database.GetRepo(1)
   161  	if err != nil {
   162  		t.Error(err)
   163  	}
   164  
   165  	// update fields
   166  	repo.Slug = "bitbucket.org/drone/drone"
   167  	repo.Host = "bitbucket.org"
   168  	repo.Private = false
   169  	repo.Disabled = true
   170  	repo.SCM = "hg"
   171  	repo.URL = "https://bitbucket.org/drone/drone"
   172  	repo.Username = "brad"
   173  	repo.Password = "password"
   174  	repo.TeamID = 0
   175  
   176  	// update the database
   177  	if err := database.SaveRepo(repo); err != nil {
   178  		t.Error(err)
   179  	}
   180  
   181  	// get the updated repo
   182  	updatedRepo, err := database.GetRepo(1)
   183  	if err != nil {
   184  		t.Error(err)
   185  	}
   186  
   187  	if updatedRepo.Slug != repo.Slug {
   188  		t.Errorf("Exepected Slug %s, got %s", updatedRepo.Slug, repo.Slug)
   189  	}
   190  
   191  	if updatedRepo.Host != repo.Host {
   192  		t.Errorf("Exepected Host %s, got %s", updatedRepo.Host, repo.Host)
   193  	}
   194  
   195  	if updatedRepo.Private != repo.Private {
   196  		t.Errorf("Exepected Private %v, got %v", updatedRepo.Private, repo.Private)
   197  	}
   198  
   199  	if updatedRepo.Disabled != repo.Disabled {
   200  		t.Errorf("Exepected Private %v, got %v", updatedRepo.Disabled, repo.Disabled)
   201  	}
   202  
   203  	if updatedRepo.SCM != repo.SCM {
   204  		t.Errorf("Exepected Type %s, got %s", true, repo.SCM)
   205  	}
   206  
   207  	if updatedRepo.URL != repo.URL {
   208  		t.Errorf("Exepected URL %s, got %s", updatedRepo.URL, repo.URL)
   209  	}
   210  
   211  	if updatedRepo.Username != repo.Username {
   212  		t.Errorf("Exepected Username %s, got %s", updatedRepo.Username, repo.Username)
   213  	}
   214  
   215  	if updatedRepo.Password != repo.Password {
   216  		t.Errorf("Exepected Password %s, got %s", updatedRepo.Password, repo.Password)
   217  	}
   218  
   219  	if updatedRepo.TeamID != repo.TeamID {
   220  		t.Errorf("Exepected TeamID %d, got %d", updatedRepo.TeamID, repo.TeamID)
   221  	}
   222  }
   223  
   224  func TestDeleteRepo(t *testing.T) {
   225  	Setup()
   226  	defer Teardown()
   227  
   228  	if err := database.DeleteRepo(1); err != nil {
   229  		t.Error(err)
   230  	}
   231  
   232  	// try to get the deleted row
   233  	_, err := database.GetRepo(1)
   234  	if err == nil {
   235  		t.Fail()
   236  	}
   237  }
   238  
   239  /*
   240  func TestListRepos(t *testing.T) {
   241  	Setup()
   242  	defer Teardown()
   243  
   244  	// repos for user_id = 1
   245  	repos, err := database.ListRepos(1)
   246  	if err != nil {
   247  		t.Error(err)
   248  	}
   249  
   250  	// verify user count
   251  	if len(repos) != 2 {
   252  		t.Errorf("Exepected %d repos in database, got %d", 2, len(repos))
   253  		return
   254  	}
   255  
   256  	// get the second repo in the list and verify
   257  	// fields are being populated correctly
   258  	// NOTE: we get the 2nd repo due to sorting
   259  	repo := repos[1]
   260  
   261  	if repo.ID != 1 {
   262  		t.Errorf("Exepected ID %d, got %d", 1, repo.ID)
   263  	}
   264  
   265  	if repo.Name != "github.com/drone/drone" {
   266  		t.Errorf("Exepected Name %s, got %s", "github.com/drone/drone", repo.Name)
   267  	}
   268  
   269  	if repo.Host != "github.com" {
   270  		t.Errorf("Exepected Host %s, got %s", "github.com", repo.Host)
   271  	}
   272  
   273  	if repo.Owner != "drone" {
   274  		t.Errorf("Exepected Owner %s, got %s", "drone", repo.Owner)
   275  	}
   276  
   277  	if repo.Slug != "drone" {
   278  		t.Errorf("Exepected Slug %s, got %s", "drone", repo.Slug)
   279  	}
   280  
   281  	if repo.Private != true {
   282  		t.Errorf("Exepected Private %v, got %v", true, repo.Private)
   283  	}
   284  
   285  	if repo.Disabled != false {
   286  		t.Errorf("Exepected Private %v, got %v", false, repo.Disabled)
   287  	}
   288  
   289  	if repo.SCM != "git" {
   290  		t.Errorf("Exepected Type %s, got %s", "git", repo.SCM)
   291  	}
   292  
   293  	if repo.URL != "git@github.com:drone/drone.git" {
   294  		t.Errorf("Exepected URL %s, got %s", "git@github.com:drone/drone.git", repo.URL)
   295  	}
   296  
   297  	if repo.Username != "no username" {
   298  		t.Errorf("Exepected Username %s, got %s", "no username", repo.Username)
   299  	}
   300  
   301  	if repo.Password != "no password" {
   302  		t.Errorf("Exepected Password %s, got %s", "no password", repo.Password)
   303  	}
   304  
   305  	if repo.PublicKey != "public key" {
   306  		t.Errorf("Exepected PublicKey %s, got %s", "public key", repo.PublicKey)
   307  	}
   308  
   309  	if repo.PrivateKey != "private key" {
   310  		t.Errorf("Exepected PrivateKey %s, got %s", "private key", repo.PrivateKey)
   311  	}
   312  
   313  	if repo.UserID != 1 {
   314  		t.Errorf("Exepected ID %d, got %d", 1, repo.UserID)
   315  	}
   316  
   317  	if repo.TeamID != 1 {
   318  		t.Errorf("Exepected ID %d, got %d", 1, repo.TeamID)
   319  	}
   320  }
   321  */
   322  
   323  func TestListReposTeam(t *testing.T) {
   324  	Setup()
   325  	defer Teardown()
   326  
   327  	// repos for team_id = 1
   328  	repos, err := database.ListReposTeam(1)
   329  	if err != nil {
   330  		t.Error(err)
   331  	}
   332  
   333  	// verify user count
   334  	if len(repos) != 2 {
   335  		t.Errorf("Exepected %d repos in database, got %d", 2, len(repos))
   336  		return
   337  	}
   338  
   339  	// get the second repo in the list and verify
   340  	// fields are being populated correctly
   341  	// NOTE: we get the 2nd repo due to sorting
   342  	repo := repos[1]
   343  
   344  	if repo.ID != 1 {
   345  		t.Errorf("Exepected ID %d, got %d", 1, repo.ID)
   346  	}
   347  
   348  	if repo.Slug != "github.com/drone/drone" {
   349  		t.Errorf("Exepected Slug %s, got %s", "github.com/drone/drone", repo.Slug)
   350  	}
   351  
   352  	if repo.Host != "github.com" {
   353  		t.Errorf("Exepected Host %s, got %s", "github.com", repo.Host)
   354  	}
   355  
   356  	if repo.Owner != "drone" {
   357  		t.Errorf("Exepected Owner %s, got %s", "drone", repo.Owner)
   358  	}
   359  
   360  	if repo.Name != "drone" {
   361  		t.Errorf("Exepected Name %s, got %s", "drone", repo.Name)
   362  	}
   363  
   364  	if repo.Private != true {
   365  		t.Errorf("Exepected Private %v, got %v", true, repo.Private)
   366  	}
   367  
   368  	if repo.Disabled != false {
   369  		t.Errorf("Exepected Private %v, got %v", false, repo.Disabled)
   370  	}
   371  
   372  	if repo.SCM != "git" {
   373  		t.Errorf("Exepected Type %s, got %s", "git", repo.SCM)
   374  	}
   375  
   376  	if repo.URL != "git@github.com:drone/drone.git" {
   377  		t.Errorf("Exepected URL %s, got %s", "git@github.com:drone/drone.git", repo.URL)
   378  	}
   379  
   380  	if repo.Username != "no username" {
   381  		t.Errorf("Exepected Username %s, got %s", "no username", repo.Username)
   382  	}
   383  
   384  	if repo.Password != "no password" {
   385  		t.Errorf("Exepected Password %s, got %s", "no password", repo.Password)
   386  	}
   387  
   388  	if repo.PublicKey != pubkey {
   389  		t.Errorf("Exepected PublicKey %s, got %s", "public key", repo.PublicKey)
   390  	}
   391  
   392  	if repo.PrivateKey != privkey {
   393  		t.Errorf("Exepected PrivateKey %s, got %s", "private key", repo.PrivateKey)
   394  	}
   395  
   396  	if repo.UserID != 1 {
   397  		t.Errorf("Exepected ID %d, got %d", 1, repo.UserID)
   398  	}
   399  
   400  	if repo.TeamID != 1 {
   401  		t.Errorf("Exepected ID %d, got %d", 1, repo.TeamID)
   402  	}
   403  }