test_views.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "../service/src/views/projection.hpp"
  2. #include <cassert>
  3. #include <iostream>
  4. #include <nlohmann/json.hpp>
  5. using smartbotic::database::applyProjection;
  6. void test_empty_projection_returns_document_unchanged() {
  7. nlohmann::json doc = {{"name", "Alice"}, {"age", 30}};
  8. auto result = applyProjection(doc, {}, {});
  9. assert(result == doc);
  10. std::cout << "PASS: empty projection returns document unchanged\n";
  11. }
  12. void test_include_keeps_only_listed_fields() {
  13. nlohmann::json doc = {{"name", "Alice"}, {"email", "a@x.com"}, {"password", "secret"}};
  14. auto result = applyProjection(doc, {"name", "email"}, {});
  15. assert(result.contains("name"));
  16. assert(result.contains("email"));
  17. assert(!result.contains("password"));
  18. std::cout << "PASS: include keeps only listed fields\n";
  19. }
  20. void test_exclude_removes_fields() {
  21. nlohmann::json doc = {{"name", "Alice"}, {"email", "a@x.com"}, {"password", "secret"}};
  22. auto result = applyProjection(doc, {}, {"password"});
  23. assert(result.contains("name"));
  24. assert(result.contains("email"));
  25. assert(!result.contains("password"));
  26. std::cout << "PASS: exclude removes fields\n";
  27. }
  28. void test_include_takes_precedence_over_exclude() {
  29. nlohmann::json doc = {{"a", 1}, {"b", 2}, {"c", 3}};
  30. auto result = applyProjection(doc, {"a"}, {"b", "c"});
  31. assert(result.contains("a"));
  32. assert(!result.contains("b"));
  33. assert(!result.contains("c"));
  34. assert(result.size() == 1);
  35. std::cout << "PASS: include takes precedence over exclude\n";
  36. }
  37. void test_nested_include_path() {
  38. nlohmann::json doc = {
  39. {"user", {
  40. {"profile", {{"name", "Alice"}, {"bio", "hi"}}},
  41. {"private", {{"ssn", "123"}}}
  42. }}
  43. };
  44. auto result = applyProjection(doc, {"user.profile.name"}, {});
  45. assert(result["user"]["profile"]["name"] == "Alice");
  46. assert(!result["user"]["profile"].contains("bio"));
  47. assert(!result["user"].contains("private"));
  48. std::cout << "PASS: nested include path\n";
  49. }
  50. void test_nested_exclude_path() {
  51. nlohmann::json doc = {
  52. {"user", {
  53. {"name", "Alice"},
  54. {"private", {{"ssn", "123"}, {"email", "a@x.com"}}}
  55. }}
  56. };
  57. auto result = applyProjection(doc, {}, {"user.private.ssn"});
  58. assert(result["user"]["name"] == "Alice");
  59. assert(result["user"]["private"]["email"] == "a@x.com");
  60. assert(!result["user"]["private"].contains("ssn"));
  61. std::cout << "PASS: nested exclude path\n";
  62. }
  63. void test_metadata_always_preserved_with_include() {
  64. nlohmann::json doc = {
  65. {"_id", "doc-1"},
  66. {"_version", 5},
  67. {"name", "Alice"},
  68. {"secret", "xyz"}
  69. };
  70. auto result = applyProjection(doc, {"name"}, {});
  71. assert(result.contains("_id"));
  72. assert(result.contains("_version"));
  73. assert(result.contains("name"));
  74. assert(!result.contains("secret"));
  75. std::cout << "PASS: metadata always preserved with include\n";
  76. }
  77. void test_metadata_cannot_be_excluded() {
  78. nlohmann::json doc = {{"_id", "doc-1"}, {"_version", 5}, {"name", "Alice"}};
  79. auto result = applyProjection(doc, {}, {"_id", "_version"});
  80. assert(result.contains("_id"));
  81. assert(result.contains("_version"));
  82. std::cout << "PASS: metadata cannot be excluded\n";
  83. }
  84. void test_missing_path_silently_skipped() {
  85. nlohmann::json doc = {{"name", "Alice"}};
  86. auto result = applyProjection(doc, {"user.profile.name"}, {});
  87. assert(!result.contains("user"));
  88. std::cout << "PASS: missing path silently skipped\n";
  89. }
  90. int main() {
  91. test_empty_projection_returns_document_unchanged();
  92. test_include_keeps_only_listed_fields();
  93. test_exclude_removes_fields();
  94. test_include_takes_precedence_over_exclude();
  95. test_nested_include_path();
  96. test_nested_exclude_path();
  97. test_metadata_always_preserved_with_include();
  98. test_metadata_cannot_be_excluded();
  99. test_missing_path_silently_skipped();
  100. std::cout << "\nAll view projection tests PASSED!\n";
  101. return 0;
  102. }