lookml-tests
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLookML Testing Standards
LookML 测试标准
Testing is critical for maintaining trust in data. LookML tests allow us to verify that our semantic model behaves as expected and that the underlying data conforms to our assumptions.
测试对于维护数据可信度至关重要。LookML测试使我们能够验证语义模型是否符合预期,以及底层数据是否符合我们的假设。
1. File Organization
1. 文件组织
- Location: Define tests in .
tests/[explore_name].test.lkml - One Suite Per Explore: Each file should contain all the test definitions for a specific Explore.
- Naming Convention: (e.g.,
[explore_name].test.lkml).orders.test.lkml
- 位置:在中定义测试。
tests/[explore_name].test.lkml - 每个Explore对应一个测试套件:每个文件应包含特定Explore的所有测试定义。
- 命名规范:(例如:
[explore_name].test.lkml)。orders.test.lkml
2. Test Structure
2. 测试结构
Each test consists of an query and an statement.
explore_sourceassertlookml
test: [test_name] {
explore_source: [explore_name] {
column: [column_name] { field: [view_name].[field_name] }
filters: {
field: [view_name].[field_name]
value: "[value]"
}
}
assert: [assertion_name] {
expression: ${[view_name].[field_name]} [operator] [value] ;;
}
}每个测试由查询和语句组成。
explore_sourceassertlookml
test: [test_name] {
explore_source: [explore_name] {
column: [column_name] { field: [view_name].[field_name] }
filters: {
field: [view_name].[field_name]
value: "[value]"
}
}
assert: [assertion_name] {
expression: ${[view_name].[field_name]} [operator] [value] ;;
}
}3. Types of Tests
3. 测试类型
A. Integrity Checks (Critical)
A. 完整性检查(关键)
Verify that Primary Keys remain unique after joins. This is the best defense against "fanout" errors caused by incorrect join definitions.
one_to_manyExample: Primary Key Uniqueness
lookml
test: orders_pk_is_unique {
explore_source: orders {
column: order_id {}
column: count {}
# Limit to recent data to save costs/time if table is large
filters: {
field: orders.created_date
value: "last 7 days"
}
}
assert: order_id_is_unique {
expression: ${orders.count} = 1 ;;
}
}验证主键在关联后保持唯一。这是防止因错误的关联定义导致“扇出”错误的最佳手段。
one_to_many示例:主键唯一性
lookml
test: orders_pk_is_unique {
explore_source: orders {
column: order_id {}
column: count {}
# 如果表较大,限制为近期数据以节省成本和时间
filters: {
field: orders.created_date
value: "last 7 days"
}
}
assert: order_id_is_unique {
expression: ${orders.count} = 1 ;;
}
}B. Accuracy Tests
B. 准确性测试
Validate specific measure values against known constants or expectations.
Example: Revenue is Positive
lookml
test: revenue_is_positive {
explore_source: orders {
column: total_revenue {}
filters: {
field: orders.created_date
value: "yesterday"
}
}
assert: revenue_greater_than_zero {
expression: ${orders.total_revenue} >= 0 ;;
}
}针对已知常量或预期值验证特定度量值。
示例:收入为正
lookml
test: revenue_is_positive {
explore_source: orders {
column: total_revenue {}
filters: {
field: orders.created_date
value: "yesterday"
}
}
assert: revenue_greater_than_zero {
expression: ${orders.total_revenue} >= 0 ;;
}
}C. Business Logic Validation
C. 业务逻辑验证
Ensure calculations behave as expected. For example, checking that is never greater than or that is never NULL for an active user.
gross_marginrevenuelifetime_ordersExample: Logic Check
lookml
test: margin_less_than_revenue {
explore_source: orders {
column: total_revenue {}
column: total_margin {}
}
assert: margin_is_valid {
expression: ${orders.total_margin} <= ${orders.total_revenue} ;;
}
}确保计算符合预期。例如,检查永远不会大于,或者活跃用户的永远不会为NULL。
gross_marginrevenuelifetime_orders示例:逻辑检查
lookml
test: margin_less_than_revenue {
explore_source: orders {
column: total_revenue {}
column: total_margin {}
}
assert: margin_is_valid {
expression: ${orders.total_margin} <= ${orders.total_revenue} ;;
}
}4. Best Practices
4. 最佳实践
- Descriptive Extensions: Use informative names for tests () and assertions (
orders_pk_is_unique).order_id_is_unique - Performance: Use filters (e.g., ) to limit the scan size for large tables, unless verifying full history is required.
last 7 days - Model Inclusion: Ensure files are included in the model file (e.g.,
test).include: "/tests/*.test.lkml"
- 描述性命名:为测试(如)和断言(如
orders_pk_is_unique)使用信息丰富的名称。order_id_is_unique - 性能优化:使用过滤器(例如)限制大表的扫描范围,除非需要验证完整历史数据。
last 7 days - 模型包含:确保文件被包含在模型文件中(例如:
test)。include: "/tests/*.test.lkml"