github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/testutil/compare_test.go (about)

     1  package testutil
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     8  	"google.golang.org/protobuf/types/known/structpb"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
    13  )
    14  
    15  func TestUnwrapOptionalValue(t *testing.T) {
    16  	a := allocator.New()
    17  	defer a.Free()
    18  	v := value.OptionalValue(value.OptionalValue(value.TextValue("a")))
    19  	val := unwrapTypedValue(value.ToYDB(v, a))
    20  	typeID := val.GetType().GetTypeId()
    21  	if typeID != Ydb.Type_UTF8 {
    22  		t.Errorf("Types are different: expected %d, actual %d", Ydb.Type_UTF8, typeID)
    23  	}
    24  	textValue := val.GetValue().GetValue().(*Ydb.Value_TextValue)
    25  	text := textValue.TextValue
    26  	if text != "a" {
    27  		t.Errorf("Values are different: expected %q, actual %q", "a", text)
    28  	}
    29  }
    30  
    31  func TestUnwrapPrimitiveValue(t *testing.T) {
    32  	a := allocator.New()
    33  	defer a.Free()
    34  	v := value.TextValue("a")
    35  	val := unwrapTypedValue(value.ToYDB(v, a))
    36  	typeID := val.GetType().GetTypeId()
    37  	if typeID != Ydb.Type_UTF8 {
    38  		t.Errorf("Types are different: expected %d, actual %d", Ydb.Type_UTF8, typeID)
    39  	}
    40  	textValue := val.GetValue().GetValue().(*Ydb.Value_TextValue)
    41  	text := textValue.TextValue
    42  	if text != "a" {
    43  		t.Errorf("Values are different: expected %q, actual %q", "a", text)
    44  	}
    45  }
    46  
    47  func TestUnwrapNullValue(t *testing.T) {
    48  	a := allocator.New()
    49  	defer a.Free()
    50  	v := value.NullValue(types.Text)
    51  	val := unwrapTypedValue(value.ToYDB(v, a))
    52  	typeID := val.GetType().GetTypeId()
    53  	if typeID != Ydb.Type_UTF8 {
    54  		t.Errorf("Types are different: expected %d, actual %d", Ydb.Type_UTF8, typeID)
    55  	}
    56  	nullFlagValue := val.GetValue().GetValue().(*Ydb.Value_NullFlagValue)
    57  	if nullFlagValue.NullFlagValue != structpb.NullValue_NULL_VALUE {
    58  		t.Errorf("Values are different: expected %d, actual %d", structpb.NullValue_NULL_VALUE, nullFlagValue.NullFlagValue)
    59  	}
    60  }
    61  
    62  func TestUint8(t *testing.T) {
    63  	l := value.Uint8Value(byte(1))
    64  	r := value.Uint8Value(byte(10))
    65  	c, err := Compare(l, r)
    66  	requireNoError(t, err)
    67  	requireEqualValues(t, -1, c)
    68  
    69  	c, err = Compare(r, l)
    70  	requireNoError(t, err)
    71  	requireEqualValues(t, 1, c)
    72  
    73  	c, err = Compare(l, l)
    74  	requireNoError(t, err)
    75  	requireEqualValues(t, 0, c)
    76  }
    77  
    78  func TestInt8(t *testing.T) {
    79  	l := value.Int8Value(int8(1))
    80  	r := value.Int8Value(int8(10))
    81  	c, err := Compare(l, r)
    82  	requireNoError(t, err)
    83  	requireEqualValues(t, -1, c)
    84  
    85  	c, err = Compare(r, l)
    86  	requireNoError(t, err)
    87  	requireEqualValues(t, 1, c)
    88  
    89  	c, err = Compare(l, l)
    90  	requireNoError(t, err)
    91  	requireEqualValues(t, 0, c)
    92  }
    93  
    94  func TestTimestamp(t *testing.T) {
    95  	l := value.TimestampValue(1)
    96  	r := value.TimestampValue(10)
    97  	c, err := Compare(l, r)
    98  	requireNoError(t, err)
    99  	requireEqualValues(t, -1, c)
   100  
   101  	c, err = Compare(r, l)
   102  	requireNoError(t, err)
   103  	requireEqualValues(t, 1, c)
   104  
   105  	c, err = Compare(l, l)
   106  	requireNoError(t, err)
   107  	requireEqualValues(t, 0, c)
   108  }
   109  
   110  func TestDateTime(t *testing.T) {
   111  	l := value.DatetimeValue(1)
   112  	r := value.DatetimeValue(10)
   113  	c, err := Compare(l, r)
   114  	requireNoError(t, err)
   115  	requireEqualValues(t, -1, c)
   116  
   117  	c, err = Compare(r, l)
   118  	requireNoError(t, err)
   119  	requireEqualValues(t, 1, c)
   120  
   121  	c, err = Compare(l, l)
   122  	requireNoError(t, err)
   123  	requireEqualValues(t, 0, c)
   124  }
   125  
   126  func TestUint64(t *testing.T) {
   127  	l := value.Uint64Value(uint64(1))
   128  	r := value.Uint64Value(uint64(10))
   129  	c, err := Compare(l, r)
   130  	requireNoError(t, err)
   131  	requireEqualValues(t, -1, c)
   132  
   133  	c, err = Compare(r, l)
   134  	requireNoError(t, err)
   135  	requireEqualValues(t, 1, c)
   136  
   137  	c, err = Compare(l, l)
   138  	requireNoError(t, err)
   139  	requireEqualValues(t, 0, c)
   140  }
   141  
   142  func TestInt64(t *testing.T) {
   143  	l := value.Int64Value(int64(1))
   144  	r := value.Int64Value(int64(10))
   145  	c, err := Compare(l, r)
   146  	requireNoError(t, err)
   147  	requireEqualValues(t, -1, c)
   148  
   149  	c, err = Compare(r, l)
   150  	requireNoError(t, err)
   151  	requireEqualValues(t, 1, c)
   152  
   153  	c, err = Compare(l, l)
   154  	requireNoError(t, err)
   155  	requireEqualValues(t, 0, c)
   156  }
   157  
   158  func TestDouble(t *testing.T) {
   159  	l := value.DoubleValue(1.0)
   160  	r := value.DoubleValue(2.0)
   161  	c, err := Compare(l, r)
   162  	requireNoError(t, err)
   163  	requireEqualValues(t, -1, c)
   164  
   165  	c, err = Compare(r, l)
   166  	requireNoError(t, err)
   167  	requireEqualValues(t, 1, c)
   168  
   169  	c, err = Compare(l, l)
   170  	requireNoError(t, err)
   171  	requireEqualValues(t, 0, c)
   172  }
   173  
   174  func TestFloat(t *testing.T) {
   175  	l := value.FloatValue(1.0)
   176  	r := value.FloatValue(2.0)
   177  	c, err := Compare(l, r)
   178  	requireNoError(t, err)
   179  	requireEqualValues(t, -1, c)
   180  
   181  	c, err = Compare(r, l)
   182  	requireNoError(t, err)
   183  	requireEqualValues(t, 1, c)
   184  
   185  	c, err = Compare(l, l)
   186  	requireNoError(t, err)
   187  	requireEqualValues(t, 0, c)
   188  }
   189  
   190  func TestUTF8(t *testing.T) {
   191  	l := value.TextValue("abc")
   192  	r := value.TextValue("abx")
   193  	c, err := Compare(l, r)
   194  	requireNoError(t, err)
   195  	requireEqualValues(t, -1, c)
   196  
   197  	c, err = Compare(r, l)
   198  	requireNoError(t, err)
   199  	requireEqualValues(t, 1, c)
   200  
   201  	c, err = Compare(l, l)
   202  	requireNoError(t, err)
   203  	requireEqualValues(t, 0, c)
   204  }
   205  
   206  func TestOptionalUTF8(t *testing.T) {
   207  	l := value.OptionalValue(value.OptionalValue(value.TextValue("abc")))
   208  	r := value.TextValue("abx")
   209  	c, err := Compare(l, r)
   210  	requireNoError(t, err)
   211  	requireEqualValues(t, -1, c)
   212  
   213  	c, err = Compare(r, l)
   214  	requireNoError(t, err)
   215  	requireEqualValues(t, 1, c)
   216  
   217  	c, err = Compare(l, l)
   218  	requireNoError(t, err)
   219  	requireEqualValues(t, 0, c)
   220  }
   221  
   222  func TestBytes(t *testing.T) {
   223  	l := value.BytesValue([]byte{1, 2, 3})
   224  	r := value.BytesValue([]byte{1, 2, 5})
   225  	c, err := Compare(l, r)
   226  	requireNoError(t, err)
   227  	requireEqualValues(t, -1, c)
   228  
   229  	c, err = Compare(r, l)
   230  	requireNoError(t, err)
   231  	requireEqualValues(t, 1, c)
   232  
   233  	c, err = Compare(l, l)
   234  	requireNoError(t, err)
   235  	requireEqualValues(t, 0, c)
   236  }
   237  
   238  func TestNull(t *testing.T) {
   239  	l := value.NullValue(types.Text)
   240  	r := value.TextValue("abc")
   241  
   242  	c, err := Compare(l, r)
   243  	requireNoError(t, err)
   244  	requireEqualValues(t, -1, c)
   245  
   246  	c, err = Compare(r, l)
   247  	requireNoError(t, err)
   248  	requireEqualValues(t, 1, c)
   249  
   250  	c, err = Compare(l, l)
   251  	requireNoError(t, err)
   252  	requireEqualValues(t, 0, c)
   253  }
   254  
   255  func TestTuple(t *testing.T) {
   256  	withNull := value.TupleValue(value.Uint64Value(1), value.NullValue(types.Text))
   257  	least := value.TupleValue(value.Uint64Value(1), value.TextValue("abc"))
   258  	medium := value.TupleValue(value.Uint64Value(1), value.TextValue("def"))
   259  	largest := value.TupleValue(value.Uint64Value(2), value.TextValue("abc"))
   260  
   261  	c, err := Compare(least, medium)
   262  	requireNoError(t, err)
   263  	requireEqualValues(t, -1, c)
   264  
   265  	c, err = Compare(least, largest)
   266  	requireNoError(t, err)
   267  	requireEqualValues(t, -1, c)
   268  
   269  	c, err = Compare(largest, medium)
   270  	requireNoError(t, err)
   271  	requireEqualValues(t, 1, c)
   272  
   273  	c, err = Compare(withNull, least)
   274  	requireNoError(t, err)
   275  	requireEqualValues(t, -1, c)
   276  
   277  	c, err = Compare(withNull, withNull)
   278  	requireNoError(t, err)
   279  	requireEqualValues(t, 0, c)
   280  }
   281  
   282  func TestList(t *testing.T) {
   283  	least := value.ListValue(value.Uint64Value(1), value.Uint64Value(1))
   284  	medium := value.ListValue(value.Uint64Value(1), value.Uint64Value(2))
   285  	largest := value.ListValue(value.Uint64Value(2), value.Uint64Value(1))
   286  
   287  	c, err := Compare(least, medium)
   288  	requireNoError(t, err)
   289  	requireEqualValues(t, -1, c)
   290  
   291  	c, err = Compare(least, largest)
   292  	requireNoError(t, err)
   293  	requireEqualValues(t, -1, c)
   294  
   295  	c, err = Compare(largest, medium)
   296  	requireNoError(t, err)
   297  	requireEqualValues(t, 1, c)
   298  }
   299  
   300  func TestDyNumber(t *testing.T) {
   301  	l := value.DyNumberValue("2")
   302  	r := value.DyNumberValue("12")
   303  	c, err := Compare(l, r)
   304  	requireNoError(t, err)
   305  	requireEqualValues(t, -1, c)
   306  
   307  	c, err = Compare(r, l)
   308  	requireNoError(t, err)
   309  	requireEqualValues(t, 1, c)
   310  
   311  	c, err = Compare(l, l)
   312  	requireNoError(t, err)
   313  	requireEqualValues(t, 0, c)
   314  }
   315  
   316  func TestUUID(t *testing.T) {
   317  	l := value.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})
   318  	r := value.UUIDValue([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17})
   319  	g := value.UUIDValue([16]byte{100, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17})
   320  	c, err := Compare(l, r)
   321  	requireNoError(t, err)
   322  	requireEqualValues(t, -1, c)
   323  
   324  	c, err = Compare(l, g)
   325  	requireNoError(t, err)
   326  	requireEqualValues(t, -1, c)
   327  
   328  	c, err = Compare(r, l)
   329  	requireNoError(t, err)
   330  	requireEqualValues(t, 1, c)
   331  
   332  	c, err = Compare(l, l)
   333  	requireNoError(t, err)
   334  	requireEqualValues(t, 0, c)
   335  }
   336  
   337  func TestIncompatiblePrimitives(t *testing.T) {
   338  	l := value.Uint64Value(1)
   339  	r := value.TimestampValue(2)
   340  	_, err := Compare(l, r)
   341  	if err == nil {
   342  		t.Errorf("WithStackTrace expected")
   343  	}
   344  	if !errors.Is(err, ErrNotComparable) {
   345  		t.Errorf("Unexpected error: %v", err)
   346  	}
   347  }
   348  
   349  func TestIncompatibleTuples(t *testing.T) {
   350  	l := value.TupleValue(value.Uint64Value(1), value.TextValue("abc"))
   351  	r := value.TupleValue(value.Uint64Value(1), value.BytesValue([]byte("abc")))
   352  	_, err := Compare(l, r)
   353  	if err == nil {
   354  		t.Error("WithStackTrace expected")
   355  	} else if !errors.Is(err, ErrNotComparable) {
   356  		t.Errorf("Unexpected error: %v", err)
   357  	}
   358  }
   359  
   360  func TestTupleOfDifferentLength(t *testing.T) {
   361  	l := value.TupleValue(value.Uint64Value(1), value.TextValue("abc"))
   362  	r := value.TupleValue(value.Uint64Value(1), value.TextValue("abc"), value.TextValue("def"))
   363  
   364  	cmp, err := Compare(l, r)
   365  	requireNoError(t, err)
   366  	requireEqualValues(t, -1, cmp)
   367  
   368  	cmp, err = Compare(r, l)
   369  	requireNoError(t, err)
   370  	requireEqualValues(t, 1, cmp)
   371  }
   372  
   373  func TestTupleInTuple(t *testing.T) {
   374  	l := value.TupleValue(value.Uint64Value(1), value.TupleValue(value.TextValue("abc"), value.BytesValue([]byte("xyz"))))
   375  	r := value.TupleValue(value.Uint64Value(1), value.TupleValue(value.TextValue("def"), value.BytesValue([]byte("xyz"))))
   376  
   377  	cmp, err := Compare(l, r)
   378  	requireNoError(t, err)
   379  	requireEqualValues(t, -1, cmp)
   380  
   381  	cmp, err = Compare(r, l)
   382  	requireNoError(t, err)
   383  	requireEqualValues(t, 1, cmp)
   384  
   385  	cmp, err = Compare(l, l)
   386  	requireNoError(t, err)
   387  	requireEqualValues(t, 0, cmp)
   388  }
   389  
   390  func TestListInList(t *testing.T) {
   391  	l := value.ListValue(
   392  		value.ListValue(
   393  			value.TextValue("abc"), value.TextValue("def"),
   394  		), value.ListValue(
   395  			value.TextValue("uvw"), value.TextValue("xyz"),
   396  		),
   397  	)
   398  	r := value.ListValue(
   399  		value.ListValue(
   400  			value.TextValue("abc"), value.TextValue("deg"),
   401  		), value.ListValue(
   402  			value.TextValue("uvw"), value.TextValue("xyz"),
   403  		),
   404  	)
   405  
   406  	cmp, err := Compare(l, r)
   407  	requireNoError(t, err)
   408  	requireEqualValues(t, -1, cmp)
   409  
   410  	cmp, err = Compare(r, l)
   411  	requireNoError(t, err)
   412  	requireEqualValues(t, 1, cmp)
   413  
   414  	cmp, err = Compare(l, l)
   415  	requireNoError(t, err)
   416  	requireEqualValues(t, 0, cmp)
   417  }
   418  
   419  func requireNoError(t *testing.T, err error) {
   420  	t.Helper()
   421  	if err != nil {
   422  		t.Errorf("Unexpected error %v", err)
   423  	}
   424  }
   425  
   426  func requireEqualValues(t *testing.T, expected int, actual int) {
   427  	t.Helper()
   428  	if expected != actual {
   429  		t.Errorf("Values not equal: expected %v, actual %v", expected, actual)
   430  	}
   431  }