tests.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. #![cfg(test)]
  2. use super::*;
  3. use crate::mock::*;
  4. use frame_support::{assert_err, assert_ok};
  5. // Create class
  6. // --------------------------------------
  7. #[test]
  8. fn create_class_successfully() {
  9. with_test_externalities(|| {
  10. let class_id = TestModule::next_class_id();
  11. assert_ok!(
  12. TestModule::create_class(good_class_name(), good_class_description(),),
  13. class_id
  14. );
  15. assert_eq!(TestModule::next_class_id(), class_id + 1);
  16. })
  17. }
  18. #[test]
  19. fn cannot_create_class_with_empty_name() {
  20. with_test_externalities(|| {
  21. let empty_name = vec![];
  22. assert_err!(
  23. TestModule::create_class(empty_name, good_class_description(),),
  24. ERROR_CLASS_NAME_TOO_SHORT
  25. );
  26. })
  27. }
  28. #[test]
  29. fn create_class_with_empty_description() {
  30. with_test_externalities(|| {
  31. let empty_description = vec![];
  32. assert_eq!(
  33. TestModule::create_class(good_class_name(), empty_description,),
  34. Ok(1)
  35. );
  36. })
  37. }
  38. // Add class schema
  39. // --------------------------------------
  40. #[test]
  41. fn cannot_add_schema_to_unknown_class() {
  42. with_test_externalities(|| {
  43. assert_err!(
  44. TestModule::add_class_schema(UNKNOWN_CLASS_ID, good_prop_ids(), good_props()),
  45. ERROR_CLASS_NOT_FOUND
  46. );
  47. })
  48. }
  49. #[test]
  50. fn cannot_add_class_schema_when_no_props_passed() {
  51. with_test_externalities(|| {
  52. let class_id = create_class();
  53. assert_err!(
  54. TestModule::add_class_schema(class_id, vec![], vec![]),
  55. ERROR_NO_PROPS_IN_CLASS_SCHEMA
  56. );
  57. })
  58. }
  59. #[test]
  60. fn cannot_add_class_schema_when_it_refers_unknown_prop_index_and_class_has_no_props() {
  61. with_test_externalities(|| {
  62. let class_id = create_class();
  63. assert_err!(
  64. TestModule::add_class_schema(class_id, vec![UNKNOWN_PROP_ID], vec![]),
  65. ERROR_CLASS_SCHEMA_REFERS_UNKNOWN_PROP_INDEX
  66. );
  67. })
  68. }
  69. #[test]
  70. fn cannot_add_class_schema_when_it_refers_unknown_prop_index() {
  71. with_test_externalities(|| {
  72. let class_id = create_class();
  73. assert_eq!(
  74. TestModule::add_class_schema(class_id, vec![], good_props()),
  75. Ok(SCHEMA_ID_0)
  76. );
  77. // Try to add a new schema that is based on one valid prop ids
  78. // plus another prop id is unknown on this class.
  79. assert_err!(
  80. TestModule::add_class_schema(class_id, vec![0, UNKNOWN_PROP_ID], vec![]),
  81. ERROR_CLASS_SCHEMA_REFERS_UNKNOWN_PROP_INDEX
  82. );
  83. // Verify that class props and schemas remain unchanged:
  84. assert_class_props(class_id, good_props());
  85. assert_class_schemas(class_id, vec![good_prop_ids()]);
  86. })
  87. }
  88. #[test]
  89. fn cannot_add_class_schema_when_it_refers_unknown_internal_id() {
  90. with_test_externalities(|| {
  91. let class_id = create_class();
  92. let bad_internal_prop = new_internal_class_prop(UNKNOWN_CLASS_ID);
  93. assert_err!(
  94. TestModule::add_class_schema(
  95. class_id,
  96. vec![],
  97. vec![good_prop_bool(), bad_internal_prop]
  98. ),
  99. ERROR_CLASS_SCHEMA_REFERS_UNKNOWN_INTERNAL_ID
  100. );
  101. })
  102. }
  103. #[test]
  104. fn should_add_class_schema_with_internal_class_prop() {
  105. with_test_externalities(|| {
  106. let class_id = create_class();
  107. let internal_class_prop = new_internal_class_prop(class_id);
  108. // Add first schema with new props.
  109. // No other props on the class at this time.
  110. assert_eq!(
  111. TestModule::add_class_schema(class_id, vec![], vec![internal_class_prop.clone()]),
  112. Ok(SCHEMA_ID_0)
  113. );
  114. assert_class_props(class_id, vec![internal_class_prop]);
  115. assert_class_schemas(class_id, vec![vec![SCHEMA_ID_0]]);
  116. })
  117. }
  118. #[test]
  119. fn should_add_class_schema_when_only_new_props_passed() {
  120. with_test_externalities(|| {
  121. let class_id = create_class();
  122. // Add first schema with new props.
  123. // No other props on the class at this time.
  124. assert_eq!(
  125. TestModule::add_class_schema(class_id, vec![], good_props()),
  126. Ok(SCHEMA_ID_0)
  127. );
  128. assert_class_props(class_id, good_props());
  129. assert_class_schemas(class_id, vec![good_prop_ids()]);
  130. })
  131. }
  132. #[test]
  133. fn should_add_class_schema_when_only_prop_ids_passed() {
  134. with_test_externalities(|| {
  135. let class_id = create_class();
  136. // Add first schema with new props.
  137. // No other props on the class at this time.
  138. assert_eq!(
  139. TestModule::add_class_schema(class_id, vec![], good_props()),
  140. Ok(SCHEMA_ID_0)
  141. );
  142. // Add a new schema that is based solely on the props ids
  143. // of the previously added schema.
  144. assert_eq!(
  145. TestModule::add_class_schema(class_id, good_prop_ids(), vec![]),
  146. Ok(SCHEMA_ID_1)
  147. );
  148. })
  149. }
  150. #[test]
  151. fn cannot_add_class_schema_when_new_props_have_duplicate_names() {
  152. with_test_externalities(|| {
  153. let class_id = create_class();
  154. // Add first schema with new props.
  155. // No other props on the class at this time.
  156. assert_eq!(
  157. TestModule::add_class_schema(class_id, vec![], good_props()),
  158. Ok(SCHEMA_ID_0)
  159. );
  160. // Add a new schema with not unique property names:
  161. assert_err!(
  162. TestModule::add_class_schema(class_id, vec![], good_props()),
  163. ERROR_PROP_NAME_NOT_UNIQUE_IN_CLASS
  164. );
  165. })
  166. }
  167. #[test]
  168. fn should_add_class_schema_when_both_prop_ids_and_new_props_passed() {
  169. with_test_externalities(|| {
  170. let class_id = create_class();
  171. // Add first schema with new props.
  172. // No other props on the class at this time.
  173. assert_eq!(
  174. TestModule::add_class_schema(class_id, vec![], vec![good_prop_bool(), good_prop_u32()]),
  175. Ok(SCHEMA_ID_0)
  176. );
  177. // Add a new schema that is based on some prop ids
  178. // added with previous schema plus some new props,
  179. // introduced by this new schema.
  180. assert_eq!(
  181. TestModule::add_class_schema(class_id, vec![1], vec![good_prop_text()]),
  182. Ok(SCHEMA_ID_1)
  183. );
  184. assert_class_props(
  185. class_id,
  186. vec![good_prop_bool(), good_prop_u32(), good_prop_text()],
  187. );
  188. assert_class_schemas(class_id, vec![vec![0, 1], vec![1, 2]]);
  189. })
  190. }
  191. // Create entity
  192. // --------------------------------------
  193. #[test]
  194. fn create_entity_successfully() {
  195. with_test_externalities(|| {
  196. let class_id = create_class();
  197. let entity_id_1 = TestModule::next_entity_id();
  198. assert_ok!(TestModule::create_entity(class_id,), entity_id_1);
  199. // TODO assert entity from storage
  200. assert_eq!(TestModule::next_entity_id(), entity_id_1 + 1);
  201. })
  202. }
  203. #[test]
  204. fn cannot_create_entity_with_unknown_class_id() {
  205. with_test_externalities(|| {
  206. assert_err!(
  207. TestModule::create_entity(UNKNOWN_CLASS_ID,),
  208. ERROR_CLASS_NOT_FOUND
  209. );
  210. })
  211. }
  212. // Add schema support to entity
  213. // --------------------------------------
  214. #[test]
  215. fn cannot_add_schema_to_entity_when_entity_not_found() {
  216. with_test_externalities(|| {
  217. assert_entity_not_found(TestModule::add_schema_support_to_entity(
  218. UNKNOWN_ENTITY_ID,
  219. 1,
  220. vec![],
  221. ));
  222. })
  223. }
  224. #[test]
  225. fn cannot_add_schema_to_entity_when_schema_already_added_to_entity() {
  226. with_test_externalities(|| {
  227. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  228. // Firstly we just add support for a valid class schema.
  229. assert_ok!(TestModule::add_schema_support_to_entity(
  230. entity_id,
  231. schema_id,
  232. vec![bool_prop_value()]
  233. ));
  234. // Secondly we try to add support for the same schema.
  235. assert_err!(
  236. TestModule::add_schema_support_to_entity(entity_id, schema_id, vec![]),
  237. ERROR_SCHEMA_ALREADY_ADDED_TO_ENTITY
  238. );
  239. })
  240. }
  241. #[test]
  242. fn cannot_add_schema_to_entity_when_schema_id_is_unknown() {
  243. with_test_externalities(|| {
  244. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  245. let unknown_schema_id = schema_id + 1;
  246. assert_err!(
  247. TestModule::add_schema_support_to_entity(
  248. entity_id,
  249. unknown_schema_id,
  250. vec![prop_value(0, PropertyValue::None)]
  251. ),
  252. ERROR_UNKNOWN_CLASS_SCHEMA_ID
  253. );
  254. })
  255. }
  256. #[test]
  257. fn cannot_add_schema_to_entity_when_prop_value_dont_match_type() {
  258. with_test_externalities(|| {
  259. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  260. assert_err!(
  261. TestModule::add_schema_support_to_entity(
  262. entity_id,
  263. schema_id,
  264. vec![
  265. bool_prop_value(),
  266. prop_value(PROP_ID_U32, PropertyValue::Bool(true))
  267. ]
  268. ),
  269. ERROR_PROP_VALUE_DONT_MATCH_TYPE
  270. );
  271. })
  272. }
  273. #[test]
  274. fn cannot_add_schema_to_entity_when_unknown_internal_entity_id() {
  275. with_test_externalities(|| {
  276. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  277. assert_err!(
  278. TestModule::add_schema_support_to_entity(
  279. entity_id,
  280. schema_id,
  281. vec![
  282. bool_prop_value(),
  283. prop_value(PROP_ID_INTERNAL, PropertyValue::Internal(UNKNOWN_ENTITY_ID))
  284. ]
  285. ),
  286. ERROR_ENTITY_NOT_FOUND
  287. );
  288. })
  289. }
  290. #[test]
  291. fn cannot_add_schema_to_entity_when_missing_required_prop() {
  292. with_test_externalities(|| {
  293. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  294. assert_err!(
  295. TestModule::add_schema_support_to_entity(
  296. entity_id,
  297. schema_id,
  298. vec![prop_value(PROP_ID_U32, PropertyValue::Uint32(456))]
  299. ),
  300. ERROR_MISSING_REQUIRED_PROP
  301. );
  302. })
  303. }
  304. #[test]
  305. fn should_add_schema_to_entity_when_some_optional_props_provided() {
  306. with_test_externalities(|| {
  307. let (_, schema_id, entity_id) = create_class_with_schema_and_entity();
  308. assert_ok!(TestModule::add_schema_support_to_entity(
  309. entity_id,
  310. schema_id,
  311. vec![
  312. bool_prop_value(),
  313. prop_value(PROP_ID_U32, PropertyValue::Uint32(123)),
  314. // Note that an optional internal prop is not provided here.
  315. ]
  316. ));
  317. let entity = TestModule::entity_by_id(entity_id);
  318. assert_eq!(entity.in_class_schema_indexes, [SCHEMA_ID_0]);
  319. assert_eq!(
  320. entity.values,
  321. vec![
  322. bool_prop_value(),
  323. prop_value(PROP_ID_U32, PropertyValue::Uint32(123)),
  324. prop_value(PROP_ID_INTERNAL, PropertyValue::None),
  325. ]
  326. );
  327. })
  328. }
  329. // Update entity properties
  330. // --------------------------------------
  331. #[test]
  332. fn cannot_update_entity_props_when_entity_not_found() {
  333. with_test_externalities(|| {
  334. assert_entity_not_found(TestModule::update_entity_property_values(
  335. UNKNOWN_ENTITY_ID,
  336. vec![],
  337. ));
  338. })
  339. }
  340. #[test]
  341. fn cannot_update_entity_props_when_prop_value_dont_match_type() {
  342. with_test_externalities(|| {
  343. let entity_id = create_entity_with_schema_support();
  344. assert_err!(
  345. TestModule::update_entity_property_values(
  346. entity_id,
  347. vec![prop_value(PROP_ID_BOOL, PropertyValue::Uint32(1))]
  348. ),
  349. ERROR_PROP_VALUE_DONT_MATCH_TYPE
  350. );
  351. })
  352. }
  353. #[test]
  354. fn cannot_update_entity_props_when_unknown_internal_entity_id() {
  355. with_test_externalities(|| {
  356. let entity_id = create_entity_with_schema_support();
  357. assert_err!(
  358. TestModule::update_entity_property_values(
  359. entity_id,
  360. vec![prop_value(
  361. PROP_ID_INTERNAL,
  362. PropertyValue::Internal(UNKNOWN_ENTITY_ID)
  363. )]
  364. ),
  365. ERROR_ENTITY_NOT_FOUND
  366. );
  367. })
  368. }
  369. #[test]
  370. fn cannot_update_entity_props_when_unknown_entity_prop_id() {
  371. with_test_externalities(|| {
  372. let entity_id = create_entity_with_schema_support();
  373. assert_err!(
  374. TestModule::update_entity_property_values(
  375. entity_id,
  376. vec![prop_value(UNKNOWN_PROP_ID, PropertyValue::Bool(true))]
  377. ),
  378. ERROR_UNKNOWN_ENTITY_PROP_ID
  379. );
  380. })
  381. }
  382. #[test]
  383. fn update_entity_props_successfully() {
  384. with_test_externalities(|| {
  385. let entity_id = create_entity_with_schema_support();
  386. assert_eq!(
  387. TestModule::entity_by_id(entity_id).values,
  388. vec![
  389. prop_value(PROP_ID_BOOL, PropertyValue::Bool(true)),
  390. prop_value(PROP_ID_U32, PropertyValue::None),
  391. prop_value(PROP_ID_INTERNAL, PropertyValue::None),
  392. ]
  393. );
  394. assert_ok!(TestModule::update_entity_property_values(
  395. entity_id,
  396. vec![
  397. prop_value(PROP_ID_BOOL, PropertyValue::Bool(false)),
  398. prop_value(PROP_ID_U32, PropertyValue::Uint32(123)),
  399. prop_value(PROP_ID_INTERNAL, PropertyValue::Internal(entity_id)),
  400. ]
  401. ));
  402. assert_eq!(
  403. TestModule::entity_by_id(entity_id).values,
  404. vec![
  405. prop_value(PROP_ID_BOOL, PropertyValue::Bool(false)),
  406. prop_value(PROP_ID_U32, PropertyValue::Uint32(123)),
  407. prop_value(PROP_ID_INTERNAL, PropertyValue::Internal(entity_id)),
  408. ]
  409. );
  410. })
  411. }
  412. // TODO test text max len
  413. // TODO test vec max len
  414. // Delete entity
  415. // --------------------------------------
  416. // #[test]
  417. // fn delete_entity_successfully() {
  418. // with_test_externalities(|| {
  419. // let entity_id = create_entity();
  420. // assert_ok!(
  421. // TestModule::delete_entity(entity_id),
  422. // ()
  423. // );
  424. // })
  425. // }
  426. // #[test]
  427. // fn cannot_delete_entity_when_entity_not_found() {
  428. // with_test_externalities(|| {
  429. // assert_entity_not_found(
  430. // TestModule::delete_entity(UNKNOWN_ENTITY_ID)
  431. // );
  432. // })
  433. // }
  434. // #[test]
  435. // fn cannot_delete_already_deleted_entity() {
  436. // with_test_externalities(|| {
  437. // let entity_id = create_entity();
  438. // let _ok = TestModule::delete_entity(entity_id);
  439. // assert_err!(
  440. // TestModule::delete_entity(entity_id),
  441. // ERROR_ENTITY_ALREADY_DELETED
  442. // );
  443. // })
  444. // }