Skip to content

Commit

Permalink
Merge PR #8 from josueBarretogit/tests
Browse files Browse the repository at this point in the history
Nothing new is added just tests.
  • Loading branch information
josueBarretogit authored Aug 12, 2024
2 parents ecd27d0 + 3f9d020 commit 8bc0c15
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 29 deletions.
12 changes: 9 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,17 @@ jobs:
with:
rust-version: stable

- name: cargo test
run: cargo test
- name: check
run: cargo check

- name: build
run: cargo build --release --verbose

- name: test
run: cargo test -- --test-threads=1


- name: clippy
run: cargo clippy --all --all-features --tests -- -D warnings
run: cargo clippy -- -D warnings
env:
RUST_BACKTRACE: full
162 changes: 149 additions & 13 deletions src/backend/filter.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::fmt::Write;
use std::fmt::{Debug, Write};
use strum::{Display, EnumIter, IntoEnumIterator};

use crate::global::PREFERRED_LANGUAGE;
use crate::view::widgets::filter_widget::state::{FilterListItem, TagListItem, TagListItemState};

pub trait IntoParam {
pub trait IntoParam: Debug {
fn into_param(self) -> String;
}

#[derive(Display, Clone)]
#[derive(Display, Clone, Debug)]
pub enum ContentRating {
#[strum(to_string = "safe")]
Safe,
Expand All @@ -32,7 +32,7 @@ impl From<&str> for ContentRating {
}
}

#[derive(Display, Clone, EnumIter, PartialEq, Eq, Default)]
#[derive(Display, Clone, EnumIter, PartialEq, Eq, Default, Debug)]
pub enum SortBy {
#[strum(to_string = "Best match")]
BestMatch,
Expand Down Expand Up @@ -101,6 +101,9 @@ impl Tags {
pub fn new(tags: Vec<TagData>) -> Self {
Self(tags)
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}

impl IntoParam for Tags {
Expand Down Expand Up @@ -167,7 +170,7 @@ impl IntoParam for SortBy {
}
}

#[derive(Display, Clone, EnumIter, PartialEq, Eq)]
#[derive(Display, Clone, EnumIter, PartialEq, Eq, Debug)]
pub enum MagazineDemographic {
Shounen,
Shoujo,
Expand Down Expand Up @@ -205,7 +208,7 @@ impl IntoParam for Vec<MagazineDemographic> {
}
}

#[derive(Default, Clone)]
#[derive(Default, Clone, Debug)]
pub struct Author(String);

impl Author {
Expand All @@ -214,7 +217,7 @@ impl Author {
}
}

#[derive(Default, Clone)]
#[derive(Default, Clone, Debug)]
pub struct Artist(String);

impl Artist {
Expand All @@ -223,8 +226,8 @@ impl Artist {
}
}

#[derive(Default, Clone)]
pub struct User<T: Clone + Default + Sized>(pub Vec<T>);
#[derive(Default, Clone, Debug)]
pub struct User<T: Clone + Default>(pub Vec<T>);

impl IntoParam for User<Author> {
fn into_param(self) -> String {
Expand Down Expand Up @@ -254,6 +257,10 @@ impl<T> User<T>
where
T: Clone + Default + Sized,
{
pub fn new(users: Vec<T>) -> Self {
Self(users)
}

pub fn set_one_user(&mut self, user: T) {
self.0.push(user);
}
Expand Down Expand Up @@ -422,7 +429,7 @@ impl IntoParam for Vec<Languages> {
}
}

#[derive(Clone, Display, EnumIter)]
#[derive(Clone, Display, EnumIter, Debug)]
pub enum PublicationStatus {
#[strum(to_string = "ongoing")]
Ongoing,
Expand Down Expand Up @@ -456,7 +463,7 @@ impl IntoParam for Vec<PublicationStatus> {
}
}

#[derive(Clone)]
#[derive(Clone, Debug)]
pub struct Filters {
pub content_rating: Vec<ContentRating>,
pub publication_status: Vec<PublicationStatus>,
Expand Down Expand Up @@ -540,13 +547,14 @@ impl Filters {
}
}

/// This test may be changed depending on the Mangadex Api
#[cfg(test)]
mod test {

use super::*;

#[test]
fn language_conversion_works() {
fn language_from_filter_list_item() {
let language_formatted = FilterListItem {
name: format!(
"{} {}",
Expand All @@ -561,6 +569,135 @@ mod test {
assert_eq!(conversion, Languages::Spanish);
}

#[test]
fn filter_by_content_rating_works() {
let content_rating = vec![
ContentRating::Safe,
ContentRating::Erotic,
ContentRating::Pornographic,
ContentRating::Suggestive,
];

assert_eq!(
"&contentRating[]=safe&contentRating[]=erotica&contentRating[]=pornographic&contentRating[]=suggestive",
content_rating.into_param()
);
}

#[test]
fn sort_by_works() {
assert_eq!("&order[relevance]=desc", SortBy::BestMatch.into_param());

assert_eq!("&order[createdAt]=asc", SortBy::OldestAdded.into_param());

assert_eq!(
"&order[followedCount]=desc",
SortBy::MostFollows.into_param()
);

assert_eq!(
"&order[followedCount]=asc",
SortBy::FewestFollows.into_param()
);

assert_eq!(
"&order[latestUploadedChapter]=desc",
SortBy::LatestUpload.into_param()
);

assert_eq!(
"&order[latestUploadedChapter]=asc",
SortBy::OldestUpload.into_param()
);

assert_eq!("&order[rating]=desc", SortBy::HighestRating.into_param());

assert_eq!("&order[rating]=asc", SortBy::LowestRating.into_param());

assert_eq!("&order[createdAt]=desc", SortBy::RecentlyAdded.into_param());

assert_eq!("&order[year]=asc", SortBy::YearAscending.into_param());

assert_eq!("&order[year]=desc", SortBy::YearDescending.into_param());

assert_eq!("&order[title]=asc", SortBy::TitleAscending.into_param());

assert_eq!("&order[title]=desc", SortBy::TitleDescending.into_param());
}

#[test]
fn filter_by_magazine_demographic_works() {
let magazine_demographic = vec![
MagazineDemographic::Shounen,
MagazineDemographic::Shoujo,
MagazineDemographic::Josei,
MagazineDemographic::Seinen,
];

assert_eq!("&publicationDemographic[]=shounen&publicationDemographic[]=shoujo&publicationDemographic[]=josei&publicationDemographic[]=seinen", magazine_demographic.into_param());
}

#[test]
fn filter_by_artist_works() {
let sample_artists: Vec<Artist> = vec![
Artist::new("id_artist1".to_string()),
Artist::new("id_artist2".to_string()),
];
let filter_artist = User::<Artist>::new(sample_artists);
assert_eq!(
"&artists[]=id_artist1&artists[]=id_artist2",
filter_artist.into_param()
);
}

#[test]
fn filter_by_author_works() {
let sample_authors: Vec<Author> = vec![
Author::new("id_author1".to_string()),
Author::new("id_author2".to_string()),
];
let filter_artist = User::<Author>::new(sample_authors);
assert_eq!(
"&authors[]=id_author1&authors[]=id_author2",
filter_artist.into_param()
);
}

#[test]
fn filter_by_language_works() {
let _ = PREFERRED_LANGUAGE.set(Languages::default());

let default_language: Vec<Languages> = vec![];
assert_eq!(
"&availableTranslatedLanguage[]=en",
default_language.into_param()
);

let languages: Vec<Languages> = vec![
Languages::English,
Languages::Spanish,
Languages::SpanishLa,
Languages::BrazilianPortuguese,
];

assert_eq!("&availableTranslatedLanguage[]=en&availableTranslatedLanguage[]=es&availableTranslatedLanguage[]=es-la&availableTranslatedLanguage[]=pt-br", languages.into_param());
}

#[test]
fn filter_by_publication_status_works() {
let publication_status: Vec<PublicationStatus> = vec![
PublicationStatus::Ongoing,
PublicationStatus::Hiatus,
PublicationStatus::Completed,
PublicationStatus::Cancelled,
];

assert_eq!(
"&status[]=ongoing&status[]=hiatus&status[]=completed&status[]=cancelled",
publication_status.into_param()
);
}

#[test]
fn filter_by_tags_works() {
let tags = Tags::new(vec![
Expand All @@ -582,7 +719,6 @@ mod test {

#[test]
fn filters_combined_work() {
PREFERRED_LANGUAGE.set(Languages::default()).unwrap();
let filters = Filters::default();

assert_eq!("&availableTranslatedLanguage[]=en&contentRating[]=safe&contentRating[]=suggestive&order[latestUploadedChapter]=desc", filters.into_param());
Expand Down
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#![forbid(unsafe_code)]
use clap::Parser;
use once_cell::sync::Lazy;
use ratatui::backend::CrosstermBackend;
use ratatui_image::picker::{Picker, ProtocolType};
use reqwest::{Client, StatusCode};
#![allow(dead_code)]
#![allow(unused)]
use self::backend::error_log::init_error_hooks;
use self::backend::fetch::{MangadexClient, MANGADEX_CLIENT_INSTANCE};
use self::backend::filter::Languages;
use self::backend::tui::{init, restore, run_app};
use self::backend::{build_data_dir, APP_DATA_DIR};
use self::cli::CliArgs;
use self::global::PREFERRED_LANGUAGE;
use clap::Parser;
use once_cell::sync::Lazy;
use ratatui::backend::CrosstermBackend;
use ratatui_image::picker::{Picker, ProtocolType};
use reqwest::{Client, StatusCode};

mod backend;
mod cli;
Expand Down Expand Up @@ -81,7 +83,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
None => PREFERRED_LANGUAGE.set(Languages::default()).unwrap(),
}


let user_agent = format!(
"manga-tui/{} ({}/{}/{})",
env!("CARGO_PKG_VERSION"),
Expand Down
Loading

0 comments on commit 8bc0c15

Please sign in to comment.