Browse Source

server: Rename create method to upsert on BlockDataHandler

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
e3ce4a4c2d
  1. 2
      server/app/com/xsn/explorer/data/BlockDataHandler.scala
  2. 4
      server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala
  3. 2
      server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala
  4. 14
      server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala

2
server/app/com/xsn/explorer/data/BlockDataHandler.scala

@ -8,7 +8,7 @@ import scala.language.higherKinds
trait BlockDataHandler[F[_]] { trait BlockDataHandler[F[_]] {
def create(block: Block): F[Block] def upsert(block: Block): F[Block]
def getBy(blockhash: Blockhash): F[Block] def getBy(blockhash: Blockhash): F[Block]

4
server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala

@ -17,8 +17,8 @@ class BlockPostgresDataHandler @Inject() (
extends BlockBlockingDataHandler extends BlockBlockingDataHandler
with AnormPostgresDataHandler { with AnormPostgresDataHandler {
override def create(block: Block): ApplicationResult[Block] = database.withConnection { implicit conn => override def upsert(block: Block): ApplicationResult[Block] = database.withConnection { implicit conn =>
val maybe = blockPostgresDAO.create(block) val maybe = blockPostgresDAO.upsert(block)
Or.from(maybe, One(BlockUnknownError)) Or.from(maybe, One(BlockUnknownError))
} }

2
server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala

@ -9,7 +9,7 @@ import com.xsn.explorer.models.rpc.Block
class BlockPostgresDAO { class BlockPostgresDAO {
def create(block: Block)(implicit conn: Connection): Option[Block] = { def upsert(block: Block)(implicit conn: Connection): Option[Block] = {
SQL( SQL(
""" """
|INSERT INTO blocks |INSERT INTO blocks

14
server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala

@ -13,23 +13,23 @@ class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
lazy val dataHandler = new BlockPostgresDataHandler(database, new BlockPostgresDAO) lazy val dataHandler = new BlockPostgresDataHandler(database, new BlockPostgresDAO)
"create" should { "upsert" should {
"add a new block" in { "add a new block" in {
// PoS block // PoS block
val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0") val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0")
val result = dataHandler.create(block) val result = dataHandler.upsert(block)
result.isGood mustEqual true result.isGood mustEqual true
matches(block, result.get) matches(block, result.get)
} }
"override an existing block" in { "override an existing block" in {
val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0") val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0")
dataHandler.create(block) dataHandler.upsert(block)
val newBlock = BlockLoader.get("25762bf01143f7fe34912c926e0b95528b082c6323de35516de0fc321f5d8058").copy(hash = block.hash) val newBlock = BlockLoader.get("25762bf01143f7fe34912c926e0b95528b082c6323de35516de0fc321f5d8058").copy(hash = block.hash)
val expected = newBlock.copy(hash = block.hash) val expected = newBlock.copy(hash = block.hash)
val result = dataHandler.create(newBlock) val result = dataHandler.upsert(newBlock)
result.isGood mustEqual true result.isGood mustEqual true
matches(expected, result.get) matches(expected, result.get)
} }
@ -39,7 +39,7 @@ class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
"return a block" in { "return a block" in {
val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0") val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0")
dataHandler.create(block) dataHandler.upsert(block)
val result = dataHandler.getBy(block.hash) val result = dataHandler.getBy(block.hash)
result.isGood mustEqual true result.isGood mustEqual true
@ -57,7 +57,7 @@ class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
"delete" should { "delete" should {
"delete a block" in { "delete a block" in {
val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0") val block = BlockLoader.get("1ca318b7a26ed67ca7c8c9b5069d653ba224bf86989125d1dfbb0973b7d6a5e0")
dataHandler.create(block) dataHandler.upsert(block)
val result = dataHandler.delete(block.hash) val result = dataHandler.delete(block.hash)
result.isGood mustEqual true result.isGood mustEqual true
@ -80,7 +80,7 @@ class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec {
val block1 = BlockLoader.get("000003fb382f6892ae96594b81aa916a8923c70701de4e7054aac556c7271ef7") val block1 = BlockLoader.get("000003fb382f6892ae96594b81aa916a8923c70701de4e7054aac556c7271ef7")
val block2 = BlockLoader.get("000004645e2717b556682e3c642a4c6e473bf25c653ff8e8c114a3006040ffb8") val block2 = BlockLoader.get("000004645e2717b556682e3c642a4c6e473bf25c653ff8e8c114a3006040ffb8")
List(block1, block2, block0).foreach(dataHandler.create) List(block1, block2, block0).foreach(dataHandler.upsert)
val result = dataHandler.getLatestBlock() val result = dataHandler.getLatestBlock()
result.isGood mustEqual true result.isGood mustEqual true

Loading…
Cancel
Save