Repository navigation
Expand file tree
/
Copy pathruntime_examples_tests.cpp
More file actions
187 lines (184 loc) · 9.59 KB
/
Copy pathruntime_examples_tests.cpp
File metadata and controls
187 lines (184 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "../../examples/scenarios.hpp"
#include <iostream>
#include <fstream>
#include <iterator>
#include <filesystem>
using namespace teaql;
static void ownership_and_provenance(const Json& reference, const Json& historical) {
for (bool graph : {false, true}) {
GlobalContext global;
auto user = global.make_user_context();
auto r = user->memory_resource();
const auto& rows = graph ? example::relations_graph(*user) : example::schema_crud(*user);
const auto& expected_rows =
std::get<Json::Object>(std::get<Json::Object>(reference.data)
.at(String("business_rows", reference.memory_resource()))
.data)
.at(String(graph ? "relations" : "schema", reference.memory_resource()));
example::require(example::observations(r, rows) == expected_rows,
"complete business rows match the current original example flow");
auto before = example::observations(r, rows).dump(global.memory_resource());
auto& retained = global.make<SmartList<example::Order>>(rows);
bool audited = false, purposed = false;
for (const auto& log : user->sql_logs()) {
auto reason = graph ? "Create the example order graph" : "Submit the example order";
audited |= log.audit_reason == reason ||
(log.comment && log.comment->find(reason) != String::npos);
purposed |= log.purpose == (graph ? "Display the saved example order"
: "Inspect the soft-deleted example order");
}
example::require(audited && purposed,
"runtime retains audit reason and query purpose in SQL provenance");
{
Json::Array observed(r);
for (const auto& log : user->sql_logs()) {
Json::Object fields(std::less<>{}, r);
std::string_view operation;
switch (log.operation) {
case SqlLogOperation::Select:
operation = "Select";
break;
case SqlLogOperation::Insert:
operation = "Insert";
break;
case SqlLogOperation::Update:
operation = "Update";
break;
case SqlLogOperation::Delete:
operation = "Delete";
break;
case SqlLogOperation::Recover:
operation = "Recover";
break;
}
fields.emplace(String("operation", r), Json(r, operation));
fields.emplace(String("purpose", r), log.purpose ? Json(r, *log.purpose) : Json(r));
fields.emplace(String("audit", r),
log.audit_reason ? Json(r, *log.audit_reason) : Json(r));
observed.push_back(Json(r, fields));
}
const auto& expected =
std::get<Json::Object>(reference.data)
.at(String(graph ? "relations" : "schema", reference.memory_resource()));
example::require(
Json(r, observed) == expected,
"operation/purpose/audit provenance matches the 8.2.0 saved-state example flow");
const auto& old_expected =
std::get<Json::Object>(historical.data)
.at(String(graph ? "relations" : "schema", historical.memory_resource()));
auto business_changes = [&](const Json& logs) {
Json::Array changes(r);
for (const auto& item : std::get<Json::Array>(logs.data)) {
const auto& fields = std::get<Json::Object>(item.data);
if (fields.at(String("operation", r)) != Json(r, "Select")) {
changes.emplace_back(item);
}
}
return Json(r, changes);
};
example::require(business_changes(expected) == business_changes(old_expected),
"historical business changes retain their operation and audit intent");
}
user.reset();
auto target = global.memory_resource();
example::require(
example::observations(target, retained).dump(target) == before,
"explicit Global copy retains complete nested results after User destruction");
if (graph) {
example::require(
retained[0].lines[0].name.get_allocator().resource() == target &&
retained[0].lines[0].product->name.get_allocator().resource() == target &&
retained[0].lines[0].product->root.memory_resource() == target,
"nested model strings and runtime snapshots use their destination domain");
}
}
}
static void graph_failure_rolls_back_all_nodes() {
GlobalContext global;
auto user = global.make_user_context();
auto r = user->memory_resource();
(void)example::relations_graph(*user);
auto service = user->entity_data_service<example::Executor>("Order");
auto& repository = example::value(service).get();
auto order = example::make_order(*user, 1, "must roll back");
order.id = 2;
for (auto id : {20u, 10u}) {
example::OrderLine line(std::allocator_arg, Allocator(r));
line.id = id;
line.order_id = 2;
line.name = "must roll back";
line.product_id = 100;
line.mark_as_new();
order.lines.data.emplace_back(std::move(line));
}
order.lines.loaded();
auto& result = AuditedSaveExt<example::Order>::save(
std::move(order).audit_as("Reject the conflicting child graph"), *user)
.wait();
example::require(!result, "a conflicting explicitly-new child fails graph save");
auto query = repository.select();
query.comment("Inspect complete graph rollback");
query.filter(Expr::eq(r, "id", std::uint64_t(2)));
PurposedSelectQuery purpose(
std::allocator_arg, Allocator(r), query, "Check complete graph rollback");
example::require(
example::value(repository.fetch_entities<example::Order>(purpose).wait()).len() == 0,
"failed child write rolls back the previously inserted root");
auto lines = user->entity_data_service<example::Executor>("OrderLine");
auto line_query = example::value(lines).get().select();
line_query.comment("Inspect child graph rollback");
line_query.filter(Expr::eq(r, "id", std::uint64_t(20)));
PurposedSelectQuery line_purpose(
std::allocator_arg, Allocator(r), line_query, "Check child graph rollback");
example::require(
example::value(
example::value(lines).get().fetch_entities<example::OrderLine>(line_purpose).wait())
.len() == 0,
"failed graph save rolls back the earlier sibling write");
auto existing = repository.select();
existing.comment("Inspect the existing graph after rollback");
SelectQuery products(std::allocator_arg, Allocator(r), "OrderLine");
products.relation("product");
existing.filter(Expr::eq(r, "id", std::uint64_t(1))).relation_query("lines", products);
PurposedSelectQuery existing_purpose(
std::allocator_arg, Allocator(r), existing, "Check existing graph after rollback");
const auto& saved =
example::value(repository.fetch_enhanced_entities<example::Order>(existing_purpose).wait());
example::require(saved.len() == 1 && saved[0].lines.len() == 2 &&
saved[0].lines[0].name == "first-line" &&
saved[0].lines[0].product->name == "keyboard",
"original graph and referenced products survive failed save");
}
int main(int argc, char** argv) {
auto* previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
try {
example::require(argc == 2, "provide the recorded original provenance fixture");
std::ifstream input(argv[1]);
example::require(bool(input), "open original provenance fixture");
std::string source{std::istreambuf_iterator<char>(input), std::istreambuf_iterator<char>()};
GlobalContext evidence;
auto reference = Json::parse(evidence.memory_resource(), source);
example::require(reference.has_value(), "parse original provenance fixture");
auto current_path =
std::filesystem::path(argv[1]).parent_path() / "runtime_examples_provenance_8_2_0.json";
std::ifstream current_input(current_path);
example::require(bool(current_input), "open current provenance fixture");
std::string current_source{std::istreambuf_iterator<char>(current_input),
std::istreambuf_iterator<char>()};
auto current = Json::parse(evidence.memory_resource(), current_source);
example::require(current.has_value(), "parse current provenance fixture");
example::require(
std::get<Json::Object>(current->data)
.at(String("baseline", evidence.memory_resource())) ==
Json(evidence.memory_resource(), "ebbc6c3ef3f241e4f35f4db997944fe31853cc51"),
"current provenance belongs to the locked Rust 8.2.0 source");
ownership_and_provenance(*current, *reference);
graph_failure_rolls_back_all_nodes();
std::pmr::set_default_resource(previous);
std::cout << "Runtime example ownership and graph rollback contracts passed\n";
} catch (const std::exception& error) {
std::pmr::set_default_resource(previous);
std::cerr << error.what() << '\n';
return 1;
}
}