code.gitea.io/gitea@v1.21.7/services/migrations/main_test.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // Copyright 2018 Jonas Franz. All rights reserved.
     3  // SPDX-License-Identifier: MIT
     4  
     5  package migrations
     6  
     7  import (
     8  	"path/filepath"
     9  	"testing"
    10  	"time"
    11  
    12  	"code.gitea.io/gitea/models/unittest"
    13  	base "code.gitea.io/gitea/modules/migration"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestMain(m *testing.M) {
    19  	unittest.MainTest(m, &unittest.TestOptions{
    20  		GiteaRootPath: filepath.Join("..", ".."),
    21  	})
    22  }
    23  
    24  func timePtr(t time.Time) *time.Time {
    25  	return &t
    26  }
    27  
    28  func assertTimeEqual(t *testing.T, expected, actual time.Time) {
    29  	assert.Equal(t, expected.UTC(), actual.UTC())
    30  }
    31  
    32  func assertTimePtrEqual(t *testing.T, expected, actual *time.Time) {
    33  	if expected == nil {
    34  		assert.Nil(t, actual)
    35  	} else {
    36  		assert.NotNil(t, actual)
    37  		assertTimeEqual(t, *expected, *actual)
    38  	}
    39  }
    40  
    41  func assertCommentEqual(t *testing.T, expected, actual *base.Comment) {
    42  	assert.Equal(t, expected.IssueIndex, actual.IssueIndex)
    43  	assert.Equal(t, expected.PosterID, actual.PosterID)
    44  	assert.Equal(t, expected.PosterName, actual.PosterName)
    45  	assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
    46  	assertTimeEqual(t, expected.Created, actual.Created)
    47  	assertTimeEqual(t, expected.Updated, actual.Updated)
    48  	assert.Equal(t, expected.Content, actual.Content)
    49  	assertReactionsEqual(t, expected.Reactions, actual.Reactions)
    50  }
    51  
    52  func assertCommentsEqual(t *testing.T, expected, actual []*base.Comment) {
    53  	if assert.Len(t, actual, len(expected)) {
    54  		for i := range expected {
    55  			assertCommentEqual(t, expected[i], actual[i])
    56  		}
    57  	}
    58  }
    59  
    60  func assertLabelEqual(t *testing.T, expected, actual *base.Label) {
    61  	assert.Equal(t, expected.Name, actual.Name)
    62  	assert.Equal(t, expected.Exclusive, actual.Exclusive)
    63  	assert.Equal(t, expected.Color, actual.Color)
    64  	assert.Equal(t, expected.Description, actual.Description)
    65  }
    66  
    67  func assertLabelsEqual(t *testing.T, expected, actual []*base.Label) {
    68  	if assert.Len(t, actual, len(expected)) {
    69  		for i := range expected {
    70  			assertLabelEqual(t, expected[i], actual[i])
    71  		}
    72  	}
    73  }
    74  
    75  func assertMilestoneEqual(t *testing.T, expected, actual *base.Milestone) {
    76  	assert.Equal(t, expected.Title, actual.Title)
    77  	assert.Equal(t, expected.Description, actual.Description)
    78  	assertTimePtrEqual(t, expected.Deadline, actual.Deadline)
    79  	assertTimeEqual(t, expected.Created, actual.Created)
    80  	assertTimePtrEqual(t, expected.Updated, actual.Updated)
    81  	assertTimePtrEqual(t, expected.Closed, actual.Closed)
    82  	assert.Equal(t, expected.State, actual.State)
    83  }
    84  
    85  func assertMilestonesEqual(t *testing.T, expected, actual []*base.Milestone) {
    86  	if assert.Len(t, actual, len(expected)) {
    87  		for i := range expected {
    88  			assertMilestoneEqual(t, expected[i], actual[i])
    89  		}
    90  	}
    91  }
    92  
    93  func assertIssueEqual(t *testing.T, expected, actual *base.Issue) {
    94  	assert.Equal(t, expected.Number, actual.Number)
    95  	assert.Equal(t, expected.PosterID, actual.PosterID)
    96  	assert.Equal(t, expected.PosterName, actual.PosterName)
    97  	assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
    98  	assert.Equal(t, expected.Title, actual.Title)
    99  	assert.Equal(t, expected.Content, actual.Content)
   100  	assert.Equal(t, expected.Ref, actual.Ref)
   101  	assert.Equal(t, expected.Milestone, actual.Milestone)
   102  	assert.Equal(t, expected.State, actual.State)
   103  	assert.Equal(t, expected.IsLocked, actual.IsLocked)
   104  	assertTimeEqual(t, expected.Created, actual.Created)
   105  	assertTimeEqual(t, expected.Updated, actual.Updated)
   106  	assertTimePtrEqual(t, expected.Closed, actual.Closed)
   107  	assertLabelsEqual(t, expected.Labels, actual.Labels)
   108  	assertReactionsEqual(t, expected.Reactions, actual.Reactions)
   109  	assert.ElementsMatch(t, expected.Assignees, actual.Assignees)
   110  }
   111  
   112  func assertIssuesEqual(t *testing.T, expected, actual []*base.Issue) {
   113  	if assert.Len(t, actual, len(expected)) {
   114  		for i := range expected {
   115  			assertIssueEqual(t, expected[i], actual[i])
   116  		}
   117  	}
   118  }
   119  
   120  func assertPullRequestEqual(t *testing.T, expected, actual *base.PullRequest) {
   121  	assert.Equal(t, expected.Number, actual.Number)
   122  	assert.Equal(t, expected.Title, actual.Title)
   123  	assert.Equal(t, expected.PosterID, actual.PosterID)
   124  	assert.Equal(t, expected.PosterName, actual.PosterName)
   125  	assert.Equal(t, expected.PosterEmail, actual.PosterEmail)
   126  	assert.Equal(t, expected.Content, actual.Content)
   127  	assert.Equal(t, expected.Milestone, actual.Milestone)
   128  	assert.Equal(t, expected.State, actual.State)
   129  	assertTimeEqual(t, expected.Created, actual.Created)
   130  	assertTimeEqual(t, expected.Updated, actual.Updated)
   131  	assertTimePtrEqual(t, expected.Closed, actual.Closed)
   132  	assertLabelsEqual(t, expected.Labels, actual.Labels)
   133  	assert.Equal(t, expected.PatchURL, actual.PatchURL)
   134  	assert.Equal(t, expected.Merged, actual.Merged)
   135  	assertTimePtrEqual(t, expected.MergedTime, actual.MergedTime)
   136  	assert.Equal(t, expected.MergeCommitSHA, actual.MergeCommitSHA)
   137  	assertPullRequestBranchEqual(t, expected.Head, actual.Head)
   138  	assertPullRequestBranchEqual(t, expected.Base, actual.Base)
   139  	assert.ElementsMatch(t, expected.Assignees, actual.Assignees)
   140  	assert.Equal(t, expected.IsLocked, actual.IsLocked)
   141  	assertReactionsEqual(t, expected.Reactions, actual.Reactions)
   142  }
   143  
   144  func assertPullRequestsEqual(t *testing.T, expected, actual []*base.PullRequest) {
   145  	if assert.Len(t, actual, len(expected)) {
   146  		for i := range expected {
   147  			assertPullRequestEqual(t, expected[i], actual[i])
   148  		}
   149  	}
   150  }
   151  
   152  func assertPullRequestBranchEqual(t *testing.T, expected, actual base.PullRequestBranch) {
   153  	assert.Equal(t, expected.CloneURL, actual.CloneURL)
   154  	assert.Equal(t, expected.Ref, actual.Ref)
   155  	assert.Equal(t, expected.SHA, actual.SHA)
   156  	assert.Equal(t, expected.RepoName, actual.RepoName)
   157  	assert.Equal(t, expected.OwnerName, actual.OwnerName)
   158  }
   159  
   160  func assertReactionEqual(t *testing.T, expected, actual *base.Reaction) {
   161  	assert.Equal(t, expected.UserID, actual.UserID)
   162  	assert.Equal(t, expected.UserName, actual.UserName)
   163  	assert.Equal(t, expected.Content, actual.Content)
   164  }
   165  
   166  func assertReactionsEqual(t *testing.T, expected, actual []*base.Reaction) {
   167  	if assert.Len(t, actual, len(expected)) {
   168  		for i := range expected {
   169  			assertReactionEqual(t, expected[i], actual[i])
   170  		}
   171  	}
   172  }
   173  
   174  func assertReleaseAssetEqual(t *testing.T, expected, actual *base.ReleaseAsset) {
   175  	assert.Equal(t, expected.ID, actual.ID)
   176  	assert.Equal(t, expected.Name, actual.Name)
   177  	assert.Equal(t, expected.ContentType, actual.ContentType)
   178  	assert.Equal(t, expected.Size, actual.Size)
   179  	assert.Equal(t, expected.DownloadCount, actual.DownloadCount)
   180  	assertTimeEqual(t, expected.Created, actual.Created)
   181  	assertTimeEqual(t, expected.Updated, actual.Updated)
   182  	assert.Equal(t, expected.DownloadURL, actual.DownloadURL)
   183  }
   184  
   185  func assertReleaseAssetsEqual(t *testing.T, expected, actual []*base.ReleaseAsset) {
   186  	if assert.Len(t, actual, len(expected)) {
   187  		for i := range expected {
   188  			assertReleaseAssetEqual(t, expected[i], actual[i])
   189  		}
   190  	}
   191  }
   192  
   193  func assertReleaseEqual(t *testing.T, expected, actual *base.Release) {
   194  	assert.Equal(t, expected.TagName, actual.TagName)
   195  	assert.Equal(t, expected.TargetCommitish, actual.TargetCommitish)
   196  	assert.Equal(t, expected.Name, actual.Name)
   197  	assert.Equal(t, expected.Body, actual.Body)
   198  	assert.Equal(t, expected.Draft, actual.Draft)
   199  	assert.Equal(t, expected.Prerelease, actual.Prerelease)
   200  	assert.Equal(t, expected.PublisherID, actual.PublisherID)
   201  	assert.Equal(t, expected.PublisherName, actual.PublisherName)
   202  	assert.Equal(t, expected.PublisherEmail, actual.PublisherEmail)
   203  	assertReleaseAssetsEqual(t, expected.Assets, actual.Assets)
   204  	assertTimeEqual(t, expected.Created, actual.Created)
   205  	assertTimeEqual(t, expected.Published, actual.Published)
   206  }
   207  
   208  func assertReleasesEqual(t *testing.T, expected, actual []*base.Release) {
   209  	if assert.Len(t, actual, len(expected)) {
   210  		for i := range expected {
   211  			assertReleaseEqual(t, expected[i], actual[i])
   212  		}
   213  	}
   214  }
   215  
   216  func assertRepositoryEqual(t *testing.T, expected, actual *base.Repository) {
   217  	assert.Equal(t, expected.Name, actual.Name)
   218  	assert.Equal(t, expected.Owner, actual.Owner)
   219  	assert.Equal(t, expected.IsPrivate, actual.IsPrivate)
   220  	assert.Equal(t, expected.IsMirror, actual.IsMirror)
   221  	assert.Equal(t, expected.Description, actual.Description)
   222  	assert.Equal(t, expected.CloneURL, actual.CloneURL)
   223  	assert.Equal(t, expected.OriginalURL, actual.OriginalURL)
   224  	assert.Equal(t, expected.DefaultBranch, actual.DefaultBranch)
   225  }
   226  
   227  func assertReviewEqual(t *testing.T, expected, actual *base.Review) {
   228  	assert.Equal(t, expected.ID, actual.ID, "ID")
   229  	assert.Equal(t, expected.IssueIndex, actual.IssueIndex, "IsssueIndex")
   230  	assert.Equal(t, expected.ReviewerID, actual.ReviewerID, "ReviewerID")
   231  	assert.Equal(t, expected.ReviewerName, actual.ReviewerName, "ReviewerName")
   232  	assert.Equal(t, expected.Official, actual.Official, "Official")
   233  	assert.Equal(t, expected.CommitID, actual.CommitID, "CommitID")
   234  	assert.Equal(t, expected.Content, actual.Content, "Content")
   235  	assert.WithinDuration(t, expected.CreatedAt, actual.CreatedAt, 10*time.Second)
   236  	assert.Equal(t, expected.State, actual.State, "State")
   237  	assertReviewCommentsEqual(t, expected.Comments, actual.Comments)
   238  }
   239  
   240  func assertReviewsEqual(t *testing.T, expected, actual []*base.Review) {
   241  	if assert.Len(t, actual, len(expected)) {
   242  		for i := range expected {
   243  			assertReviewEqual(t, expected[i], actual[i])
   244  		}
   245  	}
   246  }
   247  
   248  func assertReviewCommentEqual(t *testing.T, expected, actual *base.ReviewComment) {
   249  	assert.Equal(t, expected.ID, actual.ID)
   250  	assert.Equal(t, expected.InReplyTo, actual.InReplyTo)
   251  	assert.Equal(t, expected.Content, actual.Content)
   252  	assert.Equal(t, expected.TreePath, actual.TreePath)
   253  	assert.Equal(t, expected.DiffHunk, actual.DiffHunk)
   254  	assert.Equal(t, expected.Position, actual.Position)
   255  	assert.Equal(t, expected.Line, actual.Line)
   256  	assert.Equal(t, expected.CommitID, actual.CommitID)
   257  	assert.Equal(t, expected.PosterID, actual.PosterID)
   258  	assertReactionsEqual(t, expected.Reactions, actual.Reactions)
   259  	assertTimeEqual(t, expected.CreatedAt, actual.CreatedAt)
   260  	assertTimeEqual(t, expected.UpdatedAt, actual.UpdatedAt)
   261  }
   262  
   263  func assertReviewCommentsEqual(t *testing.T, expected, actual []*base.ReviewComment) {
   264  	if assert.Len(t, actual, len(expected)) {
   265  		for i := range expected {
   266  			assertReviewCommentEqual(t, expected[i], actual[i])
   267  		}
   268  	}
   269  }