Skip to content

Commit

Permalink
[MySQL] Adding all the LOB types + refactoring tests (#488)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Sep 10, 2024
1 parent 479b86e commit f033a07
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 64 deletions.
2 changes: 1 addition & 1 deletion lib/mysql/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func parseColumnDataType(originalS string) (DataType, *Opts, error) {
return Binary, nil, nil
case "varbinary":
return Varbinary, nil, nil
case "blob":
case "blob", "tinyblob", "mediumblob", "longblob":
return Blob, nil, nil
case "text":
return Text, nil, nil
Expand Down
132 changes: 69 additions & 63 deletions lib/mysql/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,80 +15,86 @@ func TestQuoteIdentifier(t *testing.T) {
}

func TestParseColumnDataType(t *testing.T) {
testCases := []struct {
input string
expectedType DataType
expectedOpts *Opts
expectedErr string
}{
{
input: "int",
expectedType: Int,
},
{
input: "tinyint(1)",
expectedType: Boolean,
},
{
input: "varchar(255)",
expectedType: Varchar,
expectedOpts: &Opts{Size: ptr.ToInt(255)},
},
{
// Invalid
{
input: "decimal(5,2)",
expectedType: Decimal,
expectedOpts: &Opts{
Precision: ptr.ToInt(5),
Scale: ptr2.ToUint16(2),
},
},
_, _, err := parseColumnDataType("int(10 unsigned")
assert.ErrorContains(t, err, `malformed data type: "int(10 unsigned"`)
}
{
input: "int(10) unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("foo")
assert.ErrorContains(t, err, `unknown data type: "foo"`)
}
{
input: "tinyint unsigned",
expectedType: SmallInt,
expectedOpts: nil,
},
_, _, err := parseColumnDataType("varchar(")
assert.ErrorContains(t, err, `malformed data type: "varchar("`)
}
}
{
// Integers
{
input: "smallint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int
dataType, _, err := parseColumnDataType("int")
assert.NoError(t, err)
assert.Equal(t, Int, dataType)
}
{
input: "mediumint unsigned",
expectedType: Int,
expectedOpts: nil,
},
// int unsigned
dataType, _, err := parseColumnDataType("int unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int unsigned",
expectedType: BigInt,
expectedOpts: nil,
},
// int(10) unsigned
dataType, _, err := parseColumnDataType("int(10) unsigned")
assert.NoError(t, err)
assert.Equal(t, BigInt, dataType)
}
{
input: "int(10 unsigned",
expectedErr: `malformed data type: "int(10 unsigned"`,
},
// tinyint
dataType, _, err := parseColumnDataType("tinyint")
assert.NoError(t, err)
assert.Equal(t, TinyInt, dataType)
}
{
input: "foo",
expectedErr: `unknown data type: "foo"`,
},
// tinyint unsigned
dataType, _, err := parseColumnDataType("tinyint unsigned")
assert.NoError(t, err)
assert.Equal(t, SmallInt, dataType)
}
{
input: "varchar(",
expectedErr: `malformed data type: "varchar("`,
},
// mediumint unsigned
dataType, _, err := parseColumnDataType("mediumint unsigned")
assert.NoError(t, err)
assert.Equal(t, Int, dataType)
}
}

for _, testCase := range testCases {
colType, opts, err := parseColumnDataType(testCase.input)
if testCase.expectedErr == "" {
{
// tinyint(1) or boolean
dataType, _, err := parseColumnDataType("tinyint(1)")
assert.NoError(t, err)
assert.Equal(t, Boolean, dataType)
}
{
// String
dataType, opts, err := parseColumnDataType("varchar(255)")
assert.NoError(t, err)
assert.Equal(t, Varchar, dataType)
assert.Equal(t, &Opts{Size: ptr.ToInt(255)}, opts)
}
{
// Decimal
dataType, opts, err := parseColumnDataType("decimal(5,2)")
assert.NoError(t, err)
assert.Equal(t, Decimal, dataType)
assert.Equal(t, &Opts{Precision: ptr.ToInt(5), Scale: ptr2.ToUint16(2)}, opts)
}
{
// Blob
for _, blob := range []string{"blob", "tinyblob", "mediumblob", "longblob"} {
dataType, _, err := parseColumnDataType(blob)
assert.NoError(t, err)
assert.Equal(t, testCase.expectedType, colType, testCase.input)
assert.Equal(t, testCase.expectedOpts, opts, testCase.input)
} else {
assert.ErrorContains(t, err, testCase.expectedErr, testCase.input)
assert.Equal(t, Blob, dataType, blob)
}
}
}
Expand Down

0 comments on commit f033a07

Please sign in to comment.