Skip to content

Commit

Permalink
test: Add more tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern committed Jun 13, 2024
1 parent 9f980c0 commit f358739
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/tatoeba/search/sentence.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn set_at_most(options: WordCountOptions, count: Int) -> WordCountOptions {
/// Converts word count options to a set of query parameters to be encoded in the
/// search query.
///
fn word_count_options_to_query_parameters(
pub fn word_count_options_to_query_parameters(
options: WordCountOptions,
) -> List(#(String, String)) {
[
Expand Down
4 changes: 2 additions & 2 deletions src/tatoeba/search/translation.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub type FilterStrategy {
/// Converts the filter strategy to its string representation ready to encode
/// in the query.
///
pub fn filter_strategy_to_string(strategy: FilterStrategy) -> String {
fn filter_strategy_to_string(strategy: FilterStrategy) -> String {
case strategy {
Limit -> "limit"
Exclude -> "exclude"
Expand All @@ -34,7 +34,7 @@ pub type Link {

/// Converts the link to its string representation ready to encode in the
/// query.
pub fn link_to_string(link: Link) -> String {
fn link_to_string(link: Link) -> String {
case link {
Direct -> "direct"
Indirect -> "indirect"
Expand Down
3 changes: 3 additions & 0 deletions test/api_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub fn new_request_to_test() {
todo
}
178 changes: 178 additions & 0 deletions test/search/sentence_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import gleam/option.{None, Some}
import gleam/set
import gleeunit/should
import structs
import tatoeba/search/sentence as sentence_options
import utils

pub fn new_word_count_options_test() {
let word_count_options = sentence_options.new_word_count_options()

word_count_options.at_least |> should.equal(None)
word_count_options.at_most |> should.equal(None)
}

pub fn set_at_least_test() {
let word_count_options =
sentence_options.new_word_count_options()
|> sentence_options.set_at_least(5)

word_count_options.at_least |> should.equal(Some(5))
}

pub fn set_at_most_test() {
let word_count_options =
sentence_options.new_word_count_options()
|> sentence_options.set_at_most(10)

word_count_options.at_most |> should.equal(Some(10))
}

pub fn word_count_options_to_query_parameters_test() {
structs.word_count_options()
|> sentence_options.word_count_options_to_query_parameters()
|> utils.sort_parameters()
|> should.equal(
[#("word_count_min", "10"), #("word_count_max", "20")]
|> utils.sort_parameters(),
)
}

pub fn new_test() {
let sentence_options = sentence_options.new()

sentence_options.query |> should.equal(None)
sentence_options.source_language |> should.equal(None)
sentence_options.target_language |> should.equal(None)
sentence_options.word_count_options
|> should.equal(sentence_options.new_word_count_options())
sentence_options.owner_id |> should.equal(None)
sentence_options.is_orphan |> should.equal(None)
sentence_options.is_unapproved |> should.equal(None)
sentence_options.has_audio |> should.equal(None)
sentence_options.claimed_native |> should.equal(None)
sentence_options.tags |> should.equal(set.new())
sentence_options.list_id |> should.equal(None)
}

pub fn set_query_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_query("drepequamvisacircation")

sentence_options.query |> should.equal(Some("drepequamvisacircation"))
}

pub fn set_source_language_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_source_language("ron")

sentence_options.source_language |> should.equal(Some("ron"))
}

pub fn set_target_language_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_target_language("hye")

sentence_options.target_language |> should.equal(Some("hye"))
}

pub fn set_word_count_options_test() {
let word_count_options =
sentence_options.new_word_count_options()
|> sentence_options.set_at_least(5)
let sentence_options =
sentence_options.new()
|> sentence_options.set_word_count_options(word_count_options)

sentence_options.word_count_options |> should.equal(word_count_options)
}

pub fn set_owner_id_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_owner_id(5)

sentence_options.owner_id |> should.equal(Some(5))
}

pub fn set_is_orphan_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_is_orphan(True)

sentence_options.is_orphan |> should.equal(Some(True))
}

pub fn set_is_unapproved_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_is_unapproved(True)

sentence_options.is_unapproved |> should.equal(Some(True))
}

pub fn set_has_audio_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_has_audio(True)

sentence_options.has_audio |> should.equal(Some(True))
}

pub fn set_claimed_native_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_claimed_native(True)

sentence_options.claimed_native |> should.equal(Some(True))
}

pub fn set_tag_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_tag("Fungi")

sentence_options.tags |> should.equal(set.from_list(["Fungi"]))
}

pub fn set_tags_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_tags(set.from_list(["Animals", "Plants"]))

sentence_options.tags |> should.equal(set.from_list(["Animals", "Plants"]))
}

pub fn set_list_id_test() {
let sentence_options =
sentence_options.new()
|> sentence_options.set_list_id(5)

sentence_options.list_id |> should.equal(Some(5))
}

pub fn to_query_parameters_test() {
structs.sentence_options()
|> sentence_options.to_query_parameters()
|> utils.sort_parameters()
|> should.equal(
[
#("query", "betwixt"),
#("from", "eng"),
#("to", "ron"),
#("user", "7031"),
#("orphans", "yes"),
#("unapproved", "no"),
#("has_audio", "no"),
#("native", "yes"),
#("tags", "MyTag,YourTag,TheirTag"),
#("list", "123"),
#("word_count_min", "10"),
#("word_count_max", "20"),
]
|> utils.sort_parameters(),
)
}
88 changes: 88 additions & 0 deletions test/search/translation_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import gleam/option.{None, Some}
import gleeunit/should
import structs
import tatoeba/search/translation as translation_options

pub fn new() {
let translation_options = translation_options.new()

translation_options.filter_strategy |> should.equal(translation_options.Limit)
translation_options.language |> should.equal(None)
translation_options.link |> should.equal(None)
translation_options.owner_id |> should.equal(None)
translation_options.is_orphan |> should.equal(None)
translation_options.is_unapproved |> should.equal(None)
translation_options.has_audio |> should.equal(None)
}

pub fn set_filter_strategy() {
let translation_options =
translation_options.new()
|> translation_options.set_filter_strategy(translation_options.Limit)

translation_options.filter_strategy |> should.equal(translation_options.Limit)
}

pub fn set_language() {
let translation_options =
translation_options.new()
|> translation_options.set_language("ron")

translation_options.language |> should.equal(Some("ron"))
}

pub fn set_link() {
let translation_options =
translation_options.new()
|> translation_options.set_link(translation_options.Direct)

translation_options.link |> should.equal(Some(translation_options.Direct))
}

pub fn set_owner_id() {
let translation_options =
translation_options.new()
|> translation_options.set_owner_id(5)

translation_options.owner_id |> should.equal(Some(5))
}

pub fn set_is_orphan() {
let translation_options =
translation_options.new()
|> translation_options.set_is_orphan(True)

translation_options.link |> should.equal(Some(translation_options.Direct))
}

pub fn set_is_unapproved() {
let translation_options =
translation_options.new()
|> translation_options.set_is_unapproved(True)

translation_options.is_unapproved
|> should.equal(Some(True))
}

pub fn set_has_audio() {
let translation_options =
translation_options.new()
|> translation_options.set_has_audio(True)

translation_options.has_audio
|> should.equal(Some(True))
}

pub fn to_query_parameters() {
structs.translation_options()
|> translation_options.to_query_parameters()
|> should.equal([
#("trans_filter", "limit"),
#("trans_to", "ron"),
#("trans_link", "direct"),
#("trans_user", "123"),
#("trans_orphan", "yes"),
#("trans_unapproved", "no"),
#("trans_has_audio", "yes"),
])
}
17 changes: 17 additions & 0 deletions test/search/utils_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import gleam/option.{None, Some}
import gleeunit/should
import tatoeba/search/utils

pub fn exclude_missing_test() {
[
#("key-with-present-value", Some("present")),
#("key-with-missing-value", None),
]
|> utils.select_present()
|> should.equal([#("key-with-present-value", "present")])
}

pub fn bool_to_yes_no_test() {
True |> utils.bool_to_yes_no() |> should.equal("yes")
False |> utils.bool_to_yes_no() |> should.equal("no")
}
75 changes: 75 additions & 0 deletions test/search_test.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import gleam/list
import gleam/option.{None, Some}
import gleeunit/should
import structs
import tatoeba/search
import tatoeba/search/sentence
import tatoeba/search/translation

pub fn sort_strategy_to_string_test() {
[
search.Relevance,
search.FewestWordsFirst,
search.LastCreatedFirst,
search.LastModifiedFirst,
search.Random,
]
|> list.map(search.sort_strategy_to_string)
|> should.equal(["relevance", "words", "created", "modified", "random"])
}

pub fn new_test() {
let search = search.new()

search.sentence_options |> should.equal(sentence.new())
search.translation_options |> should.equal(translation.new())
search.sort_strategy |> should.equal(None)
search.reverse_sort |> should.equal(None)
}

pub fn set_sentence_options_test() {
let sentence_options =
sentence.new()
// Eastern Armenian
|> sentence.set_source_language("hye")
let search =
search.new()
|> search.set_sentence_options(sentence_options)

search.sentence_options |> should.equal(sentence_options)
}

pub fn set_translation_options_test() {
let translation_options =
translation.new()
// Romanian
|> translation.set_language("ron")
let search =
search.new()
|> search.set_translation_options(translation_options)

search.translation_options |> should.equal(translation_options)
}

pub fn set_sort_strategy_test() {
let search = search.new() |> search.set_sort_strategy(search.Relevance)

search.sort_strategy |> should.equal(Some(search.Relevance))
}

pub fn set_reverse_sort_test() {
let search = search.new() |> search.set_reverse_sort(True)

search.reverse_sort |> should.equal(Some(True))
}

pub fn to_query_parameters_test() {
structs.search_options()
|> search.to_query_parameters()

todo
}

pub fn run_test() {
todo
}
Loading

0 comments on commit f358739

Please sign in to comment.