Browse Source

server: Clean legacy code from TransactionDataHandler (#34)

prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
c594de3a14
  1. 6
      server/app/com/xsn/explorer/data/TransactionDataHandler.scala
  2. 18
      server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala
  3. 12
      server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala
  4. 50
      server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala
  5. 6
      server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala

6
server/app/com/xsn/explorer/data/TransactionDataHandler.scala

@ -9,12 +9,6 @@ import scala.language.higherKinds
trait TransactionDataHandler[F[_]] {
def upsert(transaction: Transaction): F[Transaction]
def delete(transactionId: TransactionId): F[Transaction]
def deleteBy(blockhash: Blockhash): F[List[Transaction]]
def getBy(
address: Address,
paginatedQuery: PaginatedQuery,

18
server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala

@ -6,10 +6,9 @@ import com.alexitc.playsonify.core.ApplicationResult
import com.alexitc.playsonify.models.{FieldOrdering, PaginatedQuery, PaginatedResult}
import com.xsn.explorer.data.TransactionBlockingDataHandler
import com.xsn.explorer.data.anorm.dao.TransactionPostgresDAO
import com.xsn.explorer.errors.{TransactionNotFoundError, TransactionUnknownError}
import com.xsn.explorer.models._
import com.xsn.explorer.models.fields.TransactionField
import org.scalactic.{Good, One, Or}
import org.scalactic.Good
import play.api.db.Database
class TransactionPostgresDataHandler @Inject() (
@ -18,21 +17,6 @@ class TransactionPostgresDataHandler @Inject() (
extends TransactionBlockingDataHandler
with AnormPostgresDataHandler {
override def upsert(transaction: Transaction): ApplicationResult[Transaction] = withTransaction { implicit conn =>
val maybe = transactionPostgresDAO.upsert(transaction)
Or.from(maybe, One(TransactionUnknownError))
}
override def delete(transactionId: TransactionId): ApplicationResult[Transaction] = withTransaction { implicit conn =>
val maybe = transactionPostgresDAO.delete(transactionId)
Or.from(maybe, One(TransactionNotFoundError))
}
override def deleteBy(blockhash: Blockhash): ApplicationResult[List[Transaction]] = withTransaction { implicit conn =>
val transactions = transactionPostgresDAO.deleteBy(blockhash)
Good(transactions)
}
override def getBy(
address: Address,
paginatedQuery: PaginatedQuery,

12
server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala

@ -16,18 +16,6 @@ class TransactionFutureDataHandler @Inject() (
implicit ec: DatabaseExecutionContext)
extends TransactionDataHandler[FutureApplicationResult] {
override def upsert(transaction: Transaction): FutureApplicationResult[Transaction] = Future {
blockingDataHandler.upsert(transaction)
}
override def delete(transactionId: TransactionId): FutureApplicationResult[Transaction] = Future {
blockingDataHandler.delete(transactionId)
}
override def deleteBy(blockhash: Blockhash): FutureApplicationResult[List[Transaction]] = Future {
blockingDataHandler.deleteBy(blockhash)
}
override def getBy(
address: Address,
paginatedQuery: PaginatedQuery,

50
server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala

@ -44,7 +44,9 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
12312312L,
Size(1000),
List.empty,
List.empty
List(
Transaction.Output(0, 1000, createAddress("Xbh5pJdBNm8J9PxnEmwVcuQKRmZZ7Dkpss"), HexString.from("00").get, None, None)
)
)
val inputs = List(
@ -84,12 +86,36 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
private def prepareTransaction(transaction: Transaction) = {
try {
dataHandler.upsert(transaction)
upsertTransaction(transaction)
} catch {
case _ => ()
}
}
private def upsertTransaction(transaction: Transaction) = {
val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter)
database.withConnection { implicit conn =>
val maybe = dao.upsert(transaction)
Or.from(maybe, One(TransactionNotFoundError))
}
}
private def delete(txid: TransactionId) = {
val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter)
database.withConnection { implicit conn =>
val maybe = dao.delete(txid)
Or.from(maybe, One(TransactionNotFoundError))
}
}
private def deleteBy(blockhash: Blockhash) = {
val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter)
database.withConnection { implicit conn =>
val result = dao.deleteBy(blockhash)
Good(result)
}
}
before {
clearDatabase()
prepareBlock(block)
@ -98,7 +124,7 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
"upsert" should {
"add a new transaction" in {
val result = dataHandler.upsert(transaction)
val result = upsertTransaction(transaction)
result mustEqual Good(transaction)
}
@ -107,31 +133,31 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
time = 2313121L,
size = Size(2000))
dataHandler.upsert(transaction).isGood mustEqual true
val result = dataHandler.upsert(newTransaction)
upsertTransaction(transaction).isGood mustEqual true
val result = upsertTransaction(newTransaction)
result mustEqual Good(newTransaction)
}
}
"delete" should {
"delete a transaction" in {
dataHandler.upsert(transaction).isGood mustEqual true
val result = dataHandler.delete(transaction.id)
upsertTransaction(transaction).isGood mustEqual true
val result = delete(transaction.id)
result mustEqual Good(transaction)
}
"fail to delete a non-existent transaction" in {
dataHandler.delete(transaction.id)
val result = dataHandler.delete(transaction.id)
delete(transaction.id)
val result = delete(transaction.id)
result mustEqual Bad(TransactionNotFoundError).accumulating
}
}
"deleteBy blockhash" should {
"delete the transactions related to a block" in {
dataHandler.upsert(transaction).isGood mustEqual true
upsertTransaction(transaction).isGood mustEqual true
val result = dataHandler.deleteBy(transaction.blockhash)
val result = deleteBy(transaction.blockhash)
result.isGood mustEqual true
result.get.contains(transaction) mustEqual true
}
@ -178,7 +204,7 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be
received = 50)
val expected = PaginatedResult(query.offset, query.limit, Count(1), List(transactionWithValues))
dataHandler.upsert(transaction).isGood mustEqual true
upsertTransaction(transaction).isGood mustEqual true
val result = dataHandler.getBy(address, query, defaultOrdering)
result mustEqual Good(expected)

6
server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala

@ -8,11 +8,5 @@ import com.xsn.explorer.models.fields.TransactionField
class TransactionDummyDataHandler extends TransactionBlockingDataHandler {
override def upsert(transaction: Transaction): ApplicationResult[Transaction] = ???
override def delete(transactionId: TransactionId): ApplicationResult[Transaction] = ???
override def deleteBy(blockhash: Blockhash): ApplicationResult[List[Transaction]] = ???
override def getBy(address: Address, paginatedQuery: PaginatedQuery, ordering: FieldOrdering[TransactionField]): ApplicationResult[PaginatedResult[TransactionWithValues]] = ???
}

Loading…
Cancel
Save