/
CreateTableParserTest.scala
135 lines (126 loc) · 5.06 KB
/
CreateTableParserTest.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package troy
package cql.parser.ddl
import org.scalatest._
import troy.cql.ast.ddl.Table
import troy.cql.ast.{ CreateTable, DataType }
import troy.cql.parser.ParserTestUtils.parseSchemaAs
class CreateTableParserTest extends FlatSpec with Matchers {
"Create Table Parser" should "parse simple create table" in {
val statement = parseSchemaAs[CreateTable]("CREATE TABLE test.posts (author_id text PRIMARY KEY);")
statement.ifNotExists shouldBe false
statement.tableName.keyspace.get.name shouldBe "test"
statement.tableName.table shouldBe "posts"
statement.columns.size shouldBe 1
statement.columns.head.dataType shouldBe DataType.Text
statement.columns.head.isPrimaryKey shouldBe true
statement.columns.head.isStatic shouldBe false
statement.columns.head.name shouldBe "author_id"
}
it should "parse multiline create table" in {
val statement = parseSchemaAs[CreateTable](
"""
CREATE TABLE test.posts (
author_id text PRIMARY KEY
);
"""
)
statement.ifNotExists shouldBe false
statement.columns.size shouldBe 1
statement.columns.head.dataType shouldBe DataType.Text
statement.columns.head.isPrimaryKey shouldBe true
statement.columns.head.isStatic shouldBe false
statement.columns.head.name shouldBe "author_id"
}
it should "parse multi-fields create table" in {
val statement = parseSchemaAs[CreateTable](
"""
CREATE TABLE test.posts (
author_id text PRIMARY KEY,
author_name text static,
author_age int static,
post_id text,
post_title text
);
"""
)
statement.ifNotExists shouldBe false
statement.columns shouldBe Seq(
Table.Column("author_id", DataType.Text, false, true),
Table.Column("author_name", DataType.Text, true, false),
Table.Column("author_age", DataType.Int, true, false),
Table.Column("post_id", DataType.Text, false, false),
Table.Column("post_title", DataType.Text, false, false)
)
statement.primaryKey.isEmpty shouldBe true // Primary is defined inline instead
statement.options.isEmpty shouldBe true
}
it should "parse create table with clustering columns" in {
val statement = parseSchemaAs[CreateTable](
"""
CREATE TABLE test.posts (
author_id text,
author_name text static,
author_age int static,
post_id text,
post_title text,
PRIMARY KEY ((author_id), post_id)
);
"""
)
statement.ifNotExists shouldBe false
statement.columns shouldBe Seq(
Table.Column("author_id", DataType.Text, false, false), // Yes, it is not THE primary key, only a partition key
Table.Column("author_name", DataType.Text, true, false),
Table.Column("author_age", DataType.Int, true, false),
Table.Column("post_id", DataType.Text, false, false),
Table.Column("post_title", DataType.Text, false, false)
)
statement.primaryKey.get.partitionKeys shouldBe Seq("author_id")
statement.primaryKey.get.clusteringColumns shouldBe Seq("post_id")
}
it should "parse different primary key styles" in {
def pk(pk: String) =
parseSchemaAs[CreateTable](s"CREATE TABLE test.posts ( author_id text, author_name text, post_id text, post_title text, PRIMARY KEY $pk);").primaryKey.get
pk("((author_id), post_id)") shouldBe Table.PrimaryKey(Seq("author_id"), Seq("post_id"))
pk("(author_id, post_id)") shouldBe Table.PrimaryKey(Seq("author_id"), Seq("post_id"))
pk("((author_id, author_name), post_id)") shouldBe Table.PrimaryKey(Seq("author_id", "author_name"), Seq("post_id"))
pk("((author_id, author_name), post_id, post_title)") shouldBe Table.PrimaryKey(Seq("author_id", "author_name"), Seq("post_id", "post_title"))
pk("(author_id, post_id, post_title)") shouldBe Table.PrimaryKey(Seq("author_id"), Seq("post_id", "post_title"))
}
it should "accepts schemas with timeuuid fields" in {
parseSchemaAs[CreateTable](
"""
CREATE TABLE test.posts (
author_id text,
post_id timeuuid,
PRIMARY KEY ((author_id), post_id)
);
"""
)
}
it should "parse multi-fields create table with float datatype" in {
val statement = parseSchemaAs[CreateTable](
"""
CREATE TABLE test.users (
user_id text PRIMARY KEY,
user_name text static,
user_age int static,
user_weight float,
user_gender smallint,
user_ip inet
);
"""
)
statement.ifNotExists shouldBe false
statement.columns shouldBe Seq(
Table.Column("user_id", DataType.Text, false, true),
Table.Column("user_name", DataType.Text, true, false),
Table.Column("user_age", DataType.Int, true, false),
Table.Column("user_weight", DataType.Float, false, false),
Table.Column("user_gender", DataType.Smallint, false, false),
Table.Column("user_ip", DataType.Inet, false, false)
)
statement.primaryKey.isEmpty shouldBe true // Primary is defined inline instead
statement.options.isEmpty shouldBe true
}
}