diff --git a/src/tatoeba/search/sentence.gleam b/src/tatoeba/search/sentence.gleam index 01b04db..50f4c96 100644 --- a/src/tatoeba/search/sentence.gleam +++ b/src/tatoeba/search/sentence.gleam @@ -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)) { [ diff --git a/src/tatoeba/search/translation.gleam b/src/tatoeba/search/translation.gleam index c6154c6..08f537e 100644 --- a/src/tatoeba/search/translation.gleam +++ b/src/tatoeba/search/translation.gleam @@ -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" @@ -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" diff --git a/test/api_test.gleam b/test/api_test.gleam new file mode 100644 index 0000000..ac22b9c --- /dev/null +++ b/test/api_test.gleam @@ -0,0 +1,3 @@ +pub fn new_request_to_test() { + todo +} diff --git a/test/search/sentence_test.gleam b/test/search/sentence_test.gleam new file mode 100644 index 0000000..411a6a7 --- /dev/null +++ b/test/search/sentence_test.gleam @@ -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(), + ) +} diff --git a/test/search/translation_test.gleam b/test/search/translation_test.gleam new file mode 100644 index 0000000..319debe --- /dev/null +++ b/test/search/translation_test.gleam @@ -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"), + ]) +} diff --git a/test/search/utils_test.gleam b/test/search/utils_test.gleam new file mode 100644 index 0000000..e3d066a --- /dev/null +++ b/test/search/utils_test.gleam @@ -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") +} diff --git a/test/search_test.gleam b/test/search_test.gleam new file mode 100644 index 0000000..8f4c426 --- /dev/null +++ b/test/search_test.gleam @@ -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 +} diff --git a/test/sentence_test.gleam b/test/sentence_test.gleam index 79b5c8a..f4fb7c4 100644 --- a/test/sentence_test.gleam +++ b/test/sentence_test.gleam @@ -28,15 +28,11 @@ pub fn sentence_exists_test() { } pub fn failed_request_test() { - // TODO: Test this. - - Nil + todo } pub fn failed_decoding_test() { - // TODO: Test this. - - Nil + todo } pub fn sentence_removed_test() { @@ -50,7 +46,7 @@ pub fn sentence_removed_test() { should.be_none(sentence) } -pub fn sentence_test() { +pub fn sentence_get_test() { let assert Ok(id) = sentence.new_id(1) let assert Ok(Some(sentence)) = sentence.get(id) diff --git a/test/structs.gleam b/test/structs.gleam new file mode 100644 index 0000000..8568a87 --- /dev/null +++ b/test/structs.gleam @@ -0,0 +1,42 @@ +import gleam/set +import tatoeba/search +import tatoeba/search/sentence +import tatoeba/search/translation + +pub fn word_count_options() -> sentence.WordCountOptions { + sentence.new_word_count_options() + |> sentence.set_at_least(10) + |> sentence.set_at_most(20) +} + +pub fn sentence_options() -> sentence.SentenceOptions { + sentence.new() + |> sentence.set_query("betwixt") + |> sentence.set_source_language("eng") + |> sentence.set_target_language("ron") + |> sentence.set_word_count_options(word_count_options()) + |> sentence.set_owner_id(7031) + |> sentence.set_is_orphan(True) + |> sentence.set_is_unapproved(False) + |> sentence.set_has_audio(False) + |> sentence.set_claimed_native(True) + |> sentence.set_tags(["MyTag", "YourTag", "TheirTag"] |> set.from_list()) + |> sentence.set_list_id(123) +} + +pub fn translation_options() -> translation.TranslationOptions { + translation.new() + |> translation.set_filter_strategy(translation.Limit) + |> translation.set_language("ron") + |> translation.set_link(translation.Direct) + |> translation.set_owner_id(123) + |> translation.set_is_orphan(True) + |> translation.set_is_unapproved(False) + |> translation.set_has_audio(True) +} + +pub fn search_options() -> search.SearchOptions { + search.new() + |> search.set_sentence_options(sentence_options()) + |> search.set_translation_options(translation_options()) +} diff --git a/test/utils.gleam b/test/utils.gleam new file mode 100644 index 0000000..a4ede62 --- /dev/null +++ b/test/utils.gleam @@ -0,0 +1,10 @@ +import gleam/list +import gleam/order +import gleam/string + +pub fn sort_parameters(parameters: List(#(String, String))) { + parameters + |> list.sort(fn(a, b) { + order.compare(string.compare(a.0, b.0), string.compare(a.1, b.1)) + }) +} diff --git a/test/utils_test.gleam b/test/utils_test.gleam new file mode 100644 index 0000000..25be6fc --- /dev/null +++ b/test/utils_test.gleam @@ -0,0 +1,16 @@ +import gleam/dynamic +import gleeunit/should +import tatoeba/utils + +pub fn stringified_int_bool_test() { + utils.stringified_int_bool(dynamic.from("1")) + |> should.equal(Ok(True)) + + utils.stringified_int_bool(dynamic.from("0")) + |> should.equal(Ok(False)) + + utils.stringified_int_bool(dynamic.from("invalid")) + |> should.equal( + Error([dynamic.DecodeError("One of: \"1\" or \"0\"", "invalid", [])]), + ) +}