Skip to content

Commit

Permalink
Introduce integration tests for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Sep 7, 2024
1 parent 15680e1 commit 0eeae15
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 4 deletions.
32 changes: 28 additions & 4 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions repology-webapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ ttf-parser = "0.24.1"
strum_macros = "0.26.4"
strum = { version = "0.26.3", features = ["strum_macros", "derive"] }
chrono = { version = "0.4.38", default-features = false, features = ["std", "now"] }

[dev-dependencies]
tower-service = "0.3.3"
sxd-document = "0.3.2"
91 changes: 91 additions & 0 deletions repology-webapp/tests/badges.rs
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",
);
}
9 changes: 9 additions & 0 deletions repology-webapp/tests/fixtures/badge_data.sql
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);

0 comments on commit 0eeae15

Please sign in to comment.