Skip to content

Commit

Permalink
test: add tests to news scrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioDMFerreira committed Oct 4, 2023
1 parent e806f86 commit 229d027
Show file tree
Hide file tree
Showing 5 changed files with 443 additions and 90 deletions.
107 changes: 107 additions & 0 deletions news-scrapper/src/http_fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
use async_trait::async_trait;
use bytes::Bytes;
use reqwest::get;
use utils::error::{CommonError, HttpError};

use crate::scrapper::RssFetcher;

pub struct HttpFetcher {}

impl Default for HttpFetcher {
fn default() -> Self {
Self::new()
}
}

impl HttpFetcher {
pub fn new() -> Self {
HttpFetcher {}
}
}

#[async_trait]
impl RssFetcher for HttpFetcher {
async fn fetch(&self, fetch_url: String) -> Result<Bytes, CommonError> {
http_request(fetch_url).await.map_err(|e| e.into())
}
}

pub async fn http_request(url: String) -> Result<Bytes, HttpError> {
// Send an HTTP GET request to a URL
let response = get(url).await.map_err(|v| HttpError {
message: format!("failed to send request: {}", v),
})?;

// Check if the request was successful
if response.status().is_success() {
// Read the response body as a string
let body = response.bytes().await.map_err(|v| HttpError {
message: format!("failed to read response body: {}", v),
})?;

return Ok(body);
}

Err(HttpError {
message: format!("Request was not successful: {}", response.status().as_str()),
})
}

#[cfg(test)]
mod tests {
use super::*;

#[tokio::test]
async fn test_http_request_success() {
let mut server = mockito::Server::new();

// Arrange
let expected_body = "Hello, World!";
let _m = server
.mock("GET", "/")
.with_body(expected_body)
.with_status(200)
.create();

// Act
let result = http_request(server.url().to_string()).await;

// Assert
assert!(result.is_ok());
assert_eq!(result.unwrap(), expected_body.as_bytes().to_vec());
}

#[tokio::test]
async fn test_http_request_failure() {
let mut server = mockito::Server::new();

// Arrange
let _m = server.mock("GET", "/").with_status(500).create();

// Act
let result = http_request(server.url().to_string()).await;

// Assert
assert!(result.is_err());
assert_eq!(
result.unwrap_err().message,
format!("Request was not successful: {}", 500)
);
}

#[tokio::test]
async fn test_http_request_error() {
// Arrange
let url = "invalid url";

// Act
let result = http_request(url.to_string()).await;

// Assert
assert!(result.is_err());
assert!(result
.unwrap_err()
.message
.contains("failed to send request:"));
}
}
4 changes: 4 additions & 0 deletions news-scrapper/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pub mod config;
pub mod http_fetcher;
pub mod news_ingestor;
pub mod scrapper;
19 changes: 12 additions & 7 deletions news-scrapper/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
mod config;
mod news_ingestor;
mod scrapper;
use news_scrapper::{
config::Config,
http_fetcher::HttpFetcher,
news_ingestor::NewsIngestor,
scrapper::{RssFetcher, RssScrapper},
};
use std::{error::Error, sync::Arc, thread};
use tokio_cron_scheduler::{Job, JobScheduler};
use utils::{
Expand All @@ -22,7 +25,7 @@ use utils::{

#[tokio::main]
async fn main() {
let config = config::Config::init();
let config = Config::init();

init_logger(config.logs_path.clone());

Expand All @@ -46,9 +49,11 @@ async fn main() {
events_service.clone(),
));

let feeds_scrapper = Arc::new(scrapper::RssScrapper::default());
let rss_fetcher: Arc<dyn RssFetcher> = Arc::new(HttpFetcher::default());

let feeds_scrapper = Arc::new(RssScrapper::new(rss_fetcher.clone()));

let ingestor = news_ingestor::NewsIngestor::new(service, feeds_scrapper);
let ingestor = NewsIngestor::new(service, feeds_scrapper);

if let Err(err) = setup_cronjobs(&ingestor).await {
panic!("failed setup cronjobs: {}", err);
Expand All @@ -57,7 +62,7 @@ async fn main() {
thread::park();
}

async fn setup_cronjobs(ingestor: &news_ingestor::NewsIngestor) -> Result<(), Box<dyn Error>> {
async fn setup_cronjobs(ingestor: &NewsIngestor) -> Result<(), Box<dyn Error>> {
let ingestor = ingestor.clone();

let sched = JobScheduler::new().await?;
Expand Down
Loading

0 comments on commit 229d027

Please sign in to comment.