Skip to content

Commit a799194

Browse files
committed
Add tests for check_if_assays_are_NOT_consistently_ordered function in test-utilities.R
Implemented a comprehensive suite of tests to validate the behavior of the check_if_assays_are_NOT_consistently_ordered function across various scenarios, including cases with empty assays, inconsistent colnames, and different ordering of colnames. This enhances the robustness of the utility checks in the package.
1 parent ac4b6bc commit a799194

File tree

1 file changed

+306
-0
lines changed

1 file changed

+306
-0
lines changed

tests/testthat/test-utilities.R

Lines changed: 306 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,3 +351,309 @@ test_that("get_count_datasets works", {
351351
# )
352352
# expect_error(cds <- get_count_datasets(se1), "assays must be named")
353353
})
354+
355+
test_that("check_if_assays_are_NOT_consistently_ordered works correctly", {
356+
# Use testthat edition 3
357+
local_edition(3)
358+
359+
# Test 1: SE with no assays - should return FALSE
360+
se_empty <- SummarizedExperiment::SummarizedExperiment(
361+
assays = list()
362+
)
363+
expect_false(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_empty))
364+
365+
# Test 2: SE with assays but no colnames - should return FALSE
366+
se_no_colnames <- SummarizedExperiment::SummarizedExperiment(
367+
assays = list(
368+
mat1 = matrix(seq_len(9), nrow = 3),
369+
mat2 = matrix(seq(10, 18), nrow = 3)
370+
)
371+
)
372+
expect_false(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_no_colnames))
373+
374+
# Test 3: SE with assays, some with colnames, some without - should return FALSE
375+
se_mixed <- SummarizedExperiment::SummarizedExperiment(
376+
assays = list(
377+
mat1 = matrix(seq_len(9), nrow = 3),
378+
mat2 = matrix(seq(10, 18), nrow = 3)
379+
)
380+
)
381+
colnames(assay(se_mixed, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
382+
expect_false(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_mixed))
383+
384+
# Test 4: SE with assays, all with same colnames in same order - should return FALSE
385+
se_consistent <- SummarizedExperiment::SummarizedExperiment(
386+
assays = list(
387+
mat1 = matrix(seq_len(9), nrow = 3),
388+
mat2 = matrix(seq(10, 18), nrow = 3)
389+
)
390+
)
391+
colnames(assay(se_consistent, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
392+
colnames(assay(se_consistent, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
393+
expect_false(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_consistent))
394+
395+
# Test 5: SE with assays, all with same colnames but different order - should return TRUE
396+
se_inconsistent <- SummarizedExperiment::SummarizedExperiment(
397+
assays = list(
398+
mat1 = matrix(seq_len(9), nrow = 3),
399+
mat2 = matrix(seq(10, 18), nrow = 3)
400+
)
401+
)
402+
colnames(assay(se_inconsistent, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
403+
colnames(assay(se_inconsistent, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
404+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_inconsistent))
405+
406+
# Test 6: SE with assays, all with same colnames but different order (reverse) - should return TRUE
407+
se_reverse <- SummarizedExperiment::SummarizedExperiment(
408+
assays = list(
409+
mat1 = matrix(seq_len(9), nrow = 3),
410+
mat2 = matrix(seq(10, 18), nrow = 3)
411+
)
412+
)
413+
colnames(assay(se_reverse, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
414+
colnames(assay(se_reverse, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 2, 1))
415+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_reverse))
416+
417+
# Test 7: SE with assays, all with same colnames but different order (middle swap) - should return TRUE
418+
se_middle_swap <- SummarizedExperiment::SummarizedExperiment(
419+
assays = list(
420+
mat1 = matrix(seq_len(9), nrow = 3),
421+
mat2 = matrix(seq(10, 18), nrow = 3)
422+
)
423+
)
424+
colnames(assay(se_middle_swap, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
425+
colnames(assay(se_middle_swap, "mat2", withDimnames = FALSE)) <- paste0("S", c(1, 3, 2))
426+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_middle_swap))
427+
428+
# Test 8: SE with 3 assays, first two consistent, third different - should return TRUE
429+
se_three_assays <- SummarizedExperiment::SummarizedExperiment(
430+
assays = list(
431+
mat1 = matrix(seq_len(9), nrow = 3),
432+
mat2 = matrix(seq(10, 18), nrow = 3),
433+
mat3 = matrix(seq(19, 27), nrow = 3)
434+
)
435+
)
436+
colnames(assay(se_three_assays, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
437+
colnames(assay(se_three_assays, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
438+
colnames(assay(se_three_assays, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
439+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_three_assays))
440+
441+
# Test 9: SE with 3 assays, all consistent - should return FALSE
442+
se_three_consistent <- SummarizedExperiment::SummarizedExperiment(
443+
assays = list(
444+
mat1 = matrix(seq_len(9), nrow = 3),
445+
mat2 = matrix(seq(10, 18), nrow = 3),
446+
mat3 = matrix(seq(19, 27), nrow = 3)
447+
)
448+
)
449+
colnames(assay(se_three_consistent, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
450+
colnames(assay(se_three_consistent, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
451+
colnames(assay(se_three_consistent, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
452+
expect_false(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_three_consistent))
453+
454+
# Test 10: SE with assays, all with same colnames but different order (complex permutation) - should return TRUE
455+
se_complex <- SummarizedExperiment::SummarizedExperiment(
456+
assays = list(
457+
mat1 = matrix(seq_len(12), nrow = 3),
458+
mat2 = matrix(seq(13, 24), nrow = 3)
459+
)
460+
)
461+
colnames(assay(se_complex, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(4))
462+
colnames(assay(se_complex, "mat2", withDimnames = FALSE)) <- paste0("S", c(4, 2, 1, 3))
463+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_complex))
464+
465+
# Test 11: SE with assays, all with same colnames but different order (all different) - should return TRUE
466+
se_all_different <- SummarizedExperiment::SummarizedExperiment(
467+
assays = list(
468+
mat1 = matrix(seq_len(9), nrow = 3),
469+
mat2 = matrix(seq(10, 18), nrow = 3),
470+
mat3 = matrix(seq(19, 27), nrow = 3)
471+
)
472+
)
473+
colnames(assay(se_all_different, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
474+
colnames(assay(se_all_different, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
475+
colnames(assay(se_all_different, "mat3", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
476+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different))
477+
478+
# Test 12: SE with assays, all with same colnames but different order (pairwise different) - should return TRUE
479+
se_pairwise <- SummarizedExperiment::SummarizedExperiment(
480+
assays = list(
481+
mat1 = matrix(seq_len(9), nrow = 3),
482+
mat2 = matrix(seq(10, 18), nrow = 3),
483+
mat3 = matrix(seq(19, 27), nrow = 3)
484+
)
485+
)
486+
colnames(assay(se_pairwise, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
487+
colnames(assay(se_pairwise, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
488+
colnames(assay(se_pairwise, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
489+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_pairwise))
490+
491+
# Test 13: SE with assays, all with same colnames but different order (first and last same) - should return TRUE
492+
se_first_last <- SummarizedExperiment::SummarizedExperiment(
493+
assays = list(
494+
mat1 = matrix(seq_len(9), nrow = 3),
495+
mat2 = matrix(seq(10, 18), nrow = 3),
496+
mat3 = matrix(seq(19, 27), nrow = 3)
497+
)
498+
)
499+
colnames(assay(se_first_last, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
500+
colnames(assay(se_first_last, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
501+
colnames(assay(se_first_last, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
502+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_first_last))
503+
504+
# Test 14: SE with assays, all with same colnames but different order (middle different) - should return TRUE
505+
se_middle_different <- SummarizedExperiment::SummarizedExperiment(
506+
assays = list(
507+
mat1 = matrix(seq_len(9), nrow = 3),
508+
mat2 = matrix(seq(10, 18), nrow = 3),
509+
mat3 = matrix(seq(19, 27), nrow = 3)
510+
)
511+
)
512+
colnames(assay(se_middle_different, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
513+
colnames(assay(se_middle_different, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
514+
colnames(assay(se_middle_different, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
515+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_middle_different))
516+
517+
# Test 15: SE with assays, all with same colnames but different order (first different) - should return TRUE
518+
se_first_different <- SummarizedExperiment::SummarizedExperiment(
519+
assays = list(
520+
mat1 = matrix(seq_len(9), nrow = 3),
521+
mat2 = matrix(seq(10, 18), nrow = 3),
522+
mat3 = matrix(seq(19, 27), nrow = 3)
523+
)
524+
)
525+
colnames(assay(se_first_different, "mat1", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
526+
colnames(assay(se_first_different, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
527+
colnames(assay(se_first_different, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
528+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_first_different))
529+
530+
# Test 16: SE with assays, all with same colnames but different order (last different) - should return TRUE
531+
se_last_different <- SummarizedExperiment::SummarizedExperiment(
532+
assays = list(
533+
mat1 = matrix(seq_len(9), nrow = 3),
534+
mat2 = matrix(seq(10, 18), nrow = 3),
535+
mat3 = matrix(seq(19, 27), nrow = 3)
536+
)
537+
)
538+
colnames(assay(se_last_different, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
539+
colnames(assay(se_last_different, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
540+
colnames(assay(se_last_different, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
541+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_last_different))
542+
543+
# Test 17: SE with assays, all with same colnames but different order (all different) - should return TRUE
544+
se_all_different_2 <- SummarizedExperiment::SummarizedExperiment(
545+
assays = list(
546+
mat1 = matrix(seq_len(9), nrow = 3),
547+
mat2 = matrix(seq(10, 18), nrow = 3),
548+
mat3 = matrix(seq(19, 27), nrow = 3)
549+
)
550+
)
551+
colnames(assay(se_all_different_2, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
552+
colnames(assay(se_all_different_2, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
553+
colnames(assay(se_all_different_2, "mat3", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
554+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_2))
555+
556+
# Test 18: SE with assays, all with same colnames but different order (all different) - should return TRUE
557+
se_all_different_3 <- SummarizedExperiment::SummarizedExperiment(
558+
assays = list(
559+
mat1 = matrix(seq_len(9), nrow = 3),
560+
mat2 = matrix(seq(10, 18), nrow = 3),
561+
mat3 = matrix(seq(19, 27), nrow = 3)
562+
)
563+
)
564+
colnames(assay(se_all_different_3, "mat1", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
565+
colnames(assay(se_all_different_3, "mat2", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
566+
colnames(assay(se_all_different_3, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
567+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_3))
568+
569+
# Test 19: SE with assays, all with same colnames but different order (all different) - should return TRUE
570+
se_all_different_4 <- SummarizedExperiment::SummarizedExperiment(
571+
assays = list(
572+
mat1 = matrix(seq_len(9), nrow = 3),
573+
mat2 = matrix(seq(10, 18), nrow = 3),
574+
mat3 = matrix(seq(19, 27), nrow = 3)
575+
)
576+
)
577+
colnames(assay(se_all_different_4, "mat1", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
578+
colnames(assay(se_all_different_4, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
579+
colnames(assay(se_all_different_4, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
580+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_4))
581+
582+
# Test 20: SE with assays, all with same colnames but different order (all different) - should return TRUE
583+
se_all_different_5 <- SummarizedExperiment::SummarizedExperiment(
584+
assays = list(
585+
mat1 = matrix(seq_len(9), nrow = 3),
586+
mat2 = matrix(seq(10, 18), nrow = 3),
587+
mat3 = matrix(seq(19, 27), nrow = 3)
588+
)
589+
)
590+
colnames(assay(se_all_different_5, "mat1", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
591+
colnames(assay(se_all_different_5, "mat2", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
592+
colnames(assay(se_all_different_5, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
593+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_5))
594+
595+
# Test 21: SE with assays, all with same colnames but different order (all different) - should return TRUE
596+
se_all_different_6 <- SummarizedExperiment::SummarizedExperiment(
597+
assays = list(
598+
mat1 = matrix(seq_len(9), nrow = 3),
599+
mat2 = matrix(seq(10, 18), nrow = 3),
600+
mat3 = matrix(seq(19, 27), nrow = 3)
601+
)
602+
)
603+
colnames(assay(se_all_different_6, "mat1", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
604+
colnames(assay(se_all_different_6, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
605+
colnames(assay(se_all_different_6, "mat3", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
606+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_6))
607+
608+
# Test 22: SE with assays, all with same colnames but different order (all different) - should return TRUE
609+
se_all_different_7 <- SummarizedExperiment::SummarizedExperiment(
610+
assays = list(
611+
mat1 = matrix(seq_len(9), nrow = 3),
612+
mat2 = matrix(seq(10, 18), nrow = 3),
613+
mat3 = matrix(seq(19, 27), nrow = 3)
614+
)
615+
)
616+
colnames(assay(se_all_different_7, "mat1", withDimnames = FALSE)) <- paste0("S", seq_len(3))
617+
colnames(assay(se_all_different_7, "mat2", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
618+
colnames(assay(se_all_different_7, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
619+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_7))
620+
621+
# Test 23: SE with assays, all with same colnames but different order (all different) - should return TRUE
622+
se_all_different_8 <- SummarizedExperiment::SummarizedExperiment(
623+
assays = list(
624+
mat1 = matrix(seq_len(9), nrow = 3),
625+
mat2 = matrix(seq(10, 18), nrow = 3),
626+
mat3 = matrix(seq(19, 27), nrow = 3)
627+
)
628+
)
629+
colnames(assay(se_all_different_8, "mat1", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
630+
colnames(assay(se_all_different_8, "mat2", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
631+
colnames(assay(se_all_different_8, "mat3", withDimnames = FALSE)) <- paste0("S", seq_len(3))
632+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_8))
633+
634+
# Test 24: SE with assays, all with same colnames but different order (all different) - should return TRUE
635+
se_all_different_9 <- SummarizedExperiment::SummarizedExperiment(
636+
assays = list(
637+
mat1 = matrix(seq_len(9), nrow = 3),
638+
mat2 = matrix(seq(10, 18), nrow = 3),
639+
mat3 = matrix(seq(19, 27), nrow = 3)
640+
)
641+
)
642+
colnames(assay(se_all_different_9, "mat1", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
643+
colnames(assay(se_all_different_9, "mat2", withDimnames = FALSE)) <- paste0("S", seq_len(3))
644+
colnames(assay(se_all_different_9, "mat3", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
645+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_9))
646+
647+
# Test 25: SE with assays, all with same colnames but different order (all different) - should return TRUE
648+
se_all_different_10 <- SummarizedExperiment::SummarizedExperiment(
649+
assays = list(
650+
mat1 = matrix(seq_len(9), nrow = 3),
651+
mat2 = matrix(seq(10, 18), nrow = 3),
652+
mat3 = matrix(seq(19, 27), nrow = 3)
653+
)
654+
)
655+
colnames(assay(se_all_different_10, "mat1", withDimnames = FALSE)) <- paste0("S", c(3, 1, 2))
656+
colnames(assay(se_all_different_10, "mat2", withDimnames = FALSE)) <- paste0("S", c(2, 3, 1))
657+
colnames(assay(se_all_different_10, "mat3", withDimnames = FALSE)) <- paste0("S", c(1, 2, 3))
658+
expect_true(tidySummarizedExperiment:::check_if_assays_are_NOT_consistently_ordered(se_all_different_10))
659+
})

0 commit comments

Comments
 (0)