Skip to content

Commit 44988ff

Browse files
committed
added unit tests
Signed-off-by: Aminu Oluwaseun Joshua <[email protected]>
1 parent 4b62f9b commit 44988ff

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

crates/factor-variables/tests/factor_test.rs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,114 @@ async fn provider_works() -> anyhow::Result<()> {
3535
Ok(())
3636
}
3737

38+
#[tokio::test(flavor = "multi_thread")]
39+
async fn validate_variable_existence_successful() -> anyhow::Result<()> {
40+
let factors = TestFactors {
41+
variables: VariablesFactor::default(),
42+
};
43+
let providers = vec![Box::new(MockProvider) as _];
44+
let runtime_config = TestFactorsRuntimeConfig {
45+
variables: Some(RuntimeConfig { providers }),
46+
};
47+
let env = TestEnvironment::new(factors)
48+
.extend_manifest(toml! {
49+
[variables]
50+
foo = { required = true }
51+
52+
[component.test-component]
53+
source = "does-not-exist.wasm"
54+
})
55+
.runtime_config(runtime_config)?;
56+
57+
let state = env.build_instance_state().await?;
58+
let resolver = state.variables.expression_resolver();
59+
60+
resolver.validate_variable_existence().await?;
61+
62+
Ok(())
63+
}
64+
65+
#[tokio::test(flavor = "multi_thread")]
66+
async fn validate_variable_existence_successful_with_default_value() -> anyhow::Result<()> {
67+
let factors = TestFactors {
68+
variables: VariablesFactor::default(),
69+
};
70+
let providers = vec![Box::new(MockProvider) as _];
71+
let runtime_config = TestFactorsRuntimeConfig {
72+
variables: Some(RuntimeConfig { providers }),
73+
};
74+
let env = TestEnvironment::new(factors)
75+
.extend_manifest(toml! {
76+
[variables]
77+
baz = { default = "var" }
78+
79+
[component.test-component]
80+
source = "does-not-exist.wasm"
81+
})
82+
.runtime_config(runtime_config)?;
83+
84+
let state = env.build_instance_state().await?;
85+
let resolver = state.variables.expression_resolver();
86+
87+
resolver.validate_variable_existence().await?;
88+
89+
Ok(())
90+
}
91+
92+
#[tokio::test(flavor = "multi_thread")]
93+
async fn validate_variable_existence_successful_with_dynamic_provider() -> anyhow::Result<()> {
94+
let factors = TestFactors {
95+
variables: VariablesFactor::default(),
96+
};
97+
let providers = vec![Box::new(DynamicMockProvider) as _];
98+
let runtime_config = TestFactorsRuntimeConfig {
99+
variables: Some(RuntimeConfig { providers }),
100+
};
101+
let env = TestEnvironment::new(factors)
102+
.extend_manifest(toml! {
103+
[variables]
104+
baz = { required = true }
105+
106+
[component.test-component]
107+
source = "does-not-exist.wasm"
108+
})
109+
.runtime_config(runtime_config)?;
110+
111+
let state = env.build_instance_state().await?;
112+
let resolver = state.variables.expression_resolver();
113+
114+
resolver.validate_variable_existence().await?;
115+
116+
Ok(())
117+
}
118+
119+
#[tokio::test(flavor = "multi_thread")]
120+
async fn validate_variable_existence_fails() -> anyhow::Result<()> {
121+
let factors = TestFactors {
122+
variables: VariablesFactor::default(),
123+
};
124+
let providers = vec![Box::new(MockProvider) as _];
125+
let runtime_config = TestFactorsRuntimeConfig {
126+
variables: Some(RuntimeConfig { providers }),
127+
};
128+
let env = TestEnvironment::new(factors)
129+
.extend_manifest(toml! {
130+
[variables]
131+
baz = { required = true }
132+
133+
[component.test-component]
134+
source = "does-not-exist.wasm"
135+
})
136+
.runtime_config(runtime_config)?;
137+
138+
let state = env.build_instance_state().await?;
139+
let resolver = state.variables.expression_resolver();
140+
141+
assert!(resolver.validate_variable_existence().await.is_err());
142+
143+
Ok(())
144+
}
145+
38146
#[derive(Debug)]
39147
struct MockProvider;
40148

@@ -47,6 +155,20 @@ impl Provider for MockProvider {
47155
}
48156
}
49157

158+
fn kind(&self) -> ProviderVariableKind {
159+
ProviderVariableKind::Static
160+
}
161+
}
162+
163+
#[derive(Debug)]
164+
struct DynamicMockProvider;
165+
166+
#[spin_world::async_trait]
167+
impl Provider for DynamicMockProvider {
168+
async fn get(&self, _key: &Key) -> anyhow::Result<Option<String>> {
169+
Ok(None)
170+
}
171+
50172
fn kind(&self) -> ProviderVariableKind {
51173
ProviderVariableKind::Dynamic
52174
}

crates/trigger/src/cli/variable.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for VariablesValidatorHook {
1111
&self,
1212
configured_app: &spin_factors::ConfiguredApp<F>,
1313
) -> anyhow::Result<()> {
14-
let variables_factor = configured_app.app_state::<VariablesFactor>()?;
14+
let variables_factor_app_state = configured_app.app_state::<VariablesFactor>()?;
1515

16-
let expression_resolver = variables_factor.expression_resolver();
16+
let expression_resolver = variables_factor_app_state.expression_resolver();
1717
expression_resolver.validate_variable_existence().await?;
1818

1919
Ok(())

0 commit comments

Comments
 (0)