Passed
Pull Request — master (#1470)
by Tolga
02:39
created

pkg/database/pagination_test.go   A

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 80
dl 0
loc 139
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B database.TestNewPagination 0 34 5
A database.TestPagination 0 17 4
A database.TestNoopContinuousToken 0 14 1
B database.TestNewCursorPagination 0 34 5
A database.TestCursorPagination 0 17 4
1
package database
2
3
import (
4
	"testing"
5
6
	"github.com/stretchr/testify/assert"
7
)
8
9
func TestNewPagination(t *testing.T) {
10
	tests := []struct {
11
		name      string
12
		opts      []PaginationOption
13
		wantSize  uint32
14
		wantToken string
15
	}{
16
		{
17
			name:      "Default size",
18
			opts:      []PaginationOption{},
19
			wantSize:  0,
20
			wantToken: "",
21
		},
22
		{
23
			name: "Custom size and token",
24
			opts: []PaginationOption{
25
				Size(50),
26
				Token("abc123"),
27
			},
28
			wantSize:  50,
29
			wantToken: "abc123",
30
		},
31
	}
32
33
	for _, tt := range tests {
34
		t.Run(tt.name, func(t *testing.T) {
35
			p := NewPagination(tt.opts...)
36
37
			if p.size != tt.wantSize {
38
				t.Errorf("NewPagination() size = %d, want %d", p.size, tt.wantSize)
39
			}
40
41
			if p.token != tt.wantToken {
42
				t.Errorf("NewPagination() token = %s, want %s", p.token, tt.wantToken)
43
			}
44
		})
45
	}
46
}
47
48
func TestPagination(t *testing.T) {
49
	// Test default page size
50
	p := NewPagination()
51
	if p.PageSize() != 0 {
52
		t.Errorf("Expected default page size of %d, but got %d", 0, p.PageSize())
53
	}
54
55
	// Test custom page size
56
	p = NewPagination(Size(25))
57
	if p.PageSize() != 25 {
58
		t.Errorf("Expected page size of 25, but got %d", p.PageSize())
59
	}
60
61
	// Test token
62
	p = NewPagination(Token("my-token"))
63
	if p.Token() != "my-token" {
64
		t.Errorf("Expected token of 'my-token', but got '%s'", p.Token())
65
	}
66
}
67
68
func TestNoopContinuousToken(t *testing.T) {
69
	token := NewNoopContinuousToken()
70
71
	// Test Encode
72
	encodedToken := token.Encode()
73
	assert.Empty(t, encodedToken.String())
74
75
	// Test Decode
76
	decodedToken, err := encodedToken.Decode()
77
	assert.NoError(t, err)
78
	assert.Empty(t, decodedToken.(NoopContinuousToken).Value)
79
80
	// Test Encode and Decode
81
	assert.Empty(t, decodedToken.(NoopContinuousToken).Value)
82
}
83
84
func TestNewCursorPagination(t *testing.T) {
85
	tests := []struct {
86
		name       string
87
		opts       []CursorPaginationOption
88
		wantSort   string
89
		wantCursor string
90
	}{
91
		{
92
			name:       "Default size",
93
			opts:       []CursorPaginationOption{},
94
			wantSort:   "",
95
			wantCursor: "",
96
		},
97
		{
98
			name: "Custom size and token",
99
			opts: []CursorPaginationOption{
100
				Sort("entity_id"),
101
				Cursor("abc123"),
102
			},
103
			wantSort:   "entity_id",
104
			wantCursor: "abc123",
105
		},
106
	}
107
108
	for _, tt := range tests {
109
		t.Run(tt.name, func(t *testing.T) {
110
			p := NewCursorPagination(tt.opts...)
111
112
			if p.sort != tt.wantSort {
113
				t.Errorf("NewCursorPagination() size = %s, want %s", p.sort, tt.wantSort)
114
			}
115
116
			if p.cursor != tt.wantCursor {
117
				t.Errorf("NewCursorPagination() cursor = %s, want %s", p.cursor, tt.wantCursor)
118
			}
119
		})
120
	}
121
}
122
123
func TestCursorPagination(t *testing.T) {
124
	// Test default page size
125
	p := NewCursorPagination()
126
	if p.Sort() != "" {
127
		t.Errorf("Expected default sort empty string, but got %s", p.Sort())
128
	}
129
130
	// Test custom page size
131
	p = NewCursorPagination(Sort("entity_id"))
132
	if p.Sort() != "entity_id" {
133
		t.Errorf("Expected sort entity_id, but got %s", p.Sort())
134
	}
135
136
	// Test token
137
	p = NewCursorPagination(Cursor("my-cursor"))
138
	if p.Cursor() != "my-cursor" {
139
		t.Errorf("Expected token of 'my-cursor', but got '%s'", p.Cursor())
140
	}
141
}
142