aboutsummaryrefslogtreecommitdiffstats
path: root/models/db/paginator
diff options
context:
space:
mode:
authorwxiaoguang <wxiaoguang@gmail.com>2021-11-12 22:36:47 +0800
committerGitHub <noreply@github.com>2021-11-12 22:36:47 +0800
commitdf64fa486555de6f403a795fd16c2e9e1d59e535 (patch)
treeb899e9b9e5d57409b1bf0e3afbd606b6a3900235 /models/db/paginator
parent7f802631c54d2e91301158380b273b872d62bd80 (diff)
downloadgitea-df64fa486555de6f403a795fd16c2e9e1d59e535.tar.gz
gitea-df64fa486555de6f403a795fd16c2e9e1d59e535.zip
Decouple unit test code from business code (#17623)
Diffstat (limited to 'models/db/paginator')
-rw-r--r--models/db/paginator/main_test.go16
-rw-r--r--models/db/paginator/paginator.go8
-rw-r--r--models/db/paginator/paginator_test.go63
3 files changed, 87 insertions, 0 deletions
diff --git a/models/db/paginator/main_test.go b/models/db/paginator/main_test.go
new file mode 100644
index 0000000000..601ed89710
--- /dev/null
+++ b/models/db/paginator/main_test.go
@@ -0,0 +1,16 @@
+// Copyright 2021 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 paginator
+
+import (
+ "path/filepath"
+ "testing"
+
+ "code.gitea.io/gitea/models/unittest"
+)
+
+func TestMain(m *testing.M) {
+ unittest.MainTest(m, filepath.Join("..", "..", ".."))
+}
diff --git a/models/db/paginator/paginator.go b/models/db/paginator/paginator.go
new file mode 100644
index 0000000000..747539f30e
--- /dev/null
+++ b/models/db/paginator/paginator.go
@@ -0,0 +1,8 @@
+// Copyright 2021 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 paginator
+
+// dummy only. in the future, the models/db/list_options.go should be moved here to decouple from db package
+// otherwise the unit test will cause cycle import
diff --git a/models/db/paginator/paginator_test.go b/models/db/paginator/paginator_test.go
new file mode 100644
index 0000000000..fdb8eee441
--- /dev/null
+++ b/models/db/paginator/paginator_test.go
@@ -0,0 +1,63 @@
+// Copyright 2021 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 paginator
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/models/db"
+ "code.gitea.io/gitea/modules/setting"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestPaginator(t *testing.T) {
+ cases := []struct {
+ db.Paginator
+ Skip int
+ Take int
+ Start int
+ End int
+ }{
+ {
+ Paginator: &db.ListOptions{Page: -1, PageSize: -1},
+ Skip: 0,
+ Take: setting.API.DefaultPagingNum,
+ Start: 0,
+ End: setting.API.DefaultPagingNum,
+ },
+ {
+ Paginator: &db.ListOptions{Page: 2, PageSize: 10},
+ Skip: 10,
+ Take: 10,
+ Start: 10,
+ End: 20,
+ },
+ {
+ Paginator: db.NewAbsoluteListOptions(-1, -1),
+ Skip: 0,
+ Take: setting.API.DefaultPagingNum,
+ Start: 0,
+ End: setting.API.DefaultPagingNum,
+ },
+ {
+ Paginator: db.NewAbsoluteListOptions(2, 10),
+ Skip: 2,
+ Take: 10,
+ Start: 2,
+ End: 12,
+ },
+ }
+
+ for _, c := range cases {
+ skip, take := c.Paginator.GetSkipTake()
+ start, end := c.Paginator.GetStartEnd()
+
+ assert.Equal(t, c.Skip, skip)
+ assert.Equal(t, c.Take, take)
+ assert.Equal(t, c.Start, start)
+ assert.Equal(t, c.End, end)
+ }
+}