Repository navigation
Expand file tree
/
Copy pathcore_model_tests.cpp
More file actions
214 lines (205 loc) · 10.1 KB
/
Copy pathcore_model_tests.cpp
File metadata and controls
214 lines (205 loc) · 10.1 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <teaql/core/meta.hpp>
#include <teaql/core/mutation.hpp>
#include <teaql/runtime/context.hpp>
#include <teaql/runtime/entity_status.hpp>
#include <array>
#include <iostream>
using namespace teaql;
static void check(bool condition, const char* message) {
if (!condition) {
throw std::runtime_error(message);
}
}
struct NoDefaultAllocator {
Memory previous = std::pmr::set_default_resource(std::pmr::null_memory_resource());
~NoDefaultAllocator() {
std::pmr::set_default_resource(previous);
}
};
template <class T>
concept HasFields = requires(T& value) { value.values; };
template <class T>
concept HasPredicateId = requires(T& value) { value.id; };
template <class T>
concept HasSoftDelete = requires(T& value) { value.soft_delete; };
template <class T>
concept SetsVersion = requires(T& value) { value.expected_version(1); };
template <class T>
concept SetsField = requires(T& value) { value.value("field", 1); };
static_assert(HasFields<InsertCommand> && !HasPredicateId<InsertCommand> &&
!SetsVersion<InsertCommand>);
static_assert(HasFields<UpdateCommand> && HasPredicateId<UpdateCommand> &&
SetsVersion<UpdateCommand>);
static_assert(!HasFields<DeleteCommand> && HasSoftDelete<DeleteCommand> &&
!SetsField<DeleteCommand>);
static_assert(!HasFields<RecoverCommand> && !HasSoftDelete<RecoverCommand> &&
!SetsField<RecoverCommand>);
static_assert(!std::is_convertible_v<GeneratedValues, MutationValues>);
static_assert(!std::is_convertible_v<EntitySnapshot, MutationValues>);
static void metadata_and_batches() {
NoDefaultAllocator guard;
GlobalContext global;
auto context = global.make_user_context();
auto r = context->memory_resource();
auto& descriptor = context->make<EntityDescriptor>("CustomerOrder");
check(!descriptor.id_property() && !descriptor.version_property(),
"missing role lookup returns None like Rust");
check(!descriptor.property_by_name("missing") && !descriptor.relation_by_name("missing"),
"missing name lookup returns None like Rust");
descriptor.table("customer_orders").service("edge-database").audit_max_length(80);
descriptor.property("id", DataType::U64).id();
descriptor.property("version", DataType::I64).version();
descriptor.property("name", DataType::Text, "display_name").not_null().with_max_length(120);
descriptor.property("amount", DataType::Decimal)
.with_numeric_precision(19)
.with_numeric_scale(7);
descriptor.relation("lines", "OrderLine")
.local("id")
.foreign("order_id")
.to_many()
.detached()
.keep_missing();
descriptor.relation("customer", "Customer");
descriptor.mask_fields({"name"});
check(descriptor.id_property()->name == "id" &&
descriptor.version_property()->name == "version",
"role lookup");
check(std::ranges::distance(descriptor.writable_properties()) == 3,
"writable properties exclude ID but retain version");
const auto& relation = *descriptor.relation_by_name("lines");
check(relation.many && !relation.attach && !relation.delete_missing, "relation policy shape");
const auto& defaults = *descriptor.relation_by_name("customer");
check(!defaults.many && defaults.attach && defaults.delete_missing, "Rust relation defaults");
check(defaults.local_key == "id" && defaults.foreign_key == "id", "Rust key defaults");
check(descriptor.property_by_name("name")->max_length == 120, "property length constraint");
check(descriptor.property_by_name("amount")->numeric_scale == 7, "numeric scale constraint");
check(!descriptor.find_property("missing") && !descriptor.find_relation("missing"),
"optional metadata lookup");
auto& retained = global.make<EntityDescriptor>(descriptor);
check(retained == descriptor, "metadata equality independent of allocation domain");
auto& empty_column = context->make<PropertyDescriptor>("empty_column", DataType::Text);
empty_column.column("");
check(global.make<PropertyDescriptor>(empty_column) == empty_column,
"clone preserves explicitly empty column mapping");
check(retained.properties[2].name.get_allocator().resource() == global.memory_resource(),
"property clone allocator");
check(retained.relations[0].local_key.get_allocator().resource() == global.memory_resource(),
"relation clone allocator");
check(retained.data_service->get_allocator().resource() == global.memory_resource(),
"optional service clone allocator");
auto& snapshot = context->make<EntitySnapshot>();
snapshot.fields.emplace(String("name", r), Value(r, "previously persisted order name"));
auto& update = context->make<UpdateCommand>("CustomerOrder", 1u);
update.old_values.emplace(r, snapshot);
update.value("name", "new order name").expected_version(4).audit_as("customer correction");
check(std::get<String>(update.old_values->fields.find("name")->second.data) ==
"previously persisted order name",
"snapshot is not overwritten by mutation input");
auto& batch = context->make<BatchUpdateCommand>(
"CustomerOrder", std::initializer_list<std::string_view>{"name"});
batch.batch_ids.emplace_back(1u);
batch.batch_expected_versions.emplace_back(4);
batch.batch_values.emplace_back(update.values);
batch.trace_chains.emplace_back();
for (const auto& node : update.trace_chain) {
batch.trace_chains.back().emplace_back(node);
}
batch.batch_old_values.emplace_back(std::in_place, r, snapshot);
check(batch.batch_values.front().fields.get_allocator().resource() == r,
"nested mutation allocator");
check(batch.trace_chains.front().front().comment.get_allocator().resource() == r,
"nested trace allocator");
check(batch.batch_old_values.front()->fields.get_allocator().resource() == r,
"nested snapshot allocator");
auto& insert_batch = context->make<BatchInsertCommand>("CustomerOrder");
insert_batch.batch_values.emplace_back(update.values);
check(insert_batch.batch_values.size() == 1 && insert_batch.trace_chains.empty(),
"Rust batch insert independent trace shape");
context.reset();
check(retained.relation_by_name("lines")->target_entity == "OrderLine",
"global metadata survives context destruction");
check(retained.audit_value_max_len == 80 && retained.audit_mask_fields.front() == "name",
"audit metadata retained");
}
static void status_table() {
using S = EntityStatus;
using A = EntityAction;
// Exhaustive 7 x 4 table, taken from the Rust state machine, not derived from C++ next_status.
using Row = std::array<std::optional<S>, 4>;
const auto none = std::nullopt;
const std::array<Row, 7> expected{Row{S::New, none, S::Persisted, none},
Row{S::Updated, S::UpdatedDeleted, none, none},
Row{none, none, none, S::UpdatedRecover},
Row{S::Updated, none, S::Persisted, none},
Row{none, S::UpdatedDeleted, S::PersistedDeleted, none},
Row{none, none, S::Persisted, S::UpdatedRecover},
Row{none, none, none, none}};
for (int s = 0; s < 7; ++s) {
for (int a = 0; a < 4; ++a) {
bool failed = false;
try {
auto next = next_status(static_cast<S>(s), static_cast<A>(a));
check(expected[s][a] && next == *expected[s][a], "unexpected status transition");
} catch (const std::invalid_argument&) {
failed = true;
}
check(failed == !expected[s][a].has_value(), "invalid transitions must reject");
}
}
const std::array<bool, 7> dirty{true, false, false, true, true, true, false};
for (int s = 0; s < 7; ++s) {
check(need_persist(static_cast<S>(s)) == dirty[s], "persist flags");
}
auto state = S::New;
for (auto action : {A::Persist, A::Update, A::Persist, A::Delete, A::Persist}) {
state = next_status(state, action);
}
check(is_deleted(state), "deleted lifecycle");
state = next_status(state, A::Recover);
check(is_recover(state), "recover pending");
state = next_status(state, A::Persist);
check(state == S::Persisted, "recover persisted");
}
static void trace_assignment_domains() {
NoDefaultAllocator guard;
GlobalContext global;
auto user = global.make_user_context();
auto r = user->memory_resource();
Ref<TraceNode> retained;
{
TraceNode source(std::allocator_arg,
Allocator(r),
TraceKind::Purpose,
String(256, 'e', r),
String(4096, 'c', r),
7);
retained = user->make_global<TraceNode>(TraceKind::Operation, "old", "old");
user->get(retained) = std::move(source);
check(user->get(retained).memory_resource() == global.memory_resource() &&
user->get(retained).comment.get_allocator().resource() ==
global.memory_resource(),
"trace move assignment preserves the destination domain for both strings");
TraceChain from(r), to(r);
from.emplace_back(TraceKind::Request, "Entity", "request");
to = std::move(from);
check(to.size() == 1 && to[0].kind == TraceKind::Request,
"trace elements support standard vector move assignment");
}
user.reset();
auto next = global.make_user_context();
const auto& kept = next->get(next->rebind_global(retained));
check(kept.kind == TraceKind::Purpose && kept.entity_type.size() == 256 &&
kept.comment.size() == 4096 && kept.entity_id == 7,
"assigned trace data survives actual User deletion through explicit Global ownership");
}
int main() {
try {
metadata_and_batches();
status_table();
trace_assignment_domains();
std::cout << "Core model contracts passed\n";
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
}
}