Skip to content

Commit

Permalink
Tests for dialect and datatypes (yoyo-project#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotvezz authored Nov 28, 2020
1 parent 2b502e9 commit 983eb7b
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 29 deletions.
270 changes: 270 additions & 0 deletions internal/datatype/datatypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datatype

import (
"gopkg.in/yaml.v2"
"reflect"
"testing"
)

Expand Down Expand Up @@ -120,3 +121,272 @@ func TestDatatype_UnmarshalYAML(t *testing.T) {
})
}
}

func TestDatatype_String(t *testing.T) {
tests := []struct {
dt Datatype
want string
}{
{
dt: Integer,
want: integer,
},
{
dt: TinyInt,
want: tinyint,
},
{
dt: SmallInt,
want: smallint,
},
{
dt: MediumInt,
want: mediumint,
},
{
dt: BigInt,
want: bigint,
},
{
dt: Decimal,
want: decimal,
},
{
dt: Varchar,
want: varchar,
},
{
dt: Text,
want: text,
},
{
dt: TinyText,
want: tinytext,
},
{
dt: MediumText,
want: mediumtext,
},
{
dt: LongText,
want: longtext,
},
{
dt: Char,
want: char,
},
{
dt: Blob,
want: blob,
},
{
dt: Enum,
want: enum,
},
{
dt: Boolean,
want: boolean,
},
{
dt: 123123,
want: "NONE",
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.dt.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_IsInt(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: true,
},
{
dt: Text,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.IsInt(); got != tt.want {
t.Errorf("IsInt() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_IsNumeric(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: true,
},
{
dt: Decimal,
want: true,
},
{
dt: Text,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.IsNumeric(); got != tt.want {
t.Errorf("IsNumeric() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_IsBinary(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: false,
},
{
dt: Decimal,
want: false,
},
{
dt: Text,
want: false,
},
{
dt: Blob,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.IsBinary(); got != tt.want {
t.Errorf("IsNumeric() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_RequiresScale(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: false,
},
{
dt: Decimal,
want: true,
},
{
dt: Text,
want: false,
},
{
dt: Enum,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.RequiresScale(); got != tt.want {
t.Errorf("IsNumeric() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_IsString(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: false,
},
{
dt: Decimal,
want: false,
},
{
dt: Text,
want: true,
},
{
dt: Enum,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.IsString(); got != tt.want {
t.Errorf("IsNumeric() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_IsSignable(t *testing.T) {
tests := []struct {
dt Datatype
want bool
}{
{
dt: Integer,
want: true,
},
{
dt: Decimal,
want: true,
},
{
dt: Text,
want: false,
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
if got := tt.dt.IsSignable(); got != tt.want {
t.Errorf("IsNumeric() = %v, want %v", got, tt.want)
}
})
}
}

func TestDatatype_MarshalYAML(t *testing.T) {
tests := []struct {
dt Datatype
want string
}{
{
dt: Integer,
want: "integer",
},
}
for _, tt := range tests {
t.Run(tt.dt.String(), func(t *testing.T) {
got, _ := tt.dt.MarshalYAML()
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("MarshalYAML() got = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/dbms/base/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func (d *Base) TypeString(dt datatype.Datatype) (string, error) {
s := dt.String()

if s == "NONE" {
return "", d.InvalidDatatype(dt)
return "", datatype.UnknownDatatype
}

return s, nil
Expand Down
57 changes: 57 additions & 0 deletions internal/dbms/base/dialect_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package base

import (
"github.com/dotvezz/yoyo/internal/datatype"
"testing"
)

func TestBase_TypeString(t *testing.T) {
type fields struct {
Dialect string
}
type args struct {
dt datatype.Datatype
}
tests := []struct {
fields fields
args args
want string
wantErr bool
}{
{
fields: fields{
Dialect: "mysql",
},
args: args{
dt: datatype.Integer,
},
want: "INTEGER",
wantErr: false,
},
{
fields: fields{
Dialect: "mysql",
},
args: args{
dt: 0,
},
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.args.dt.String(), func(t *testing.T) {
d := &Base{
Dialect: tt.fields.Dialect,
}
got, err := d.TypeString(tt.args.dt)
if (err != nil) != tt.wantErr {
t.Errorf("TypeString() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("TypeString() got = %v, want %v", got, tt.want)
}
})
}
}
27 changes: 0 additions & 27 deletions internal/dbms/base/errors.go

This file was deleted.

3 changes: 2 additions & 1 deletion internal/dbms/mysql/migrator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mysql

import (
"errors"
"fmt"
"github.com/dotvezz/yoyo/internal/datatype"
"github.com/dotvezz/yoyo/internal/dbms/base"
Expand All @@ -23,7 +24,7 @@ type migrator struct {

func (d *migrator) TypeString(dt datatype.Datatype) (s string, err error) {
if dt&datatype.MySQL != datatype.MySQL {
return "", d.UnsupportedDatatype(dt)
return "", errors.New("unsupported datatype")
}
switch dt {
case datatype.Integer:
Expand Down

0 comments on commit 983eb7b

Please sign in to comment.