|
| 1 | +#! /usr/bin/env Rscript |
| 2 | + |
| 3 | +## Tests for cross-inventory integrity. Checks that: |
| 4 | +## 1. No source accidentally split one language (Glottocode, LanguageName, SpecificDialect, BibtexKey) across two InventoryIDs |
| 5 | +## 2. Every BibtexKey in the mapping file resolves to a real entry in |
| 6 | +## phoible-references.bib. |
| 7 | +## 3. Every InventoryID in the mapping files exists in the aggregated |
| 8 | +## data, and vice versa. |
| 9 | + |
| 10 | +library(dplyr, warn.conflicts=FALSE) |
| 11 | +library(testthat, warn.conflicts=FALSE) |
| 12 | + |
| 13 | +## load PHOIBLE data |
| 14 | +phoible_data_file <- file.path("..", "data", "phoible.csv") |
| 15 | +phoible_col_types <- readr::cols(InventoryID="c", Marginal="l", .default="c") |
| 16 | +phoible <- readr::read_csv(phoible_data_file, col_types=phoible_col_types) |
| 17 | + |
| 18 | +bibtex_mapping_file <- file.path("..", "mappings", "InventoryID-Bibtex.csv") |
| 19 | +bibtex_mapping <- readr::read_csv(bibtex_mapping_file, col_types=readr::cols(.default="c")) |
| 20 | + |
| 21 | +# Join the phoible data with the mapping file to get a combined dataset for testing |
| 22 | +phoible_with_bibtex <- phoible %>% |
| 23 | + left_join(bibtex_mapping, by="InventoryID", relationship="many-to-many") |
| 24 | + |
| 25 | +references_bib_file <- file.path("..", "mappings", "phoible-references.bib") |
| 26 | + |
| 27 | +context("Cross-inventory integrity") |
| 28 | + |
| 29 | +test_that("no source accidentally split one language across two InventoryIDs", { |
| 30 | + ## Checks whether the same (Source, Glottocode, LanguageName, SpecificDialect, BibtexKey) tuple |
| 31 | + ## appears in more than one InventoryID. |
| 32 | + phoible_with_bibtex %>% |
| 33 | + filter(!is.na(Glottocode)) %>% |
| 34 | + distinct(InventoryID, Source, Glottocode, LanguageName, SpecificDialect, BibtexKey) %>% |
| 35 | + group_by(Source, Glottocode, LanguageName, SpecificDialect, BibtexKey) %>% |
| 36 | + filter(n_distinct(InventoryID) > 1) %>% |
| 37 | + ungroup() -> |
| 38 | + split_duplicates |
| 39 | + |
| 40 | + expect(nrow(split_duplicates) == 0, |
| 41 | + paste(c("SAME (Source, Glottocode, LanguageName, SpecificDialect, BibtexKey) APPEARS IN MULTIPLE INVENTORYIDS:", |
| 42 | + capture.output(print(split_duplicates, n=Inf))), |
| 43 | + sep="\n") |
| 44 | + ) |
| 45 | + } |
| 46 | +) |
| 47 | + |
| 48 | +test_that("every BibtexKey resolves to a real bib entry", { |
| 49 | + ## Check that every BibtexKey in the mapping file resolves to a real entry in phoible-references.bib. |
| 50 | + |
| 51 | + ## regex to match BibTeX keys in the .bib file |
| 52 | + citekey_re <- "(?m)^@\\w+\\s*\\{\\s*([^,]+?)\\s*," |
| 53 | + |
| 54 | + bib_keys <- stringr::str_match_all( |
| 55 | + readr::read_file(references_bib_file), citekey_re |
| 56 | + )[[1]][, 2] |
| 57 | + |
| 58 | + mapping_keys <- bibtex_mapping %>% pull(BibtexKey) %>% unique() |
| 59 | + missing_keys <- setdiff(mapping_keys, bib_keys) |
| 60 | + |
| 61 | + # Exclude special case of InventoryID 201 (Arrente), which has "NO SOURCE GIVEN" as its BibtexKey |
| 62 | + missing_keys <- setdiff(missing_keys, bibtex_mapping %>% filter(InventoryID == 201) %>% pull(BibtexKey)) |
| 63 | + |
| 64 | + ## report which InventoryID(s) cite each missing key |
| 65 | + offending_rows <- bibtex_mapping %>% |
| 66 | + filter(BibtexKey %in% missing_keys) %>% |
| 67 | + arrange(BibtexKey, as.integer(InventoryID)) |
| 68 | + offending_summary <- offending_rows %>% |
| 69 | + group_by(BibtexKey) %>% |
| 70 | + summarise(InventoryIDs = paste(InventoryID, collapse=", "), .groups="drop") |
| 71 | + |
| 72 | + expect(length(missing_keys) == 0, |
| 73 | + paste(c(paste("BIBTEXKEYS WITH NO MATCHING ENTRY IN", |
| 74 | + basename(references_bib_file)), |
| 75 | + paste0(offending_summary$BibtexKey, " (InventoryID ", |
| 76 | + offending_summary$InventoryIDs, ")")), |
| 77 | + sep="\n") |
| 78 | + ) |
| 79 | + } |
| 80 | +) |
| 81 | + |
| 82 | +test_that("mapping-file and aggregated-data InventoryIDs match in both directions", { |
| 83 | + phoible_ids <- phoible %>% pull(InventoryID) %>% unique() |
| 84 | + mapping_ids <- bibtex_mapping %>% pull(InventoryID) %>% as.integer() %>% unique() |
| 85 | + |
| 86 | + only_in_mapping <- setdiff(mapping_ids, phoible_ids) |
| 87 | + only_in_phoible <- setdiff(phoible_ids, mapping_ids) |
| 88 | + |
| 89 | + expect(length(only_in_mapping) == 0, |
| 90 | + paste(c("INVENTORYIDS IN InventoryID-Bibtex.csv BUT MISSING FROM", |
| 91 | + "AGGREGATED DATA:", paste(only_in_mapping, collapse=" ")), |
| 92 | + sep="\n") |
| 93 | + ) |
| 94 | + expect(length(only_in_phoible) == 0, |
| 95 | + paste(c("INVENTORYIDS IN AGGREGATED DATA BUT MISSING FROM", |
| 96 | + "InventoryID-Bibtex.csv:", paste(only_in_phoible, collapse=" ")), |
| 97 | + sep="\n") |
| 98 | + ) |
| 99 | + } |
| 100 | +) |
0 commit comments