StorageTest.vala 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /**
  2. * StorageTest - Unit tests for high-level Storage interface
  3. */
  4. using Implexus.Core;
  5. using Implexus.Storage;
  6. public static int main(string[] args) {
  7. int passed = 0;
  8. int failed = 0;
  9. // Test 1: Store and get entity metadata
  10. if (test_store_entity_metadata()) {
  11. passed++;
  12. stdout.puts("PASS: test_store_entity_metadata\n");
  13. } else {
  14. failed++;
  15. stdout.puts("FAIL: test_store_entity_metadata\n");
  16. }
  17. // Test 2: Entity exists
  18. if (test_entity_exists()) {
  19. passed++;
  20. stdout.puts("PASS: test_entity_exists\n");
  21. } else {
  22. failed++;
  23. stdout.puts("FAIL: test_entity_exists\n");
  24. }
  25. // Test 3: Delete entity
  26. if (test_delete_entity()) {
  27. passed++;
  28. stdout.puts("PASS: test_delete_entity\n");
  29. } else {
  30. failed++;
  31. stdout.puts("FAIL: test_delete_entity\n");
  32. }
  33. // Test 4: Store and load properties
  34. if (test_store_load_properties()) {
  35. passed++;
  36. stdout.puts("PASS: test_store_load_properties\n");
  37. } else {
  38. failed++;
  39. stdout.puts("FAIL: test_store_load_properties\n");
  40. }
  41. // Test 5: Add and get children
  42. if (test_add_get_children()) {
  43. passed++;
  44. stdout.puts("PASS: test_add_get_children\n");
  45. } else {
  46. failed++;
  47. stdout.puts("FAIL: test_add_get_children\n");
  48. }
  49. // Test 6: Remove child
  50. if (test_remove_child()) {
  51. passed++;
  52. stdout.puts("PASS: test_remove_child\n");
  53. } else {
  54. failed++;
  55. stdout.puts("FAIL: test_remove_child\n");
  56. }
  57. // Test 7: Has child
  58. if (test_has_child()) {
  59. passed++;
  60. stdout.puts("PASS: test_has_child\n");
  61. } else {
  62. failed++;
  63. stdout.puts("FAIL: test_has_child\n");
  64. }
  65. // Test 8: Store and get category config
  66. if (test_category_config()) {
  67. passed++;
  68. stdout.puts("PASS: test_category_config\n");
  69. } else {
  70. failed++;
  71. stdout.puts("FAIL: test_category_config\n");
  72. }
  73. // Test 9: Type label storage
  74. if (test_type_label()) {
  75. passed++;
  76. stdout.puts("PASS: test_type_label\n");
  77. } else {
  78. failed++;
  79. stdout.puts("FAIL: test_type_label\n");
  80. }
  81. // Test 10: Multiple entity types
  82. if (test_multiple_entity_types()) {
  83. passed++;
  84. stdout.puts("PASS: test_multiple_entity_types\n");
  85. } else {
  86. failed++;
  87. stdout.puts("FAIL: test_multiple_entity_types\n");
  88. }
  89. // Test 11: Root entity
  90. if (test_root_entity()) {
  91. passed++;
  92. stdout.puts("PASS: test_root_entity\n");
  93. } else {
  94. failed++;
  95. stdout.puts("FAIL: test_root_entity\n");
  96. }
  97. // Test 12: Nested paths
  98. if (test_nested_paths()) {
  99. passed++;
  100. stdout.puts("PASS: test_nested_paths\n");
  101. } else {
  102. failed++;
  103. stdout.puts("FAIL: test_nested_paths\n");
  104. }
  105. // Test 13: Complex properties
  106. if (test_complex_properties()) {
  107. passed++;
  108. stdout.puts("PASS: test_complex_properties\n");
  109. } else {
  110. failed++;
  111. stdout.puts("FAIL: test_complex_properties\n");
  112. }
  113. // Test 14: Empty children
  114. if (test_empty_children()) {
  115. passed++;
  116. stdout.puts("PASS: test_empty_children\n");
  117. } else {
  118. failed++;
  119. stdout.puts("FAIL: test_empty_children\n");
  120. }
  121. // Test 15: Persistence
  122. if (test_storage_persistence()) {
  123. passed++;
  124. stdout.puts("PASS: test_storage_persistence\n");
  125. } else {
  126. failed++;
  127. stdout.puts("FAIL: test_storage_persistence\n");
  128. }
  129. stdout.printf("\nResults: %d passed, %d failed\n", passed, failed);
  130. return failed > 0 ? 1 : 0;
  131. }
  132. // Helper to create temporary directory
  133. string create_temp_dir() {
  134. string temp_dir = DirUtils.mkdtemp("implexus_storage_test_XXXXXX");
  135. return temp_dir;
  136. }
  137. // Test 1: Store and get entity metadata
  138. bool test_store_entity_metadata() {
  139. string temp_dir = create_temp_dir();
  140. try {
  141. var storage = new BasicStorage.with_directory(temp_dir);
  142. var path = new EntityPath("/users");
  143. storage.store_entity_metadata(path, EntityType.CONTAINER, null);
  144. var type = storage.get_entity_type(path);
  145. if (type == null) return false;
  146. if ((!) type != EntityType.CONTAINER) return false;
  147. cleanup_dir(temp_dir);
  148. return true;
  149. } catch (Error e) {
  150. cleanup_dir(temp_dir);
  151. return false;
  152. }
  153. }
  154. // Test 2: Entity exists
  155. bool test_entity_exists() {
  156. string temp_dir = create_temp_dir();
  157. try {
  158. var storage = new BasicStorage.with_directory(temp_dir);
  159. var path = new EntityPath("/users");
  160. if (storage.entity_exists(path)) return false; // Should not exist yet
  161. storage.store_entity_metadata(path, EntityType.CONTAINER, null);
  162. if (!storage.entity_exists(path)) return false; // Should exist now
  163. cleanup_dir(temp_dir);
  164. return true;
  165. } catch (Error e) {
  166. cleanup_dir(temp_dir);
  167. return false;
  168. }
  169. }
  170. // Test 3: Delete entity
  171. bool test_delete_entity() {
  172. string temp_dir = create_temp_dir();
  173. try {
  174. var storage = new BasicStorage.with_directory(temp_dir);
  175. var path = new EntityPath("/users");
  176. storage.store_entity_metadata(path, EntityType.CONTAINER, null);
  177. if (!storage.entity_exists(path)) return false;
  178. storage.delete_entity(path);
  179. if (storage.entity_exists(path)) return false;
  180. cleanup_dir(temp_dir);
  181. return true;
  182. } catch (Error e) {
  183. cleanup_dir(temp_dir);
  184. return false;
  185. }
  186. }
  187. // Test 4: Store and load properties
  188. bool test_store_load_properties() {
  189. string temp_dir = create_temp_dir();
  190. try {
  191. var storage = new BasicStorage.with_directory(temp_dir);
  192. var path = new EntityPath("/users/john");
  193. storage.store_entity_metadata(path, EntityType.DOCUMENT, "User");
  194. var props = new Invercargill.DataStructures.PropertyDictionary();
  195. props.set("name", new Invercargill.NativeElement<string>("John Doe"));
  196. props.set("age", new Invercargill.NativeElement<int64?>(30));
  197. props.set("active", new Invercargill.NativeElement<bool?>(true));
  198. storage.store_properties(path, props);
  199. var loaded = storage.load_properties(path);
  200. if (loaded == null) return false;
  201. // Check name
  202. var name_elem = ((!) loaded).get("name");
  203. if (name_elem == null || ((!) name_elem).is_null()) return false;
  204. string name = ((!) name_elem).as<string>();
  205. if (name != "John Doe") return false;
  206. // Check age
  207. var age_elem = ((!) loaded).get("age");
  208. if (age_elem == null || ((!) age_elem).is_null()) return false;
  209. int64? age = ((!) age_elem).as<int64?>();
  210. if (age == null || (!) age != 30) return false;
  211. // Check active
  212. var active_elem = ((!) loaded).get("active");
  213. if (active_elem == null || ((!) active_elem).is_null()) return false;
  214. bool active = ((!) active_elem).as<bool?>();
  215. if (active != true) return false;
  216. cleanup_dir(temp_dir);
  217. return true;
  218. } catch (Error e) {
  219. cleanup_dir(temp_dir);
  220. return false;
  221. }
  222. }
  223. // Test 5: Add and get children
  224. bool test_add_get_children() {
  225. string temp_dir = create_temp_dir();
  226. try {
  227. var storage = new BasicStorage.with_directory(temp_dir);
  228. var parent = new EntityPath("/users");
  229. storage.store_entity_metadata(parent, EntityType.CONTAINER, null);
  230. storage.add_child(parent, "john");
  231. storage.add_child(parent, "jane");
  232. storage.add_child(parent, "bob");
  233. var children = storage.get_children(parent);
  234. if (children.count() != 3) return false;
  235. // Check all children are present
  236. var child_set = new Invercargill.DataStructures.HashSet<string>();
  237. foreach (var child in children) {
  238. child_set.add(child);
  239. }
  240. if (!child_set.has("john")) return false;
  241. if (!child_set.has("jane")) return false;
  242. if (!child_set.has("bob")) return false;
  243. cleanup_dir(temp_dir);
  244. return true;
  245. } catch (Error e) {
  246. cleanup_dir(temp_dir);
  247. return false;
  248. }
  249. }
  250. // Test 6: Remove child
  251. bool test_remove_child() {
  252. string temp_dir = create_temp_dir();
  253. try {
  254. var storage = new BasicStorage.with_directory(temp_dir);
  255. var parent = new EntityPath("/users");
  256. storage.store_entity_metadata(parent, EntityType.CONTAINER, null);
  257. storage.add_child(parent, "john");
  258. storage.add_child(parent, "jane");
  259. if (storage.get_children(parent).count() != 2) return false;
  260. storage.remove_child(parent, "john");
  261. var children = storage.get_children(parent);
  262. if (children.count() != 1) return false;
  263. foreach (var child in children) {
  264. if (child != "jane") return false;
  265. }
  266. cleanup_dir(temp_dir);
  267. return true;
  268. } catch (Error e) {
  269. cleanup_dir(temp_dir);
  270. return false;
  271. }
  272. }
  273. // Test 7: Has child
  274. bool test_has_child() {
  275. string temp_dir = create_temp_dir();
  276. try {
  277. var storage = new BasicStorage.with_directory(temp_dir);
  278. var parent = new EntityPath("/users");
  279. storage.store_entity_metadata(parent, EntityType.CONTAINER, null);
  280. if (storage.has_child(parent, "john")) return false;
  281. storage.add_child(parent, "john");
  282. if (!storage.has_child(parent, "john")) return false;
  283. if (storage.has_child(parent, "jane")) return false;
  284. cleanup_dir(temp_dir);
  285. return true;
  286. } catch (Error e) {
  287. cleanup_dir(temp_dir);
  288. return false;
  289. }
  290. }
  291. // Test 8: Store and get category config
  292. bool test_category_config() {
  293. string temp_dir = create_temp_dir();
  294. try {
  295. var storage = new BasicStorage.with_directory(temp_dir);
  296. var path = new EntityPath("/users/by_name");
  297. storage.store_entity_metadata(path, EntityType.CATEGORY, null);
  298. storage.store_category_config(path, "User", "name");
  299. var config = storage.get_category_config(path);
  300. if (config == null) return false;
  301. if (((!) config).type_label != "User") return false;
  302. if (((!) config).expression != "name") return false;
  303. cleanup_dir(temp_dir);
  304. return true;
  305. } catch (Error e) {
  306. cleanup_dir(temp_dir);
  307. return false;
  308. }
  309. }
  310. // Test 9: Type label storage
  311. bool test_type_label() {
  312. string temp_dir = create_temp_dir();
  313. try {
  314. var storage = new BasicStorage.with_directory(temp_dir);
  315. var path = new EntityPath("/users/john");
  316. storage.store_entity_metadata(path, EntityType.DOCUMENT, "User");
  317. var label = storage.get_entity_type_label(path);
  318. if (label == null || (!) label != "User") return false;
  319. // Test without type label
  320. var path2 = new EntityPath("/categories");
  321. storage.store_entity_metadata(path2, EntityType.CONTAINER, null);
  322. var label2 = storage.get_entity_type_label(path2);
  323. // Should be null for containers without type label
  324. if (label2 != null) return false;
  325. cleanup_dir(temp_dir);
  326. return true;
  327. } catch (Error e) {
  328. cleanup_dir(temp_dir);
  329. return false;
  330. }
  331. }
  332. // Test 10: Multiple entity types
  333. bool test_multiple_entity_types() {
  334. string temp_dir = create_temp_dir();
  335. try {
  336. var storage = new BasicStorage.with_directory(temp_dir);
  337. // Container
  338. var cat_path = new EntityPath("/categories");
  339. storage.store_entity_metadata(cat_path, EntityType.CONTAINER, null);
  340. // Document
  341. var doc_path = new EntityPath("/documents/doc1");
  342. storage.store_entity_metadata(doc_path, EntityType.DOCUMENT, "Doc");
  343. // Category
  344. var category_path = new EntityPath("/categories/cat1");
  345. storage.store_entity_metadata(category_path, EntityType.CATEGORY, null);
  346. // Index
  347. var index_path = new EntityPath("/indices/idx1");
  348. storage.store_entity_metadata(index_path, EntityType.INDEX, null);
  349. // Verify types
  350. var cat_type = storage.get_entity_type(cat_path);
  351. if (cat_type == null || (!) cat_type != EntityType.CONTAINER) return false;
  352. var doc_type = storage.get_entity_type(doc_path);
  353. if (doc_type == null || (!) doc_type != EntityType.DOCUMENT) return false;
  354. var category_type = storage.get_entity_type(category_path);
  355. if (category_type == null || (!) category_type != EntityType.CATEGORY) return false;
  356. var index_type = storage.get_entity_type(index_path);
  357. if (index_type == null || (!) index_type != EntityType.INDEX) return false;
  358. cleanup_dir(temp_dir);
  359. return true;
  360. } catch (Error e) {
  361. cleanup_dir(temp_dir);
  362. return false;
  363. }
  364. }
  365. // Test 11: Root entity
  366. bool test_root_entity() {
  367. string temp_dir = create_temp_dir();
  368. try {
  369. var storage = new BasicStorage.with_directory(temp_dir);
  370. var root = new EntityPath.root();
  371. storage.store_entity_metadata(root, EntityType.CONTAINER, null);
  372. var type = storage.get_entity_type(root);
  373. if (type == null || (!) type != EntityType.CONTAINER) return false;
  374. if (!storage.entity_exists(root)) return false;
  375. cleanup_dir(temp_dir);
  376. return true;
  377. } catch (Error e) {
  378. cleanup_dir(temp_dir);
  379. return false;
  380. }
  381. }
  382. // Test 12: Nested paths
  383. bool test_nested_paths() {
  384. string temp_dir = create_temp_dir();
  385. try {
  386. var storage = new BasicStorage.with_directory(temp_dir);
  387. // Create nested structure
  388. var level1 = new EntityPath("/level1");
  389. var level2 = new EntityPath("/level1/level2");
  390. var level3 = new EntityPath("/level1/level2/level3");
  391. storage.store_entity_metadata(level1, EntityType.CONTAINER, null);
  392. storage.store_entity_metadata(level2, EntityType.CONTAINER, null);
  393. storage.store_entity_metadata(level3, EntityType.DOCUMENT, "Deep");
  394. // Verify all exist
  395. if (!storage.entity_exists(level1)) return false;
  396. if (!storage.entity_exists(level2)) return false;
  397. if (!storage.entity_exists(level3)) return false;
  398. // Verify types
  399. var type1 = storage.get_entity_type(level1);
  400. var type2 = storage.get_entity_type(level2);
  401. var type3 = storage.get_entity_type(level3);
  402. if (type1 == null || (!) type1 != EntityType.CONTAINER) return false;
  403. if (type2 == null || (!) type2 != EntityType.CONTAINER) return false;
  404. if (type3 == null || (!) type3 != EntityType.DOCUMENT) return false;
  405. cleanup_dir(temp_dir);
  406. return true;
  407. } catch (Error e) {
  408. cleanup_dir(temp_dir);
  409. return false;
  410. }
  411. }
  412. // Test 13: Complex properties
  413. bool test_complex_properties() {
  414. string temp_dir = create_temp_dir();
  415. try {
  416. var storage = new BasicStorage.with_directory(temp_dir);
  417. var path = new EntityPath("/complex");
  418. storage.store_entity_metadata(path, EntityType.DOCUMENT, "Complex");
  419. var props = new Invercargill.DataStructures.PropertyDictionary();
  420. // Nested array
  421. var array = new Invercargill.DataStructures.Vector<Invercargill.Element>();
  422. array.add(new Invercargill.NativeElement<string>("item1"));
  423. array.add(new Invercargill.NativeElement<string>("item2"));
  424. array.add(new Invercargill.NativeElement<string>("item3"));
  425. props.set("items", new Invercargill.NativeElement<Invercargill.Enumerable<Invercargill.Element>>(array));
  426. // Nested dictionary
  427. var dict = new Invercargill.DataStructures.PropertyDictionary();
  428. dict.set("key1", new Invercargill.NativeElement<string>("value1"));
  429. dict.set("key2", new Invercargill.NativeElement<string>("value2"));
  430. props.set("nested", new Invercargill.NativeElement<Invercargill.Properties>(dict));
  431. storage.store_properties(path, props);
  432. var loaded = storage.load_properties(path);
  433. if (loaded == null) return false;
  434. // Check array
  435. var items_elem = ((!) loaded).get("items");
  436. if (items_elem == null || ((!) items_elem).is_null()) return false;
  437. var items = ((!) items_elem).as<Invercargill.Enumerable<Invercargill.Element>>();
  438. if (items.count() != 3) return false;
  439. // Check dictionary
  440. var nested_elem = ((!) loaded).get("nested");
  441. if (nested_elem == null || ((!) nested_elem).is_null()) return false;
  442. var nested = ((!) nested_elem).as<Invercargill.Properties>();
  443. if (nested.count() != 2) return false;
  444. cleanup_dir(temp_dir);
  445. return true;
  446. } catch (Error e) {
  447. cleanup_dir(temp_dir);
  448. return false;
  449. }
  450. }
  451. // Test 14: Empty children
  452. bool test_empty_children() {
  453. string temp_dir = create_temp_dir();
  454. try {
  455. var storage = new BasicStorage.with_directory(temp_dir);
  456. var parent = new EntityPath("/empty");
  457. storage.store_entity_metadata(parent, EntityType.CONTAINER, null);
  458. var children = storage.get_children(parent);
  459. if (children.count() != 0) return false;
  460. if (storage.has_child(parent, "any")) return false;
  461. cleanup_dir(temp_dir);
  462. return true;
  463. } catch (Error e) {
  464. cleanup_dir(temp_dir);
  465. return false;
  466. }
  467. }
  468. // Test 15: Persistence
  469. bool test_storage_persistence() {
  470. string temp_dir = create_temp_dir();
  471. try {
  472. // Create and write
  473. var storage1 = new BasicStorage.with_directory(temp_dir);
  474. var path = new EntityPath("/persistent");
  475. storage1.store_entity_metadata(path, EntityType.DOCUMENT, "Persistent");
  476. var props = new Invercargill.DataStructures.PropertyDictionary();
  477. props.set("key", new Invercargill.NativeElement<string>("value"));
  478. storage1.store_properties(path, props);
  479. // Create new instance
  480. var storage2 = new BasicStorage.with_directory(temp_dir);
  481. // Verify data persists
  482. if (!storage2.entity_exists(path)) return false;
  483. var type = storage2.get_entity_type(path);
  484. if (type == null || (!) type != EntityType.DOCUMENT) return false;
  485. var loaded = storage2.load_properties(path);
  486. if (loaded == null) return false;
  487. var key_elem = ((!) loaded).get("key");
  488. if (key_elem == null || ((!) key_elem).is_null()) return false;
  489. string key = ((!) key_elem).as<string>();
  490. if (key != "value") return false;
  491. cleanup_dir(temp_dir);
  492. return true;
  493. } catch (Error e) {
  494. cleanup_dir(temp_dir);
  495. return false;
  496. }
  497. }
  498. // Cleanup helper
  499. void cleanup_dir(string path) {
  500. try {
  501. Dir dir = Dir.open(path, 0);
  502. string? name;
  503. while ((name = dir.read_name()) != null) {
  504. FileUtils.unlink(Path.build_filename(path, name));
  505. }
  506. } catch (FileError e) {
  507. // Ignore errors
  508. }
  509. DirUtils.remove(path);
  510. }