Skip to content

Commit

Permalink
✅ add tests for export_fasta and read_fasta
Browse files Browse the repository at this point in the history
  • Loading branch information
Wytamma committed May 31, 2023
1 parent cb8da27 commit 9d0fb88
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
8 changes: 8 additions & 0 deletions tests/testthat/data/test.fa
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
>A
TTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAG
>B
TTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAG
>C
TTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAG
>D
TTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAG
44 changes: 44 additions & 0 deletions tests/testthat/test-fasta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
test_that("read_fasta works", {
fa_df <- read_fasta('data/test.fa')
expect_equal(names(fa_df), c('strain', 'sequence'))
expect_equal(fa_df$strain, c('A', 'B', 'C', 'D'))
expect_equal(fa_df$sequence[4], 'TTCGTCCGTGTTGCAGCCGATCATCAGCACATCTAG')
})


test_that("export_fasta works", {

# Define a temporary file for testing
temp_file <- tempfile(fileext = ".fasta")

# Mock seqs data frame
seqs <- data.frame(country = c("Country1", "Country2"),
pangolin_lineage = c("Lineage1", "Lineage2"),
accession_id = c("Accession1", "Accession2"),
date = as.Date(c("2023-01-01", "2023-02-01")),
description = c("Description1", "Description2"),
sequence = c("AGCT", "TCGA"))

# Apply the function
export_fasta(seqs, temp_file, columns = c("country", "pangolin_lineage", "accession_id", "date"), date_format = "%Y-%m")

# Check if file exists
expect_true(file.exists(temp_file))

# Read the generated file
fasta_content <- readLines(temp_file)

# Check the format of the FASTA headers
fasta_headers <- fasta_content[seq(1, length(fasta_content), 2)]
expected_headers <- c(">Country1@Lineage1@Accession1@2023-01",
">Country2@Lineage2@Accession2@2023-02")
expect_equal(fasta_headers, expected_headers)

# Check the sequences
fasta_sequences <- trimws(fasta_content[seq(2, length(fasta_content), 2)])
expected_sequences <- c("AGCT", "TCGA")
expect_equal(fasta_sequences, expected_sequences)

# Remove temporary file
unlink(temp_file)
})

0 comments on commit 9d0fb88

Please sign in to comment.