Skip to content

Commit

Permalink
pmrmodel: set default file and view
Browse files Browse the repository at this point in the history
- Address the need to correctly constrain the relevant values to
  acceptable ranges, i.e. the file/view is actually part of the parent.
- Rough documentation on the traits.
  • Loading branch information
metatoaster committed Jul 12, 2023
1 parent b819af4 commit 400bfa8
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 5 deletions.

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

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

36 changes: 36 additions & 0 deletions pmrmodel/src/model/db/sqlite/exposure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,30 @@ WHERE workspace_id = ?1
Ok(rec.into())
}

async fn set_default_file_sqlite(
sqlite: &SqliteBackend,
id: i64,
file_id: i64,
) -> Result<bool, BackendError> {
let rows_affected = sqlx::query!(r#"
UPDATE exposure
SET default_file_id = ?2
WHERE id = ?1
AND ?2 IN (
SELECT id
FROM exposure_file
WHERE exposure_id = ?1
)
"#,
id,
file_id,
)
.execute(&*sqlite.pool)
.await?
.rows_affected();
Ok(rows_affected > 0)
}

#[async_trait]
impl ExposureBackend for SqliteBackend {
async fn insert(
Expand Down Expand Up @@ -148,6 +172,18 @@ impl ExposureBackend for SqliteBackend {
id,
).await
}

async fn set_default_file(
&self,
id: i64,
file_id: i64,
) -> Result<bool, BackendError> {
set_default_file_sqlite(
&self,
id,
file_id,
).await
}
}

#[cfg(test)]
Expand Down
52 changes: 49 additions & 3 deletions pmrmodel/src/model/db/sqlite/exposure_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,31 @@ WHERE exposure_id = ?1
Ok(rec.into())
}


async fn set_default_view_sqlite(
sqlite: &SqliteBackend,
id: i64,
view_id: i64,
) -> Result<bool, BackendError> {
let rows_affected = sqlx::query!(r#"
UPDATE exposure_file
SET default_view_id = ?2
WHERE id = ?1
AND ?2 IN (
SELECT id
FROM exposure_file_view
WHERE exposure_file_id = ?1
)
"#,
id,
view_id,
)
.execute(&*sqlite.pool)
.await?
.rows_affected();
Ok(rows_affected > 0)
}

#[async_trait]
impl ExposureFileBackend for SqliteBackend {
async fn insert(
Expand Down Expand Up @@ -126,13 +151,26 @@ impl ExposureFileBackend for SqliteBackend {
id,
).await
}

async fn set_default_view(
&self,
id: i64,
view_id: i64
) -> Result<bool, BackendError> {
set_default_view_sqlite(
&self,
id,
view_id,
).await
}
}

#[cfg(test)]
pub(crate) mod testing {
use pmrmodel_base::{
exposure::{
ExposureFile,
traits::ExposureBackend,
traits::ExposureFileBackend,
},
};
Expand Down Expand Up @@ -182,20 +220,21 @@ pub(crate) mod testing {
}

#[async_std::test]
async fn test_list_exposure_file() -> anyhow::Result<()> {
async fn test_using_exposure_files() -> anyhow::Result<()> {
let backend = SqliteBackend::from_url("sqlite::memory:")
.await?
.run_migration_profile(Profile::Pmrapp)
.await?;
let efb: &dyn ExposureFileBackend = &backend;
let eb: &dyn ExposureBackend = &backend;

let w1 = make_example_workspace(&backend).await?;
let e1 = make_example_exposure(&backend, w1).await?;
let _ = make_example_exposure_file(
let ef0 = make_example_exposure_file(
&backend, e1, "README.md").await?;

let e2 = make_example_exposure(&backend, w1).await?;
make_example_exposure_file(&backend, e2, "README.md").await?;
let ef1 = make_example_exposure_file(&backend, e2, "README.md").await?;
make_example_exposure_file(&backend, e2, "model.cellml").await?;
make_example_exposure_file(&backend, e2, "lib/units.cellml").await?;
let results = efb.list_for_exposure(e2).await?;
Expand All @@ -207,6 +246,13 @@ pub(crate) mod testing {
.collect::<Vec<_>>(),
);

// Matching pairing of exposure id and file
assert!(eb.set_default_file(e1, ef0).await?);
assert!(eb.set_default_file(e2, ef1).await?);
// Mismatching pairing of exposure id and file
assert!(!eb.set_default_file(e2, ef0).await?);
assert!(!eb.set_default_file(e1, ef1).await?);

Ok(())
}

Expand Down
19 changes: 17 additions & 2 deletions pmrmodel/src/model/db/sqlite/exposure_file_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ impl ExposureFileViewBackend for SqliteBackend {
}
}

// TODO generalize the testing modules across related modules (actually
// all db access) and instantiate the test of all db implementations
// against all relevant tests.
#[cfg(test)]
pub(crate) mod testing {
use pmrmodel_base::{
exposure::{
ExposureFileView,
traits::ExposureFileBackend,
traits::ExposureFileViewBackend,
},
};
Expand Down Expand Up @@ -175,18 +179,21 @@ pub(crate) mod testing {
}

#[async_std::test]
async fn test_list_exposure_file_view() -> anyhow::Result<()> {
async fn test_using_exposure_file_view() -> anyhow::Result<()> {
let backend = SqliteBackend::from_url("sqlite::memory:")
.await?
.run_migration_profile(Profile::Pmrapp)
.await?;
let efvb: &dyn ExposureFileViewBackend = &backend;
let efb: &dyn ExposureFileBackend = &backend;

let w1 = make_example_workspace(&backend).await?;
let _ = make_example_exposure(&backend, w1).await?;
let e2 = make_example_exposure(&backend, w1).await?;
let e2f1 = make_example_exposure_file(&backend, e2, "README.md").await?;
make_example_exposure_file_view(&backend, e2f1, "view").await?;
let e2f1v1 = make_example_exposure_file_view(
&backend, e2f1, "view").await?;

let e2f2 = make_example_exposure_file(
&backend, e2, "model.cellml").await?;
make_example_exposure_file_view(&backend, e2f2, "model").await?;
Expand All @@ -209,6 +216,14 @@ pub(crate) mod testing {
views,
);

// Matching pairing of exposure file and view
assert!(efb.set_default_view(e2f1, e2f1v1).await?);
assert!(efb.set_default_view(e2f2, 2).await?);
assert!(efb.set_default_view(e2f2, 3).await?);
// Mismatching pairing of exposure file and view
assert!(!efb.set_default_view(e2f1, 2).await?);
assert!(!efb.set_default_view(e2f2, e2f1v1).await?);

Ok(())
}

Expand Down
37 changes: 37 additions & 0 deletions pmrmodel_base/src/exposure/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,89 @@ use crate::{

#[async_trait]
pub trait ExposureBackend {
/// Inserts a new `Exposure` entry.
///
/// Returns the id of the inserted entry.
async fn insert(
&self,
workspace_id: i64,
workspace_tag_id: Option<i64>,
commit_id: &str,
default_file_id: Option<i64>,
) -> Result<i64, BackendError>;

/// Returns all `Exposures` for the given `workspace_id`.
async fn list_for_workspace(
&self,
workspace_id: i64,
) -> Result<Exposures, BackendError>;

/// Returns the `Exposure` for the given `id`.
async fn get_id(
&self,
id: i64,
) -> Result<Exposure, BackendError>;

/// For the given `Exposure` identified by its `id`, set the default
/// `ExposureFile` via its `id`.
async fn set_default_file(
&self,
id: i64,
file_id: i64,
) -> Result<bool, BackendError>;
}

#[async_trait]
pub trait ExposureFileBackend {
/// Inserts a new `ExposureFile` entry.
///
/// Returns the id of the inserted entry.
async fn insert(
&self,
exposure_id: i64,
workspace_file_path: &str,
default_view_id: Option<i64>,
) -> Result<i64, BackendError>;

/// Returns all `ExposureFiles` for the given `exposure_id`.
async fn list_for_exposure(
&self,
exposure_id: i64,
) -> Result<ExposureFiles, BackendError>;

/// Returns the `ExposureFile` for the given `id`.
async fn get_id(
&self,
id: i64,
) -> Result<ExposureFile, BackendError>;

/// For the given `ExposureFile` identified by its `id`, set the
/// default `ExposureFileView` via its `id`.
async fn set_default_view(
&self,
id: i64,
file_id: i64,
) -> Result<bool, BackendError>;
}

#[async_trait]
pub trait ExposureFileViewBackend {
/// Inserts a new `ExposureFileView` entry.
///
/// Returns the id of the inserted entry.
async fn insert(
&self,
exposure_file_id: i64,
view_key: &str,
) -> Result<i64, BackendError>;

/// Returns all `ExposureFileViews` for the given `exposure_file_id`.
async fn list_for_exposure_file(
&self,
exposure_file_id: i64,
) -> Result<ExposureFileViews, BackendError>;

/// Returns the `ExposureFileView` for the given `id`.
async fn get_id(
&self,
id: i64,
Expand Down

0 comments on commit 400bfa8

Please sign in to comment.