Skip to content

Commit

Permalink
Detach add_document and add_schema from TestDatabase (#237)
Browse files Browse the repository at this point in the history
* Refactor test helper as detached functions

* Update changelog

* Fix merge

Co-authored-by: Andreas Dzialocha <x12@adz.garden>
  • Loading branch information
cafca and adzialocha authored Aug 22, 2022
1 parent c95a789 commit 1745b33
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 321 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Give error types of worker a string for better debugging [#194](https://github.com/p2panda/aquadoggo/pull/194)
- Bump `p2panda-rs` which now supports log id's starting from `0` [#207](https://github.com/p2panda/aquadoggo/pull/207)
- Removed unused field `entry_hash` from operation data model [#221](https://github.com/p2panda/aquadoggo/pull/221)
- Detach test helpers from test storage provider implementation [#237](https://github.com/p2panda/aquadoggo/pull/237)
- Remove `Scalar` suffix from scalar types in GraphQL schema [#231](https://github.com/p2panda/aquadoggo/pull/231)
- Implement new API for untagged operations [#245](https://github.com/p2panda/aquadoggo/pull/235)
- Use `DocumentStore` trait from `p2panda_rs`[#249](https://github.com/p2panda/aquadoggo/pull/249)
Expand Down
22 changes: 11 additions & 11 deletions aquadoggo/src/db/provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mod tests {
use p2panda_rs::{document::DocumentViewId, schema::FieldType};
use rstest::rstest;

use crate::db::stores::test_utils::{test_db, TestDatabase, TestDatabaseRunner};
use crate::db::stores::test_utils::{add_schema, test_db, TestDatabase, TestDatabaseRunner};

#[rstest]
fn test_get_schema_for_view(
Expand All @@ -113,16 +113,16 @@ mod tests {
runner: TestDatabaseRunner,
) {
runner.with_db_teardown(|mut db: TestDatabase| async move {
let schema = db
.add_schema(
"venue",
vec![
("description", FieldType::String),
("profile_name", FieldType::String),
],
&key_pair,
)
.await;
let schema = add_schema(
&mut db,
"venue",
vec![
("description", FieldType::String),
("profile_name", FieldType::String),
],
&key_pair,
)
.await;

let document_view_id = match schema.id() {
SchemaId::Application(_, view_id) => view_id,
Expand Down
49 changes: 26 additions & 23 deletions aquadoggo/src/db/stores/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,25 @@ mod tests {
use p2panda_rs::test_utils::fixtures::{key_pair, random_document_view_id};
use rstest::rstest;

use crate::db::stores::test_utils::{test_db, TestDatabase, TestDatabaseRunner};
use crate::db::stores::test_utils::{
add_document, add_schema, test_db, TestDatabase, TestDatabaseRunner,
};

use super::SchemaStore;

#[rstest]
fn get_schema(key_pair: KeyPair, #[from(test_db)] runner: TestDatabaseRunner) {
runner.with_db_teardown(move |mut db: TestDatabase| async move {
let schema = db
.add_schema(
"test_schema",
vec![
("description", FieldType::String),
("profile_name", FieldType::String),
],
&key_pair,
)
.await;
let schema = add_schema(
&mut db,
"test_schema",
vec![
("description", FieldType::String),
("profile_name", FieldType::String),
],
&key_pair,
)
.await;

let document_view_id = match schema.id() {
SchemaId::Application(_, view_id) => view_id,
Expand All @@ -136,7 +138,8 @@ mod tests {
fn get_all_schema(key_pair: KeyPair, #[from(test_db)] runner: TestDatabaseRunner) {
runner.with_db_teardown(move |mut db: TestDatabase| async move {
for i in 0..5 {
db.add_schema(
add_schema(
&mut db,
&format!("test_schema_{}", i),
vec![
("description", FieldType::String),
Expand All @@ -156,17 +159,17 @@ mod tests {
fn schema_fields_do_not_exist(#[from(test_db)] runner: TestDatabaseRunner, key_pair: KeyPair) {
runner.with_db_teardown(|mut db: TestDatabase| async move {
// Create a schema definition but no schema field definitions
let document_view_id = db
.add_document(
&SchemaId::SchemaDefinition(1),
vec![
("name", "test_schema".into()),
("description", "My schema without fields".into()),
("fields", vec![random_document_view_id()].into()),
],
&key_pair,
)
.await;
let document_view_id = add_document(
&mut db,
&SchemaId::SchemaDefinition(1),
vec![
("name", "test_schema".into()),
("description", "My schema without fields".into()),
("fields", vec![random_document_view_id()].into()),
],
&key_pair,
)
.await;

// Retrieve the schema by it's document view id. We unwrap here as we expect an `Ok`
// result for the succeeding db query, even though the schema could not be built.
Expand Down
121 changes: 119 additions & 2 deletions aquadoggo/src/db/stores/test_utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

use log::{debug, info};
use p2panda_rs::document::{DocumentId, DocumentViewId};
use p2panda_rs::entry::traits::AsEncodedEntry;
use p2panda_rs::hash::Hash;
use p2panda_rs::identity::KeyPair;
use p2panda_rs::operation::{
OperationValue, PinnedRelation, PinnedRelationList, Relation, RelationList,
OperationBuilder, OperationValue, PinnedRelation, PinnedRelationList, Relation, RelationList,
};
use p2panda_rs::schema::{Schema, SchemaId};
use p2panda_rs::schema::{FieldType, Schema, SchemaId};
use p2panda_rs::test_utils::constants;
use p2panda_rs::test_utils::db::test_db::send_to_store;
use p2panda_rs::test_utils::fixtures::{schema, schema_fields};

use crate::db::stores::test_utils::TestDatabase;
use crate::materializer::tasks::{dependency_task, reduce_task, schema_task};
use crate::materializer::TaskInput;

fn doggo_schema_id() -> SchemaId {
SchemaId::new_application("doggo_schema", &constants::HASH.to_owned().parse().unwrap())
}
Expand Down Expand Up @@ -78,3 +87,111 @@ pub fn doggo_fields() -> Vec<(&'static str, OperationValue)> {
),
]
}

/// Publish a document and materialise it in a given `TestDatabase`.
///
/// Also runs dependency task for document.
pub async fn add_document(
test_db: &mut TestDatabase,
schema_id: &SchemaId,
fields: Vec<(&str, OperationValue)>,
key_pair: &KeyPair,
) -> DocumentViewId {
info!("Creating document for {}", schema_id);

// Get requested schema from store.
let schema = test_db
.context
.schema_provider
.get(schema_id)
.await
.expect("Schema not found");

// Build, publish and reduce create operation for document.
let create_op = OperationBuilder::new(schema.id())
.fields(&fields)
.build()
.expect("Build operation");

let (entry_signed, _) = send_to_store(&test_db.store, &create_op, &schema, key_pair)
.await
.expect("Publish CREATE operation");

let input = TaskInput::new(Some(DocumentId::from(entry_signed.hash())), None);
let dependency_tasks = reduce_task(test_db.context.clone(), input.clone())
.await
.expect("Reduce document");

// Run dependency tasks
if let Some(tasks) = dependency_tasks {
for task in tasks {
dependency_task(test_db.context.clone(), task.input().to_owned())
.await
.expect("Run dependency task");
}
}
DocumentViewId::from(entry_signed.hash())
}

/// Publish a schema and materialise it in a given `TestDatabase`.
pub async fn add_schema(
test_db: &mut TestDatabase,
name: &str,
fields: Vec<(&str, FieldType)>,
key_pair: &KeyPair,
) -> Schema {
info!("Creating schema {}", name);
let mut field_ids = Vec::new();

// Build and reduce schema field definitions
for field in fields {
let create_field_op = Schema::create_field(field.0, field.1.clone());
let (entry_signed, _) = send_to_store(
&test_db.store,
&create_field_op,
&Schema::get_system(SchemaId::SchemaFieldDefinition(1)).unwrap(),
key_pair,
)
.await
.expect("Publish schema fields");

let input = TaskInput::new(Some(DocumentId::from(entry_signed.hash())), None);
reduce_task(test_db.context.clone(), input).await.unwrap();

info!("Added field '{}' ({})", field.0, field.1);
field_ids.push(DocumentViewId::from(entry_signed.hash()));
}

// Build and reduce schema definition
let create_schema_op = Schema::create(name, "test schema description", field_ids);
let (entry_signed, _) = send_to_store(
&test_db.store,
&create_schema_op,
&Schema::get_system(SchemaId::SchemaDefinition(1)).unwrap(),
key_pair,
)
.await
.expect("Publish schema");

let input = TaskInput::new(Some(DocumentId::from(entry_signed.hash())), None);
reduce_task(test_db.context.clone(), input.clone())
.await
.expect("Reduce schema document");

// Run schema task for this spec
let input = TaskInput::new(None, Some(DocumentViewId::from(entry_signed.hash())));
schema_task(test_db.context.clone(), input)
.await
.expect("Run schema task");

let view_id = DocumentViewId::from(entry_signed.hash());
let schema_id = SchemaId::Application(name.to_string(), view_id);

debug!("Done building {}", schema_id);
test_db
.context
.schema_provider
.get(&schema_id)
.await
.expect("Failed adding schema to provider.")
}
2 changes: 1 addition & 1 deletion aquadoggo/src/db/stores/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ mod helpers;
mod runner;
mod store;

pub use helpers::{doggo_fields, doggo_schema};
pub use helpers::{add_document, add_schema, doggo_fields, doggo_schema};
pub use runner::{test_db, with_db_manager_teardown, TestDatabaseManager, TestDatabaseRunner};
pub use store::{TestData, TestDatabase};
Loading

0 comments on commit 1745b33

Please sign in to comment.