Skip to content

Commit

Permalink
Return NULL when a record's related field is empty (#28)
Browse files Browse the repository at this point in the history
* Add a simple unit test which queries laminlabs/lamindata

* update changelog

* Return `NULL` when a record's related field is empty

* update changelog

* add checks to test
  • Loading branch information
rcannood authored Oct 11, 2024
1 parent 48b2331 commit b59c996
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

* Fixed the parsing of the env files in `~/.lamin` due to changes in the lamindb-setup Python package (PR #12).

* Return `NULL` when a record's related field is empty (PR #28).

# laminr v0.0.1

Initial POC implementation of the LaminDB API client for R.
Expand Down
9 changes: 8 additions & 1 deletion R/Record.R
Original file line number Diff line number Diff line change
Expand Up @@ -163,18 +163,25 @@ Record <- R6::R6Class( # nolint object_name_linter
} else if (key %in% private$.registry$get_field_names()) {
field <- private$.registry$get_field(key)

## TODO: use related_registry_class$get_records instead
# refetch the record to get the related data
related_data <- private$.api$get_record(
module_name = field$module_name,
registry_name = field$registry_name,
id_or_uid = private$.data[["uid"]],
select = key
)[[key]]

# return NULL if the related data is NULL
if (is.null(related_data)) {
return(NULL)
}

# if the related data is not NULL, create a record class for it
related_module <- private$.instance$get_module(field$related_module_name)
related_registry <- related_module$get_registry(field$related_registry_name)
related_registry_class <- related_registry$get_record_class()

# if the relation type is one-to-many or many-to-many, iterate over the list
if (field$relation_type %in% c("one-to-one", "many-to-one")) {
related_registry_class$new(related_data)
} else {
Expand Down
8 changes: 7 additions & 1 deletion tests/testthat/test-connect_lamindata.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ test_that("Connecting to lamindata works", {
expect_equal(artifact$uid, "mePviem4DGM4SFzvLXf3")
expect_equal(artifact$suffix, ".csv")

# try to fetch linked records
# try to fetch related field
created_by <- artifact$created_by
expect_equal(created_by$handle, "sunnyosun")

# access a related field which is empty for this record
expect_null(artifact$type) # one to one

expect_type(artifact$wells, "list") # one-to-many
expect_length(artifact$wells, 0)
})

0 comments on commit b59c996

Please sign in to comment.