Skip to content

Commit a6832c2

Browse files
ethantkoeniglunny
authored andcommitted
Unit tests for models/star (#752)
1 parent 8c23811 commit a6832c2

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

models/fixtures/star.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-
2+
id: 1
3+
uid: 2
4+
repo_id: 2
5+
6+
-
7+
id: 2
8+
uid: 2
9+
repo_id: 4

models/star_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2017 The Gitea Authors. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
package models
6+
7+
import (
8+
"testing"
9+
10+
"github.com/stretchr/testify/assert"
11+
)
12+
13+
func TestStarRepo(t *testing.T) {
14+
assert.NoError(t, PrepareTestDatabase())
15+
const userID = 2
16+
const repoID = 1
17+
AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID})
18+
assert.NoError(t, StarRepo(userID, repoID, true))
19+
AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID})
20+
assert.NoError(t, StarRepo(userID, repoID, true))
21+
AssertExistsAndLoadBean(t, &Star{UID: userID, RepoID: repoID})
22+
assert.NoError(t, StarRepo(userID, repoID, false))
23+
AssertNotExistsBean(t, &Star{UID: userID, RepoID: repoID})
24+
}
25+
26+
func TestIsStaring(t *testing.T) {
27+
assert.NoError(t, PrepareTestDatabase())
28+
assert.True(t, IsStaring(2, 4))
29+
assert.False(t, IsStaring(3, 4))
30+
}
31+
32+
func TestRepository_GetStargazers(t *testing.T) {
33+
// repo with stargazers
34+
assert.NoError(t, PrepareTestDatabase())
35+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 4}).(*Repository)
36+
gazers, err := repo.GetStargazers(0)
37+
assert.NoError(t, err)
38+
assert.Len(t, gazers, 1)
39+
assert.Equal(t, int64(2), gazers[0].ID)
40+
}
41+
42+
func TestRepository_GetStargazers2(t *testing.T) {
43+
// repo with stargazers
44+
assert.NoError(t, PrepareTestDatabase())
45+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 3}).(*Repository)
46+
gazers, err := repo.GetStargazers(0)
47+
assert.NoError(t, err)
48+
assert.Len(t, gazers, 0)
49+
}
50+
51+
func TestUser_GetStarredRepos(t *testing.T) {
52+
// user who has starred repos
53+
assert.NoError(t, PrepareTestDatabase())
54+
55+
user := AssertExistsAndLoadBean(t, &User{ID: 2}).(*User)
56+
starred, err := user.GetStarredRepos(false)
57+
assert.NoError(t, err)
58+
assert.Len(t, starred, 1)
59+
assert.Equal(t, int64(4), starred[0].ID)
60+
61+
starred, err = user.GetStarredRepos(true)
62+
assert.NoError(t, err)
63+
assert.Len(t, starred, 2)
64+
assert.Equal(t, int64(2), starred[0].ID)
65+
assert.Equal(t, int64(4), starred[1].ID)
66+
}
67+
68+
func TestUser_GetStarredRepos2(t *testing.T) {
69+
// user who has no starred repos
70+
assert.NoError(t, PrepareTestDatabase())
71+
72+
user := AssertExistsAndLoadBean(t, &User{ID: 1}).(*User)
73+
starred, err := user.GetStarredRepos(false)
74+
assert.NoError(t, err)
75+
assert.Len(t, starred, 0)
76+
77+
starred, err = user.GetStarredRepos(true)
78+
assert.NoError(t, err)
79+
assert.Len(t, starred, 0)
80+
}

0 commit comments

Comments
 (0)