Skip to content

Commit a4364e4

Browse files
authored
More tests, workflows PR comment for feature test (#405)
1 parent 79a8458 commit a4364e4

8 files changed

Lines changed: 269 additions & 2 deletions
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: comment feature test results
2+
3+
on:
4+
workflow_run:
5+
workflows: ["test data"]
6+
types: [completed]
7+
8+
permissions:
9+
actions: read
10+
pull-requests: write
11+
12+
jobs:
13+
comment:
14+
runs-on: ubuntu-latest
15+
if: github.event.workflow_run.event == 'pull_request'
16+
steps:
17+
- name: download feature test results
18+
uses: actions/download-artifact@v4
19+
with:
20+
name: feature-test-results
21+
run-id: ${{ github.event.workflow_run.id }}
22+
github-token: ${{ secrets.GITHUB_TOKEN }}
23+
24+
- name: comment on PR
25+
env:
26+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
27+
GH_REPO: ${{ github.repository }}
28+
run: |
29+
PR_NUMBER=$(cat pr_number.txt)
30+
gh pr comment "$PR_NUMBER" --body-file phoneme_feature_test_results.txt

‎.github/workflows/main.yml‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,39 @@ on:
55
branches: [ master ]
66
pull_request:
77
branches: [ master ]
8-
98
jobs:
109
test-data:
1110
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
1213
steps:
1314
- uses: actions/checkout@v7
1415
- uses: prefix-dev/setup-pixi@v0.10.2
1516
with:
1617
pixi-version: v0.66.0
1718
activate-environment: "ci"
19+
## The following steps are temporary until we fix the phoneme feature test to pass in CI.
20+
## Once the phoneme feature test passes, update the below run tests command and remove filter and invert, and delete the following steps.
21+
- name: run feature test (write to PR comment)
22+
working-directory: tests
23+
run: |
24+
Rscript -e "testthat::test_file('test-phonemes.R', reporter = 'summary')" > phoneme_feature_test_results.txt 2>&1 || true
25+
## Fork PRs get a read-only GITHUB_TOKEN here, so this job can't comment
26+
## directly. comment-feature-test.yml (triggered via workflow_run) does it instead.
27+
- name: save PR number
28+
if: github.event_name == 'pull_request'
29+
working-directory: tests
30+
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt
31+
- name: upload feature test results
32+
if: github.event_name == 'pull_request'
33+
uses: actions/upload-artifact@v4
34+
with:
35+
name: feature-test-results
36+
path: |
37+
tests/phoneme_feature_test_results.txt
38+
tests/pr_number.txt
39+
## Run the rest of the tests, excluding the phoneme feature test (which is run above).
1840
- name: run tests
1941
working-directory: tests
2042
run: |
21-
Rscript -e "testthat::test_dir('.', stop_on_failure=TRUE)"
43+
Rscript -e "testthat::test_dir('.', filter = 'phonemes', invert = TRUE, stop_on_failure = TRUE)"

‎pixi.toml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ platforms = ["linux-64"]
66
version = "0.1.0"
77

88
[tasks]
9+
test = [{ task = "tests"}]
910

1011
[dependencies]
1112
r = "4.*"
@@ -16,6 +17,7 @@ r-stringi = ">=1.8.7,<2"
1617

1718
[feature.agg.tasks]
1819
aggregate = { cmd = "Rscript aggregate-raw-data.R", cwd = "scripts" }
20+
add_features = { cmd = "Rscript add-features.R", cwd = "scripts" }
1921

2022
[feature.ci.dependencies]
2123
r-dplyr = "1.1.1.*"
@@ -26,6 +28,9 @@ r-stringr = ">=1.5.1,<2"
2628
r-testthat = ">=3.2.1.1,<4"
2729
r-withr = ">=3.0.0,<4"
2830

31+
[feature.ci.tasks]
32+
tests = { cmd = "Rscript -e \"testthat::test_dir('.', stop_on_failure=TRUE)\"", cwd = "tests" }
33+
2934
[environments]
3035
agg = { features = ["agg"] }
3136
ci = { features = ["ci"] }

‎scripts/add-features.R‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ make_feat_vec_from_mat <- function(feat_mat) {
144144
zero=TRUE)
145145
## look up the combined base glyphs in the special_feats table
146146
base_vec <- special_feats[special_feats$segment == base_pair, ]
147+
stopifnot(nrow(base_vec) == 1)
147148
## populate the feature vector from the special_feats table
148149
feat_vec[feature_cols] <- base_vec[feature_cols]
149150
# delete two bases
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
)

‎tests/test-inventory-metadata.R‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ test_that("glottocodes are valid", {
141141
glotto_url <- "https://github.com/glottolog/glottolog-cldf/blob/master/cldf/languages.csv?raw=true"
142142
glotto_table <- readr::read_csv(glotto_url, col_types=default_char_cols)
143143
glotto_valid <- pull(glotto_table, ID)
144+
iso_valid_map <- pull(glotto_table, ISO639P3code)
144145

145146
glotto_phoible <- pull(phoible, Glottocode)
146147
glotto_invalid <- setdiff(glotto_phoible, glotto_valid)
@@ -166,6 +167,39 @@ test_that("glottocodes are valid", {
166167
collapse=" "),
167168
sep="\n")
168169
)
170+
171+
## Check that the ISO code in glottolog matches phoible's ISO code, for
172+
## every inventory with a known Glottocode (i.e. excluding Djindewal).
173+
has_glottocode <- !is.na(phoible$Glottocode)
174+
175+
iso_from_glottolog <- iso_valid_map[match(phoible$Glottocode[has_glottocode], glotto_valid)]
176+
iso_from_phoible <- phoible$ISO6393[has_glottocode]
177+
178+
## Exclude known glottolog-ISO mismatches
179+
known_mismatches <- rbind(
180+
c(868, "nepa1253", "kru"), # kxl (deprecated) merged into kru
181+
c(1307, "krim1238", "bmf"), # krm (deprecated) merged into bmf
182+
c(2750, "dira1238", "dif"), # dit (deprecated) merged into dif
183+
c(2875, "band1337", "drl"), # bjd (deprecated) merged into drl
184+
c(2256, "dark1243", "khk") # drh (deprecated) merged into khk
185+
)
186+
187+
overridable <- phoible$Glottocode[has_glottocode] %in% known_mismatches[, 2]
188+
override <- setNames(known_mismatches[, 3], known_mismatches[, 2])
189+
iso_from_glottolog[overridable] <- override[phoible$Glottocode[has_glottocode][overridable]]
190+
191+
mismatch_indices <- which(iso_from_glottolog != iso_from_phoible)
192+
193+
mismatch_ids <- phoible$InventoryID[has_glottocode][mismatch_indices]
194+
mismatch_glottocodes <- phoible$Glottocode[has_glottocode][mismatch_indices]
195+
mismatches <- unique(paste0(mismatch_ids, " (", mismatch_glottocodes, "): ", iso_from_phoible[mismatch_indices],
196+
" - ", iso_from_glottolog[mismatch_indices]))
197+
198+
expect(length(mismatches) == 0,
199+
paste("MISMATCHED ISO CODES BETWEEN PHOIBLE AND GLOTTOLOG (InventoryID (glottocode)): phoible - glottolog):",
200+
paste0("- ", mismatches, collapse="\n"),
201+
sep="\n")
202+
)
169203
}
170204
)
171205

‎tests/test-regression-c-cedilla.R‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#! /usr/bin/env Rscript
2+
3+
## Regression test for c-cedilla staying precomposed in order_ipa()
4+
## (aggregation-helper-functions.R), rather than decomposing it.
5+
6+
library(stringi, warn.conflicts=FALSE)
7+
library(testthat, warn.conflicts=FALSE)
8+
9+
source(file.path("..", "scripts", "aggregation-helper-functions.R"))
10+
11+
context("Special-case exceptions regression")
12+
13+
test_that("c-cedilla is unchanged by order_ipa()", {
14+
ordered <- order_ipa("ç")
15+
expect(identical(ordered, "ç"),
16+
paste("Expected c-cedilla phoneme to be unchanged by order_ipa(), got",
17+
shQuote(ordered)))
18+
})
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#! /usr/bin/env Rscript
2+
3+
## Regression test for the click/modifier typing in order_ipa() (aggregation-helper-functions.R).
4+
## Clicks are deliberately typed "M", the same glyph-type code as real modifier letters
5+
## so they factor into feature-vector assignment the same way
6+
## (see create_glyph_type_variables()/make_typestring()).
7+
8+
library(stringi, warn.conflicts=FALSE)
9+
library(testthat, warn.conflicts=FALSE)
10+
11+
source(file.path("..", "scripts", "aggregation-helper-functions.R"))
12+
13+
context("Click/modifier regression")
14+
15+
test_that("a click is not deleted when followed by a modifier letter", {
16+
## dental click U+01C0 + aspirated U+02B0 both typed "M"
17+
## triggering the "MM" canonical-reordering block
18+
result <- order_ipa("ǀʰ")
19+
expect(stri_detect_fixed(result, "ǀ"),
20+
paste("Click character was lost from", shQuote(result)))
21+
expect(nchar(result) == 2,
22+
paste("Expected a 2-character result, got", shQuote(result),
23+
"(", nchar(result), "characters)"))
24+
})
25+
26+
test_that("a click is not deleted when preceding a modifier letter", {
27+
## same "MM" span, reversed input order
28+
result <- order_ipa("ʰǀ")
29+
expect(stri_detect_fixed(result, "ǀ"),
30+
paste("Click character was lost from", shQuote(result)))
31+
expect(nchar(result) == 2,
32+
paste("Expected a 2-character result, got", shQuote(result),
33+
"(", nchar(result), "characters)"))
34+
})
35+
36+
test_that("a click followed by a diacritic is not swapped the way a modifier would be", {
37+
## dental click + devoiced/ring-below diacritic (U+0325) - Typed "MD"
38+
## Should not be swapped to "DM" because the click is not a real modifier letter.
39+
result <- order_ipa("ǀ̥")
40+
expect(stri_detect_fixed(result, "ǀ"),
41+
paste("Click character was lost from", shQuote(result)))
42+
expect(identical(stri_sub(result, 1, 1), "ǀ"),
43+
paste("Expected click to remain first, got", shQuote(result)))
44+
})
45+
46+
test_that("a click preceding a real modifier+diacritic doesn't block their swap", {
47+
## typestring "MMD" with a click first, then a modifier letter, then a diacritic.
48+
## The modifier+diacritic pair should still be swapped, but the click should remain first.
49+
result <- order_ipa("ǀʰ̥")
50+
expect(stri_detect_fixed(result, "ǀ"),
51+
paste("Click character was lost from", shQuote(result)))
52+
expect(identical(stri_sub(result, 1, 1), "ǀ"),
53+
paste("Expected click to remain first, got", shQuote(result)))
54+
expect(identical(result, "ǀ̥ʰ"),
55+
paste("Expected the modifier+diacritic pair to still be swapped",
56+
"(click skip should not suppress it), got", shQuote(result)))
57+
})

0 commit comments

Comments
 (0)