Fixed CryptoPrices tests.

This commit is contained in:
Erik C. Thauvin 2021-05-11 01:47:41 -07:00
parent faf40d3048
commit 273ac60c7d
3 changed files with 19 additions and 47 deletions

View file

@ -31,36 +31,17 @@
*/
package net.thauvin.erik.mobibot.modules
import okhttp3.OkHttpClient
import okhttp3.Request
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.crypto.CryptoException
import net.thauvin.erik.crypto.CryptoPrice
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.mobibot.Mobibot
import net.thauvin.erik.mobibot.Utils
import net.thauvin.erik.mobibot.msg.ErrorMessage
import net.thauvin.erik.mobibot.msg.Message
import net.thauvin.erik.mobibot.msg.NoticeMessage
import net.thauvin.erik.mobibot.msg.PublicMessage
import org.json.JSONException
import org.json.JSONObject
import java.io.IOException
import java.net.URL
import java.text.DecimalFormat
import java.time.format.DateTimeFormatter
import java.time.Instant
import java.time.temporal.ChronoUnit
import java.time.ZoneId
import java.time.ZoneOffset
data class Price(val base: String, val currency: String, val amount: Double)
/**
* The Cryptocurrency Prices module.
*/
class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneOffset.UTC)
val decimalFormat = DecimalFormat("0.00")
/**
* Returns the cryptocurrency market price from [Coinbase](https://developers.coinbase.com/api/v2#get-spot-price).
*/
@ -68,10 +49,8 @@ class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
val debugMessage = "crypto($cmd $args)"
with(bot) {
if (args.matches("\\w+( [a-zA-Z]{3}+)?".toRegex())) {
val params = args.trim().split(" ");
try {
val currency = if (params.size == 2) params[1] else "USD"
val price = marketPrice(params[0], currency)
val price = currentPrice(args.split(' '))
send(sender, PublicMessage("${price.base}: ${price.amount} [${price.currency}]"))
} catch (e: CryptoException) {
if (logger.isWarnEnabled) logger.warn("$debugMessage => ${e.statusCode}", e)
@ -89,6 +68,16 @@ class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
companion object {
// Crypto command
private const val CRYPTO_CMD = "crypto"
/**
* Get current market price.
*/
fun currentPrice(args: List<String>): CryptoPrice {
return if (args.size == 2)
marketPrice(args[0], args[1])
else
marketPrice(args[0])
}
}
init {

View file

@ -31,10 +31,8 @@
*/
package net.thauvin.erik.mobibot.modules
import net.thauvin.erik.crypto.CryptoException
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
import net.thauvin.erik.mobibot.modules.CryptoPrices.Companion.currentPrice
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.testng.annotations.Test
/**
@ -44,29 +42,14 @@ class CryptoPricesTest {
@Test
@Throws(ModuleException::class)
fun testMarketPrice() {
var price = marketPrice("BTC", "USD")
var price = currentPrice(listOf("BTC"))
assertThat(price.base).describedAs("is BTC").isEqualTo("BTC")
assertThat(price.currency).describedAs("is USD").isEqualTo("USD")
assertThat(price.amount).describedAs("BTC > 0").isGreaterThan(0.00)
price = marketPrice("ETH", "EUR")
price = currentPrice(listOf("ETH", "EUR"))
assertThat(price.base).describedAs("is ETH").isEqualTo("ETH")
assertThat(price.currency).describedAs("is EUR").isEqualTo("EUR")
assertThat(price.amount).describedAs("ETH > 0").isGreaterThan(0.00)
price = marketPrice("ETH2", "GBP")
assertThat(price.base).describedAs("is ETH2").isEqualTo("ETH2")
assertThat(price.currency).describedAs("is GBP").isEqualTo("GBP")
assertThat(price.amount).describedAs("ETH2 > 0").isGreaterThan(0.00)
assertThatThrownBy { marketPrice("FOO", "USD") }
.describedAs("FOO")
.isInstanceOf(CryptoException::class.java)
.hasMessageContaining("Invalid base currency")
assertThatThrownBy { marketPrice("FOO", "BAR") }
.describedAs("FOO-BAR")
.isInstanceOf(CryptoException::class.java)
.hasMessageContaining("Invalid currency (BAR)")
}
}

View file

@ -1,9 +1,9 @@
#Generated by the Semver Plugin for Gradle
#Tue May 11 00:47:17 PDT 2021
version.buildmeta=722
#Tue May 11 01:30:42 PDT 2021
version.buildmeta=726
version.major=0
version.minor=8
version.patch=0
version.prerelease=beta
version.project=mobibot
version.semver=0.8.0-beta+722
version.semver=0.8.0-beta+726