Skip to content

Commit

Permalink
Issue 24 (suggest-go#55)
Browse files Browse the repository at this point in the history
* Add stretchr/testify dep, rewrite alphabet_test with assert

* issue-24 pkg/analysis migrate to testify/assert

* issue-24 compression package now uses stretchr/testify/assert

* issue-24 migrate pkg/index

* issue-24 pkg/merger migrate

* issue-24 pkg/lm migrate

* issue-24 migrate all code

* issue-24 replace t.Errorf with assert
  • Loading branch information
alldroll authored Aug 6, 2020
1 parent 7a42b21 commit b1e078d
Show file tree
Hide file tree
Showing 18 changed files with 261 additions and 425 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ require (
github.com/snowballstem/snowball v2.0.0+incompatible
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.6.1
golang.org/x/sync v0.0.0-20190423024810-112230192c58
golang.org/x/sys v0.0.0-20190426135247-a129542de9ae // indirect
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tinylib/msgp v1.1.0 h1:9fQd+ICuRIu/ue4vxJZu6/LzxN0HwMds2nq/0cFvxHU=
github.com/tinylib/msgp v1.1.0/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
github.com/willf/bitset v1.1.10 h1:NotGKqX0KwQ72NUzqrjZq5ipPNDQex9lo3WpaS8L2sc=
Expand All @@ -52,3 +55,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45 changes: 22 additions & 23 deletions pkg/alphabet/alphabet_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package alphabet

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSequentialAlphabet(t *testing.T) {
alphabet := NewRussianAlphabet()

cases := []struct {
testCases := []struct {
char rune
expected bool
}{
Expand All @@ -19,25 +20,16 @@ func TestSequentialAlphabet(t *testing.T) {
{'7', false},
}

for _, c := range cases {
actual := alphabet.Has(c.char)

if c.expected != actual {
t.Errorf("Test Fail, expected %v, got %v", c.expected, actual)
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test #%d", i), func(t *testing.T) {
alphabet := NewRussianAlphabet()
assert.Equal(t, testCase.expected, alphabet.Has(testCase.char))
})
}
}

func TestCompositeAlphabet(t *testing.T) {
alphabet := NewCompositeAlphabet(
[]Alphabet{
NewRussianAlphabet(),
NewEnglishAlphabet(),
NewNumberAlphabet(),
},
)

cases := []struct {
testCases := []struct {
char rune
expected bool
}{
Expand All @@ -53,11 +45,18 @@ func TestCompositeAlphabet(t *testing.T) {
{'-', false},
}

for _, c := range cases {
actual := alphabet.Has(c.char)
if c.expected != actual {
t.Errorf("Test Fail, expected %v, got %v", c.expected, actual)
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test #%d", i), func(t *testing.T) {
alphabet := NewCompositeAlphabet(
[]Alphabet{
NewRussianAlphabet(),
NewEnglishAlphabet(),
NewNumberAlphabet(),
},
)

assert.Equal(t, testCase.expected, alphabet.Has(testCase.char))
})
}
}

Expand Down
23 changes: 10 additions & 13 deletions pkg/analysis/ngram_tokenizer_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package analysis

import (
"reflect"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTokenizeNGrams(t *testing.T) {
cases := []struct {
testCases := []struct {
word string
k int
ngrams []Token
Expand Down Expand Up @@ -43,17 +45,12 @@ func TestTokenizeNGrams(t *testing.T) {
},
}

for _, c := range cases {
tokenizer := NewNGramTokenizer(c.k)
actual := tokenizer.Tokenize(c.word)

if !reflect.DeepEqual(actual, c.ngrams) {
t.Errorf(
"Test Fail, expected %v, got %v",
c.ngrams,
actual,
)
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test #%d", i+1), func(t *testing.T) {
tokenizer := NewNGramTokenizer(testCase.k)
actual := tokenizer.Tokenize(testCase.word)
assert.Equal(t, testCase.ngrams, actual)
})
}
}

Expand Down
13 changes: 4 additions & 9 deletions pkg/analysis/stemmer_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package analysis

import (
"fmt"
"reflect"
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRussianStemmerFilter(t *testing.T) {
Expand All @@ -25,14 +26,11 @@ func TestRussianStemmerFilter(t *testing.T) {
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test #%d", i+1), func(t *testing.T) {
filter := NewRussianStemmerFilter()

actual := filter.Filter(
strings.Split(testCase.sentence, " "),
)

if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Expected %v, got %v", testCase.expected, actual)
}
assert.Equal(t, testCase.expected, actual)
})
}
}
Expand All @@ -55,14 +53,11 @@ func TestEnglishStemmerFilter(t *testing.T) {
for i, testCase := range testCases {
t.Run(fmt.Sprintf("test #%d", i+1), func(t *testing.T) {
filter := NewEnglishStemmerFilter()

actual := filter.Filter(
strings.Split(testCase.sentence, " "),
)

if !reflect.DeepEqual(actual, testCase.expected) {
t.Errorf("Expected %v, got %v", testCase.expected, actual)
}
assert.Equal(t, testCase.expected, actual)
})
}
}
15 changes: 7 additions & 8 deletions pkg/analysis/word_tokenizer_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package analysis

import (
"reflect"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/suggest-go/suggest/pkg/alphabet"
)

func TestTokenize(t *testing.T) {
cases := []struct {
testCases := []struct {
text string
expected []Token
}{
Expand All @@ -27,12 +28,10 @@ func TestTokenize(t *testing.T) {
},
))

for _, c := range cases {
actual := tokenizer.Tokenize(c.text)

if !reflect.DeepEqual(actual, c.expected) {
t.Errorf("Test fail, expected %v, got %v", c.expected, actual)
}
for i, testCase := range testCases {
t.Run(fmt.Sprintf("Test #%d", i+1), func(t *testing.T) {
assert.Equal(t, testCase.expected, tokenizer.Tokenize(testCase.text))
})
}
}

Expand Down
32 changes: 15 additions & 17 deletions pkg/compression/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package compression

import (
"bytes"
"fmt"
"math/rand"
"reflect"
"sort"
"testing"

"github.com/stretchr/testify/assert"
"github.com/suggest-go/suggest/pkg/store"
)

Expand All @@ -24,7 +25,7 @@ func TestEncodeDecode(t *testing.T) {
{"skipping", skipEnc, skipDec},
}

cases := []struct {
testCases := []struct {
p []uint32
}{
{[]uint32{824, 829, 215406}},
Expand All @@ -36,24 +37,21 @@ func TestEncodeDecode(t *testing.T) {
encoder := ins.encoder
decoder := ins.decoder

for _, c := range cases {
buf := &bytes.Buffer{}
output := store.NewBytesOutput(buf)
list := make([]uint32, len(c.p))
for i, testCase := range testCases {
t.Run(fmt.Sprintf("%s_%d", ins.name, i+1), func(t *testing.T) {
buf := &bytes.Buffer{}
output := store.NewBytesOutput(buf)
list := make([]uint32, len(testCase.p))

if _, err := encoder.Encode(c.p, output); err != nil {
t.Errorf("[%s] Unexpected error occurs: %v", ins.name, err)
}
_, err := encoder.Encode(testCase.p, output)
assert.NoError(t, err)

in := store.NewBytesInput(buf.Bytes())
in := store.NewBytesInput(buf.Bytes())
_, err = decoder.Decode(in, list)

if _, err := decoder.Decode(in, list); err != nil {
t.Errorf("[%s] Unexpected error occurs: %v", ins.name, err)
}

if !reflect.DeepEqual(list, c.p) {
t.Errorf("Fail [%s], expected posting list: %v, got: %v", ins.name, c.p, list)
}
assert.NoError(t, err)
assert.Equal(t, list, testCase.p)
})
}
}
}
Expand Down
58 changes: 21 additions & 37 deletions pkg/index/posting_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"bytes"
"fmt"
"io"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/suggest-go/suggest/pkg/compression"
"github.com/suggest-go/suggest/pkg/store"
)
Expand Down Expand Up @@ -36,7 +36,7 @@ func TestSkipping(t *testing.T) {
},
}

cases := []struct {
testCases := []struct {
name string
list []uint32
to uint32
Expand Down Expand Up @@ -90,61 +90,45 @@ func TestSkipping(t *testing.T) {
}

for _, p := range postings {
t.Run(fmt.Sprintf("Test %s posting", p.name), func(t *testing.T) {
posting := p.posting
encoder := p.encoder
posting := p.posting
encoder := p.encoder

for _, c := range cases {
for _, testCase := range testCases {
t.Run(fmt.Sprintf("Test %s posting %s", testCase.name, p.name), func(t *testing.T) {
buf := &bytes.Buffer{}
out := store.NewBytesOutput(buf)

if _, err := encoder.Encode(c.list, out); err != nil {
t.Errorf("Unexpected error occurs: %v", err)
}
_, err := encoder.Encode(testCase.list, out)
assert.NoError(t, err)

err := posting.Init(PostingListContext{
ListSize: len(c.list),
err = posting.Init(PostingListContext{
ListSize: len(testCase.list),
Reader: store.NewBytesInput(buf.Bytes()),
})

if err != nil {
t.Errorf("Unexpected error occurs: %v", err)
}
assert.NoError(t, err)

actual := []uint32{}
v, err := posting.LowerBound(c.to)

if v != c.lowerBound {
t.Errorf("Test %s fail, expected %v, got %v", c.name, c.lowerBound, v)
}

if err != nil && !c.expectedError {
t.Errorf("Unexpected error: %v", err)
}
v, err := posting.LowerBound(testCase.to)
assert.Equal(t, testCase.expectedError, err != nil)
assert.Equal(t, testCase.lowerBound, v)

for !c.expectedError {
for !testCase.expectedError {
v, err := posting.Get()

if err != nil {
t.Errorf("Unexpected error: %v", err)
}
assert.NoError(t, err)

actual = append(actual, v)

if !posting.HasNext() {
break
}

if _, err = posting.Next(); err != nil {
t.Errorf("Unexpected error: %v", err)
}
_, err = posting.Next()
assert.NoError(t, err)
}

if !reflect.DeepEqual(c.tail, actual) {
t.Errorf("Test %s fail, expected posting list: %v, got: %v", c.name, c.tail, actual)
}
}
})
assert.Equal(t, testCase.tail, actual)
})
}
}
}

Expand Down
Loading

0 comments on commit b1e078d

Please sign in to comment.