Commit cc4e999a authored by illo's avatar illo
Browse files

code coverage and linting

parent 5533f4a2
Pipeline #233479308 passed with stage
in 18 seconds
......@@ -9,3 +9,4 @@
^cran-comments\.md$
^\.github$
^\.gitlab-ci\.yml
^_build.R
Package: mongopipe
Title: Translate R code into Mongo aggregation pipelines
Title: Translate R Code into MongoDB Aggregation Pipelines
Version: 0.0.0.9000
Authors@R:
person(given = "Oliver",
family = "Haag",
role = c("aut", "cre"),
email = "oliver_haag@e.mail.de")
Description: Translate R code into Mongo aggregation pipelines.
URL: https://gitlab.com/rpkgs/mongopipe
Description: Translate R code into MongoDB aggregation pipelines.
License: MIT + file LICENSE
Suggests:
testthat (>= 3.0.0),
......@@ -17,8 +18,6 @@ Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.1
Remotes:
jeroen/mongolite
Imports:
magrittr,
jsonlite,
......
#' Conditional expression ($cond)
#'
#' Evaluates a boolean expression to return one of the two specified return expressions.
#' Evaluates a boolean expression to return one
#' of the two specified return expressions.
#'
#' @param test Expression which returns a boolean value.
#' @param yes Return this if the test returns true.
......@@ -8,12 +9,14 @@
#'
#' @examples
#' \dontrun{
#' cond <- condition(test = list("$isArray"="$chart"), yes = list("$size"="$chart"), no = 0)
#' cond <- condition(test = list("$isArray"="$chart"),
#' yes = list("$size"="$chart"),
#' no = 0)
#' jsonlite::toJSON(cond)
#' }
#'
#' @export
condition <- function(test, yes, no) {
query <- check_query(list("if"=test, "then"=yes, "else"=no))
structure(list("$cond" = query), class=c("mongo_operator", "list"))
list("$cond" = query)
}
#' Pipeline Stages
#'
#' These functions translate R code to json readably by mongodb.
#' These functions translate R code to json readably by Mongodb.
#'
#' @param x Object of class mongo
#' @param x Object of class `mongo`
#' @param ... list object
#' @export
match <- function(x, ...) {
......@@ -40,8 +40,19 @@ lookup <-function(x, ...) {
}
#' @rdname match
#' @export
lookup.mongopipe <- function(x, from, local_field=from, foreign_field="_id", as=from, ...) {
add_stage(x, "$lookup", list(from=from, localField=local_field, foreignField=foreign_field, as=as))
lookup.mongopipe <- function(x,
from,
local_field=from,
foreign_field="_id",
as=from,
...) {
add_stage(x,
"$lookup",
list(from=from,
localField=local_field,
foreignField=foreign_field,
as=as))
}
#' @rdname match
......
......@@ -42,10 +42,11 @@ library(nycflights13)
m_flights <- mongo("test_flights", verbose = FALSE)
m_airports <- mongo("test_airports", verbose = FALSE)
m_flights$drop()
m_flights$insert(flights)
m_airports$drop()
m_airports$insert(airports)
if(!m_flights$count())
m_flights$insert(flights)
if(!m_airports$count())
m_airports$insert(airports)
result <- mongopipe() %>%
match(faa="ABQ") %>%
......
# check package
devtools::check()
# update docs
pkgdown::build_site()
# check for cran
# rhub::check_for_cran() not working because of mongodb
# Check for win-builder
devtools::check_win_devel()
# Checking for good practice
goodpractice::gp()
......@@ -14,11 +14,14 @@ condition(test, yes, no)
\item{no}{Return this if the test returns false.}
}
\description{
Evaluates a boolean expression to return one of the two specified return expressions.
Evaluates a boolean expression to return one
of the two specified return expressions.
}
\examples{
\dontrun{
cond <- condition(test = list("$isArray"="$chart"), yes = list("$size"="$chart"), no = 0)
cond <- condition(test = list("$isArray"="$chart"),
yes = list("$size"="$chart"),
no = 0)
jsonlite::toJSON(cond)
}
......
......@@ -40,7 +40,7 @@ project(x, ...)
\method{project}{mongopipe}(x, ...)
}
\arguments{
\item{x}{Object of class mongo}
\item{x}{Object of class \code{mongo}}
\item{...}{list object}
......@@ -57,5 +57,5 @@ project(x, ...)
\item{limit}{Integer to limit the number of documents.}
}
\description{
These functions translate R code to json readably by mongodb.
These functions translate R code to json readably by Mongodb.
}
......@@ -4,9 +4,16 @@
\name{mongopipe-package}
\alias{mongopipe-package}
\alias{_PACKAGE}
\title{mongopipe: Translate R code into Mongo aggregation pipelines}
\title{mongopipe: Translate R Code into MongoDB Aggregation Pipelines}
\description{
Translate R code into Mongo aggregation pipelines.
Translate R code into MongoDB aggregation pipelines.
}
\seealso{
Useful links:
\itemize{
\item \url{https://gitlab.com/rpkgs/mongopipe}
}
}
\author{
\strong{Maintainer}: Oliver Haag \email{oliver_haag@e.mail.de}
......
......@@ -4,15 +4,18 @@ library(nycflights13)
m_flights <- mongo("test_flights", verbose = FALSE)
m_airports <- mongo("test_airports", verbose = FALSE)
m_flights$drop()
m_flights$insert(flights)
m_airports$drop()
m_airports$insert(airports)
if(!m_flights$count())
m_flights$insert(flights)
if(!m_airports$count())
m_airports$insert(airports)
test_that("check_query", {
expect_error(check_query(list(1)), "all elements in query should have names!")
expect_error(check_query(list(x=list(1,2,3))), "all elements in query should have names!")
expect_error(check_query(list(1)),
"all elements in query should have names!")
expect_error(check_query(list(x=list(1,2,3))),
"all elements in query should have names!")
expect_error(check_query(list(x=2)), NA)
expect_error(check_query(list(x=2:5)), NA)
......@@ -51,7 +54,9 @@ test_that("pipe", {
result <- mongopipe() %>%
match(faa="ABQ") %>%
lookup(from = "test_flights", local_field = "faa", foreign_field = "dest") %>%
lookup(from = "test_flights",
local_field = "faa",
foreign_field = "dest") %>%
unwind(field = "test_flights") %>%
m_airports$aggregate()
......@@ -59,7 +64,9 @@ test_that("pipe", {
result2 <- mongopipe() %>%
match(tzone="America/New_York") %>%
lookup(from = "test_flights", local_field = "faa", foreign_field = "dest") %>%
lookup(from = "test_flights",
local_field = "faa",
foreign_field = "dest") %>%
field(min_distance = list("$min"="$test_flights.distance")) %>%
match(min_distance=list("$ne"=NA)) %>%
project("min_distance"=1, "faa"=1) %>%
......
test_that("condition", {
cond <- condition(list("$gte"='[ "$qty", 250 ]'), 30, 20)
expect_equal(as.character(toJSON(cond)),
'{"$cond":{"if":{"$gte":"[ \\\"$qty\\\", 250 ]"},"then":30,"else":20}}')
})
test_that("add_stage", {
expect_error(add_stage(mongopipe()), "type")
expect_error(add_stage(mongopipe(), "$match"), "...")
})
test_that("check_query", {
expect_s3_class(check_query(1), c("scalar", "numeric"))
})
test_that("print.mongopipe", {
expect_output(print(mongopipe()), "\\[\\n\\n\\]")
})
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment