|
1 | 1 | use crate::entity_manager::server::EntityStoreResponse;
|
2 | 2 | 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; |
3 | 5 | use crate::entity_manager::tests::model::Command;
|
4 | 6 | use crate::entity_manager::tests::model::Commands;
|
5 | 7 | use crate::entity_manager::tests::model::Protocol::HTTP;
|
@@ -100,6 +102,62 @@ async fn check_registrations(registrations: Commands) {
|
100 | 102 | }
|
101 | 103 | }
|
102 | 104 |
|
| 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 | + |
103 | 161 | mod entity {
|
104 | 162 | use crate::entity_manager::server::EntityStoreRequest;
|
105 | 163 | use crate::entity_manager::server::EntityStoreResponse;
|
|
0 commit comments