diff options
author | Ethan Koenig <ethantkoenig@gmail.com> | 2016-12-28 20:03:40 -0500 |
---|---|---|
committer | Kim "BKC" Carlbäcker <kim.carlbacker@gmail.com> | 2016-12-31 10:17:45 +0100 |
commit | de8b73dd9241375d78a08bb01910d2c820b5e481 (patch) | |
tree | bfa78d8524398eee3416b1c0cb5e8aaf4bdd0daa /models/update_test.go | |
parent | c0904f1942071ce870ab9d87bd6c7f49f614ee82 (diff) | |
download | gitea-de8b73dd9241375d78a08bb01910d2c820b5e481.tar.gz gitea-de8b73dd9241375d78a08bb01910d2c820b5e481.zip |
Unit tests for token and update models
Diffstat (limited to 'models/update_test.go')
-rw-r--r-- | models/update_test.go | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/models/update_test.go b/models/update_test.go new file mode 100644 index 0000000000..141eb2fd51 --- /dev/null +++ b/models/update_test.go @@ -0,0 +1,124 @@ +// Copyright 2016 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package models + +import ( + "container/list" + "testing" + "time" + + "code.gitea.io/git" + + "github.com/stretchr/testify/assert" +) + +func TestAddUpdateTask(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + task := &UpdateTask{ + UUID: "uuid4", + RefName: "refName4", + OldCommitID: "oldCommitId4", + NewCommitID: "newCommitId4", + } + assert.NoError(t, AddUpdateTask(task)) + + sess := x.NewSession() + defer sess.Close() + has, err := sess.Get(task) + assert.NoError(t, err) + assert.True(t, has) + assert.Equal(t, "uuid4", task.UUID) +} + +func TestGetUpdateTaskByUUID(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + task, err := GetUpdateTaskByUUID("uuid1") + assert.NoError(t, err) + assert.Equal(t, "uuid1", task.UUID) + assert.Equal(t, "refName1", task.RefName) + assert.Equal(t, "oldCommitId1", task.OldCommitID) + assert.Equal(t, "newCommitId1", task.NewCommitID) +} + +func TestDeleteUpdateTaskByUUID(t *testing.T) { + assert.NoError(t, PrepareTestDatabase()) + assert.NoError(t, DeleteUpdateTaskByUUID("uuid1")) + sess := x.NewSession() + defer sess.Close() + has, err := sess.Get(&UpdateTask{UUID: "uuid1"}) + assert.NoError(t, err) + assert.False(t, has) + + assert.NoError(t, DeleteUpdateTaskByUUID("invalid")) +} + +func TestCommitToPushCommit(t *testing.T) { + now := time.Now() + sig := &git.Signature{ + Email: "example@example.com", + Name: "John Doe", + When: now, + } + const hexString = "0123456789abcdef0123456789abcdef01234567" + sha1, err := git.NewIDFromString(hexString) + assert.NoError(t, err) + pushCommit := CommitToPushCommit(&git.Commit{ + ID: sha1, + Author: sig, + Committer: sig, + CommitMessage: "Commit Message", + }) + assert.Equal(t, hexString, pushCommit.Sha1) + assert.Equal(t, "Commit Message", pushCommit.Message) + assert.Equal(t, "example@example.com", pushCommit.AuthorEmail) + assert.Equal(t, "John Doe", pushCommit.AuthorName) + assert.Equal(t, "example@example.com", pushCommit.CommitterEmail) + assert.Equal(t, "John Doe", pushCommit.CommitterName) + assert.Equal(t, now, pushCommit.Timestamp) +} + +func TestListToPushCommits(t *testing.T) { + now := time.Now() + sig := &git.Signature{ + Email: "example@example.com", + Name: "John Doe", + When: now, + } + + const hexString1 = "0123456789abcdef0123456789abcdef01234567" + hash1, err := git.NewIDFromString(hexString1) + assert.NoError(t, err) + const hexString2 = "fedcba9876543210fedcba9876543210fedcba98" + hash2, err := git.NewIDFromString(hexString2) + assert.NoError(t, err) + + l := list.New() + l.PushBack(&git.Commit{ + ID: hash1, + Author: sig, + Committer: sig, + CommitMessage: "Message1", + }) + l.PushBack(&git.Commit{ + ID: hash2, + Author: sig, + Committer: sig, + CommitMessage: "Message2", + }) + + pushCommits := ListToPushCommits(l) + assert.Equal(t, 2, pushCommits.Len) + assert.Equal(t, 2, len(pushCommits.Commits)) + + assert.Equal(t, "Message1", pushCommits.Commits[0].Message) + assert.Equal(t, hexString1, pushCommits.Commits[0].Sha1) + assert.Equal(t, "example@example.com", pushCommits.Commits[0].AuthorEmail) + assert.Equal(t, now, pushCommits.Commits[0].Timestamp) + + assert.Equal(t, "Message2", pushCommits.Commits[1].Message) + assert.Equal(t, hexString2, pushCommits.Commits[1].Sha1) + assert.Equal(t, "example@example.com", pushCommits.Commits[1].AuthorEmail) + assert.Equal(t, now, pushCommits.Commits[1].Timestamp) +} |