Skip to content

Commit 66e61ef

Browse files
committed
add tests, fix bugs
1 parent 19e45cd commit 66e61ef

File tree

3 files changed

+132
-3
lines changed

3 files changed

+132
-3
lines changed

R/connect.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -824,16 +824,16 @@ Connect <- R6::R6Class(
824824
#' @param to Optional `Date` or `POSIXt`; end of the time window. If a
825825
#' `Date`, coerced to `YYYY-MM-DDT23:59:59` in the caller's time zone.
826826
inst_content_hits = function(from = NULL, to = NULL) {
827-
error_if_less_than(client$version, "2025.04.0")
827+
error_if_less_than(self$version, "2025.04.0")
828828

829829
# If this is called with date objects with no timestamp attached, it's
830830
# reasonable to assume that the caller is indicating the days as an
831831
# inclusive range.
832832
if (inherits(from, "Date")) {
833-
from <- as.POSIXct(paste(from, "00:00:00"), tz = "")
833+
from <- as.POSIXct(paste(from, "00:00:00"))
834834
}
835835
if (inherits(to, "Date")) {
836-
to <- as.POSIXct(paste(to, "23:59:59"), tz = "")
836+
to <- as.POSIXct(paste(to, "23:59:59"))
837837
}
838838

839839
self$GET(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
[
2+
{
3+
"id": 8966707,
4+
"user_guid": null,
5+
"content_guid": "475618c9",
6+
"timestamp": "2025-04-30T12:49:16.269904Z",
7+
"data": {
8+
"path": "/hello",
9+
"user_agent": "Datadog/Synthetics"
10+
}
11+
},
12+
{
13+
"id": 8966708,
14+
"user_guid": null,
15+
"content_guid": "475618c9",
16+
"timestamp": "2025-04-30T12:49:17.002848Z",
17+
"data": {
18+
"path": "/world",
19+
"user_agent": null
20+
}
21+
},
22+
{
23+
"id": 8967206,
24+
"user_guid": null,
25+
"content_guid": "475618c9",
26+
"timestamp": "2025-04-30T13:01:47.40738Z",
27+
"data": {
28+
"path": "/chinchilla",
29+
"user_agent": "Datadog/Synthetics"
30+
}
31+
},
32+
{
33+
"id": 8967210,
34+
"user_guid": null,
35+
"content_guid": "475618c9",
36+
"timestamp": "2025-04-30T13:04:13.176791Z",
37+
"data": {
38+
"path": "/lava-lamp",
39+
"user_agent": "Datadog/Synthetics"
40+
}
41+
},
42+
{
43+
"id": 8966214,
44+
"user_guid": "fecbd383",
45+
"content_guid": "b0eaf295",
46+
"timestamp": "2025-04-30T12:36:13.818466Z",
47+
"data": {
48+
"path": null,
49+
"user_agent": null
50+
}
51+
}
52+
]

tests/testthat/test-get.R

+77
Original file line numberDiff line numberDiff line change
@@ -374,3 +374,80 @@ test_that("get_content only requests vanity URLs for Connect 2024.06.0 and up",
374374
)
375375
})
376376
})
377+
378+
test_that("get_usage() returns usage data in the expected shape", {
379+
with_mock_dir("2025.04.0", {
380+
client <- connect(server = "https://connect.example", api_key = "fake")
381+
usage <- get_usage(
382+
client,
383+
from = as.POSIXct("2025-04-01 00:00:01", tz = "UTC")
384+
)
385+
386+
expect_equal(
387+
usage,
388+
tibble::tibble(
389+
id = c(8966707L, 8966708L, 8967206L, 8967210L, 8966214L),
390+
user_guid = c(NA, NA, NA, NA, "fecbd383"),
391+
content_guid = c(
392+
"475618c9",
393+
"475618c9",
394+
"475618c9",
395+
"475618c9",
396+
"b0eaf295"
397+
),
398+
timestamp = c(
399+
parse_connect_rfc3339("2025-04-30T12:49:16.269904Z"),
400+
parse_connect_rfc3339("2025-04-30T12:49:17.002848Z"),
401+
parse_connect_rfc3339("2025-04-30T13:01:47.40738Z"),
402+
parse_connect_rfc3339("2025-04-30T13:04:13.176791Z"),
403+
parse_connect_rfc3339("2025-04-30T12:36:13.818466Z")
404+
),
405+
path = c("/hello", "/world", "/chinchilla", "/lava-lamp", NA),
406+
user_agent = c(
407+
"Datadog/Synthetics",
408+
NA,
409+
"Datadog/Synthetics",
410+
"Datadog/Synthetics",
411+
NA
412+
)
413+
)
414+
)
415+
})
416+
})
417+
418+
test_that("Metrics firehose is called with expected parameters", {
419+
with_mock_api({
420+
client <- Connect$new(server = "https://connect.example", api_key = "fake")
421+
# $version is loaded lazily, we need it before calling get_usage()
422+
client$version
423+
424+
without_internet({
425+
expect_GET(
426+
get_usage(client),
427+
"https://connect.example/__api__/v1/instrumentation/content/hits"
428+
)
429+
expect_GET(
430+
get_usage(
431+
client,
432+
from = as.POSIXct("2025-04-01 00:00:01", tz = "UTC"),
433+
to = as.POSIXct("2025-04-02 00:00:01", tz = "UTC")
434+
),
435+
"https://connect.example/__api__/v1/instrumentation/content/hits?from=2025-04-01T00%3A00%3A01Z&to=2025-04-02T00%3A00%3A01Z"
436+
)
437+
438+
# Dates are converted to timestamps with the system's time zone, so for
439+
# repeatability we're gonna set it here.
440+
441+
withr::local_envvar(TZ = "UTC")
442+
443+
expect_GET(
444+
get_usage(
445+
client,
446+
from = as.Date("2025-04-01"),
447+
to = as.Date("2025-04-02")
448+
),
449+
"https://connect.example/__api__/v1/instrumentation/content/hits?from=2025-04-01T00%3A00%3A00Z&to=2025-04-02T23%3A59%3A59Z"
450+
)
451+
})
452+
})
453+
})

0 commit comments

Comments
 (0)