-
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
7 changed files
with
141 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,20 +1,43 @@ | ||
package domain.portfolios | ||
|
||
import auth.User | ||
import db.* | ||
import java.sql.ResultSet | ||
import java.util.* | ||
import javax.sql.DataSource | ||
|
||
class PortfolioRepository { | ||
|
||
fun listAll(user: User): List<Portfolio> { | ||
return emptyList() | ||
class PortfolioRepository(db: DataSource): BaseRepository(db, "portfolios") { | ||
private val mapper: ResultSet.() -> Portfolio = { | ||
Portfolio(getId(), getString("name")) | ||
} | ||
|
||
fun findById(id: UUID, user: User): Portfolio? { | ||
return null | ||
fun listAll(user: User): List<Portfolio> = | ||
db.query(table, mapOf("user_id" to user.id), mapper = mapper) | ||
|
||
fun findById(id: UUID, user: User): Portfolio? = | ||
db.query(table, mapOf("id" to id, "user_id" to user.id), mapper = mapper).firstOrNull() | ||
|
||
fun create(portfolio: Portfolio, user: User): Portfolio = portfolio.also { | ||
db.insert(table, mapOf( | ||
"id" to portfolio.id, | ||
"user_id" to user.id, | ||
"name" to portfolio.name | ||
)) | ||
} | ||
|
||
fun save(portfolio: Portfolio, user: User): Portfolio { | ||
return portfolio | ||
fun update(portfolio: Portfolio, user: User): Portfolio = portfolio.also { | ||
db.update(table, | ||
mapOf( | ||
"id" to portfolio.id, | ||
"user_id" to user.id | ||
), | ||
mapOf( | ||
"name" to portfolio.name | ||
)).also { | ||
if (it == 0) { | ||
throw NoSuchElementException("portfolio with id=${portfolio.id} and user=${user.login} is absent") | ||
} | ||
} | ||
} | ||
|
||
} |
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,86 @@ | ||
package domain.portfolios | ||
|
||
import auth.HashingService | ||
import auth.Role | ||
import auth.User | ||
import auth.UserRepository | ||
import db.DBTest.Companion.db | ||
import db.exec | ||
import io.mockk.mockk | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import org.junit.jupiter.api.assertThrows | ||
|
||
class PortfolioRepositoryTest { | ||
private val repository = PortfolioRepository(db) | ||
private val userRepository = UserRepository(db, HashingService(), mockk(relaxed = true)) | ||
private lateinit var user1: User | ||
private lateinit var user2: User | ||
private lateinit var user3: User | ||
|
||
@BeforeEach | ||
internal fun emptyPortfolioTable() { | ||
db.exec("delete from portfolios") | ||
db.exec("delete from users where login != 'admin'") | ||
user1 = user("user1") | ||
user2 = user("user2") | ||
user3 = user("user3") | ||
} | ||
|
||
@Test | ||
internal fun createAndGet() { | ||
val portfolio = Portfolio(name = "portfolio") | ||
val savedPortfolio = repository.create(portfolio, user1) | ||
assertThat(savedPortfolio.id).isEqualTo(portfolio.id) | ||
assertThat(savedPortfolio.name).isEqualTo("portfolio") | ||
|
||
val receivedPortfolio = repository.findById(portfolio.id, user1) | ||
assertThat(receivedPortfolio).isNotNull | ||
assertThat(receivedPortfolio).isEqualTo(portfolio) | ||
|
||
val emptyResult = repository.findById(portfolio.id, user2) | ||
assertThat(emptyResult).isNull() | ||
} | ||
|
||
@Test | ||
internal fun updatePortfolio() { | ||
val portfolioBeforeUpdate = Portfolio(name = "portfolio") | ||
.also { repository.create(it, user1) } | ||
.let { repository.findById(it.id, user1) }!! | ||
|
||
assertThat(portfolioBeforeUpdate.name).isEqualTo("portfolio") | ||
|
||
val portfolioAfterUpdate = Portfolio(id = portfolioBeforeUpdate.id, name = "new name") | ||
.also { repository.update(it, user1) } | ||
.let { repository.findById(portfolioBeforeUpdate.id, user1) }!! | ||
|
||
assertThat(portfolioAfterUpdate.name).isEqualTo("new name") | ||
} | ||
|
||
@Test | ||
internal fun updatePortfolioByAnotherUser() { | ||
val portfolio = Portfolio(name = "portfolio") | ||
.also { repository.create(it, user1) } | ||
.copy(name = "ne name") | ||
|
||
assertThrows<NoSuchElementException> { repository.update(portfolio, user2) } | ||
} | ||
|
||
@Test | ||
internal fun findAll() { | ||
val portfolio1 = Portfolio(name = "portfolio1").also { repository.create(it, user1) } | ||
val portfolio2 = Portfolio(name = "portfolio2").also { repository.create(it, user1) } | ||
val portfolio3 = Portfolio(name = "portfolio3").also { repository.create(it, user2) } | ||
|
||
assertThat(repository.listAll(user1)).hasSize(2) | ||
assertThat(repository.listAll(user1)).hasSameElementsAs(listOf(portfolio1, portfolio2)) | ||
assertThat(repository.listAll(user2)).hasSize(1) | ||
assertThat(repository.listAll(user2)).hasSameElementsAs(listOf(portfolio3)) | ||
assertThat(repository.listAll(user3)).hasSize(0) | ||
} | ||
|
||
private fun user(login: String): User { | ||
return userRepository.create(login, Role.USER, "en", "123") | ||
} | ||
} |