Skip to content

Commit

Permalink
Changed hostname in integration tests to deal with weirdness in resol…
Browse files Browse the repository at this point in the history
…ving localhost on Travis
  • Loading branch information
jameslamb committed Jun 2, 2018
1 parent 9fe9a22 commit 6081b87
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 23 deletions.
8 changes: 7 additions & 1 deletion R/elasticsearch_eda_funs.R
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,14 @@ get_fields <- function(es_host
#' @importFrom data.table data.table
#' @importFrom utils read.table
.process_alias <- function(alias_string) {

# process the string provided by the /_cat/aliases API into a data.frame and then a data.table
aliasDT <- data.table::data.table(utils::read.table(text = alias_string, stringsAsFactors = FALSE))
aliasDT <- data.table::data.table(
utils::read.table(
text = alias_string
, stringsAsFactors = FALSE
)
)

# return only the first two columns
return(aliasDT[, .(alias = V1, index = V2)])
Expand Down
1 change: 1 addition & 0 deletions R/es_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,7 @@ es_search <- function(es_host
, url = reqURL
, body = query_body
)
print(httr::content(result, as = "text"))
httr::stop_for_status(result)
result <- httr::content(result, as = "text")

Expand Down
4 changes: 3 additions & 1 deletion R/get_fields.R
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ get_fields <- function(es_host
httr::stop_for_status(result)
resultContent <- httr::content(result, as = 'text')

if (is.null(resultContent)) {
# NOTE: with ES6, this results in an empty string instead of a NULL
if (is.null(resultContent) || identical(resultContent, "")) {
# there are no aliases in this Elasticsearch cluster
return(invisible(NULL))
} else {
Expand All @@ -133,6 +134,7 @@ get_fields <- function(es_host
#' @importFrom data.table data.table
#' @importFrom utils read.table
.process_alias <- function(alias_string) {

# process the string provided by the /_cat/aliases API into a data.frame and then a data.table
aliasDT <- data.table::data.table(
utils::read.table(
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: '3'
services:
es_6:
image: "docker.elastic.co/elasticsearch/elasticsearch:6.2.0"
image: "docker.elastic.co/elasticsearch/elasticsearch:5.5.0"
environment:
- "discovery.type=single-node"
#- "discovery.type=single-node"
- "xpack.security.enabled=false"
ports:
- "9200:9200"
2 changes: 1 addition & 1 deletion setup_es.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ curl --silent \
curl -X POST "http://${ES_HOST}:9200/_refresh"

# Check that we got something
curl -X GET "http://${ES_HOST}/shakespeare/_search?size=1"
curl -X GET "http://${ES_HOST}:9200/shakespeare/_search?size=1"
26 changes: 26 additions & 0 deletions tests/testthat/test-es_search.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ test_that("es_search should reject NULL index", {
}, regexp = "es_index is not a string")
})

# Should reject bad queries
test_that("es_search should reject malformed queries", {
# Length greater than 1
expect_error({
es_search(
es_host = "http://mycompany.com:9200"
, es_index = "_all"
, query = c(
'{"_source": {"include": ["stuff.*"]},'
, '{"aggs": {"superman": {"terms": {"field": "hi"}}}}}'
)
)
}, regexp = "You gave an object of length 2")

# Specified as a list (like you might get from jsonlite::fromJSON)
expect_error({
es_search(
es_host = "http://mycompany.com:9200"
, es_index = "_all"
, query = list(
'{"_source": {"include": ["stuff.*"]},{"aggs": {"superman": {"terms": {"field": "hi"}}}}}'
)
)
}, regexp = "query_body should be a single string")
})

#---- .ConvertToSec

# .ConvertToSec should work for seconds
Expand Down
36 changes: 18 additions & 18 deletions tests/testthat/test-integration.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ futile.logger::flog.threshold(0)
testthat::skip_on_cran()

outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 48
, size = 48
, max_hits = 100
, size = 100
)

expect_true(data.table::is.data.table(outDT))
Expand All @@ -36,35 +36,35 @@ futile.logger::flog.threshold(0)
testthat::skip_on_cran()

outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 50
, max_hits = 30
, size = 2
)
expect_true(data.table::is.data.table(outDT))
expect_true(nrow(outDT) == 48)
expect_true(nrow(outDT) == 30)
})

test_that("es_search works in single-threaded mode", {
testthat::skip_on_cran()

outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 50
, max_hits = 30
, size = 2
, n_cores = 1
)
expect_true(data.table::is.data.table(outDT))
expect_true(nrow(outDT) == 48)
expect_true(nrow(outDT) == 30)
})

test_that("es_search rejects scrolls longer than 1 hour", {
testthat::skip_on_cran()

expect_error({
outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 100
, size = 100
Expand All @@ -79,7 +79,7 @@ futile.logger::flog.threshold(0)

expect_warning({
outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 9999
)
Expand All @@ -93,7 +93,7 @@ futile.logger::flog.threshold(0)

expect_warning({
outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 12
, size = 7
Expand All @@ -110,7 +110,7 @@ futile.logger::flog.threshold(0)
# and I want to avoid exposing our tests to changes in the query DSL
expect_warning({
outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "empty_index"
)
}, regexp = "Query is syntactically valid but 0 documents were matched")
Expand All @@ -122,14 +122,14 @@ futile.logger::flog.threshold(0)
testthat::skip_on_cran()

outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 100
, query = '{"aggs": {"thing": {"terms": {"field": "speaker", "size": 12}}}}'
)

expect_true(data.table::is.data.table(outDT))
expect_true(nrow(outDT) == 3)
expect_true(nrow(outDT) == 12)
expect_named(
outDT
, c("thing", "doc_count")
Expand All @@ -145,7 +145,7 @@ futile.logger::flog.threshold(0)
testthat::skip_on_cran()

outDT <- es_search(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_index = "shakespeare"
, max_hits = 100
, query = '{"aggs": {"name_i_picked": {"terms": {"field": "speaker", "size": 12}}}}'
Expand All @@ -161,7 +161,7 @@ futile.logger::flog.threshold(0)

# ther stuff we might as well test
expect_true(data.table::is.data.table(outDT))
expect_true(nrow(outDT) == 3)
expect_true(nrow(outDT) == 12)
expect_true(is.numeric(outDT[, doc_count]))
expect_true(is.character(outDT[, name_i_picked]))
expect_true(all(outDT[, doc_count > 0]))
Expand All @@ -173,7 +173,7 @@ futile.logger::flog.threshold(0)
testthat::skip_on_cran()

fieldDT <- get_fields(
es_host = "http://localhost:9200"
es_host = "http://127.0.0.1:9200"
, es_indices = "_all"
)
expect_true(data.table::is.data.table(fieldDT))
Expand Down

0 comments on commit 6081b87

Please sign in to comment.