Skip to content

Commit

Permalink
Add m3ninx query proptests
Browse files Browse the repository at this point in the history
  • Loading branch information
prateek committed Sep 18, 2018
1 parent f0f878e commit 47d648f
Show file tree
Hide file tree
Showing 22 changed files with 757 additions and 145 deletions.
2 changes: 1 addition & 1 deletion glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,4 @@ testImport:
version: b433bbd6d743c1854040b39062a3916ed5f78fe8

- package: github.com/leanovate/gopter
version: f778776473e0ef7764e1434dd01a61cc1ec574b4
version: f0356731348c8fffa27bab27c37ec8be5b0662c8
Original file line number Diff line number Diff line change
Expand Up @@ -281,8 +281,7 @@ func genPropTestInputs(blockStart time.Time) gopter.Gen {
gen.IntRange(1, maxPoints),
gen.Bool(),
gen.Bool(),
).Map(func(val interface{}) propTestInput {
inputs := val.([]interface{})
).Map(func(inputs []interface{}) propTestInput {
return propTestInput{
blockSize: time.Duration(inputs[0].(int64)),
bufferPast: time.Duration(inputs[1].(int64)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,15 +454,15 @@ func genPropTestInput(
ns string,
) gopter.Gen {
return gen.SliceOfN(numDatapoints, genWrite(start, bufferPast, bufferFuture, ns)).
Map(func(val interface{}) propTestInput {
Map(func(val []generatedWrite) propTestInput {
return propTestInput{
currentTime: start,
bufferFuture: bufferFuture,
bufferPast: bufferPast,
snapshotTime: snapshotTime,
snapshotExists: snapshotExists,
commitLogExists: commitLogExists,
writes: val.([]generatedWrite),
writes: val,
}
})
}
Expand Down
3 changes: 1 addition & 2 deletions src/dbnode/storage/namespace/convert_prop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ func TestConvert(t *testing.T) {
// map generator
func genMap() gopter.Gen {
return gen.SliceOf(genMetadata()).Map(
func(values interface{}) namespace.Map {
metadatas := values.([]namespace.Metadata)
func(metadatas []namespace.Metadata) namespace.Map {
nsMap, err := namespace.NewMap(metadatas)
if err != nil {
panic(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion src/dbnode/storage/shard_race_prop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func propTestDatabaseShard(t *testing.T, tickBatchSize int) (*dbShard, Options)

func anyIDs() gopter.Gen {
return gen.IntRange(0, 20).
Map(func(n int) interface{} {
Map(func(n int) []ident.ID {
ids := make([]ident.ID, 0, n)
for i := 0; i < n; i++ {
ids = append(ids, ident.StringID(fmt.Sprintf("foo.%d", i)))
Expand Down
190 changes: 190 additions & 0 deletions src/m3ninx/index/regexp_prop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// +build big
//
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package index

import (
"fmt"
"math/rand"
"os"
"regexp"
"regexp/syntax"
"strings"
"testing"
"time"
"unicode"

"github.com/leanovate/gopter"
"github.com/leanovate/gopter/gen"
"github.com/leanovate/gopter/prop"
"github.com/stretchr/testify/require"
)

func TestRegexpCompilationProperty(t *testing.T) {
parameters := gopter.DefaultTestParameters()
seed := time.Now().UnixNano()
parameters.MinSuccessfulTests = 100000
parameters.MaxSize = 40
parameters.Rng = rand.New(rand.NewSource(seed))
properties := gopter.NewProperties(parameters)

properties.Property("Regexp matches same strings after modifications", prop.ForAll(
func(x *inputCase) (bool, error) {
compiled := compileRegexp(x.re, t)

re, err := regexp.Compile(x.re)
if err != nil {
return false, fmt.Errorf("unable to compile re [%v]: %v", x.re, err)
}
input := []byte(x.str)

originalMatch := re.Match(input)
compiledMatch := compiled.Match(input)
if originalMatch != compiledMatch {
return false, fmt.Errorf("don't match %v %v %+v", originalMatch, compiled, x)
}

return true, nil
},
genInputCase(),
))

reporter := gopter.NewFormatedReporter(true, 160, os.Stdout)
if !properties.Run(reporter) {
t.Errorf("failed with initial seed: %d", seed)
}
}

func compileRegexp(x string, t *testing.T) *regexp.Regexp {
ast, err := parseRegexp(x)
require.NoError(t, err)
astp, err := ensureRegexpUnanchored(ast)
require.NoError(t, err)
ast2p, err := ensureRegexpAnchored(astp)
require.NoError(t, err)
re, err := regexp.Compile(ast2p.String())
require.NoError(t, err)
return re
}

func genInputCase() gopter.Gen {
return genRegexp(unicode.ASCII_Hex_Digit).Map(
func(reString string, params *gopter.GenParameters) *inputCase {
val := gen.RegexMatch(reString)(params)
strAny, ok := val.Retrieve()
if !ok {
return nil
}
return &inputCase{
re: reString,
str: strAny.(string),
}
}).SuchThat(func(ic *inputCase) bool {
return ic != nil
})
}

type inputCase struct {
re string
str string
}

const regexpFlags = syntax.Perl

func genRegexp(language *unicode.RangeTable) gopter.Gen {
return genRegexpAst(language).Map(func(r *syntax.Regexp) string {
return strings.Replace(r.Simplify().String(), "\\", "\\\\", -1)
})
}

func genRegexpAst(language *unicode.RangeTable) gopter.Gen {
return gen.OneGenOf(
genRegexpNoOperands(language),
genRegexpLiteral(language),
genRegexpSingleOperands(language),
genRegexpAnyOperands(language),
)
}

func genRegexpNoOperands(l *unicode.RangeTable) gopter.Gen {
return gen.OneConstOf(
syntax.OpEmptyMatch,
syntax.OpAnyChar,
syntax.OpBeginText,
syntax.OpEndText,
syntax.OpWordBoundary,
syntax.OpNoWordBoundary,
).Map(func(op syntax.Op) *syntax.Regexp {
return &syntax.Regexp{
Op: op,
Flags: regexpFlags,
}
})
}

func genRegexpLiteral(language *unicode.RangeTable) gopter.Gen {
return gen.UnicodeString(language).Map(func(s string) *syntax.Regexp {
return &syntax.Regexp{
Op: syntax.OpLiteral,
Flags: regexpFlags,
Rune: []rune(s),
}
})
}

func genRegexpSingleOperands(l *unicode.RangeTable) gopter.Gen {
return gopter.CombineGens(
gen.OneGenOf(genRegexpLiteral(l), genRegexpNoOperands(l)),
gen.OneConstOf(
syntax.OpCapture,
syntax.OpStar,
syntax.OpPlus,
syntax.OpQuest,
syntax.OpRepeat),
).Map(func(vals []interface{}) *syntax.Regexp {
return &syntax.Regexp{
Op: vals[1].(syntax.Op),
Flags: regexpFlags,
Sub: []*syntax.Regexp{vals[0].(*syntax.Regexp)},
}
})
}

func genRegexpAnyOperands(l *unicode.RangeTable) gopter.Gen {
return gopter.CombineGens(
gen.SliceOf(
gen.OneGenOf(
genRegexpLiteral(l),
genRegexpNoOperands(l),
genRegexpSingleOperands(l),
)),
gen.OneConstOf(
syntax.OpConcat,
syntax.OpAlternate),
).Map(func(vals []interface{}) *syntax.Regexp {
return &syntax.Regexp{
Op: vals[1].(syntax.Op),
Flags: regexpFlags,
Sub: vals[0].([]*syntax.Regexp),
}
})
}
4 changes: 2 additions & 2 deletions src/m3ninx/index/segment/fst/encoding/docs/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"testing"

"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/util"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -76,7 +76,7 @@ func TestStoredFieldsData(t *testing.T) {
},
{
name: "node exporter metrics",
docs: util.MustReadDocs("../../../../util/testdata/node_exporter.json", 2000),
docs: util.MustReadDocs("../../../../../util/testdata/node_exporter.json", 2000),
},
}

Expand Down
4 changes: 2 additions & 2 deletions src/m3ninx/index/segment/fst/writer_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import (
"github.com/m3db/m3/src/m3ninx/index"
sgmt "github.com/m3db/m3/src/m3ninx/index/segment"
"github.com/m3db/m3/src/m3ninx/index/segment/mem"
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/util"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -79,7 +79,7 @@ var (
},
},
}
lotsTestDocuments = util.MustReadDocs("../../util/testdata/node_exporter.json", 2000)
lotsTestDocuments = util.MustReadDocs("../../../util/testdata/node_exporter.json", 2000)

testDocuments = []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion src/m3ninx/index/segment/mem/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package mem

import (
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/postings/roaring"
"github.com/m3db/m3/src/m3ninx/util"
"github.com/m3db/m3/src/m3ninx/x/bytes"

"github.com/m3db/m3x/instrument"
Expand Down
2 changes: 1 addition & 1 deletion src/m3ninx/index/segment/mem/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import (
"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/m3ninx/index"
sgmt "github.com/m3db/m3/src/m3ninx/index/segment"
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/util"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions src/m3ninx/index/segment/mem/segment_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"testing"

"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/util"
)

var (
Expand Down Expand Up @@ -53,7 +53,7 @@ func BenchmarkSegment(b *testing.B) {
},
}

docs, err := util.ReadDocs("../../util/testdata/node_exporter.json", 2000)
docs, err := util.ReadDocs("../../../util/testdata/node_exporter.json", 2000)
if err != nil {
b.Fatalf("unable to read documents for benchmarks: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions src/m3ninx/index/segment/mem/terms_dict_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import (
"testing"

"github.com/m3db/m3/src/m3ninx/doc"
"github.com/m3db/m3/src/m3ninx/index/util"
"github.com/m3db/m3/src/m3ninx/postings"
"github.com/m3db/m3/src/m3ninx/util"
)

var (
Expand Down Expand Up @@ -54,7 +54,7 @@ func BenchmarkTermsDict(b *testing.B) {
},
}

docs, err := util.ReadDocs("../../util/testdata/node_exporter.json", 2000)
docs, err := util.ReadDocs("../../../util/testdata/node_exporter.json", 2000)
if err != nil {
b.Fatalf("unable to read documents for benchmarks: %v", err)
}
Expand Down
Loading

0 comments on commit 47d648f

Please sign in to comment.