Exemple #1
0
func TestCharaterSet(t *testing.T) {
	qr, err := framework.NewDefaultClient().Execute("select * from vtocc_test where intval=1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "intval",
				Type:  3,
				Flags: 0,
			}, {
				Name:  "floatval",
				Type:  4,
				Flags: 0,
			}, {
				Name:  "charval",
				Type:  253,
				Flags: 0,
			}, {
				Name:  "binval",
				Type:  253,
				Flags: mysql.FlagBinary,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.12345")},
				sqltypes.Value{Inner: sqltypes.String("\xc2\xa2")},
				sqltypes.Value{Inner: sqltypes.String("\x00\xff")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
func TestCompareRows(t *testing.T) {
	table := []struct {
		fields      []mproto.Field
		left, right []sqltypes.Value
		want        int
	}{
		{
			fields: []mproto.Field{{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("123")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("14")}},
			want:   1,
		},
		{
			fields: []mproto.Field{
				{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG},
				{"b", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG},
			},
			left: []sqltypes.Value{
				{sqltypes.Numeric("555")},
				{sqltypes.Numeric("12")},
			},
			right: []sqltypes.Value{
				{sqltypes.Numeric("555")},
				{sqltypes.Numeric("144")},
			},
			want: -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONG, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("144")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("144")}},
			want:   0,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONGLONG, mproto.VT_UNSIGNED_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("9223372036854775809")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("9223372036854775810")}},
			want:   -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_LONGLONG, mproto.VT_UNSIGNED_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Numeric("9223372036854775819")}},
			right:  []sqltypes.Value{{sqltypes.Numeric("9223372036854775810")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_DOUBLE, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Fractional("3.14")}},
			right:  []sqltypes.Value{{sqltypes.Fractional("3.2")}},
			want:   -1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_DOUBLE, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.Fractional("123.4")}},
			right:  []sqltypes.Value{{sqltypes.Fractional("123.2")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_STRING, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.String("abc")}},
			right:  []sqltypes.Value{{sqltypes.String("abb")}},
			want:   1,
		},
		{
			fields: []mproto.Field{{"a", mproto.VT_STRING, mproto.VT_ZEROVALUE_FLAG}},
			left:   []sqltypes.Value{{sqltypes.String("abc")}},
			right:  []sqltypes.Value{{sqltypes.String("abd")}},
			want:   -1,
		},
	}
	for _, tc := range table {
		got, err := CompareRows(tc.fields, len(tc.fields), tc.left, tc.right)
		if err != nil {
			t.Errorf("CompareRows error: %v", err)
			continue
		}
		if got != tc.want {
			t.Errorf("CompareRows(%v, %v, %v) = %v, want %v", tc.fields, tc.left, tc.right, got, tc.want)
		}
	}
}
Exemple #3
0
func TestFractionals(t *testing.T) {
	client := framework.NewDefaultClient()
	defer client.Execute("delete from vtocc_fracts", nil)

	_, err := client.Execute(
		"insert into vtocc_fracts values(:id, :deci, :num, :f, :d)",
		map[string]interface{}{
			"id":   1,
			"deci": "1.99",
			"num":  "2.99",
			"f":    3.99,
			"d":    4.99,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vtocc_fracts where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "deci",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "num",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "f",
				Type:  mysql.TypeFloat,
				Flags: 0,
			}, {
				Name:  "d",
				Type:  mysql.TypeDouble,
				Flags: 0,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("2.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("3.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("4.99")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
}
func TestFractionals(t *testing.T) {
	client := framework.NewClient()
	defer client.Execute("delete from vitess_fracts", nil)

	_, err := client.Execute(
		"insert into vitess_fracts values(:id, :deci, :num, :f, :d)",
		map[string]interface{}{
			"id":   1,
			"deci": "1.99",
			"num":  "2.99",
			"f":    3.99,
			"d":    4.99,
		},
	)
	if err != nil {
		t.Error(err)
		return
	}
	qr, err := client.Execute("select * from vitess_fracts where id = 1", nil)
	if err != nil {
		t.Error(err)
		return
	}
	want := mproto.QueryResult{
		Fields: []mproto.Field{
			{
				Name:  "id",
				Type:  mysql.TypeLong,
				Flags: 0,
			}, {
				Name:  "deci",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "num",
				Type:  mysql.TypeNewDecimal,
				Flags: 0,
			}, {
				Name:  "f",
				Type:  mysql.TypeFloat,
				Flags: 0,
			}, {
				Name:  "d",
				Type:  mysql.TypeDouble,
				Flags: 0,
			},
		},
		RowsAffected: 1,
		Rows: [][]sqltypes.Value{
			[]sqltypes.Value{
				sqltypes.Value{Inner: sqltypes.Numeric("1")},
				sqltypes.Value{Inner: sqltypes.Fractional("1.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("2.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("3.99")},
				sqltypes.Value{Inner: sqltypes.Fractional("4.99")},
			},
		},
	}
	if !reflect.DeepEqual(*qr, want) {
		t.Errorf("Execute: \n%#v, want \n%#v", *qr, want)
	}
	wantTypes := []query.Type{
		sqltypes.Int32,
		sqltypes.Decimal,
		sqltypes.Decimal,
		sqltypes.Float32,
		sqltypes.Float64,
	}
	for i, field := range qr.Fields {
		got, err := sqltypes.MySQLToType(field.Type, field.Flags)
		if err != nil {
			t.Errorf("col: %d, err: %v", i, err)
			continue
		}
		if got != wantTypes[i] {
			t.Errorf("Unexpected type: col: %d, %d, want %d", i, got, wantTypes[i])
		}
	}
}