Files
jspg/add_test.py

105 lines
3.4 KiB
Python

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()