Fixed CryptoPrices tests.
This commit is contained in:
parent
faf40d3048
commit
273ac60c7d
3 changed files with 19 additions and 47 deletions
|
@ -31,36 +31,17 @@
|
||||||
*/
|
*/
|
||||||
package net.thauvin.erik.mobibot.modules
|
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.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.Mobibot
|
||||||
import net.thauvin.erik.mobibot.Utils
|
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 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.
|
* The Cryptocurrency Prices module.
|
||||||
*/
|
*/
|
||||||
class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
|
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).
|
* 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)"
|
val debugMessage = "crypto($cmd $args)"
|
||||||
with(bot) {
|
with(bot) {
|
||||||
if (args.matches("\\w+( [a-zA-Z]{3}+)?".toRegex())) {
|
if (args.matches("\\w+( [a-zA-Z]{3}+)?".toRegex())) {
|
||||||
val params = args.trim().split(" ");
|
|
||||||
try {
|
try {
|
||||||
val currency = if (params.size == 2) params[1] else "USD"
|
val price = currentPrice(args.split(' '))
|
||||||
val price = marketPrice(params[0], currency)
|
|
||||||
send(sender, PublicMessage("${price.base}: ${price.amount} [${price.currency}]"))
|
send(sender, PublicMessage("${price.base}: ${price.amount} [${price.currency}]"))
|
||||||
} catch (e: CryptoException) {
|
} catch (e: CryptoException) {
|
||||||
if (logger.isWarnEnabled) logger.warn("$debugMessage => ${e.statusCode}", e)
|
if (logger.isWarnEnabled) logger.warn("$debugMessage => ${e.statusCode}", e)
|
||||||
|
@ -89,6 +68,16 @@ class CryptoPrices(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
companion object {
|
companion object {
|
||||||
// Crypto command
|
// Crypto command
|
||||||
private const val CRYPTO_CMD = "crypto"
|
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 {
|
init {
|
||||||
|
|
|
@ -31,10 +31,8 @@
|
||||||
*/
|
*/
|
||||||
package net.thauvin.erik.mobibot.modules
|
package net.thauvin.erik.mobibot.modules
|
||||||
|
|
||||||
import net.thauvin.erik.crypto.CryptoException
|
import net.thauvin.erik.mobibot.modules.CryptoPrices.Companion.currentPrice
|
||||||
import net.thauvin.erik.crypto.CryptoPrice.Companion.marketPrice
|
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
|
||||||
import org.testng.annotations.Test
|
import org.testng.annotations.Test
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -44,29 +42,14 @@ class CryptoPricesTest {
|
||||||
@Test
|
@Test
|
||||||
@Throws(ModuleException::class)
|
@Throws(ModuleException::class)
|
||||||
fun testMarketPrice() {
|
fun testMarketPrice() {
|
||||||
var price = marketPrice("BTC", "USD")
|
var price = currentPrice(listOf("BTC"))
|
||||||
assertThat(price.base).describedAs("is BTC").isEqualTo("BTC")
|
assertThat(price.base).describedAs("is BTC").isEqualTo("BTC")
|
||||||
assertThat(price.currency).describedAs("is USD").isEqualTo("USD")
|
assertThat(price.currency).describedAs("is USD").isEqualTo("USD")
|
||||||
assertThat(price.amount).describedAs("BTC > 0").isGreaterThan(0.00)
|
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.base).describedAs("is ETH").isEqualTo("ETH")
|
||||||
assertThat(price.currency).describedAs("is EUR").isEqualTo("EUR")
|
assertThat(price.currency).describedAs("is EUR").isEqualTo("EUR")
|
||||||
assertThat(price.amount).describedAs("ETH > 0").isGreaterThan(0.00)
|
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)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
#Generated by the Semver Plugin for Gradle
|
#Generated by the Semver Plugin for Gradle
|
||||||
#Tue May 11 00:47:17 PDT 2021
|
#Tue May 11 01:30:42 PDT 2021
|
||||||
version.buildmeta=722
|
version.buildmeta=726
|
||||||
version.major=0
|
version.major=0
|
||||||
version.minor=8
|
version.minor=8
|
||||||
version.patch=0
|
version.patch=0
|
||||||
version.prerelease=beta
|
version.prerelease=beta
|
||||||
version.project=mobibot
|
version.project=mobibot
|
||||||
version.semver=0.8.0-beta+722
|
version.semver=0.8.0-beta+726
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue