diff --git a/server/test/com/xsn/explorer/data/BalancePostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/BalancePostgresDataHandlerSpec.scala index a82fc73..2a7b2b1 100644 --- a/server/test/com/xsn/explorer/data/BalancePostgresDataHandlerSpec.scala +++ b/server/test/com/xsn/explorer/data/BalancePostgresDataHandlerSpec.scala @@ -5,6 +5,7 @@ import com.xsn.explorer.data.anorm.dao.BalancePostgresDAO import com.xsn.explorer.data.common.PostgresDataHandlerSpec import com.xsn.explorer.helpers.DataHelper import com.xsn.explorer.models.Balance +import com.xsn.explorer.models.base.{Limit, Offset, PaginatedQuery} import org.scalactic.Good class BalancePostgresDataHandlerSpec extends PostgresDataHandlerSpec { @@ -48,6 +49,57 @@ class BalancePostgresDataHandlerSpec extends PostgresDataHandlerSpec { } } + "getRichest" should { + + val balances = List( + Balance( + address = DataHelper.createAddress("XxQ7j37LfuXgsLd5DZAwFKhT3s2ZMkW85F"), + received = BigDecimal("1000"), + spent = BigDecimal("0")), + + Balance( + address = DataHelper.createAddress("Xbh5pJdBNm8J9PxnEmwVcuQKRmZZ7DkpcF"), + received = BigDecimal("1000"), + spent = BigDecimal("100")), + + Balance( + address = DataHelper.createAddress("XfAATXtkRgCdMTrj2fxHvLsKLLmqAjhEAt"), + received = BigDecimal("10000"), + spent = BigDecimal("1000")), + + Balance( + address = DataHelper.createAddress("XiHW7SR56UPHeXKwcpeVsE4nUfkHv5RqE3"), + received = BigDecimal("1000"), + spent = BigDecimal("500")) + ).sortBy(_.available).reverse + + def prepare() = { + clearDatabase() + + balances + .map(dataHandler.upsert) + .foreach(_.isGood mustEqual true) + } + + "return the first 3 richest addresses" in { + prepare() + val query = PaginatedQuery(Offset(0), Limit(3)) + val expected = balances.take(3) + + val result = dataHandler.getRichest(query) + result.map(_.data) mustEqual Good(expected) + } + + "skip the first richest address" in { + prepare() + val query = PaginatedQuery(Offset(1), Limit(3)) + val expected = balances.drop(1).take(3) + + val result = dataHandler.getRichest(query) + result.map(_.data) mustEqual Good(expected) + } + } + private def combine(balances: Balance*): Balance = { balances.reduce { (a, b) => Balance(a.address, a.received + b.received, a.spent + b.spent) diff --git a/server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala index de968a2..d408d1b 100644 --- a/server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala +++ b/server/test/com/xsn/explorer/data/BlockPostgresDataHandlerSpec.scala @@ -119,12 +119,6 @@ class BlockPostgresDataHandlerSpec extends PostgresDataHandlerSpec { } } - private def clearDatabase() = { - database.withConnection { implicit conn => - _root_.anorm.SQL("""DELETE FROM blocks""").execute() - } - } - private def matches(expected: Block, result: Block) = { // NOTE: transactions and confirmations are not matched intentionally result.hash mustEqual expected.hash diff --git a/server/test/com/xsn/explorer/data/common/PostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/common/PostgresDataHandlerSpec.scala index de4abf6..7a83a19 100644 --- a/server/test/com/xsn/explorer/data/common/PostgresDataHandlerSpec.scala +++ b/server/test/com/xsn/explorer/data/common/PostgresDataHandlerSpec.scala @@ -57,4 +57,15 @@ trait PostgresDataHandlerSpec database } + + protected def clearDatabase() = { + database.withConnection { implicit conn => + _root_.anorm.SQL("""DELETE FROM transaction_outputs""").execute() + _root_.anorm.SQL("""DELETE FROM transaction_inputs""").execute() + _root_.anorm.SQL("""DELETE FROM transactions""").execute() + _root_.anorm.SQL("""DELETE FROM blocks""").execute() + _root_.anorm.SQL("""DELETE FROM balances""").execute() + } + } + } diff --git a/server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala b/server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala index 8b9c203..8fad34d 100644 --- a/server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala +++ b/server/test/com/xsn/explorer/processors/BlockEventsProcessorSpec.scala @@ -80,16 +80,6 @@ class BlockEventsProcessorSpec extends PostgresDataHandlerSpec with ScalaFutures } } - private def clearDatabase() = { - database.withConnection { implicit conn => - _root_.anorm.SQL("""DELETE FROM transaction_outputs""").execute() - _root_.anorm.SQL("""DELETE FROM transaction_inputs""").execute() - _root_.anorm.SQL("""DELETE FROM transactions""").execute() - _root_.anorm.SQL("""DELETE FROM blocks""").execute() - _root_.anorm.SQL("""DELETE FROM balances""").execute() - } - } - private def countBlocks() = { database.withConnection { implicit conn => _root_.anorm.SQL("""SELECT COUNT(*) FROM blocks""").as(_root_.anorm.SqlParser.scalar[Int].single)