Skip to content

Commit

Permalink
Fix bug for parsing companies with no historic of short positions
Browse files Browse the repository at this point in the history
  • Loading branch information
felipet committed Feb 10, 2025
1 parent 3eb1a67 commit 80ffce7
Show file tree
Hide file tree
Showing 4 changed files with 1,611 additions and 6 deletions.
6 changes: 4 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ tracing = "0.1.41"
sqlx = { version = "0.8", features = ["runtime-tokio", "macros", "postgres", "uuid", "chrono"] }
thiserror = "2.0.11"
uuid = { version = "1.13.1", features = ["v4"] }
regex = "1.11.1"
once_cell = "1.20.3"

[dev-dependencies]
tokio = {version = "1.43.0", features = ["rt-multi-thread", "macros"]}
29 changes: 25 additions & 4 deletions src/domain/short_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use crate::domain::{CnmvError, DataProviderError};
use chrono::{DateTime, Utc};
use once_cell::sync::Lazy;
use regex::Regex;
use std::fmt;

/// Wrapper for a Result enum that might contain [ShortPosition] entries.
Expand Down Expand Up @@ -94,14 +96,19 @@ impl ShortResponse {
/// Use this method to check whether a response of the GET method returned valid
/// content or not.
pub fn parse(s: String) -> Result<Self, CnmvError> {
static REG_ISIN: Lazy<Regex> = Lazy::new(|| Regex::new(r"ES(\d){10}").unwrap());

match s.find("No se han encontrado datos disponibles") {
Some(_) => match s.find("Serie histórica") {
Some(_) => Ok(Self(s)),
// Companies with a lack of historic (see Puig Brands) fall in this branch, though it is not an error.
None => match s.find("ES") {
Some(_) => Ok(Self(s)),
None => Err(CnmvError::UnknownCompany),
},
None => {
if REG_ISIN.is_match(&s) {
Ok(Self(s))
} else {
Err(CnmvError::UnknownCompany)
}
}
},
None => Ok(Self(s)),
}
Expand Down Expand Up @@ -134,13 +141,27 @@ mod tests {
.expect("Failed to read the valid short response fixture")
}

#[fixture]
#[once]
fn short_valid_fixture_puig() -> String {
read_to_string("test/fixtures/valid_short_response_puig.html")
.expect("Failed to read the valid short response fixture")
}

#[rstest]
fn parse_valid_response(short_valid_fixture: &String) -> Result<(), CnmvError> {
ShortResponse::parse(short_valid_fixture.clone())?;

Ok(())
}

#[rstest]
fn parse_valid_response_puig(short_valid_fixture_puig: &String) -> Result<(), CnmvError> {
ShortResponse::parse(short_valid_fixture_puig.clone())?;

Ok(())
}

#[rstest]
fn parse_invalid_response(short_invalid_fixture: &String) {
assert!(ShortResponse::parse(short_invalid_fixture.clone()).is_err());
Expand Down
Loading

0 comments on commit 80ffce7

Please sign in to comment.