Fixed country code lookup and added more tests.
This commit is contained in:
parent
d05499a76f
commit
1d15d669b1
2 changed files with 70 additions and 19 deletions
|
@ -84,21 +84,28 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
|
|
||||||
// Weather command
|
// Weather command
|
||||||
private const val WEATHER_CMD = "weather"
|
private const val WEATHER_CMD = "weather"
|
||||||
private fun getCountry(countryCode: String): Country {
|
|
||||||
|
/**
|
||||||
|
* Converts and rounds temperature from °F to °C.
|
||||||
|
*/
|
||||||
|
fun ftoC(d: Double?): Pair<Int, Int> {
|
||||||
|
@Suppress("MagicNumber")
|
||||||
|
val c = (d!! - 32) * 5 / 9
|
||||||
|
return d.roundToInt() to c.roundToInt()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a country based on its country code. Defaults to [Country.UNITED_STATES] if not found.
|
||||||
|
*/
|
||||||
|
fun getCountry(countryCode: String): Country {
|
||||||
for (c in Country.values()) {
|
for (c in Country.values()) {
|
||||||
if (c.name.equals(countryCode, ignoreCase = true)) {
|
if (c.value.equals(countryCode, ignoreCase = true)) {
|
||||||
return c
|
return c
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Country.UNITED_STATES
|
return Country.UNITED_STATES
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getTemps(d: Double?): String {
|
|
||||||
@Suppress("MagicNumber")
|
|
||||||
val c = (d!! - 32) * 5 / 9
|
|
||||||
return "${d.roundToInt()} °F, ${c.roundToInt()} °C"
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the weather data.
|
* Retrieves the weather data.
|
||||||
*/
|
*/
|
||||||
|
@ -131,7 +138,8 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
with(cwd.mainData) {
|
with(cwd.mainData) {
|
||||||
if (this != null) {
|
if (this != null) {
|
||||||
if (hasTemp()) {
|
if (hasTemp()) {
|
||||||
messages.add(PublicMessage("Temperature: ${getTemps(temp)}"))
|
val t = ftoC(temp)
|
||||||
|
messages.add(PublicMessage("Temperature: ${t.first}°F, ${t.second}°C"))
|
||||||
}
|
}
|
||||||
if (hasHumidity() && humidity != null) {
|
if (hasHumidity() && humidity != null) {
|
||||||
messages.add(NoticeMessage("Humidity: ${(humidity!!).roundToInt()}%"))
|
messages.add(NoticeMessage("Humidity: ${(humidity!!).roundToInt()}%"))
|
||||||
|
@ -141,7 +149,8 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
if (cwd.hasWindData()) {
|
if (cwd.hasWindData()) {
|
||||||
with(cwd.windData) {
|
with(cwd.windData) {
|
||||||
if (this != null && hasSpeed() && speed != null) {
|
if (this != null && hasSpeed() && speed != null) {
|
||||||
messages.add(NoticeMessage("Wind: ${wind(speed!!)}"))
|
val w = mphToKmh(speed!!)
|
||||||
|
messages.add(NoticeMessage("Wind: ${w.first} mph, ${w.second} km/h"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -176,7 +185,7 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: APIException) {
|
} catch (e: APIException) {
|
||||||
throw ModuleException("getWeather($query)", "A weather API error has occurred.", e)
|
throw ModuleException("getWeather($query)", "A weather API error has occurred: ${e.message}", e)
|
||||||
} catch (e: NullPointerException) {
|
} catch (e: NullPointerException) {
|
||||||
throw ModuleException("getWeather($query)", "Unable to perform weather lookup.", e)
|
throw ModuleException("getWeather($query)", "Unable to perform weather lookup.", e)
|
||||||
}
|
}
|
||||||
|
@ -188,10 +197,13 @@ class Weather2(bot: Mobibot) : ThreadedModule(bot) {
|
||||||
return messages
|
return messages
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun wind(w: Double): String {
|
/**
|
||||||
|
* Converts and rounds temperature from mph to km/h.
|
||||||
|
*/
|
||||||
|
fun mphToKmh(w: Double): Pair<Int, Int> {
|
||||||
@Suppress("MagicNumber")
|
@Suppress("MagicNumber")
|
||||||
val kmh = w * 1.60934
|
val kmh = w * 1.60934
|
||||||
return "${w.roundToInt()} mph, ${kmh.roundToInt()} km/h"
|
return w.roundToInt() to kmh.roundToInt()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -32,29 +32,68 @@
|
||||||
package net.thauvin.erik.mobibot.modules
|
package net.thauvin.erik.mobibot.modules
|
||||||
|
|
||||||
import net.aksingh.owmjapis.api.APIException
|
import net.aksingh.owmjapis.api.APIException
|
||||||
|
import net.aksingh.owmjapis.core.OWM
|
||||||
import net.thauvin.erik.mobibot.LocalProperties
|
import net.thauvin.erik.mobibot.LocalProperties
|
||||||
|
import net.thauvin.erik.mobibot.modules.Weather2.Companion.OWM_API_KEY_PROP
|
||||||
|
import net.thauvin.erik.mobibot.modules.Weather2.Companion.ftoC
|
||||||
|
import net.thauvin.erik.mobibot.modules.Weather2.Companion.getCountry
|
||||||
import net.thauvin.erik.mobibot.modules.Weather2.Companion.getWeather
|
import net.thauvin.erik.mobibot.modules.Weather2.Companion.getWeather
|
||||||
|
import net.thauvin.erik.mobibot.modules.Weather2.Companion.mphToKmh
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.assertj.core.api.Assertions.assertThatThrownBy
|
import org.assertj.core.api.Assertions.assertThatThrownBy
|
||||||
import org.testng.annotations.Test
|
import org.testng.annotations.Test
|
||||||
|
import kotlin.random.Random
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The `Weather2Test` class.
|
* The `Weather2Test` class.
|
||||||
*/
|
*/
|
||||||
class Weather2Test : LocalProperties() {
|
class Weather2Test : LocalProperties() {
|
||||||
|
@Test
|
||||||
|
fun testFtoC() {
|
||||||
|
val t = ftoC(32.0)
|
||||||
|
assertThat(t.second).describedAs("32 °F is 0 °C").isEqualTo(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testGetCountry() {
|
||||||
|
assertThat(getCountry("foo")).describedAs("not a country").isEqualTo(OWM.Country.UNITED_STATES)
|
||||||
|
assertThat(getCountry("fr")).describedAs("fr is france").isEqualTo(OWM.Country.FRANCE)
|
||||||
|
|
||||||
|
val country = OWM.Country.values()
|
||||||
|
repeat(3) {
|
||||||
|
val rand = country[Random.nextInt(0, country.size - 1)]
|
||||||
|
assertThat(getCountry(rand.value)).describedAs(rand.name).isEqualTo(rand)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testMphToKmh() {
|
||||||
|
val w = mphToKmh(0.62)
|
||||||
|
assertThat(w.second).describedAs("0.62 mph is 1 km/h").isEqualTo(1)
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Throws(ModuleException::class)
|
@Throws(ModuleException::class)
|
||||||
fun testWeather() {
|
fun testWeather() {
|
||||||
var messages = getWeather("98204", getProperty(Weather2.OWM_API_KEY_PROP))
|
var messages = getWeather("98204", getProperty(OWM_API_KEY_PROP))
|
||||||
assertThat(messages[0].msg).describedAs("is Everett").contains("Everett").contains("US")
|
assertThat(messages[0].msg).describedAs("is Everett").contains("Everett").contains("US")
|
||||||
assertThat(messages[messages.size - 1].msg).describedAs("is City Search").endsWith("98204%2CUS")
|
assertThat(messages[messages.size - 1].msg).describedAs("is Everett zip code").endsWith("98204%2CUS")
|
||||||
messages = getWeather("London, UK", getProperty(Weather2.OWM_API_KEY_PROP))
|
|
||||||
|
messages = getWeather("San Francisco", getProperty(OWM_API_KEY_PROP))
|
||||||
|
assertThat(messages[0].msg).describedAs("is San Francisco").contains("San Francisco").contains("US")
|
||||||
|
assertThat(messages[messages.size - 1].msg).describedAs("is San Fran city code").endsWith("5391959")
|
||||||
|
|
||||||
|
messages = getWeather("London, UK", getProperty(OWM_API_KEY_PROP))
|
||||||
assertThat(messages[0].msg).describedAs("is UK").contains("London").contains("UK")
|
assertThat(messages[0].msg).describedAs("is UK").contains("London").contains("UK")
|
||||||
assertThat(messages[messages.size - 1].msg).describedAs("is City Code").endsWith("4517009")
|
assertThat(messages[messages.size - 1].msg).describedAs("is London city code").endsWith("4517009")
|
||||||
assertThatThrownBy { getWeather("Montpellier, FR", getProperty(Weather2.OWM_API_KEY_PROP)) }
|
|
||||||
.describedAs("Montpellier not found").hasCauseInstanceOf(APIException::class.java)
|
assertThatThrownBy { getWeather("Foo, US", getProperty(OWM_API_KEY_PROP)) }
|
||||||
|
.describedAs("foo not found").hasCauseInstanceOf(APIException::class.java)
|
||||||
assertThatThrownBy { getWeather("test", "") }
|
assertThatThrownBy { getWeather("test", "") }
|
||||||
.describedAs("no API key").isInstanceOf(ModuleException::class.java).hasNoCause()
|
.describedAs("no API key").isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||||
|
assertThatThrownBy { getWeather("test", null) }
|
||||||
|
.describedAs("null API key").isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||||
|
|
||||||
messages = getWeather("", "apikey")
|
messages = getWeather("", "apikey")
|
||||||
assertThat(messages[0].isError).describedAs("no query").isTrue
|
assertThat(messages[0].isError).describedAs("no query").isTrue
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue