Skip to content

Commit 1bf2849

Browse files
committed
Fixed & improved tests
1 parent 95c788f commit 1bf2849

File tree

5 files changed

+75
-72
lines changed

5 files changed

+75
-72
lines changed

core/src/test/scala/programmerlist/ExampleSpec.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,9 @@ class ExampleSpec extends FlatSpec with ShouldMatchers with DBSettings with Logg
1414
val findAllFuture: Future[List[Programmer]] = AsyncDB.withPool { implicit session =>
1515
Programmer.findAll
1616
}
17-
18-
Await.result(findAllFuture, 5.seconds)
19-
findAllFuture.foreach { programmers: List[Programmer] =>
20-
21-
log.debug(s"Programmers: ${programmers}")
22-
programmers.size should be > 0
23-
}
17+
val programmers: List[Programmer] = Await.result(findAllFuture, 5.seconds)
18+
log.debug(s"Programmers: ${programmers}")
19+
programmers.size should be > 0
2420
}
2521

2622
it should "work within a transaction" in {

core/src/test/scala/programmerlist/Programmer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ object Programmer extends SQLSyntaxSupport[Programmer] with ShortenedNames {
4242
id = rs.long(p.id),
4343
name = rs.string(p.name),
4444
companyId = rs.longOpt(p.companyId),
45-
createdAt = rs.timestamp(p.createdAt).toDateTime,
46-
deletedAt = rs.timestampOpt(p.deletedAt).map(_.toDateTime)
45+
createdAt = rs.dateTime(p.createdAt),
46+
deletedAt = rs.dateTimeOpt(p.deletedAt)
4747
)
4848

4949
// join query with company table

core/src/test/scala/sample/AsyncLover.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ object AsyncLover extends SQLSyntaxSupport[AsyncLover] {
2727
rating = rs.int(c.rating),
2828
isReactive = rs.boolean(c.isReactive),
2929
lunchtime = rs.timeOpt(c.lunchtime),
30-
birthday = rs.dateOpt(c.lunchtime).map(_.toDateTime),
31-
createdAt = rs.timestamp(c.createdAt).toDateTime)
30+
birthday = rs.dateTimeOpt(c.lunchtime),
31+
createdAt = rs.dateTime(c.createdAt))
3232

3333
}
3434

core/src/test/scala/unit/PgListDBInitializer.scala

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ object PgListDBInitializer {
88
val log = LoggerFactory.getLogger(this.getClass)
99

1010
def initPostgreSQL() {
11-
DB readOnly { implicit s =>
11+
DB autoCommit { implicit s =>
12+
try sql"drop table programmer".execute.apply()
13+
catch { case e: Exception => log.debug(e.getMessage, e) }
1214
try {
13-
sql"select 1 from programmer limit 1".map(_.long(1)).single.apply()
14-
} catch {
15-
case e: java.sql.SQLException =>
16-
DB autoCommit { implicit s =>
17-
try {
18-
sql"""
15+
sql"""
1916
create table programmer (
2017
id bigserial primary key,
2118
name varchar(255) not null,
@@ -24,9 +21,12 @@ create table programmer (
2421
deleted_timestamp timestamp without time zone
2522
);
2623
""".execute.apply()
27-
} catch { case e: Exception => log.debug(e.getMessage, e) }
28-
try {
29-
sql"""
24+
} catch { case e: Exception => log.debug(e.getMessage, e) }
25+
26+
try sql"drop table company".execute.apply()
27+
catch { case e: Exception => log.debug(e.getMessage, e) }
28+
try {
29+
sql"""
3030
create table company (
3131
id bigserial primary key,
3232
name varchar(255) not null,
@@ -35,28 +35,35 @@ create table company (
3535
deleted_at timestamp without time zone
3636
);
3737
""".execute.apply()
38-
} catch { case e: Exception => log.debug(e.getMessage, e) }
39-
try {
40-
sql"""
38+
} catch { case e: Exception => log.debug(e.getMessage, e) }
39+
40+
try sql"drop table skill".execute.apply()
41+
catch { case e: Exception => log.debug(e.getMessage, e) }
42+
try {
43+
sql"""
4144
create table skill (
4245
id bigserial primary key,
4346
name varchar(255) not null,
4447
created_at timestamp without time zone not null,
4548
deleted_at timestamp without time zone
4649
);
4750
""".execute.apply()
48-
} catch { case e: Exception => log.debug(e.getMessage, e) }
49-
try {
50-
sql"""
51+
} catch { case e: Exception => log.debug(e.getMessage, e) }
52+
53+
try sql"drop table programmer_skill".execute.apply()
54+
catch { case e: Exception => log.debug(e.getMessage, e) }
55+
try {
56+
sql"""
5157
create table programmer_skill (
5258
programmer_id bigint not null,
5359
skill_id bigint not null,
5460
primary key(programmer_id, skill_id)
5561
);
5662
""".execute.apply()
57-
} catch { case e: Exception => log.debug(e.getMessage, e) }
58-
try {
59-
sql"""
63+
} catch { case e: Exception => log.debug(e.getMessage, e) }
64+
65+
try {
66+
sql"""
6067
insert into company (name, url, created_at) values ('Typesafe', 'http://typesafe.com/', current_timestamp);
6168
insert into company (name, url, created_at) values ('Oracle', 'http://www.oracle.com/', current_timestamp);
6269
insert into company (name, url, created_at) values ('Google', 'http://www.google.com/', current_timestamp);
@@ -76,21 +83,17 @@ insert into programmer_skill values (1, 1);
7683
insert into programmer_skill values (1, 2);
7784
insert into programmer_skill values (2, 2);
7885
""".execute.apply()
79-
} catch { case e: Exception => log.debug(e.getMessage, e) }
80-
}
81-
}
86+
} catch { case e: Exception => log.debug(e.getMessage, e) }
8287
}
8388
}
8489

8590
def initMySQL() {
86-
NamedDB('mysql) readOnly { implicit s =>
91+
NamedDB('mysql) autoCommit { implicit s =>
92+
93+
try sql"drop table programmer_skill".execute.apply()
94+
catch { case e: Exception => log.debug(e.getMessage, e) }
8795
try {
88-
sql"select 1 from programmer limit 1".map(_.long(1)).single.apply()
89-
} catch {
90-
case e: java.sql.SQLException =>
91-
DB autoCommit { implicit s =>
92-
try {
93-
sql"""
96+
sql"""
9497
create table programmer (
9598
id bigint auto_increment primary key,
9699
name varchar(255) not null,
@@ -99,9 +102,12 @@ create table programmer (
99102
deleted_timestamp timestamp
100103
);
101104
""".execute.apply()
102-
} catch { case e: Exception => log.debug(e.getMessage, e) }
103-
try {
104-
sql"""
105+
} catch { case e: Exception => log.debug(e.getMessage, e) }
106+
107+
try sql"drop table company".execute.apply()
108+
catch { case e: Exception => log.debug(e.getMessage, e) }
109+
try {
110+
sql"""
105111
create table company (
106112
id bigint auto_increment primary key,
107113
name varchar(255) not null,
@@ -110,28 +116,35 @@ create table company (
110116
deleted_at timestamp
111117
);
112118
""".execute.apply()
113-
} catch { case e: Exception => log.debug(e.getMessage, e) }
114-
try {
115-
sql"""
119+
} catch { case e: Exception => log.debug(e.getMessage, e) }
120+
121+
try sql"drop table skill".execute.apply()
122+
catch { case e: Exception => log.debug(e.getMessage, e) }
123+
try {
124+
sql"""
116125
create table skill (
117126
id bigint auto_increment primary key,
118127
name varchar(255) not null,
119128
created_at timestamp not null,
120129
deleted_at timestamp
121130
);
122131
""".execute.apply()
123-
} catch { case e: Exception => log.debug(e.getMessage, e) }
124-
try {
125-
sql"""
132+
} catch { case e: Exception => log.debug(e.getMessage, e) }
133+
134+
try sql"drop table programmer_skill".execute.apply()
135+
catch { case e: Exception => log.debug(e.getMessage, e) }
136+
try {
137+
sql"""
126138
create table programmer_skill (
127139
programmer_id bigint not null,
128140
skill_id bigint not null,
129141
primary key(programmer_id, skill_id)
130142
);
131143
""".execute.apply()
132-
} catch { case e: Exception => log.debug(e.getMessage, e) }
133-
try {
134-
sql"""
144+
} catch { case e: Exception => log.debug(e.getMessage, e) }
145+
146+
try {
147+
sql"""
135148
insert into company (name, url, created_at) values ('Typesafe', 'http://typesafe.com/', current_timestamp);
136149
insert into company (name, url, created_at) values ('Oracle', 'http://www.oracle.com/', current_timestamp);
137150
insert into company (name, url, created_at) values ('Google', 'http://www.google.com/', current_timestamp);
@@ -151,10 +164,8 @@ insert into programmer_skill values (1, 1);
151164
insert into programmer_skill values (1, 2);
152165
insert into programmer_skill values (2, 2);
153166
""".execute.apply()
154-
} catch { case e: Exception => log.debug(e.getMessage, e) }
155-
}
156-
}
167+
} catch { case e: Exception => log.debug(e.getMessage, e) }
157168
}
158169
}
159170

160-
}
171+
}

core/src/test/scala/unit/SampleDBInitializer.scala

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ create table async_lover (
1212
is_reactive boolean default true,
1313
lunchtime time,
1414
birthday date,
15-
created_at timestamp without time zone not null,
16-
deleted_at timestamp without time zone
15+
created_at timestamp with time zone not null,
16+
deleted_at timestamp with time zone
1717
);
1818
"""
1919

@@ -41,25 +41,21 @@ create table async_lover (
4141

4242
def initPostgreSQL(): Unit = {
4343
DB autoCommit { implicit s =>
44-
try {
45-
sql"select 1 from async_lover limit 1".map(_.long(1)).single.apply()
46-
} catch {
47-
case e: Exception =>
48-
SQL(pgCreateTable).execute.apply()
49-
insertQueries.foreach(q => SQL(q).update.apply())
50-
}
44+
try sql"drop table async_lover".execute.apply()
45+
catch { case e: Exception => }
46+
47+
SQL(pgCreateTable).execute.apply()
48+
insertQueries.foreach(q => SQL(q).update.apply())
5149
}
5250
}
5351

5452
def initMySQL(): Unit = {
5553
NamedDB('mysql) autoCommit { implicit s =>
56-
try {
57-
sql"select 1 from async_lover limit 1".map(_.long(1)).single.apply()
58-
} catch {
59-
case e: Exception =>
60-
SQL(mysqlCreateTable).execute.apply()
61-
insertQueries.foreach(q => SQL(q).update.apply())
62-
}
54+
try sql"drop table async_lover".execute.apply()
55+
catch { case e: Exception => }
56+
57+
SQL(mysqlCreateTable).execute.apply()
58+
insertQueries.foreach(q => SQL(q).update.apply())
6359
}
6460
}
6561

0 commit comments

Comments
 (0)