Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit e232ba1

Browse files
committed
test: add tests for count function
1 parent 3100f49 commit e232ba1

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed

database/memory/count_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package memory
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCount(t *testing.T) {
8+
task1 := newTask("task_with_filter", false)
9+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
14+
task2 := newTask("task_with_filter", false)
15+
_, err = datastore.CreateDocument(adminAuth, confDBName, colName, task2)
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
20+
var clauses [][]interface{}
21+
22+
clauses = append(clauses, []interface{}{"title", "=", "task_with_filter"})
23+
24+
filters, err := datastore.ParseQuery(clauses)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
29+
count, err := datastore.Count(adminAuth, confDBName, colName, filters)
30+
if err != nil {
31+
t.Fatal(err)
32+
}
33+
34+
if count != 2 {
35+
t.Fatalf("expected 2 got %v", count)
36+
}
37+
}
38+
39+
func TestCountWIthNoFilters(t *testing.T) {
40+
task1 := newTask("tasknof", false)
41+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
46+
count, err := datastore.Count(adminAuth, confDBName, colName, nil)
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
51+
// Here we expect this count because after running all tests total count of documents will be 16 + 3
52+
if count != 19 {
53+
t.Fatalf("expected 19 got %v", count)
54+
}
55+
}

database/mongo/count_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package mongo
2+
3+
import "testing"
4+
5+
func TestCount(t *testing.T) {
6+
task1 := newTask("task_with_filter", false)
7+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
8+
if err != nil {
9+
t.Fatal(err)
10+
}
11+
12+
task2 := newTask("task_with_filter", false)
13+
_, err = datastore.CreateDocument(adminAuth, confDBName, colName, task2)
14+
if err != nil {
15+
t.Fatal(err)
16+
}
17+
18+
var clauses [][]interface{}
19+
20+
clauses = append(clauses, []interface{}{"title", "=", "task_with_filter"})
21+
22+
filters, err := datastore.ParseQuery(clauses)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
27+
count, err := datastore.Count(adminAuth, confDBName, colName, filters)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
32+
if count != 2 {
33+
t.Fatalf("expected 2 got %v", count)
34+
}
35+
}
36+
37+
func TestCountWithNoFilters(t *testing.T) {
38+
task1 := newTask("should be in list", false)
39+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
44+
count, err := datastore.Count(adminAuth, confDBName, colName, nil)
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
// Here we expect this count because after running all tests total count of documents will be 16 + 3
50+
if count != 19 {
51+
t.Fatalf("expected 19 got %v", count)
52+
}
53+
}

database/postgresql/count_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package postgresql
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCount(t *testing.T) {
8+
task1 := newTask("task_with_filter", false)
9+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
14+
task2 := newTask("task_with_filter", false)
15+
_, err = datastore.CreateDocument(adminAuth, confDBName, colName, task2)
16+
if err != nil {
17+
t.Fatal(err)
18+
}
19+
20+
var clauses [][]interface{}
21+
22+
clauses = append(clauses, []interface{}{"title", "=", "task_with_filter"})
23+
24+
filters, err := datastore.ParseQuery(clauses)
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
count, err := datastore.Count(adminAuth, confDBName, colName, filters)
29+
if err != nil {
30+
t.Fatal(err)
31+
}
32+
33+
if count != 2 {
34+
t.Fatalf("expected 2 got %v", count)
35+
}
36+
}
37+
38+
func TestCountWithNoFilter(t *testing.T) {
39+
task1 := newTask("task1", false)
40+
_, err := datastore.CreateDocument(adminAuth, confDBName, colName, task1)
41+
if err != nil {
42+
t.Fatal(err)
43+
}
44+
45+
count, err := datastore.Count(adminAuth, confDBName, colName, nil)
46+
if err != nil {
47+
t.Fatal(err)
48+
}
49+
50+
// Here we expect this count because after running all tests total count of documents will be 16 + 3
51+
if count != 19 {
52+
t.Fatalf("expected 19 got %v", count)
53+
}
54+
}

0 commit comments

Comments
 (0)