refactor(router): Simplify builder code patterns

Use more fluent method names and leverage defaulting
to remove the need for an option on the namespace autocreate
policy.
pull/24376/head
Fraser Savage 2023-03-21 10:26:23 +00:00
parent 2c4908bf51
commit 392972a3ad
No known key found for this signature in database
GPG Key ID: DE47C33CE8C5C446
3 changed files with 19 additions and 32 deletions

View File

@ -37,25 +37,22 @@ pub fn test_context() -> TestContextBuilder {
#[derive(Debug, Default)]
pub struct TestContextBuilder {
namespace_autocreate_policy: Option<NamespaceAutocreatePolicy>,
namespace_autocreate_policy: NamespaceAutocreatePolicy,
}
impl TestContextBuilder {
pub fn autocreate_namespace(mut self, enabled: bool) -> Self {
self.namespace_autocreate_policy = match self.namespace_autocreate_policy {
Some(p) => Some(NamespaceAutocreatePolicy { enabled, ..p }),
None => Some(NamespaceAutocreatePolicy::new(true, None)),
pub fn with_autocreate_namespace(mut self, enabled: bool) -> Self {
self.namespace_autocreate_policy = NamespaceAutocreatePolicy {
enabled,
..self.namespace_autocreate_policy
};
self
}
pub fn autocreate_namespace_retention_period_nanos(mut self, period_nanos: i64) -> Self {
self.namespace_autocreate_policy = match self.namespace_autocreate_policy {
Some(p) => Some(NamespaceAutocreatePolicy {
retention_period_nanos: Some(period_nanos),
..p
}),
None => Some(NamespaceAutocreatePolicy::new(false, Some(period_nanos))),
pub fn with_autocreate_namespace_retention_period_nanos(mut self, period_nanos: i64) -> Self {
self.namespace_autocreate_policy = NamespaceAutocreatePolicy {
retention_period_nanos: Some(period_nanos),
..self.namespace_autocreate_policy
};
self
}
@ -67,8 +64,7 @@ impl TestContextBuilder {
test_helpers::maybe_start_logging();
let namespace_autocreate_policy = namespace_autocreate_policy
.unwrap_or_else(|| NamespaceAutocreatePolicy::new(false, None));
let namespace_autocreate_policy = namespace_autocreate_policy;
let metrics: Arc<metric::Registry> = Default::default();
@ -78,21 +74,12 @@ impl TestContextBuilder {
}
}
#[derive(Debug)]
#[derive(Debug, Default)]
struct NamespaceAutocreatePolicy {
enabled: bool,
retention_period_nanos: Option<i64>,
}
impl NamespaceAutocreatePolicy {
fn new(enabled: bool, retention_period_nanos: Option<i64>) -> Self {
Self {
enabled,
retention_period_nanos,
}
}
}
#[derive(Debug)]
pub struct TestContext {
client: Arc<MockWriteClient>,

View File

@ -140,7 +140,7 @@ async fn test_namespace_create() {
#[tokio::test]
async fn test_namespace_delete() {
// Initialise a TestContext with implicit namespace creation.
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
const RETENTION: i64 = Duration::from_secs(42 * 60 * 60).as_nanos() as _;

View File

@ -16,7 +16,7 @@ pub mod common;
#[tokio::test]
async fn test_write_ok() {
// Create a test context with implicit namespace creation.
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
// Write data inside retention period
let now = SystemProvider::default()
@ -91,8 +91,8 @@ async fn test_write_ok() {
#[tokio::test]
async fn test_write_outside_retention_period() {
let ctx = test_context()
.autocreate_namespace(true)
.autocreate_namespace_retention_period_nanos(TEST_RETENTION_PERIOD_NS)
.with_autocreate_namespace(true)
.with_autocreate_namespace_retention_period_nanos(TEST_RETENTION_PERIOD_NS)
.build()
.await;
@ -120,7 +120,7 @@ async fn test_write_outside_retention_period() {
#[tokio::test]
async fn test_schema_conflict() {
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
// data inside the retention period
let now = SystemProvider::default()
@ -199,7 +199,7 @@ async fn test_rejected_ns() {
#[tokio::test]
async fn test_schema_limit() {
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
let now = SystemProvider::default()
.now()
@ -253,7 +253,7 @@ async fn test_schema_limit() {
#[tokio::test]
async fn test_write_propagate_ids() {
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
// Create the namespace and a set of tables.
let ns = ctx
@ -338,7 +338,7 @@ async fn test_write_propagate_ids() {
#[tokio::test]
async fn test_delete_unsupported() {
let ctx = test_context().autocreate_namespace(true).build().await;
let ctx = test_context().with_autocreate_namespace(true).build().await;
// Create the namespace and a set of tables.
ctx.catalog()