Permalink
Browse files

Add test case to text, varchar, char with default value.

  • Loading branch information...
AtkinsChang committed Aug 1, 2016
1 parent 41ceb9e commit f632d10e58bf5f70746b33f3a893f60d02aea787
@@ -47,10 +47,16 @@ CREATE TABLE "test" ("smallint_auto_inc" SMALLSERIAL,
"numeric_default_zero" NUMERIC DEFAULT 0.0,
"numeric_default_pos" NUMERIC DEFAULT 1.1,
"numeric_default_neg" NUMERIC DEFAULT -1.1,
"char_dafault" CHAR(5) DEFAULT 'abcde',
"varchar_default" VARCHAR DEFAULT 'abcde',
"varchar_default_null" VARCHAR DEFAULT NULL,
"text_default" TEXT DEFAULT 'abcde',
"char_default_empty" char(1) NOT NULL DEFAULT '',
"char_default_valid" char(1) NOT NULL DEFAULT 'a',
"char_default_invalid" char(1) NOT NULL DEFAULT 'ab'
"text_default_null" TEXT DEFAULT NULL,
-- for String
"char_default_empty" CHAR(2) DEFAULT 'ab',
"char_default_null" CHAR(2) DEFAULT NULL,
-- for Char
"char1_default_empty" CHAR(1) DEFAULT '',
"char1_default_valid" CHAR(1) DEFAULT 'a',
"char1_default_invalid" CHAR(1) DEFAULT 'ab',
"char1_default_null" CHAR(1) DEFAULT NULL
);
@@ -146,15 +146,15 @@ object GeneratedCodeTest {
val smallserialOptions = optionsOfColumn(Test.baseTableRow.smallintAutoInc)
val serialOptions = optionsOfColumn(Test.baseTableRow.intAutoInc)
val bigserialOptions = optionsOfColumn(Test.baseTableRow.bigintAutoInc)
val charEmptyOptions = optionsOfColumn(Test.baseTableRow.charDefaultEmpty)
val charValidOptions = optionsOfColumn(Test.baseTableRow.charDefaultValid)
val charInvalidOptions = optionsOfColumn(Test.baseTableRow.charDefaultInvalid)
val char1EmptyOptions = optionsOfColumn(Test.baseTableRow.char1DefaultEmpty)
val char1ValidOptions = optionsOfColumn(Test.baseTableRow.char1DefaultValid)
val char1InvalidOptions = optionsOfColumn(Test.baseTableRow.char1DefaultInvalid)
assertTrue("smallint_auto_inc should be AutoInc", smallserialOptions.exists(option => (option equals Test.baseTableRow.O.AutoInc)))
assertTrue("int_auto_inc should be AutoInc", serialOptions.exists(option => (option equals Test.baseTableRow.O.AutoInc)))
assertTrue("bigint_auto_inc should be AutoInc", bigserialOptions.exists(option => (option equals Test.baseTableRow.O.AutoInc)))
assertTrue("default value of char_default_empty should be ' '", charEmptyOptions.exists(option => (option equals Test.baseTableRow.O.Default(' '))))
assertTrue("default value of char_default_valid should be 'a'", charValidOptions.exists(option => (option equals Test.baseTableRow.O.Default('a'))))
assertTrue("default value of char_default_invalid should not exist", charInvalidOptions.forall(option => (option.isInstanceOf[RelationalProfile.ColumnOption.Default[_]])))
assertTrue("default value of char1_default_empty should be ' '", char1EmptyOptions.exists(option => (option equals Test.baseTableRow.O.Default(Some(' ')))))
assertTrue("default value of char1_default_valid should be 'a'", char1ValidOptions.exists(option => (option equals Test.baseTableRow.O.Default(Some('a')))))
assertTrue("default value of char1_default_invalid should not exist", char1InvalidOptions.forall(option => (option.isInstanceOf[RelationalProfile.ColumnOption.Default[_]])))
}
)
}
@@ -63,18 +63,44 @@ trait PostgresProfile extends JdbcProfile {
override def schema = super.schema.filter(_ != "public") // remove default schema
}
override def createColumnBuilder(tableBuilder: TableBuilder, meta: MColumn): ColumnBuilder = new ColumnBuilder(tableBuilder, meta) {
/*
The default value for numeric type behave different with postgres version
PG9.5 - PG9.6:
positive default value in int boundary: 1
negative default value in int boundary: '-1'::integer
positive default value between int boundary and long boundary: '123123214232131312'::bitint
negative default value between int boundary and long boundary: '-123123214232131312'::bitint
positive default value beyond long boundary: '111111111111111111111111111'::numeric
negative default value beyond long boundary: '-111111111111111111111111111'::numeric
positive floating: '1.1'::numeric
negative floating: '-.1.1'::numeric
PGX.X to PG9.4:
positive default value in int boundary: 1
negative default value in int boundary: (-1)
positive default value between int boundary and long boundary: 123123214232131312::bitint
negative default value between int boundary and long boundary: (-123123214232131312)::bitint
positive default value beyond long boundary: 111111111111111111111111111::numeric
negative default value beyond long boundary: (-111111111111111111111111111)::numeric
positive floating: 1.1
negative floating: (-.1.1)
*/
val NumericPattern = "^['(]?(-?[0-9]+\\.?[0-9]*)[')]?(?:::(?:numeric|bigint|integer))?".r
val TextPattern = "^'(.*)'::(?:bpchar|character varying|text)".r
val UUIDPattern = "^'(.*)'::uuid".r
override def default = meta.columnDef.map((_,tpe)).collect{
case ("true","Boolean") => Some(Some(true))
case ("false","Boolean") => Some(Some(false))
case (TextPattern(str),"String") => Some(Some(str))
case ("NULL::bpchar", "String") => Some(None)
case (TextPattern(str),"Char") => str.length match {
case 0 => Some(Some(' ')) // Default to one space, as the char will be space padded anyway
case 1 => Some(Some(str.head))
case _ => None // This is invalid, so let's not supply any default
}
case ("NULL::bpchar", "Char") => Some(None)
case (NumericPattern(v),"Short") => Some(Some(v.toShort))
case (NumericPattern(v),"Int") => Some(Some(v.toInt))
case (NumericPattern(v),"Long") => Some(Some(v.toLong))

0 comments on commit f632d10

Please sign in to comment.