Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes #15 - provide default sort order if not supplied in URL #109

Merged
merged 3 commits into from
Oct 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Description: Provides easier interaction with
format and manages throttling by 'Socrata'.
Users can upload data to Socrata portals directly
from R.
Version: 1.7.1-15
Date: 2016-10-06
Version: 1.7.1-17
Date: 2016-10-12
Author: Hugh Devlin, Ph. D., Tom Schenk, Jr., and John Malc
Maintainer: "Tom Schenk Jr." <developers@cityofchicago.org>
Depends:
Expand Down
10 changes: 10 additions & 0 deletions R/RSocrata.R
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,16 @@ read.socrata <- function(url, app_token = NULL, email = NULL, password = NULL,
validUrl <- validateUrl(url, app_token) # check url syntax, allow human-readable Socrata url
parsedUrl <- httr::parse_url(validUrl)
mimeType <- mime::guess_type(parsedUrl$path)
if (!is.null(names(parsedUrl$query))) { # check if URL has any queries
## if there is a query, check for $order within the query
orderTest <- any(names(parsedUrl$query) == "$order")
if(!orderTest) # sort by Socrata unique identifier
validUrl <- paste(validUrl, if(is.null(parsedUrl$query)) {'?'} else {"&"}, '$order=:id', sep='')
}
else {
validUrl <- paste(validUrl, {'?'}, '$order=:id', sep='')
parsedUrl <- httr::parse_url(validUrl) # reparse because URL now has a query
}
if(!(mimeType %in% c('text/csv','application/json')))
stop("Error in read.socrata: ", mimeType, " not a supported data format.")
response <- getResponse(validUrl, email, password)
Expand Down
53 changes: 53 additions & 0 deletions tests/testthat/test-all.R
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,59 @@ test_that("read Socrata JSON with missing fields (issue 19)", {
expect_equal(9, ncol(df), label="columns", info = "https://github.com/Chicago/RSocrata/issues/19")
})

test_that("If URL has no queries, insert $order:id into URL", {
## Define and test issue 15
## Ensure that the $order=:id is inserted when no other query parameters are used.
df <- read.socrata("https://data.cityofchicago.org/resource/kn9c-c2s2.json")
expect_equal("21.5", df$percent_aged_under_18_or_over_64[7],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("38", df$percent_aged_under_18_or_over_64[23],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("40.4", df$percent_aged_under_18_or_over_64[36],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("36.1", df$percent_aged_under_18_or_over_64[42],
info = "https://github.com/Chicago/RSocrata/issues/15")

})

test_that("If URL has an $order clause, do not insert ?$order:id into URL", {
## Define and test issue 15
## Ensure that $order=:id is not used when other $order parameters are requested by the user.
df <- read.socrata("https://data.cityofchicago.org/resource/kn9c-c2s2.json?$order=hardship_index")
expect_equal("35.3", df$percent_aged_under_18_or_over_64[7],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("37.6", df$percent_aged_under_18_or_over_64[23],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("38.5", df$percent_aged_under_18_or_over_64[36],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("32", df$percent_aged_under_18_or_over_64[42],
info = "https://github.com/Chicago/RSocrata/issues/15")
})

test_that("If URL has only non-order query parameters, insert $order:id into URL", {
## Define and test issue 15
## Ensure that $order=:id is inserted when other (non-$order) arguments are used.
df <- read.socrata("https://data.cityofchicago.org/resource/kn9c-c2s2.json?$limit=50")
expect_equal("21.5", df$percent_aged_under_18_or_over_64[7],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("38", df$percent_aged_under_18_or_over_64[23],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("40.4", df$percent_aged_under_18_or_over_64[36],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("36.1", df$percent_aged_under_18_or_over_64[42],
info = "https://github.com/Chicago/RSocrata/issues/15")
df <- read.socrata("https://data.cityofchicago.org/resource/kn9c-c2s2.json?$where=hardship_index>20")
expect_equal("34", df$percent_aged_under_18_or_over_64[7],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("30.7", df$percent_aged_under_18_or_over_64[23],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("41.2", df$percent_aged_under_18_or_over_64[36],
info = "https://github.com/Chicago/RSocrata/issues/15")
expect_equal("42.9", df$percent_aged_under_18_or_over_64[42],
info = "https://github.com/Chicago/RSocrata/issues/15")
})


context("Checks the validity of 4x4")

test_that("is 4x4", {
Expand Down