Skip Google search tests with CI to preserve daily rate limit
This commit is contained in:
parent
40ee593c3b
commit
7581c26d59
8 changed files with 79 additions and 46 deletions
|
@ -34,10 +34,11 @@ package net.thauvin.erik.mobibot.commands.seen
|
|||
|
||||
import java.io.Serializable
|
||||
|
||||
class NickComparator: Comparator<String>, Serializable {
|
||||
class NickComparator : Comparator<String>, Serializable {
|
||||
override fun compare(a: String, b: String): Int {
|
||||
return a.lowercase().compareTo(b.lowercase())
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val serialVersionUID = 1L
|
||||
}
|
||||
|
|
|
@ -31,6 +31,7 @@
|
|||
*/
|
||||
package net.thauvin.erik.mobibot.modules
|
||||
|
||||
import net.thauvin.erik.mobibot.ReleaseInfo
|
||||
import net.thauvin.erik.mobibot.Utils.capitalise
|
||||
import net.thauvin.erik.mobibot.Utils.encodeUrl
|
||||
import net.thauvin.erik.mobibot.Utils.helpFormat
|
||||
|
@ -64,8 +65,10 @@ class GoogleSearch : ThreadedModule() {
|
|||
if (args.isNotBlank()) {
|
||||
try {
|
||||
val results = searchGoogle(
|
||||
args, properties[GOOGLE_API_KEY_PROP],
|
||||
properties[GOOGLE_CSE_KEY_PROP]
|
||||
args,
|
||||
properties[GOOGLE_API_KEY_PROP],
|
||||
properties[GOOGLE_CSE_KEY_PROP],
|
||||
event.user.nick
|
||||
)
|
||||
for (msg in results) {
|
||||
event.sendMessage(channel, msg)
|
||||
|
@ -96,7 +99,12 @@ class GoogleSearch : ThreadedModule() {
|
|||
*/
|
||||
@JvmStatic
|
||||
@Throws(ModuleException::class)
|
||||
fun searchGoogle(query: String, apiKey: String?, cseKey: String?): List<Message> {
|
||||
fun searchGoogle(
|
||||
query: String,
|
||||
apiKey: String?,
|
||||
cseKey: String?,
|
||||
quotaUser: String = ReleaseInfo.PROJECT
|
||||
): List<Message> {
|
||||
if (apiKey.isNullOrBlank() || cseKey.isNullOrBlank()) {
|
||||
throw ModuleException(
|
||||
"${GoogleSearch::class.java.name} is disabled.",
|
||||
|
@ -108,7 +116,7 @@ class GoogleSearch : ThreadedModule() {
|
|||
try {
|
||||
val url = URL(
|
||||
"https://www.googleapis.com/customsearch/v1?key=$apiKey&cx=$cseKey" +
|
||||
"&q=${query.encodeUrl()}&filter=1&num=5&alt=json"
|
||||
""aUser=${quotaUser}&q=${query.encodeUrl()}&filter=1&num=5&alt=json"
|
||||
)
|
||||
val json = JSONObject(url.reader())
|
||||
if (json.has("items")) {
|
||||
|
|
|
@ -52,35 +52,35 @@ import org.testng.annotations.Test
|
|||
*/
|
||||
class GoogleSearchTest : LocalProperties() {
|
||||
@Test
|
||||
fun testAPIKeys() {
|
||||
assertThat(
|
||||
searchGoogle("", "apikey", "cssKey").first(),
|
||||
"empty query"
|
||||
).isInstanceOf(ErrorMessage::class.java)
|
||||
|
||||
assertThat { searchGoogle("test", "", "apiKey") }.isFailure()
|
||||
.isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||
|
||||
assertThat { searchGoogle("test", "apiKey", "") }.isFailure()
|
||||
.isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||
}
|
||||
|
||||
@Test(groups = ["no-ci"])
|
||||
@Throws(ModuleException::class)
|
||||
fun testSearchGoogle() {
|
||||
val apiKey = getProperty(GoogleSearch.GOOGLE_API_KEY_PROP)
|
||||
val cseKey = getProperty(GoogleSearch.GOOGLE_CSE_KEY_PROP)
|
||||
try {
|
||||
var messages = searchGoogle("mobitopia", apiKey, cseKey)
|
||||
assertThat(messages, "mobitopia results not empty").isNotEmpty()
|
||||
assertThat(messages[0].msg, "found mobibtopia").contains("mobitopia", true)
|
||||
|
||||
messages = searchGoogle("aapl", apiKey, cseKey)
|
||||
assertThat(messages, "aapl results not empty").isNotEmpty()
|
||||
assertThat(messages[0].msg, "found apple").contains("apple", true)
|
||||
try {
|
||||
var messages = searchGoogle("mobibot", apiKey, cseKey)
|
||||
assertThat(messages, "mobibot results not empty").isNotEmpty()
|
||||
assertThat(messages[0].msg, "found mobibot").contains("mobibot", true)
|
||||
|
||||
messages = searchGoogle("adadflkjl", apiKey, cseKey)
|
||||
assertThat(messages[0], "not found").all {
|
||||
isInstanceOf(ErrorMessage::class.java)
|
||||
prop(Message::msg).isEqualTo("No results found.")
|
||||
}
|
||||
|
||||
assertThat(
|
||||
searchGoogle("", "apikey", "cssKey").first(),
|
||||
"empty query"
|
||||
).isInstanceOf(ErrorMessage::class.java)
|
||||
|
||||
assertThat { searchGoogle("test", "", "apiKey") }.isFailure()
|
||||
.isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||
|
||||
assertThat { searchGoogle("test", "apiKey", "") }.isFailure()
|
||||
.isInstanceOf(ModuleException::class.java).hasNoCause()
|
||||
} catch (e: ModuleException) {
|
||||
// Avoid displaying api keys in CI logs
|
||||
if ("true" == System.getenv("CI")) {
|
||||
|
|
14
src/test/resources/testng-ci.xml
Normal file
14
src/test/resources/testng-ci.xml
Normal file
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
|
||||
<suite name="testng-ci">
|
||||
<test name="ci">
|
||||
<groups>
|
||||
<run>
|
||||
<exclude name="no-ci"/>
|
||||
</run>
|
||||
</groups>
|
||||
<packages>
|
||||
<package name="net.thauvin.erik.mobibot.*"/>
|
||||
</packages>
|
||||
</test>
|
||||
</suite>
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd" >
|
||||
<suite name="tests">
|
||||
<test name="full">
|
||||
<suite name="testng">
|
||||
<test name="all">
|
||||
<packages>
|
||||
<package name="net.thauvin.erik.mobibot.*"/>
|
||||
</packages>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue