Skip to content

Commit

Permalink
Added Get_category_by_name tested
Browse files Browse the repository at this point in the history
  • Loading branch information
0xCAB0 committed Jul 8, 2023
1 parent 5d4e151 commit 68c1316
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 22 deletions.
21 changes: 20 additions & 1 deletion src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ use bb8_postgres::{
use postgres_types::ToSql;
use thiserror::Error;

use crate::model::filter::brand::Brand;
use crate::model::filter::{brand::Brand, category::Category};

const GET_BRAND_BY_NAME: &str = include_str!("sql_queries/GET_BRAND_BY_NAME.sql");
const GET_BRANDS_BY_NAME: &str = include_str!("sql_queries/GET_BRANDS_BY_NAME.sql");
const GET_CATEGORY_BY_NAME: &str = include_str!("sql_queries/GET_CATEGORY_BY_NAME.sql");

#[derive(Error, Debug)]
pub enum DbError {
Expand Down Expand Up @@ -85,4 +86,22 @@ where

Ok(brands)
}

/**
*
*
*
*/
pub async fn get_category_by_title<S: AsRef<str> + Sync + ToSql + Display>(
&self,
name: &S,
) -> Result<Category, DbError> {
let conn = self.pool.get().await?;
let row: Row = conn.query_one(GET_CATEGORY_BY_NAME, &[&name]).await?;

// Funciona porque está implementado From<Row> for Category
let cat: Category = row.into();

Ok(cat)
}
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ async fn main() {

let filter: Filter = Filter::builder().search_text(String::from("shoes")).build();

let items = vinted.get_items(&filter, Some(5)).await.unwrap();
let items = vinted.get_items(&filter, 5).await.unwrap();

print!("{items:?}");

Expand Down
22 changes: 19 additions & 3 deletions src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ pub enum CookieError {
GetCookiesError,
}

#[derive(Error, Debug)]
pub enum VintedWrapperError {
#[error(transparent)]
CookiesError(#[from] CookieError),
#[error("Number of items must be non-zero value")]
ItemNumberError,
}

impl From<reqwest::Error> for VintedWrapperError{
fn from(value: reqwest::Error) -> Self {
VintedWrapperError::CookiesError(CookieError::ReqWestError(value))
}
}

const DOMAINS: [&str; 18] = [
"fr", "be", "es", "lu", "nl", "lt", "de", "at", "it", "uk", "pt", "com", "cz", "sk", "pl",
"se", "ro", "hu",
Expand Down Expand Up @@ -200,9 +214,11 @@ impl<'a> VintedWrapper<'a> {
pub async fn get_items(
&self,
filters: &Filter,
num: Option<i32>,
) -> Result<Items, CookieError> {
let num = num.unwrap_or(1);
num: u32,
) -> Result<Items, VintedWrapperError> {
if num == 0 {
return Err(VintedWrapperError::ItemNumberError);
}

let domain: &str = &format!("https://www.vinted.{}/api/v2/catalog/items", self.host);

Expand Down
1 change: 1 addition & 0 deletions src/sql_queries/GET_CATEGORY_BY_NAME.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM CATEGORY WHERE UPPER(title) = UPPER($1);
28 changes: 25 additions & 3 deletions src/tests/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{db::DbController, model::filter::brand::Brand};
use crate::{
db::DbController,
model::filter::{brand::Brand, category::Category},
};
use bb8_postgres::tokio_postgres::NoTls;

const DB_URL: &str = "postgres://postgres:postgres@localhost/vinted-rs";
Expand All @@ -9,7 +12,6 @@ async fn test_get_brand_by_name() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();

let brand_name: String = String::from("adidas");
//let brand_name : &str = "adidas";

let b: Brand = db.get_brand_by_name(&brand_name).await.unwrap();

Expand All @@ -28,9 +30,29 @@ async fn test_get_brands_by_name() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();

let brand_name: String = String::from("adidas");
//let brand_name : &str = "adidas";

let brands: Vec<Brand> = db.get_brands_by_name(&brand_name).await.unwrap();

assert_eq!(brands.len(), 38);
}

#[tokio::test]
async fn test_get_category_by_name() {
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();

let category_name: String = String::from("Women");

let c = db.get_category_by_title(&category_name).await.unwrap();

assert_eq!(
c,
Category::builder()
.code(String::from("WOMEN_ROOT"))
.url(String::from("/women"))
.url_en(String::from("/women"))
.title(String::from("Women"))
.id(1904)
.parent_id(0)
.build()
);
}
54 changes: 41 additions & 13 deletions src/tests/queries.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::db::DbController;
use crate::model::filter::Filter;
use crate::{CookieError, VintedWrapper};
use crate::queries::VintedWrapperError;
use crate::{VintedWrapper};
use bb8_postgres::tokio_postgres::NoTls;

const DB_URL: &str = "postgres://postgres:postgres@localhost/vinted-rs";
Expand All @@ -12,14 +13,14 @@ async fn test_get_item_query_text() {

let filter: Filter = Filter::builder().search_text(String::from("shoes")).build();

match vinted.get_items(&filter, None).await {
match vinted.get_items(&filter, 1).await {
// Limitado el numero de elementos a 1
Ok(items) => {
assert_eq!(items.items.len(), 1);
}
Err(err) => match err {
CookieError::ReqWestError(_) => unreachable!(),
CookieError::GetCookiesError => (),
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}
Expand All @@ -29,18 +30,17 @@ async fn test_get_item_brands() {
let vinted = VintedWrapper::new();
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let brand = db.get_brand_by_name(&String::from("Adidas")).await.unwrap();

let filter: Filter = Filter::builder().brand_ids(vec![brand.id]).build();


match vinted.get_items(&filter, None).await {
match vinted.get_items(&filter, 1).await {
// Limitado el numero de elementos a 1
Ok(items) => {
assert_eq!(items.items.get(0).unwrap().brand_title, brand.title);
}
Err(err) => match err {
CookieError::ReqWestError(_) => unreachable!(),
CookieError::GetCookiesError => (),
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}
Expand All @@ -50,18 +50,46 @@ async fn test_get_items_brands() {
let vinted = VintedWrapper::new();
let db: DbController<NoTls> = DbController::new(DB_URL, POOL_SIZE, NoTls).await.unwrap();
let brand = db.get_brand_by_name(&String::from("Adidas")).await.unwrap();

let filter: Filter = Filter::builder().brand_ids(vec![brand.id]).build();

match vinted.get_items(&filter, Some(10)).await {
match vinted.get_items(&filter, 10).await {
Ok(items) => {
for item in items.items {
assert_eq!(item.brand_title, brand.title);
}
}
Err(err) => match err {
CookieError::ReqWestError(_) => unreachable!(),
CookieError::GetCookiesError => (),
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}

#[tokio::test]
async fn test_get_items_catalogs_no_db() {
let vinted = VintedWrapper::new();
//Woman elements
let filter: Filter = Filter::builder().catalog_ids(vec![1904]).build();
let substrings = vec![
"women", "mujer", "femme", "kobiety", "donna", "moterims", "noi",
];

match vinted.get_items(&filter, 10).await {
Ok(items) => {
let all_ok = items.items.iter().all(|item| {
let ok_once = substrings.iter().any(|w| item.url.contains(w));
if !ok_once {
println!("{}", item.url)
}
ok_once
});

assert!(all_ok);
}
Err(err) => match err {
VintedWrapperError::ItemNumberError => unreachable!(),
VintedWrapperError::CookiesError(_) => (),
},
};
}
2 changes: 1 addition & 1 deletion vinted-db-feeder

0 comments on commit 68c1316

Please sign in to comment.