Skip to content

Commit

Permalink
chore: refactor tests folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
eredotpkfr committed Sep 17, 2024
1 parent cd043e4 commit 1070460
Show file tree
Hide file tree
Showing 24 changed files with 375 additions and 388 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ strum = "0.26.3"
strum_macros = "0.26.4"

[dev-dependencies]
automod = "1.0.14"
stubr = "0.6.2"
5 changes: 1 addition & 4 deletions tests/cache_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
mod common;

use common::constants::TEST_URL;
use reqwest::header::{HeaderMap, HeaderValue, CONTENT_LENGTH, USER_AGENT};
use std::time::Duration;
use strum::IntoEnumIterator;
Expand Down Expand Up @@ -30,7 +27,7 @@ mod requesters {
(USER_AGENT, HeaderValue::from_static("x-api-key")),
(CONTENT_LENGTH, HeaderValue::from_static("10000")),
]),
proxy: Some(TEST_URL.to_string()),
proxy: Some("http://foo.com".to_string()),
};

for requester in cache::ALL_REQUESTERS.values() {
Expand Down
20 changes: 20 additions & 0 deletions tests/extractors/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
pub mod constants {
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
pub const TEST_BAZ_SUBDOMAIN: &str = "baz.foo.com";
pub const READ_ERROR: &str = "Cannot read file!";
}

pub mod funcs {
use super::constants::READ_ERROR;
use std::fs;
use std::path::{Path, PathBuf};

fn testdata_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("testing/testdata")
}

pub fn read_testdata(path: &str) -> String {
fs::read_to_string(testdata_path().join(path)).expect(READ_ERROR)
}
}
33 changes: 33 additions & 0 deletions tests/extractors/html_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::common::{
constants::{TEST_BAR_SUBDOMAIN, TEST_BAZ_SUBDOMAIN, TEST_DOMAIN},
funcs::read_testdata,
};
use subscan::extractors::html::HTMLExtractor;
use subscan::interfaces::extractor::SubdomainExtractorInterface;

#[tokio::test]
async fn extract_without_removes() {
let html = read_testdata("html/subdomains.html");

let selector = String::from("article > div > a > span:first-child");
let extractor = HTMLExtractor::new(selector, vec![]);
let result = extractor.extract(html, TEST_DOMAIN.to_string()).await;

assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}

#[tokio::test]
async fn extract_with_removes() {
let html = read_testdata("html/subdomains-with-removes.html");

let selector = String::from("article > div > a > span");
let extractor = HTMLExtractor::new(selector, vec!["<br>".to_string()]);
let result = extractor.extract(html, TEST_DOMAIN.to_string()).await;

let expected = [
TEST_BAR_SUBDOMAIN.to_string(),
TEST_BAZ_SUBDOMAIN.to_string(),
];

assert_eq!(result, expected.into());
}
1 change: 1 addition & 0 deletions tests/extractors/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
automod::dir!("tests/extractors");
33 changes: 33 additions & 0 deletions tests/extractors/regex_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_BAZ_SUBDOMAIN, TEST_DOMAIN};
use subscan::extractors::regex::RegexExtractor;
use subscan::interfaces::extractor::SubdomainExtractorInterface;

#[tokio::test]
async fn extract_one_test() {
let extractor = RegexExtractor::default();

let matches = String::from(TEST_BAR_SUBDOMAIN);
let no_match = String::from("foobarbaz");

assert!(extractor
.extract_one(matches, TEST_DOMAIN.to_string())
.is_some());
assert!(extractor
.extract_one(no_match, TEST_DOMAIN.to_string())
.is_none());
}

#[tokio::test]
async fn extract_test() {
let content = String::from("bar.foo.com\nbaz.foo.com");

let extractor = RegexExtractor::default();
let result = extractor.extract(content, TEST_DOMAIN.to_string()).await;

let expected = [
TEST_BAR_SUBDOMAIN.to_string(),
TEST_BAZ_SUBDOMAIN.to_string(),
];

assert_eq!(result, expected.into());
}
82 changes: 0 additions & 82 deletions tests/extractors_test.rs

This file was deleted.

66 changes: 0 additions & 66 deletions tests/module_engines_test.rs

This file was deleted.

31 changes: 0 additions & 31 deletions tests/module_generics_test.rs

This file was deleted.

23 changes: 2 additions & 21 deletions tests/common/mod.rs → tests/modules/common.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,11 @@
pub mod constants {
#![allow(dead_code)]
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
pub const TEST_BAZ_SUBDOMAIN: &str = "baz.foo.com";
pub const TEST_MODULE_NAME: &str = "foo-module";
pub const TEST_URL: &str = "http://foo.com";
}

pub mod funcs {
#![allow(dead_code)]
use std::fs;
use std::path::{Path, PathBuf};

const READ_ERROR: &str = "Cannot read file!";

fn testdata_path() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("testing/testdata")
}

pub fn read_testdata(path: &str) -> String {
fs::read_to_string(testdata_path().join(path)).expect(READ_ERROR)
}
pub const TEST_DOMAIN: &str = "foo.com";
pub const TEST_BAR_SUBDOMAIN: &str = "bar.foo.com";
}

pub mod mocks {
#![allow(dead_code)]
use super::constants::TEST_MODULE_NAME;
use reqwest::Url;
use subscan::{
Expand Down
16 changes: 16 additions & 0 deletions tests/modules/engines/bing_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{interfaces::module::SubscanModuleInterface, modules::engines::bing};

#[tokio::test]
#[stubr::mock("module/engines/bing.json")]
async fn bing_run_test() {
let mut bing = bing::Bing::new();

bing.url = Url::parse(stubr.path("/search").as_str()).unwrap();

let result = bing.run(TEST_DOMAIN.to_string()).await;

assert_eq!(bing.name().await, "Bing");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
20 changes: 20 additions & 0 deletions tests/modules/engines/duckduckgo_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{
cache::requesters, enums::RequesterType, interfaces::module::SubscanModuleInterface,
modules::engines::duckduckgo,
};

#[tokio::test]
#[stubr::mock("module/engines/duckduckgo.json")]
async fn duckduckgo_run_test() {
let mut duckduckgo = duckduckgo::DuckDuckGo::new();

duckduckgo.requester = requesters::get_by_type(&RequesterType::HTTPClient);
duckduckgo.url = Url::parse(stubr.uri().as_str()).unwrap();

let result = duckduckgo.run(TEST_DOMAIN.to_string()).await;

assert_eq!(duckduckgo.name().await, "DuckDuckGo");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
16 changes: 16 additions & 0 deletions tests/modules/engines/google_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::common::constants::{TEST_BAR_SUBDOMAIN, TEST_DOMAIN};
use reqwest::Url;
use subscan::{interfaces::module::SubscanModuleInterface, modules::engines::google};

#[tokio::test]
#[stubr::mock("module/engines/google.json")]
async fn foo_test() {
let mut google = google::Google::new();

google.url = Url::parse(stubr.path("/search").as_str()).unwrap();

let result = google.run(TEST_DOMAIN.to_string()).await;

assert_eq!(google.name().await, "Google");
assert_eq!(result, [TEST_BAR_SUBDOMAIN.to_string()].into());
}
1 change: 1 addition & 0 deletions tests/modules/engines/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
automod::dir!("tests/modules/engines");
Loading

0 comments on commit 1070460

Please sign in to comment.