-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
245 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package repositories | ||
|
||
import org.specs2.matcher.FutureMatchers | ||
import org.specs2.mutable.Specification | ||
import org.specs2.specification.BeforeAfterAll | ||
import repositories.PostgresProfile.api._ | ||
import slick.basic.DatabaseConfig | ||
import slick.jdbc.JdbcProfile | ||
import utils.TestApp | ||
|
||
import java.util.UUID | ||
import scala.concurrent.Await | ||
import scala.concurrent.ExecutionContext | ||
import scala.concurrent.duration.Duration | ||
|
||
case class TestModel(id: UUID, s: String) | ||
|
||
class TestModelTable(tag: Tag) extends DatabaseTable[TestModel](tag, "test_model") { | ||
def s = column[String]("s") | ||
|
||
override def * = (id, s) <> ((TestModel.apply _).tupled, TestModel.unapply) | ||
} | ||
|
||
object TestModelTable { | ||
val table = TableQuery[TestModelTable] | ||
|
||
} | ||
|
||
class CRUDRepositorySpec extends Specification with FutureMatchers with BeforeAfterAll { | ||
|
||
val (app, components) = TestApp.buildApp() | ||
implicit val ec = components.executionContext | ||
val db = components.dbConfig.db | ||
|
||
val repository = new CRUDRepository[TestModelTable, TestModel]() { | ||
override val table = TestModelTable.table | ||
implicit override val ec: ExecutionContext = components.executionContext | ||
override val dbConfig: DatabaseConfig[JdbcProfile] = components.dbConfig | ||
} | ||
|
||
override def beforeAll(): Unit = | ||
Await.result(db.run(TestModelTable.table.schema.create), Duration.Inf) | ||
|
||
override def afterAll(): Unit = | ||
Await.result(db.run(TestModelTable.table.schema.drop), Duration.Inf) | ||
|
||
"CRUDRepository" should { | ||
"create and delete a model" in { | ||
val id = UUID.randomUUID() | ||
val s = UUID.randomUUID().toString | ||
for { | ||
afterCreate <- repository.create(TestModel(id, s)) | ||
get1 <- repository.get(id) | ||
_ <- repository.delete(id) | ||
get2 <- repository.get(id) | ||
} yield (afterCreate shouldEqual TestModel(id, s)) && | ||
(get1 shouldEqual Some(TestModel(id, s))) && | ||
(get2 shouldEqual None) | ||
} | ||
|
||
"update a model" in { | ||
val id = UUID.randomUUID() | ||
UUID.randomUUID().toString | ||
val s2 = UUID.randomUUID().toString | ||
for { | ||
_ <- repository.create(TestModel(id, s2)) | ||
_ <- repository.update(id, TestModel(id, s2)) | ||
get <- repository.get(id) | ||
_ <- repository.delete(id) | ||
} yield get shouldEqual Some(TestModel(id, s2)) | ||
} | ||
|
||
"createOrUpdate a model and list models" in { | ||
val id1 = UUID.randomUUID() | ||
val id2 = UUID.randomUUID() | ||
val s0 = UUID.randomUUID().toString | ||
val s1 = UUID.randomUUID().toString | ||
val s2 = UUID.randomUUID().toString | ||
for { | ||
_ <- repository.createOrUpdate(TestModel(id1, s0)) | ||
_ <- repository.createOrUpdate(TestModel(id2, s1)) | ||
_ <- repository.createOrUpdate(TestModel(id2, s2)) | ||
list <- repository.list() | ||
} yield list shouldEqual List(TestModel(id1, s0), TestModel(id2, s2)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package repositories | ||
|
||
import org.specs2.concurrent.ExecutionEnv | ||
import org.specs2.matcher.FutureMatchers | ||
import org.specs2.matcher.TraversableMatchers | ||
import org.specs2.mutable | ||
import org.specs2.specification.BeforeAfterAll | ||
import utils.Fixtures | ||
import utils.TestApp | ||
|
||
import java.time.OffsetDateTime | ||
import scala.concurrent.Await | ||
import scala.concurrent.duration.Duration | ||
|
||
class ConsumerRepositorySpec(implicit ee: ExecutionEnv) | ||
extends mutable.Specification | ||
with TraversableMatchers | ||
with FutureMatchers | ||
with BeforeAfterAll { | ||
|
||
val (app, components) = TestApp.buildApp() | ||
|
||
val consumer = Fixtures.genConsumer.sample.get | ||
val deletedConsumer = Fixtures.genConsumer.sample.get.copy(deleteDate = Some(OffsetDateTime.now())) | ||
|
||
"ConsumerRepository" should { | ||
"getAll" should { | ||
"exclude soft deleted users" in { | ||
for { | ||
res <- components.consumerRepository.getAll() | ||
} yield res.map(_.id) shouldEqual Seq(consumer.id) | ||
} | ||
} | ||
} | ||
|
||
override def beforeAll(): Unit = { | ||
Await.result(components.consumerRepository.create(consumer), Duration.Inf) | ||
Await.result(components.consumerRepository.create(deletedConsumer), Duration.Inf) | ||
() | ||
} | ||
|
||
override def afterAll(): Unit = { | ||
Await.result(components.consumerRepository.delete(consumer.id), Duration.Inf) | ||
Await.result(components.consumerRepository.delete(deletedConsumer.id), Duration.Inf) | ||
() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters