-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce integration tests for badges
- Loading branch information
Showing
4 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use anyhow::Error; | ||
use axum::http::{header, Request, StatusCode}; | ||
use sqlx::PgPool; | ||
use tower_service::Service; | ||
|
||
use repology_webapp::create_app; | ||
|
||
struct Response { | ||
pub status: StatusCode, | ||
pub content_type: Option<String>, | ||
pub body: String, | ||
} | ||
|
||
async fn get(pool: PgPool, uri: &str) -> Result<Response, Error> { | ||
let mut app = create_app(pool).await?; | ||
let response = app | ||
.call( | ||
Request::builder() | ||
.uri(uri) | ||
.method("GET") | ||
.body("".to_owned())?, | ||
) | ||
.await?; | ||
Ok(Response { | ||
status: response.status(), | ||
content_type: response | ||
.headers() | ||
.get(header::CONTENT_TYPE) | ||
.and_then(|value| value.to_str().ok().map(|value| value.into())), | ||
body: std::str::from_utf8(&axum::body::to_bytes(response.into_body(), 10000).await?)? | ||
.into(), | ||
}) | ||
} | ||
|
||
macro_rules! check_code { | ||
($pool:ident, $uri:literal, $code:ident) => { | ||
let resp = get($pool.clone(), $uri).await.unwrap(); | ||
assert_eq!(resp.status, StatusCode::$code); | ||
}; | ||
} | ||
|
||
macro_rules! check_svg { | ||
($pool:ident, $uri:literal $(, $($has:literal)? $(!$hasnt:literal)? )*) => { | ||
let resp = get($pool.clone(), $uri) | ||
.await | ||
.unwrap(); | ||
assert!(sxd_document::parser::parse(&resp.body).is_ok(), "failed to parse XML document"); | ||
assert_eq!(resp.status, StatusCode::OK); | ||
assert_eq!(resp.content_type, Some(mime::IMAGE_SVG.as_ref().into())); | ||
|
||
$( | ||
$( | ||
assert!(resp.body.contains($has)); | ||
)? | ||
$( | ||
assert!(!resp.body.contains($hasnt)); | ||
)? | ||
)* | ||
}; | ||
} | ||
|
||
#[sqlx::test(migrator = "repology_common::MIGRATOR", fixtures("badge_data"))] | ||
async fn test_badge_tiny_repos(pool: PgPool) { | ||
check_code!(pool, "/badge/tiny-repos/nonexistent", NOT_FOUND); | ||
check_svg!( | ||
pool, | ||
"/badge/tiny-repos/nonexistent.svg", | ||
"in repositories", | ||
">0<" | ||
); | ||
check_svg!(pool, "/badge/tiny-repos/zsh.svg", "in repositories", ">2<"); | ||
check_svg!( | ||
pool, | ||
"/badge/tiny-repos/zsh.svg?header=Repository+Count", | ||
!"in repositories", | ||
"Repository Count", | ||
">2<" | ||
); | ||
} | ||
|
||
#[sqlx::test(migrator = "repology_common::MIGRATOR", fixtures("badge_data"))] | ||
async fn test_badge_version_for_repo(pool: PgPool) { | ||
check_code!(pool, "/badge/version_for_repo/freebsd/zsh", NOT_FOUND); | ||
check_svg!( | ||
pool, | ||
"/badge/version-for-repo/freebsd/zsh.svg", | ||
"FreeBSD port", | ||
">1.1<", | ||
"#4c1", | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
INSERT INTO repositories(id, name, sortname, "desc", state, first_seen, last_seen, metadata) VALUES | ||
(1, 'freebsd', 'freebsd', 'FreeBSD', 'active', now(), now(), '{"singular": "FreeBSD port", "type": "repository"}'::json), | ||
(2, 'ubuntu', 'ubuntu', 'Ubuntu', 'active', now(), now(), '{"singular": "Ubuntu package", "type": "repository"}'::json), | ||
(3, 'freshcode', 'freshcode', 'freshcode.club', 'active', now(), now(), '{"singular": "freshcode.club entry", "type": "site"}'::json); | ||
|
||
INSERT INTO packages(effname, version, versionclass, flags, repo, family, trackname, visiblename, projectname_seed, origversion, rawversion, shadow) VALUES | ||
('zsh', '1.1', 1, 0, 'freebsd', 'freebsd', 'zsh', 'zsh', 'zsh', '1.0', '1.0', false), | ||
('zsh', '1.0', 2, 0, 'ubuntu', 'ubuntu', 'zsh', 'zsh', 'zsh', '1.0', '1.0', false), | ||
('zsh', '1.2', 3, 0, 'ubuntu', 'ubuntu', 'zsh', 'zsh', 'zsh', '1.0', '1.0', false); |