github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/connectors/cassandra/sort_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 cassandra
    22  
    23  import (
    24  	"sort"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/uber-go/dosa"
    29  )
    30  
    31  func TestSortedField(t *testing.T) {
    32  	a := []string{"l", "z", "21", "a"}
    33  	sort.Strings(a)
    34  	assert.Equal(t, a, []string{"21", "a", "l", "z"})
    35  }
    36  
    37  func TestSortedConditions(t *testing.T) {
    38  	conds := map[string][]*dosa.Condition{
    39  		"b": {{Op: dosa.Eq, Value: 9}},
    40  		"c": {{Op: dosa.Gt, Value: 0}, {Op: dosa.Lt, Value: 1}},
    41  		"a": {
    42  			{Op: dosa.Gt, Value: 0},
    43  			{Op: dosa.LtOrEq, Value: 2},
    44  			{Op: dosa.GtOrEq, Value: 3},
    45  			{Op: dosa.Lt, Value: 5},
    46  			{Op: dosa.Eq, Value: 4},
    47  		},
    48  	}
    49  
    50  	expected := []*ColumnCondition{
    51  		{
    52  			Name:      "a",
    53  			Condition: &dosa.Condition{Op: dosa.Eq, Value: 4},
    54  		},
    55  		{
    56  			Name:      "a",
    57  			Condition: &dosa.Condition{Op: dosa.Lt, Value: 5},
    58  		},
    59  		{
    60  			Name:      "a",
    61  			Condition: &dosa.Condition{Op: dosa.LtOrEq, Value: 2},
    62  		},
    63  		{
    64  			Name:      "a",
    65  			Condition: &dosa.Condition{Op: dosa.Gt, Value: 0},
    66  		},
    67  		{
    68  			Name:      "a",
    69  			Condition: &dosa.Condition{Op: dosa.GtOrEq, Value: 3},
    70  		},
    71  		{
    72  			Name:      "b",
    73  			Condition: &dosa.Condition{Op: dosa.Eq, Value: 9},
    74  		},
    75  		{
    76  			Name:      "c",
    77  			Condition: &dosa.Condition{Op: dosa.Lt, Value: 1},
    78  		},
    79  		{
    80  			Name:      "c",
    81  			Condition: &dosa.Condition{Op: dosa.Gt, Value: 0},
    82  		},
    83  	}
    84  
    85  	cc, _, _ := prepareConditions(conds)
    86  	for i, c := range cc {
    87  		assert.Equal(t, c.Name, expected[i].Name)
    88  		assert.Equal(t, c.Condition, expected[i].Condition)
    89  	}
    90  }