fleet/server/datastore/inmem/inmem_test.go
John Murphy 6a825c11e3 Datastore refactor (#439)
Removed Gorm, replaced it with Sqlx

* Added SQL bundling command to Makfile

* Using go-kit logger

* Added soft delete capability

* Changed SearchLabel to accept a variadic param for optional omit list
instead of array

* Gorm removed

* Refactor table structures to use CURRENT_TIMESTAMP mysql function

* Moved Inmem datastore into it's own package

* Updated README

* Implemented code review suggestions from @zwass

* Removed reference to Gorm from glide.yaml
2016-11-16 21:47:49 +08:00

69 lines
1.9 KiB
Go

package inmem
import (
"testing"
"github.com/kolide/kolide-ose/server/kolide"
"github.com/stretchr/testify/assert"
)
func TestApplyLimitOffset(t *testing.T) {
im := Datastore{}
data := []int{}
// should work with empty
low, high := im.getLimitOffsetSliceBounds(kolide.ListOptions{}, len(data))
result := data[low:high]
assert.Len(t, result, 0)
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{Page: 1, PerPage: 20}, len(data))
result = data[low:high]
assert.Len(t, result, 0)
// insert some data
for i := 0; i < 100; i++ {
data = append(data, i)
}
// unlimited
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{}, len(data))
result = data[low:high]
assert.Len(t, result, 100)
assert.Equal(t, data, result)
// reasonable limit page 0
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{PerPage: 20}, len(data))
result = data[low:high]
assert.Len(t, result, 20)
assert.Equal(t, data[:20], result)
// too many per page
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{PerPage: 200}, len(data))
result = data[low:high]
assert.Len(t, result, 100)
assert.Equal(t, data, result)
// offset should be past end (zero results)
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{Page: 1, PerPage: 200}, len(data))
result = data[low:high]
assert.Len(t, result, 0)
// all pages appended should equal the original data
result = []int{}
for i := 0; i < 5; i++ { // 5 used intentionally
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{Page: uint(i), PerPage: 25}, len(data))
result = append(result, data[low:high]...)
}
assert.Len(t, result, 100)
assert.Equal(t, data, result)
// again with different params
result = []int{}
for i := 0; i < 100; i++ { // 5 used intentionally
low, high = im.getLimitOffsetSliceBounds(kolide.ListOptions{Page: uint(i), PerPage: 1}, len(data))
result = append(result, data[low:high]...)
}
assert.Len(t, result, 100)
assert.Equal(t, data, result)
}