Skip to content

Commit 12717d6

Browse files
committed
Combine proptest with contract testing
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent 8189c0b commit 12717d6

File tree

1 file changed

+58
-0
lines changed
  • crates/core/tedge_agent/src/entity_manager

1 file changed

+58
-0
lines changed

crates/core/tedge_agent/src/entity_manager/tests.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::entity_manager::server::EntityStoreResponse;
22
use crate::entity_manager::tests::model::Action;
3+
use crate::entity_manager::tests::model::Action::AddDevice;
4+
use crate::entity_manager::tests::model::Action::AddService;
35
use crate::entity_manager::tests::model::Command;
46
use crate::entity_manager::tests::model::Commands;
57
use crate::entity_manager::tests::model::Protocol::HTTP;
@@ -100,6 +102,62 @@ async fn check_registrations(registrations: Commands) {
100102
}
101103
}
102104

105+
proptest! {
106+
#[test]
107+
fn it_works_from_user_pov(registrations in model::walk(10)) {
108+
tokio::runtime::Builder::new_current_thread()
109+
.enable_all()
110+
.build()
111+
.unwrap()
112+
.block_on(check_registrations_from_user_pov(registrations))
113+
}
114+
}
115+
116+
async fn check_registrations_from_user_pov(registrations: Commands) {
117+
let (mut entity_store, _mqtt_output) = entity::server("device-under-test");
118+
119+
// Trigger all operations over HTTP to avoid pending entities (which are not visible to the user)
120+
for action in registrations.0.into_iter().map(|c| c.action) {
121+
let parent_id = action.parent_topic_id();
122+
let topic_id = action.topic_id();
123+
let entity_type = action.target_type();
124+
125+
match &action {
126+
AddDevice { .. } | AddService { .. } => {
127+
let previous = entity_store.get(&topic_id).cloned();
128+
if previous.is_none()
129+
&& (parent_id.is_none()
130+
|| entity_store.get(parent_id.as_ref().unwrap()).is_some())
131+
{
132+
// If not registered and with a parent that is registered
133+
// then the new entity should be registered
134+
assert!(matches!(
135+
entity_store.handle((HTTP, action).into()).await,
136+
EntityStoreResponse::Create(Ok(_))
137+
));
138+
let registered = entity_store.get(&topic_id).unwrap();
139+
assert_eq!(registered.parent, parent_id);
140+
assert_eq!(registered.r#type, entity_type);
141+
} else {
142+
// If already registered and with a parent that is not registered
143+
// then the registration should be rejected
144+
// and the previous entity be unchanged, if any
145+
assert!(matches!(
146+
entity_store.handle((HTTP, action).into()).await,
147+
EntityStoreResponse::Create(Err(_))
148+
));
149+
assert_eq!(previous.as_ref(), entity_store.get(&topic_id));
150+
}
151+
}
152+
153+
Action::RemDevice { .. } | Action::RemService { .. } => {
154+
entity_store.handle((HTTP, action).into()).await;
155+
assert!(entity_store.get(&topic_id).is_none());
156+
}
157+
}
158+
}
159+
}
160+
103161
mod entity {
104162
use crate::entity_manager::server::EntityStoreRequest;
105163
use crate::entity_manager::server::EntityStoreResponse;

0 commit comments

Comments
 (0)