Skip to content

Commit

Permalink
api for test reports
Browse files Browse the repository at this point in the history
  • Loading branch information
DenysVuika committed Jul 4, 2023
1 parent b88d05f commit f2bbcbc
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
3 changes: 2 additions & 1 deletion src/assets/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ CREATE TABLE IF NOT EXISTS tests (
sid INTEGER NOT NULL,
path TEXT NOT NULL,
url TEXT,
kind TEXT
kind TEXT,
cases INTEGER DEFAULT 0
);

CREATE TABLE IF NOT EXISTS test_cases (
Expand Down
56 changes: 46 additions & 10 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,8 +416,8 @@ pub fn create_test(
kind: TestKind,
) -> Result<i64> {
conn.execute(
"INSERT INTO tests (sid, path, url, kind) VALUES (?1, ?2, ?3, ?4)",
params![sid, path, url, kind],
"INSERT INTO tests (sid, path, url, kind, cases) VALUES (?1, ?2, ?3, ?4, ?5)",
params![sid, path, url, kind, test_cases.len()],
)?;
let test_id = conn.last_insert_rowid();
let mut stmt = conn.prepare("INSERT INTO test_cases (test_id, name) VALUES (?1, ?2)")?;
Expand Down Expand Up @@ -550,14 +550,8 @@ pub fn get_dependencies(conn: &Connection, sid: i64) -> Result<Vec<PackageDepend
}

pub fn get_tests(conn: &Connection, sid: i64, kind: TestKind) -> Result<Vec<TestEntry>> {
let mut stmt = conn.prepare(
r#"
SELECT t.path, COUNT(DISTINCT tc.name) as cases, t.url FROM tests t
LEFT JOIN test_cases tc on t.OID = tc.test_id
WHERE t.sid=:sid AND t.kind=:kind
GROUP BY t.path
"#,
)?;
let mut stmt =
conn.prepare("SELECT path, cases, url FROM tests WHERE sid=:sid AND kind=:kind")?;

let rows = stmt
.query_map(named_params! {":sid": sid, ":kind": kind}, |row| {
Expand All @@ -572,6 +566,48 @@ pub fn get_tests(conn: &Connection, sid: i64, kind: TestKind) -> Result<Vec<Test
Ok(rows)
}

#[derive(Serialize)]
pub struct TestInfo {
pub oid: i64,
pub tag: String,
pub unit_tests: i64,
pub unit_cases: i64,
pub e2e_tests: i64,
pub e2e_cases: i64,
}

pub fn get_tests_stats(conn: &Connection, pid: i64) -> Result<Vec<TestInfo>> {
let mut stmt = conn.prepare(
"
SELECT
snapshots.OID AS sid, snapshots.tag,
SUM(case when tests.kind='unit' then 1 else 0 end) unit_tests,
SUM(case when tests.kind='unit' then tests.cases else 0 end) unit_cases,
SUM(case when tests.kind='e2e' then 1 else 0 end) e2e_tests,
SUM(case when tests.kind='e2e' then tests.cases else 0 end) e2e_cases
FROM snapshots
LEFT JOIN tests ON tests.sid = snapshots.OID
WHERE snapshots.pid = :pid
GROUP BY snapshots.OID, snapshots.timestamp
ORDER BY snapshots.timestamp",
)?;

let rows = stmt
.query_map(named_params! {":pid": pid }, |row| {
Ok(TestInfo {
oid: row.get(0)?,
tag: row.get(1)?,
unit_tests: row.get(2)?,
unit_cases: row.get(3)?,
e2e_tests: row.get(4)?,
e2e_cases: row.get(5)?,
})
})?
.filter_map(|entry| entry.ok())
.collect();
Ok(rows)
}

pub fn get_ng_entities(conn: &Connection, sid: i64, kind: NgKind) -> Result<Vec<NgEntity>> {
let mut stmt = conn
.prepare("SELECT path, url, standalone FROM ng_entities WHERE sid=:sid AND kind=:kind;")?;
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ pub async fn run(config: &Config) -> Result<()> {
tags.push(project.branch()?);

// let tags: Vec<String> = vec![
// // "3.1.0".to_owned(),
// // "4.0.0".to_owned(),
// "3.1.0".to_owned(),
// "4.0.0".to_owned(),
// // "4.0.0-A.1".to_owned(),
// // "4.0.0-A.2".to_owned(),
// // "4.0.0-A.3".to_owned(),
// // "develop".to_owned(),
// // "4.0.0-A.3".to_owned(),
// "develop".to_owned(),
// // "1.6.0-beta6".to_owned(),
// ];

Expand Down
12 changes: 12 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub async fn run_server(working_dir: PathBuf, open: bool) -> Result<()> {
.service(get_projects)
.service(get_tags)
.service(get_project_warnings)
.service(get_project_tests)
.service(get_angular_metadata)
.service(get_snapshots)
.service(get_angular)
Expand Down Expand Up @@ -92,6 +93,17 @@ async fn get_project_warnings(
Ok(web::Json(result))
}

#[get("/reports/{pid}/tests")]
async fn get_project_tests(
path: web::Path<i64>,
data: web::Data<AppState>,
) -> Result<impl Responder> {
let pid = path.into_inner();
let conn = &data.connection;
let result = db::get_tests_stats(conn, pid).unwrap_or(vec![]);
Ok(web::Json(result))
}

#[get("/reports/{pid}/angular")]
async fn get_angular_metadata(
path: web::Path<i64>,
Expand Down

0 comments on commit f2bbcbc

Please sign in to comment.