github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/range_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package dosa
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  )
    28  
    29  var rangeTestCases = []struct {
    30  	descript  string
    31  	rop       *RangeOp
    32  	stringer  string
    33  	converted string
    34  	err       string
    35  }{
    36  	{
    37  		descript:  "empty rangeop, valid",
    38  		rop:       NewRangeOp(&AllTypes{}),
    39  		stringer:  "<empty>",
    40  		converted: "<empty>",
    41  	},
    42  	{
    43  		descript:  "single string, valid",
    44  		rop:       NewRangeOp(&AllTypes{}).Eq("StringType", "word"),
    45  		stringer:  "StringType Eq word",
    46  		converted: "stringtype Eq word",
    47  	},
    48  	{
    49  		descript: "bad field name, invalid",
    50  		rop:      NewRangeOp(&AllTypes{}).Eq("badfield", "data"),
    51  		stringer: "badfield Eq data",
    52  		err:      "badfield",
    53  	},
    54  	{
    55  		descript: "numeric in string field, invalid",
    56  		rop:      NewRangeOp(&AllTypes{}).Gt("StringType", 1),
    57  		stringer: "StringType Gt 1",
    58  		err:      "invalid value for string",
    59  	},
    60  	{
    61  		descript:  "two conditions, valid",
    62  		rop:       NewRangeOp(&AllTypes{}).GtOrEq("Int32Type", int32(5)).LtOrEq("Int32Type", int32(10)),
    63  		stringer:  "Int32Type GtOrEq 5, Int32Type LtOrEq 10",
    64  		converted: "int32type GtOrEq 5, int32type LtOrEq 10",
    65  	},
    66  	{
    67  		descript:  "empty with limit",
    68  		rop:       NewRangeOp(&AllTypes{}).Limit(10),
    69  		stringer:  "<empty> limit 10",
    70  		converted: "<empty> limit 10",
    71  	},
    72  	{
    73  		descript:  "empty with token",
    74  		rop:       NewRangeOp(&AllTypes{}).Offset("toketoketoke"),
    75  		stringer:  "<empty> token \"toketoketoke\"",
    76  		converted: "<empty> token \"toketoketoke\"",
    77  	},
    78  	{
    79  		descript: "error in one field",
    80  		rop:      NewRangeOp(&AllTypes{}).Lt("badfieldpropogate", "oopsie").Lt("StringType", "42").Limit(10),
    81  		stringer: "StringType Lt 42, badfieldpropogate Lt oopsie limit 10",
    82  		err:      "badfieldpropogate",
    83  	},
    84  	{
    85  		descript:  "valid, mixed types",
    86  		rop:       NewRangeOp(&AllTypes{}).Eq("StringType", "word").Eq("Int32Type", int32(-1)),
    87  		stringer:  "Int32Type Eq -1, StringType Eq word",
    88  		converted: "int32type Eq -1, stringtype Eq word",
    89  	},
    90  	{
    91  		descript:  "with valid field list",
    92  		rop:       NewRangeOp(&AllTypes{}).Fields([]string{"StringType"}),
    93  		stringer:  "<empty>",
    94  		converted: "<empty>",
    95  	},
    96  }
    97  
    98  func TestNewRangeOp(t *testing.T) {
    99  	assert.NotNil(t, NewRangeOp(&AllTypes{}))
   100  }
   101  
   102  func TestRangeOpStringer(t *testing.T) {
   103  
   104  	for _, test := range rangeTestCases {
   105  		assert.Equal(t, test.stringer, test.rop.String(), test.descript)
   106  	}
   107  }
   108  
   109  func TestRangeOpMatcher(t *testing.T) {
   110  	RangeOp0 := NewRangeOp(&AllTypes{}).Eq("StringType", "Hello")
   111  	RangeOp1 := NewRangeOp(&AllTypes{}).Eq("StringType", "Hello")
   112  	RangeOp2 := NewRangeOp(&AllTypes{}).Lt("StringType", "Hello")
   113  	RangeOp3 := NewRangeOp(&AllTypes{}).Eq("StringType", "Hello").Offset("token1")
   114  	RangeOp4 := NewRangeOp(&AllTypes{}).Eq("StringType", "Hello").Limit(5)
   115  	RangeOp5 := NewRangeOp(&AllTypes{}).Eq("StringType", "Hello").Fields([]string{"BoolType"})
   116  
   117  	matcher := EqRangeOp(RangeOp0)
   118  	assert.True(t, matcher.Matches(RangeOp1))
   119  	assert.False(t, matcher.Matches(RangeOp2))
   120  	assert.False(t, matcher.Matches(RangeOp3))
   121  	assert.False(t, matcher.Matches(RangeOp4))
   122  	assert.False(t, matcher.Matches(RangeOp5))
   123  	assert.False(t, matcher.Matches(3))
   124  }