import json file_path = "fixtures/queryer.json" with open(file_path, "r") as f: data = json.load(f) # Find the test case test_case = next(tc for tc in data if tc.get("description") == "Base entity family select on polymorphic tree") # The sql is an array of strings expected_sql = test_case["expect"]["sql"][0] # We need to extract the entity block and move it # The blocks are delimited by " WHEN t1_obj_t1.type = " # Actually, the easiest way is to re-build the expected_sql from the actual output logged, # or simply swap the entity block in the array strings. # But since we just want to reorder the WHEN clauses... # Let's just fix it by string manipulation or we can just replace the whole expect[sql][0] block new_sql = [ "(SELECT jsonb_build_object(", " 'id', t1_obj_t1.id,", " 'type', CASE WHEN t1_obj_t1.type = 'address' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'city', t1_obj_t1_obj_t1.city,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.address t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'contact' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t3.archived,", " 'created_at', t1_obj_t1_obj_t3.created_at,", " 'id', t1_obj_t1_obj_t3.id,", " 'is_primary', t1_obj_t1_obj_t1.is_primary,", " 'name', t1_obj_t1_obj_t3.name,", " 'type', t1_obj_t1_obj_t3.type)", " FROM agreego.contact t1_obj_t1_obj_t1", " JOIN agreego.relationship t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " JOIN agreego.entity t1_obj_t1_obj_t3 ON t1_obj_t1_obj_t3.id = t1_obj_t1_obj_t2.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'email_address' THEN ((SELECT jsonb_build_object(", " 'address', t1_obj_t1_obj_t1.address,", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.email_address t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'entity' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t1.archived,", " 'created_at', t1_obj_t1_obj_t1.created_at,", " 'id', t1_obj_t1_obj_t1.id,", " 'name', t1_obj_t1_obj_t1.name,", " 'type', t1_obj_t1_obj_t1.type)", " FROM agreego.entity t1_obj_t1_obj_t1", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'order' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'customer_id', t1_obj_t1_obj_t1.customer_id,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'total', t1_obj_t1_obj_t1.total,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.order t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'order_line' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'order_id', t1_obj_t1_obj_t1.order_id,", " 'price', t1_obj_t1_obj_t1.price,", " 'product', t1_obj_t1_obj_t1.product,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.order_line t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'organization' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.organization t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'person' THEN ((SELECT jsonb_build_object(", " 'age', t1_obj_t1_obj_t1.age,", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'first_name', t1_obj_t1_obj_t1.first_name,", " 'id', t1_obj_t1_obj_t2.id,", " 'last_name', t1_obj_t1_obj_t1.last_name,", " 'name', t1_obj_t1_obj_t2.name,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.person t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'phone_number' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'number', t1_obj_t1_obj_t1.number,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.phone_number t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " WHEN t1_obj_t1.type = 'relationship' THEN ((SELECT jsonb_build_object(", " 'archived', t1_obj_t1_obj_t2.archived,", " 'created_at', t1_obj_t1_obj_t2.created_at,", " 'id', t1_obj_t1_obj_t2.id,", " 'name', t1_obj_t1_obj_t2.name,", " 'type', t1_obj_t1_obj_t2.type)", " FROM agreego.relationship t1_obj_t1_obj_t1", " JOIN agreego.entity t1_obj_t1_obj_t2 ON t1_obj_t1_obj_t2.id = t1_obj_t1_obj_t1.id", " WHERE NOT t1_obj_t1_obj_t1.archived))", " ELSE NULL END)", "FROM agreego.entity t1_obj_t1", "WHERE NOT t1_obj_t1.archived)" ] test_case["expect"]["sql"][0] = new_sql with open(file_path, "w") as f: json.dump(data, f, indent=4) print("Fixed queryer.json expected sql array ordering.")