2022-01-03 18:35:01 +03:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-01-03 18:35:01 +03:00
package models
import (
"testing"
2022-04-08 12:11:15 +03:00
"code.gitea.io/gitea/models/db"
2022-03-31 12:20:39 +03:00
issues_model "code.gitea.io/gitea/models/issues"
2022-01-03 18:35:01 +03:00
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"github.com/stretchr/testify/assert"
)
func TestMigrate_InsertMilestones ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
reponame := "repo1"
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { Name : reponame } )
2022-01-03 18:35:01 +03:00
name := "milestonetest1"
2022-04-08 12:11:15 +03:00
ms := & issues_model . Milestone {
2022-01-03 18:35:01 +03:00
RepoID : repo . ID ,
Name : name ,
}
err := InsertMilestones ( ms )
assert . NoError ( t , err )
unittest . AssertExistsAndLoadBean ( t , ms )
2022-08-16 05:22:25 +03:00
repoModified := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { ID : repo . ID } )
2022-01-03 18:35:01 +03:00
assert . EqualValues ( t , repo . NumMilestones + 1 , repoModified . NumMilestones )
2022-04-08 12:11:15 +03:00
unittest . CheckConsistencyFor ( t , & issues_model . Milestone { } )
2022-01-03 18:35:01 +03:00
}
2022-02-07 18:43:08 +03:00
func assertCreateIssues ( t * testing . T , isPull bool ) {
2022-01-03 18:35:01 +03:00
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-02-07 18:43:08 +03:00
reponame := "repo1"
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { Name : reponame } )
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : repo . OwnerID } )
label := unittest . AssertExistsAndLoadBean ( t , & issues_model . Label { ID : 1 } )
milestone := unittest . AssertExistsAndLoadBean ( t , & issues_model . Milestone { ID : 1 } )
2022-01-03 18:35:01 +03:00
assert . EqualValues ( t , milestone . ID , 1 )
2022-03-31 12:20:39 +03:00
reaction := & issues_model . Reaction {
2022-01-03 18:35:01 +03:00
Type : "heart" ,
UserID : owner . ID ,
}
title := "issuetitle1"
2022-06-13 12:37:59 +03:00
is := & issues_model . Issue {
2022-01-03 18:35:01 +03:00
RepoID : repo . ID ,
MilestoneID : milestone . ID ,
Repo : repo ,
Title : title ,
Content : "issuecontent1" ,
IsPull : isPull ,
PosterID : owner . ID ,
Poster : owner ,
IsClosed : true ,
2022-06-13 12:37:59 +03:00
Labels : [ ] * issues_model . Label { label } ,
2022-03-31 12:20:39 +03:00
Reactions : [ ] * issues_model . Reaction { reaction } ,
2022-01-03 18:35:01 +03:00
}
err := InsertIssues ( is )
assert . NoError ( t , err )
2022-08-16 05:22:25 +03:00
i := unittest . AssertExistsAndLoadBean ( t , & issues_model . Issue { Title : title } )
2022-03-31 12:20:39 +03:00
unittest . AssertExistsAndLoadBean ( t , & issues_model . Reaction { Type : "heart" , UserID : owner . ID , IssueID : i . ID } )
2022-01-03 18:35:01 +03:00
}
func TestMigrate_CreateIssuesIsPullFalse ( t * testing . T ) {
2022-02-07 18:43:08 +03:00
assertCreateIssues ( t , false )
2022-01-03 18:35:01 +03:00
}
func TestMigrate_CreateIssuesIsPullTrue ( t * testing . T ) {
2022-02-07 18:43:08 +03:00
assertCreateIssues ( t , true )
2022-01-03 18:35:01 +03:00
}
func TestMigrate_InsertIssueComments ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
2022-08-16 05:22:25 +03:00
issue := unittest . AssertExistsAndLoadBean ( t , & issues_model . Issue { ID : 1 } )
2022-04-08 12:11:15 +03:00
_ = issue . LoadRepo ( db . DefaultContext )
2022-08-16 05:22:25 +03:00
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : issue . Repo . OwnerID } )
2022-03-31 12:20:39 +03:00
reaction := & issues_model . Reaction {
2022-01-03 18:35:01 +03:00
Type : "heart" ,
UserID : owner . ID ,
}
2022-06-13 12:37:59 +03:00
comment := & issues_model . Comment {
2022-01-03 18:35:01 +03:00
PosterID : owner . ID ,
Poster : owner ,
IssueID : issue . ID ,
Issue : issue ,
2022-03-31 12:20:39 +03:00
Reactions : [ ] * issues_model . Reaction { reaction } ,
2022-01-03 18:35:01 +03:00
}
2022-06-13 12:37:59 +03:00
err := InsertIssueComments ( [ ] * issues_model . Comment { comment } )
2022-01-03 18:35:01 +03:00
assert . NoError ( t , err )
2022-08-16 05:22:25 +03:00
issueModified := unittest . AssertExistsAndLoadBean ( t , & issues_model . Issue { ID : 1 } )
2022-01-03 18:35:01 +03:00
assert . EqualValues ( t , issue . NumComments + 1 , issueModified . NumComments )
2022-06-13 12:37:59 +03:00
unittest . CheckConsistencyFor ( t , & issues_model . Issue { } )
2022-01-03 18:35:01 +03:00
}
func TestMigrate_InsertPullRequests ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
reponame := "repo1"
2022-08-16 05:22:25 +03:00
repo := unittest . AssertExistsAndLoadBean ( t , & repo_model . Repository { Name : reponame } )
owner := unittest . AssertExistsAndLoadBean ( t , & user_model . User { ID : repo . OwnerID } )
2022-01-03 18:35:01 +03:00
2022-06-13 12:37:59 +03:00
i := & issues_model . Issue {
2022-01-03 18:35:01 +03:00
RepoID : repo . ID ,
Repo : repo ,
Title : "title1" ,
Content : "issuecontent1" ,
IsPull : true ,
PosterID : owner . ID ,
Poster : owner ,
}
2022-06-13 12:37:59 +03:00
p := & issues_model . PullRequest {
2022-01-03 18:35:01 +03:00
Issue : i ,
}
err := InsertPullRequests ( p )
assert . NoError ( t , err )
2022-08-16 05:22:25 +03:00
_ = unittest . AssertExistsAndLoadBean ( t , & issues_model . PullRequest { IssueID : i . ID } )
2022-01-03 18:35:01 +03:00
2022-06-13 12:37:59 +03:00
unittest . CheckConsistencyFor ( t , & issues_model . Issue { } , & issues_model . PullRequest { } )
2022-01-03 18:35:01 +03:00
}
func TestMigrate_InsertReleases ( t * testing . T ) {
assert . NoError ( t , unittest . PrepareTestDatabase ( ) )
a := & repo_model . Attachment {
UUID : "a0eebc91-9c0c-4ef7-bb6e-6bb9bd380a12" ,
}
2022-08-25 05:31:57 +03:00
r := & repo_model . Release {
2022-01-03 18:35:01 +03:00
Attachments : [ ] * repo_model . Attachment { a } ,
}
err := InsertReleases ( r )
assert . NoError ( t , err )
}