Skip to content

Commit

Permalink
Writing tests and connecting to query
Browse files Browse the repository at this point in the history
  • Loading branch information
NicholasLYang committed Sep 5, 2024
1 parent b682441 commit 6773794
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 33 deletions.
41 changes: 18 additions & 23 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ async-compression = { version = "0.3.13", default-features = false, features = [
"gzip",
"tokio",
] }
async-graphql = "7.0.7"
async-graphql-axum = "7.0.7"
async-trait = "0.1.64"
atty = "0.2.14"
axum = "0.7.5"
Expand Down
1 change: 1 addition & 0 deletions crates/turborepo-api-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ turborepo-vercel-api-mock = { workspace = true }
workspace = true

[dependencies]
async-graphql = { workspace = true }
bytes.workspace = true
chrono = { workspace = true, features = ["serde"] }
lazy_static = { workspace = true }
Expand Down
9 changes: 5 additions & 4 deletions crates/turborepo-api-client/src/spaces.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use async_graphql::{Enum, SimpleObject};
use chrono::{DateTime, Local};
use reqwest::Method;
use serde::Serialize;
Expand Down Expand Up @@ -29,7 +30,7 @@ pub struct SpaceClientSummary {
pub version: String,
}

#[derive(Debug, Serialize, Default)]
#[derive(Debug, Serialize, Default, SimpleObject)]
#[serde(rename_all = "camelCase")]
pub struct SpacesCacheStatus {
pub status: CacheStatus,
Expand All @@ -38,21 +39,21 @@ pub struct SpacesCacheStatus {
pub time_saved: u64,
}

#[derive(Debug, Serialize, Copy, Clone)]
#[derive(Debug, Serialize, Copy, Clone, PartialEq, Eq, Enum)]
#[serde(rename_all = "UPPERCASE")]
pub enum CacheStatus {
Hit,
Miss,
}

#[derive(Debug, Serialize, Copy, Clone)]
#[derive(Debug, Serialize, Copy, Clone, PartialEq, Eq, Enum)]
#[serde(rename_all = "UPPERCASE")]
pub enum CacheSource {
Local,
Remote,
}

#[derive(Default, Debug, Serialize)]
#[derive(Default, Debug, Serialize, SimpleObject)]
#[serde(rename_all = "camelCase")]
pub struct SpaceTaskSummary {
pub key: String,
Expand Down
6 changes: 6 additions & 0 deletions crates/turborepo-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ version = "0.1.0"
edition = "2021"

[dependencies]
async-graphql = "7.0.7"
camino = { workspace = true }
serde_json = { workspace = true }
sqlx = { version = "0.8.1", features = ["runtime-tokio", "sqlite"] }
tempfile = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["full"] }
turbopath = { workspace = true }
turborepo-api-client = { workspace = true }
uuid = { workspace = true, features = ["v4"] }

[dev-dependencies]
anyhow = { workspace = true }

[lints]
workspace = true
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE IF NOT EXISTS runs (
id TEXT PRIMARY KEY, -- primary key should be uuid
start_time INTEGER NOT NULL,
end_time INTEGER,
start_time BIGINT NOT NULL,
end_time BIGINT,
exit_code INTEGER,
status TEXT NOT NULL,
command TEXT NOT NULL,
Expand Down
162 changes: 160 additions & 2 deletions crates/turborepo-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use async_graphql::SimpleObject;
use camino::Utf8Path;
use sqlx::{Pool, Sqlite, SqlitePool};
use sqlx::{sqlite::SqliteRow, Pool, Row, Sqlite, SqlitePool};
use thiserror::Error;
use turbopath::{AbsoluteSystemPath, AbsoluteSystemPathBuf};
use turborepo_api_client::spaces::{CreateSpaceRunPayload, RunStatus, SpaceTaskSummary};
use turborepo_api_client::spaces::{
CreateSpaceRunPayload, RunStatus, SpaceTaskSummary, SpacesCacheStatus,
};
use uuid::Uuid;

#[derive(Debug, Error)]
Expand All @@ -15,6 +18,24 @@ pub enum Error {
Serialize(#[from] serde_json::Error),
}

#[derive(Debug, Default, SimpleObject)]
pub struct Run {
id: String,
start_time: i64,
end_time: Option<i64>,
exit_code: Option<u32>,
status: String,
command: String,
package_inference_root: Option<String>,
context: String,
git_branch: Option<String>,
git_sha: Option<String>,
origination_user: String,
client_id: String,
client_name: String,
client_version: String,
}

#[derive(Clone)]
pub struct DatabaseHandle {
pool: Pool<Sqlite>,
Expand All @@ -34,6 +55,71 @@ impl DatabaseHandle {
Ok(Self { pool })
}

pub async fn get_runs(&self, limit: Option<u32>) -> Result<Vec<Run>, Error> {
let query = if let Some(limit) = limit {
sqlx::query(
"SELECT id, start_time, end_time, exit_code, status, command, \
package_inference_root, context, git_branch, git_sha, origination_user, \
client_id, client_name, client_version FROM runs LIMIT ?",
)
.bind(limit)
} else {
sqlx::query(
"SELECT id, start_time, end_time, exit_code, status, command, \
package_inference_root, context, git_branch, git_sha, origination_user, \
client_id, client_name, client_version FROM runs",
)
};

Ok(query
.map(|row| Run {
id: row.get("id"),
start_time: row.get("start_time"),
end_time: row.get("end_time"),
exit_code: row.get("exit_code"),
status: row.get("status"),
command: row.get("command"),
package_inference_root: row.get("package_inference_root"),
context: row.get("context"),
git_branch: row.get("git_branch"),
git_sha: row.get("git_sha"),
origination_user: row.get("origination_user"),
client_id: row.get("client_id"),
client_name: row.get("client_name"),
client_version: row.get("client_version"),
})
.fetch_all(&self.pool)
.await?)
}

pub async fn get_tasks_for_run(&self, run_id: Uuid) -> Result<Vec<SpaceTaskSummary>, Error> {
let query = sqlx::query(
"SELECT key, name, workspace, hash, start_time, end_time, cache_status, exit_code, \
logs FROM tasks WHERE run_id = ?",
)
.bind(run_id.to_string());
Ok(query
.map(|row: SqliteRow| SpaceTaskSummary {
key: row.get("key"),
name: row.get("name"),
workspace: row.get("workspace"),
hash: row.get("hash"),
start_time: row.get("start_time"),
end_time: row.get("end_time"),
cache: SpacesCacheStatus {
status: row.get("cache_status"),

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `CacheStatus: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `CacheStatus: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `CacheStatus: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `CacheStatus: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `CacheStatus: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `CacheStatus: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `CacheStatus: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 110 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `CacheStatus: sqlx::Type<Sqlite>` is not satisfied
source: None,
time_saved: row.get("time_saved"),
},
exit_code: row.get("exit_code"),
dependencies: row.get("dependencies"),

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 115 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied
dependents: row.get("dependents"),

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (ubuntu-latest)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (macos-12)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo Integration (windows-latest)

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `Vec<std::string::String>: sqlx::Decode<'_, Sqlite>` is not satisfied

Check failure on line 116 in crates/turborepo-db/src/lib.rs

View workflow job for this annotation

GitHub Actions / Turborepo rust check

the trait bound `Vec<std::string::String>: sqlx::Type<Sqlite>` is not satisfied
logs: row.get("logs"),
})
.fetch_all(&self.pool)
.await?)
}

pub async fn create_run(&self, payload: &CreateSpaceRunPayload) -> Result<Uuid, Error> {
let id = Uuid::new_v4();
sqlx::query(
Expand Down Expand Up @@ -111,3 +197,75 @@ impl DatabaseHandle {
Ok(())
}
}

#[cfg(test)]
mod test {
use turbopath::AbsoluteSystemPath;
use turborepo_api_client::spaces::{
CacheStatus, CreateSpaceRunPayload, RunStatus, SpaceClientSummary, SpaceRunType,
SpaceTaskSummary, SpacesCacheStatus,
};

use crate::DatabaseHandle;

#[tokio::test]
async fn test_get_runs() -> Result<(), anyhow::Error> {
let dir = tempfile::tempdir().unwrap();

let db = DatabaseHandle::new(
dir.path().try_into()?,
AbsoluteSystemPath::from_std_path(dir.path())?,
)
.await?;

let id = db
.create_run(&CreateSpaceRunPayload {
start_time: 0,
status: RunStatus::Running,
command: "test".to_string(),
package_inference_root: "test".to_string(),
run_context: "",
git_branch: None,
git_sha: None,
ty: SpaceRunType::Turbo,
user: "test".to_string(),
client: SpaceClientSummary {
id: "my-id",
name: "turbo",
version: "1.0.0".to_string(),
},
})
.await
.unwrap();
let runs = db.get_runs(None).await.unwrap();
assert_eq!(runs.len(), 1);
assert_eq!(runs[0].id.len(), 36);
assert_eq!(runs[0].git_sha, Some("test".to_string()));
assert_eq!(runs[0].status, "RUNNING".to_string());

db.finish_task(
id.clone(),
&SpaceTaskSummary {
key: "test#build".to_string(),
name: "test".to_string(),
workspace: "test".to_string(),
hash: "test".to_string(),
start_time: 0,
end_time: 0,
cache: SpacesCacheStatus {
status: CacheStatus::Miss,
source: None,
time_saved: 0,
},
exit_code: Some(0),
dependencies: vec![],
dependents: vec![],
logs: "".to_string(),
},
)
.await
.unwrap();

Ok(())
}
}
4 changes: 2 additions & 2 deletions crates/turborepo-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ turborepo-vercel-api-mock = { workspace = true }
workspace = true

[dependencies]
async-graphql = "7.0.7"
async-graphql-axum = "7.0.7"
async-graphql = { workspace = true }
async-graphql-axum = { workspace = true }
atty = { workspace = true }
axum = { workspace = true }
biome_deserialize = { workspace = true }
Expand Down
Loading

0 comments on commit 6773794

Please sign in to comment.