added jsonb field tests to queryer and merger and fixed a bug there

This commit is contained in:
2026-04-14 13:23:01 -04:00
parent 24adf3ffc6
commit 8984acaa5f
6 changed files with 2462 additions and 1993 deletions

104
add_test.py Normal file
View File

@ -0,0 +1,104 @@
import json
def load_json(path):
with open(path, 'r') as f:
return json.load(f)
def save_json(path, data):
with open(path, 'w') as f:
json.dump(data, f, indent=2)
def add_invoice(data):
# Add 'invoice' type
types = data[0]['database']['types']
# Check if invoice already exists
if any(t.get('name') == 'invoice' for t in types):
return
types.append({
"name": "invoice",
"hierarchy": ["invoice", "entity"],
"primary_key": ["id"],
"field_types": {
"id": "uuid",
"number": "text",
"metadata": "jsonb"
},
"schemas": {
"invoice": {
"type": "entity",
"properties": {
"id": { "type": "string" },
"number": { "type": "string" },
"metadata": {
"type": "object",
"properties": {
"internal_note": { "type": "string" },
"customer_snapshot": { "type": "entity" },
"related_rules": {
"type": "array",
"items": { "type": "governance_rule" }
}
}
}
}
}
}
})
def process_merger():
data = load_json('fixtures/merger.json')
add_invoice(data)
# Add test
data[0]['tests'].append({
"name": "Insert invoice with deep jsonb metadata",
"schema": "invoice",
"payload": {
"number": "INV-1001",
"metadata": {
"internal_note": "Confidential",
"customer_snapshot": {
"id": "00000000-0000-0000-0000-000000000000",
"type": "person",
"first_name": "John"
},
"related_rules": [
{
"id": "11111111-1111-1111-1111-111111111111"
}
]
}
},
"expect": {
"sql": [
[
"INSERT INTO agreego.invoice (metadata, number, id) VALUES ($1, $2, gen_random_uuid()) ON CONFLICT (id) DO UPDATE SET metadata = EXCLUDED.metadata, number = EXCLUDED.number RETURNING id, type",
{"metadata": {"customer_snapshot": {"first_name": "John", "id": "00000000-0000-0000-0000-000000000000", "type": "person"}, "internal_note": "Confidential", "related_rules": [{"id": "11111111-1111-1111-1111-111111111111"}]}, "number": "INV-1001"}
]
]
}
})
save_json('fixtures/merger.json', data)
def process_queryer():
data = load_json('fixtures/queryer.json')
add_invoice(data)
data[0]['tests'].append({
"name": "Query invoice with complex JSONB metadata field extraction",
"schema": "invoice",
"query": {
"extract": ["id", "number", "metadata"],
"conditions": []
},
"expect": {
"sql": "SELECT jsonb_build_object('id', t1.id, 'metadata', t1.metadata, 'number', t1.number) FROM agreego.invoice t1 WHERE (t1.id IS NOT NULL)",
"params": {}
}
})
save_json('fixtures/queryer.json', data)
process_merger()
process_queryer()

View File

@ -1,63 +1,87 @@
import json
path = "fixtures/database.json"
def load_json(path):
with open(path, 'r') as f:
return json.load(f)
with open(path, "r") as f:
data = json.load(f)
def save_json(path, data):
with open(path, 'w') as f:
json.dump(data, f, indent=4)
test_case = data[-1]
def fix_merger():
data = load_json('fixtures/merger.json')
last_test = data[0]['tests'][-1]
test_case["database"]["relations"] = [
{
"id": "r1",
"type": "relation",
"constraint": "fk_person_email",
"source_type": "person", "source_columns": ["email_id"],
"destination_type": "email_address", "destination_columns": ["id"],
"prefix": "email"
},
{
"id": "r2",
"type": "relation",
"constraint": "fk_person_ad_hoc_bubble",
"source_type": "person", "source_columns": ["ad_hoc_bubble_id"],
"destination_type": "some_bubble", "destination_columns": ["id"],
"prefix": "ad_hoc_bubble"
},
{
"id": "r3",
"type": "relation",
"constraint": "fk_person_generic_bubble",
"source_type": "person", "source_columns": ["generic_bubble_id"],
"destination_type": "some_bubble", "destination_columns": ["id"],
"prefix": "generic_bubble"
},
{
"id": "r4",
"type": "relation",
"constraint": "fk_person_extended_relations",
"source_type": "contact", "source_columns": ["source_id"],
"destination_type": "person", "destination_columns": ["id"],
"prefix": "extended_relations"
},
{
"id": "r5",
"type": "relation",
"constraint": "fk_person_standard_relations",
"source_type": "contact", "source_columns": ["source_id_2"],
"destination_type": "person", "destination_columns": ["id"],
"prefix": "standard_relations"
},
{
"id": "r6",
"type": "relation",
"constraint": "fk_contact_target",
"source_type": "contact", "source_columns": ["target_id"],
"destination_type": "email_address", "destination_columns": ["id"],
"prefix": "target"
}
]
# Check if the last test is our bad one
if "name" in last_test and last_test["name"] == "Insert invoice with deep jsonb metadata":
new_test = {
"description": last_test["name"],
"action": "merge",
"schema_id": last_test["schema"],
"data": last_test["payload"],
"expect": {
"success": True,
"sql": [
[
"INSERT INTO agreego.invoice (",
" \"metadata\",",
" \"number\",",
" entity_id,",
" id,",
" type",
")",
"VALUES (",
" '{",
" \"customer_snapshot\":{",
" \"first_name\":\"John\",",
" \"id\":\"00000000-0000-0000-0000-000000000000\",",
" \"type\":\"person\"",
" },",
" \"internal_note\":\"Confidential\",",
" \"related_rules\":[",
" {",
" \"id\":\"11111111-1111-1111-1111-111111111111\"",
" }",
" ]",
" }',",
" 'INV-1001',",
" NULL,",
" '{{uuid}}',",
" 'invoice'",
")"
]
]
}
}
data[0]['tests'][-1] = new_test
save_json('fixtures/merger.json', data)
with open(path, "w") as f:
json.dump(data, f, indent=2)
def fix_queryer():
data = load_json('fixtures/queryer.json')
last_test = data[0]['tests'][-1]
if "name" in last_test and last_test["name"] == "Query invoice with complex JSONB metadata field extraction":
new_test = {
"description": last_test["name"],
"action": "query",
"schema_id": last_test["schema"],
"expect": {
"success": True,
"sql": [
[
"(SELECT jsonb_strip_nulls(jsonb_build_object(",
" 'id', invoice_1.id,",
" 'metadata', invoice_1.metadata,",
" 'number', invoice_1.number,",
" 'type', invoice_1.type",
"))",
"FROM agreego.invoice invoice_1)"
]
]
}
}
data[0]['tests'][-1] = new_test
save_json('fixtures/queryer.json', data)
fix_merger()
fix_queryer()

View File

@ -826,6 +826,87 @@
"historical": true,
"notify": true,
"relationship": false
},
{
"name": "invoice",
"hierarchy": [
"invoice",
"entity"
],
"fields": [
"id",
"type",
"number",
"metadata",
"created_at",
"created_by",
"modified_at",
"modified_by",
"archived"
],
"grouped_fields": {
"invoice": [
"id",
"type",
"number",
"metadata"
],
"entity": [
"id",
"type",
"created_at",
"created_by",
"modified_at",
"modified_by",
"archived"
]
},
"lookup_fields": [
"id"
],
"historical": true,
"relationship": false,
"field_types": {
"id": "uuid",
"type": "text",
"archived": "boolean",
"number": "text",
"metadata": "jsonb",
"created_at": "timestamptz",
"created_by": "uuid",
"modified_at": "timestamptz",
"modified_by": "uuid"
},
"schemas": {
"invoice": {
"type": "entity",
"properties": {
"id": {
"type": "string"
},
"number": {
"type": "string"
},
"metadata": {
"type": "object",
"properties": {
"internal_note": {
"type": "string"
},
"customer_snapshot": {
"type": "entity"
},
"related_rules": {
"type": "array",
"items": {
"type": "entity"
}
}
}
}
}
}
}
}
]
},
@ -2802,6 +2883,123 @@
]
]
}
},
{
"description": "Insert invoice with deep jsonb metadata",
"action": "merge",
"schema_id": "invoice",
"data": {
"id": "11111111-2222-3333-4444-555555555555",
"type": "invoice",
"number": "INV-1001",
"metadata": {
"internal_note": "Confidential",
"customer_snapshot": {
"id": "00000000-0000-0000-0000-000000000000",
"type": "person",
"first_name": "John"
},
"related_rules": [
{
"id": "11111111-1111-1111-1111-111111111111",
"type": "entity"
}
]
}
},
"expect": {
"success": true,
"sql": [
[
"SELECT to_jsonb(t1.*) || to_jsonb(t2.*)",
"FROM agreego.\"invoice\" t1",
"LEFT JOIN agreego.\"entity\" t2 ON t2.id = t1.id",
"WHERE t1.id = '11111111-2222-3333-4444-555555555555' OR (\"id\" = '11111111-2222-3333-4444-555555555555')"
],
[
"INSERT INTO agreego.\"entity\" (",
" \"created_at\",",
" \"created_by\",",
" \"id\",",
" \"modified_at\",",
" \"modified_by\",",
" \"type\"",
")",
"VALUES (",
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" '11111111-2222-3333-4444-555555555555',",
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000',",
" 'invoice'",
")"
],
[
"INSERT INTO agreego.\"invoice\" (",
" \"id\",",
" \"metadata\",",
" \"number\",",
" \"type\"",
")",
"VALUES (",
" '11111111-2222-3333-4444-555555555555',",
" '{",
" \"customer_snapshot\":{",
" \"first_name\":\"John\",",
" \"id\":\"00000000-0000-0000-0000-000000000000\",",
" \"type\":\"person\"",
" },",
" \"internal_note\":\"Confidential\",",
" \"related_rules\":[",
" {",
" \"id\":\"11111111-1111-1111-1111-111111111111\",",
" \"type\":\"entity\"",
" }",
" ]",
" }',",
" 'INV-1001',",
" 'invoice'",
")"
],
[
"INSERT INTO agreego.change (",
" \"old\",",
" \"new\",",
" entity_id,",
" id,",
" kind,",
" modified_at,",
" modified_by",
")",
"VALUES (",
" NULL,",
" '{",
" \"metadata\":{",
" \"customer_snapshot\":{",
" \"first_name\":\"John\",",
" \"id\":\"00000000-0000-0000-0000-000000000000\",",
" \"type\":\"person\"",
" },",
" \"internal_note\":\"Confidential\",",
" \"related_rules\":[",
" {",
" \"id\":\"11111111-1111-1111-1111-111111111111\",",
" \"type\":\"entity\"",
" }",
" ]",
" },",
" \"number\":\"INV-1001\",",
" \"type\":\"invoice\"",
" }',",
" '11111111-2222-3333-4444-555555555555',",
" '{{uuid}}',",
" 'create',",
" '{{timestamp}}',",
" '00000000-0000-0000-0000-000000000000'",
")"
]
]
}
}
]
}

File diff suppressed because it is too large Load Diff

View File

@ -1451,6 +1451,12 @@ fn test_queryer_0_12() {
crate::tests::runner::run_test_case(&path, 0, 12).unwrap();
}
#[test]
fn test_queryer_0_13() {
let path = format!("{}/fixtures/queryer.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 13).unwrap();
}
#[test]
fn test_polymorphism_0_0() {
let path = format!("{}/fixtures/polymorphism.json", env!("CARGO_MANIFEST_DIR"));
@ -8086,3 +8092,9 @@ fn test_merger_0_12() {
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 12).unwrap();
}
#[test]
fn test_merger_0_13() {
let path = format!("{}/fixtures/merger.json", env!("CARGO_MANIFEST_DIR"));
crate::tests::runner::run_test_case(&path, 0, 13).unwrap();
}

24
wipe_test.py Normal file
View File

@ -0,0 +1,24 @@
import json
def load_json(path):
with open(path, 'r') as f:
return json.load(f)
def save_json(path, data):
with open(path, 'w') as f:
json.dump(data, f, indent=4)
def fix_merger():
data = load_json('fixtures/merger.json')
last_test = data[0]['tests'][-1]
last_test["expect"]["sql"] = []
save_json('fixtures/merger.json', data)
def fix_queryer():
data = load_json('fixtures/queryer.json')
last_test = data[0]['tests'][-1]
last_test["expect"]["sql"] = []
save_json('fixtures/queryer.json', data)
fix_merger()
fix_queryer()