1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-02-02 15:09:33 -05:00

update test

This commit is contained in:
Lunny Xiao 2024-12-27 21:49:43 -08:00
parent 378fc6d8dc
commit 3462b2d296
No known key found for this signature in database
GPG Key ID: C3B7C91B632F738A
3 changed files with 34 additions and 31 deletions

View File

@ -50,7 +50,7 @@ func getCreateFileOptions() api.CreateFileOptions {
} }
} }
func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string) *api.FileResponse { func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCommitSHA string, latestCommitWhen time.Time) *api.FileResponse {
sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf" sha := "a635aa942442ddfdba07468cf9661c08fbdf0ebf"
encoding := "base64" encoding := "base64"
content := "VGhpcyBpcyBuZXcgdGV4dA==" content := "VGhpcyBpcyBuZXcgdGV4dA=="
@ -60,18 +60,19 @@ func getExpectedFileResponseForCreate(repoFullName, commitID, treePath, latestCo
downloadURL := setting.AppURL + repoFullName + "/raw/branch/master/" + treePath downloadURL := setting.AppURL + repoFullName + "/raw/branch/master/" + treePath
return &api.FileResponse{ return &api.FileResponse{
Content: &api.ContentsResponse{ Content: &api.ContentsResponse{
Name: filepath.Base(treePath), Name: filepath.Base(treePath),
Path: treePath, Path: treePath,
SHA: sha, SHA: sha,
LastCommitSHA: latestCommitSHA, LastCommitSHA: latestCommitSHA,
Size: 16, LastCommitWhen: latestCommitWhen,
Type: "file", Size: 16,
Encoding: &encoding, Type: "file",
Content: &content, Encoding: &encoding,
URL: &selfURL, Content: &content,
HTMLURL: &htmlURL, URL: &selfURL,
GitURL: &gitURL, HTMLURL: &htmlURL,
DownloadURL: &downloadURL, GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{ Links: &api.FileLinksResponse{
Self: &selfURL, Self: &selfURL,
GitURL: &gitURL, GitURL: &gitURL,
@ -170,7 +171,7 @@ func TestAPICreateFile(t *testing.T) {
gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1) gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1)
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
latestCommit, _ := gitRepo.GetCommitByPath(treePath) latestCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String()) expectedFileResponse := getExpectedFileResponseForCreate("user2/repo1", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
var fileResponse api.FileResponse var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse) DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
@ -288,7 +289,7 @@ func TestAPICreateFile(t *testing.T) {
gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), emptyRepo) gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), emptyRepo)
commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName) commitID, _ := gitRepo.GetBranchCommitID(createFileOptions.NewBranchName)
latestCommit, _ := gitRepo.GetCommitByPath(treePath) latestCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String()) expectedFileResponse := getExpectedFileResponseForCreate("user2/empty-repo", commitID, treePath, latestCommit.ID.String(), latestCommit.Committer.When)
DecodeJSON(t, resp, &fileResponse) DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)
assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA) assert.EqualValues(t, expectedFileResponse.Commit.SHA, fileResponse.Commit.SHA)

View File

@ -11,6 +11,7 @@ import (
"net/url" "net/url"
"path/filepath" "path/filepath"
"testing" "testing"
"time"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
@ -48,7 +49,7 @@ func getUpdateFileOptions() *api.UpdateFileOptions {
} }
} }
func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string) *api.FileResponse { func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string, lastCommitWhen time.Time) *api.FileResponse {
sha := "08bd14b2e2852529157324de9c226b3364e76136" sha := "08bd14b2e2852529157324de9c226b3364e76136"
encoding := "base64" encoding := "base64"
content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ=" content := "VGhpcyBpcyB1cGRhdGVkIHRleHQ="
@ -58,18 +59,19 @@ func getExpectedFileResponseForUpdate(commitID, treePath, lastCommitSHA string)
downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath downloadURL := setting.AppURL + "user2/repo1/raw/branch/master/" + treePath
return &api.FileResponse{ return &api.FileResponse{
Content: &api.ContentsResponse{ Content: &api.ContentsResponse{
Name: filepath.Base(treePath), Name: filepath.Base(treePath),
Path: treePath, Path: treePath,
SHA: sha, SHA: sha,
LastCommitSHA: lastCommitSHA, LastCommitSHA: lastCommitSHA,
Type: "file", LastCommitWhen: lastCommitWhen,
Size: 20, Type: "file",
Encoding: &encoding, Size: 20,
Content: &content, Encoding: &encoding,
URL: &selfURL, Content: &content,
HTMLURL: &htmlURL, URL: &selfURL,
GitURL: &gitURL, HTMLURL: &htmlURL,
DownloadURL: &downloadURL, GitURL: &gitURL,
DownloadURL: &downloadURL,
Links: &api.FileLinksResponse{ Links: &api.FileLinksResponse{
Self: &selfURL, Self: &selfURL,
GitURL: &gitURL, GitURL: &gitURL,
@ -138,7 +140,7 @@ func TestAPIUpdateFile(t *testing.T) {
gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1) gitRepo, _ := gitrepo.OpenRepository(stdCtx.Background(), repo1)
commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName) commitID, _ := gitRepo.GetBranchCommitID(updateFileOptions.NewBranchName)
lasCommit, _ := gitRepo.GetCommitByPath(treePath) lasCommit, _ := gitRepo.GetCommitByPath(treePath)
expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String()) expectedFileResponse := getExpectedFileResponseForUpdate(commitID, treePath, lasCommit.ID.String(), lasCommit.Committer.When)
var fileResponse api.FileResponse var fileResponse api.FileResponse
DecodeJSON(t, resp, &fileResponse) DecodeJSON(t, resp, &fileResponse)
assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content) assert.EqualValues(t, expectedFileResponse.Content, fileResponse.Content)

View File

@ -100,8 +100,8 @@ func TestAPIChangeFiles(t *testing.T) {
commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName) commitID, _ := gitRepo.GetBranchCommitID(changeFilesOptions.NewBranchName)
createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath) createLasCommit, _ := gitRepo.GetCommitByPath(createTreePath)
updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath) updateLastCommit, _ := gitRepo.GetCommitByPath(updateTreePath)
expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String()) expectedCreateFileResponse := getExpectedFileResponseForCreate(fmt.Sprintf("%v/%v", user2.Name, repo1.Name), commitID, createTreePath, createLasCommit.ID.String(), createLasCommit.Committer.When)
expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String()) expectedUpdateFileResponse := getExpectedFileResponseForUpdate(commitID, updateTreePath, updateLastCommit.ID.String(), updateLastCommit.Committer.When)
var filesResponse api.FilesResponse var filesResponse api.FilesResponse
DecodeJSON(t, resp, &filesResponse) DecodeJSON(t, resp, &filesResponse)