Browse Source

Unit tests for models/admin

Ethan Koenig 3 years ago
parent
commit
b316b2e740
3 changed files with 126 additions and 1 deletions
  1. 1 1
      models/admin.go
  2. 111 0
      models/admin_test.go
  3. 14 0
      models/fixtures/notice.yml

+ 1 - 1
models/admin.go

@@ -103,7 +103,7 @@ func CountNotices() int64 {
103 103
 	return count
104 104
 }
105 105
 
106
-// Notices returns number of notices in given page.
106
+// Notices returns notices in given page.
107 107
 func Notices(page, pageSize int) ([]*Notice, error) {
108 108
 	notices := make([]*Notice, 0, pageSize)
109 109
 	return notices, x.

+ 111 - 0
models/admin_test.go

@@ -0,0 +1,111 @@
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 TestNotice_TrStr(t *testing.T) {
14
+	notice := &Notice{
15
+		Type:        NoticeRepository,
16
+		Description: "test description",
17
+	}
18
+	assert.Equal(t, "admin.notices.type_1", notice.TrStr())
19
+}
20
+
21
+func TestCreateNotice(t *testing.T) {
22
+	assert.NoError(t, PrepareTestDatabase())
23
+
24
+	noticeBean := &Notice{
25
+		Type:        NoticeRepository,
26
+		Description: "test description",
27
+	}
28
+	AssertNotExistsBean(t, noticeBean)
29
+	assert.NoError(t, CreateNotice(noticeBean.Type, noticeBean.Description))
30
+	AssertExistsAndLoadBean(t, noticeBean)
31
+}
32
+
33
+func TestCreateRepositoryNotice(t *testing.T) {
34
+	assert.NoError(t, PrepareTestDatabase())
35
+
36
+	noticeBean := &Notice{
37
+		Type:        NoticeRepository,
38
+		Description: "test description",
39
+	}
40
+	AssertNotExistsBean(t, noticeBean)
41
+	assert.NoError(t, CreateRepositoryNotice(noticeBean.Description))
42
+	AssertExistsAndLoadBean(t, noticeBean)
43
+}
44
+
45
+// TODO TestRemoveAllWithNotice
46
+
47
+func TestCountNotices(t *testing.T) {
48
+	assert.NoError(t, PrepareTestDatabase())
49
+	assert.Equal(t, int64(3), CountNotices())
50
+}
51
+
52
+func TestNotices(t *testing.T) {
53
+	assert.NoError(t, PrepareTestDatabase())
54
+
55
+	notices, err := Notices(1, 2)
56
+	assert.NoError(t, err)
57
+	assert.Len(t, notices, 2)
58
+	assert.Equal(t, int64(3), notices[0].ID)
59
+	assert.Equal(t, int64(2), notices[1].ID)
60
+
61
+	notices, err = Notices(2, 2)
62
+	assert.NoError(t, err)
63
+	assert.Len(t, notices, 1)
64
+	assert.Equal(t, int64(1), notices[0].ID)
65
+}
66
+
67
+func TestDeleteNotice(t *testing.T) {
68
+	assert.NoError(t, PrepareTestDatabase())
69
+
70
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
71
+	assert.NoError(t, DeleteNotice(3))
72
+	AssertNotExistsBean(t, &Notice{ID: 3})
73
+}
74
+
75
+func TestDeleteNotices(t *testing.T) {
76
+	// delete a non-empty range
77
+	assert.NoError(t, PrepareTestDatabase())
78
+
79
+	AssertExistsAndLoadBean(t, &Notice{ID: 1})
80
+	AssertExistsAndLoadBean(t, &Notice{ID: 2})
81
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
82
+	assert.NoError(t, DeleteNotices(1, 2))
83
+	AssertNotExistsBean(t, &Notice{ID: 1})
84
+	AssertNotExistsBean(t, &Notice{ID: 2})
85
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
86
+}
87
+
88
+func TestDeleteNotices2(t *testing.T) {
89
+	// delete an empty range
90
+	assert.NoError(t, PrepareTestDatabase())
91
+
92
+	AssertExistsAndLoadBean(t, &Notice{ID: 1})
93
+	AssertExistsAndLoadBean(t, &Notice{ID: 2})
94
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
95
+	assert.NoError(t, DeleteNotices(3, 2))
96
+	AssertExistsAndLoadBean(t, &Notice{ID: 1})
97
+	AssertExistsAndLoadBean(t, &Notice{ID: 2})
98
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
99
+}
100
+
101
+func TestDeleteNoticesByIDs(t *testing.T) {
102
+	assert.NoError(t, PrepareTestDatabase())
103
+
104
+	AssertExistsAndLoadBean(t, &Notice{ID: 1})
105
+	AssertExistsAndLoadBean(t, &Notice{ID: 2})
106
+	AssertExistsAndLoadBean(t, &Notice{ID: 3})
107
+	assert.NoError(t, DeleteNoticesByIDs([]int64{1, 3}))
108
+	AssertNotExistsBean(t, &Notice{ID: 1})
109
+	AssertExistsAndLoadBean(t, &Notice{ID: 2})
110
+	AssertNotExistsBean(t, &Notice{ID: 3})
111
+}

+ 14 - 0
models/fixtures/notice.yml

@@ -0,0 +1,14 @@
1
+-
2
+  id: 1
3
+  type: 1 # NoticeRepository
4
+  description: description1
5
+
6
+-
7
+  id: 2
8
+  type: 1 # NoticeRepository
9
+  description: description2
10
+
11
+-
12
+  id: 3
13
+  type: 1 # NoticeRepository
14
+  description: description3