feat: paginated introduced starchart instances list with tests

This commit is contained in:
Aravinth Manivannan 2023-02-28 16:13:41 +05:30
parent 9ff4788b69
commit 83175b333a
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
4 changed files with 60 additions and 0 deletions

View file

@ -58,6 +58,13 @@ pub mod dev {
pub use async_trait::async_trait;
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// Data related to a Starchart instance
pub struct Starchart {
/// URL of the Starchart instance
pub instance_url: String,
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
/// create a new forge on the database
pub struct CreateForge<'a> {
@ -232,6 +239,13 @@ pub trait SCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
/// Add Starchart instance to introducer
async fn add_starchart_to_introducer(&self, url: &Url) -> DBResult<()>;
/// Get all introduced Starchart instances
async fn get_all_introduced_starchart_instances(
&self,
offset: u32,
limit: u32,
) -> DBResult<Vec<Starchart>>;
}
/// Trait to clone SCDatabase

View file

@ -108,3 +108,15 @@ pub async fn forge_type_exists_helper<T: SCDatabase>(db: &T) {
println!("Testing forge implementation exists for: {}", f.to_str());
assert!(db.forge_type_exists(&f).await.unwrap());
}
/// test if all instance introducer methods work
pub async fn instance_introducer_helper<T: SCDatabase>(db: &T, instance_url: &Url) {
db.add_starchart_to_introducer(instance_url).await.unwrap();
let instances = db
.get_all_introduced_starchart_instances(0, 100)
.await
.unwrap();
assert!(instances
.iter()
.any(|i| i.instance_url == instance_url.as_str()));
}

View file

@ -831,6 +831,29 @@ impl SCDatabase for Database {
.map_err(map_register_err)?;
Ok(())
}
/// Get all introduced Starchart instances
async fn get_all_introduced_starchart_instances(
&self,
offset: u32,
limit: u32,
) -> DBResult<Vec<Starchart>> {
let s = sqlx::query_as!(
Starchart,
"SELECT
instance_url
FROM
starchart_introducer
LIMIT $1 OFFSET $2;
",
limit,
offset
)
.fetch_all(&self.pool)
.await
.map_err(|e| DBError::DBError(Box::new(e)))?;
Ok(s)
}
}
fn now_unix_time_stamp() -> i64 {

View file

@ -89,6 +89,17 @@ async fn everything_works() {
.await;
}
#[actix_rt::test]
async fn introducer_works() {
let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var");
let pool_options = SqlitePoolOptions::new().max_connections(2);
let connection_options = ConnectionOptions::Fresh(Fresh { pool_options, url });
let db = connection_options.connect().await.unwrap();
let instance_url = Url::parse("https://introducer_works_sqlite_sqlx.example.com").unwrap();
instance_introducer_helper(&db, &instance_url).await;
}
#[actix_rt::test]
async fn forge_type_exists() {
let url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var");