lookml-tests

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

LookML 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:
    [explore_name].test.lkml
    (e.g.,
    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
explore_source
query and an
assert
statement.
lookml
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_source
查询和
assert
语句组成。
lookml
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
one_to_many
join definitions.
Example: 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
gross_margin
is never greater than
revenue
or that
lifetime_orders
is never NULL for an active user.
Example: 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} ;;
  }
}
确保计算符合预期。例如,检查
gross_margin
永远不会大于
revenue
,或者活跃用户的
lifetime_orders
永远不会为NULL。
示例:逻辑检查
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 (
    orders_pk_is_unique
    ) and assertions (
    order_id_is_unique
    ).
  • Performance: Use filters (e.g.,
    last 7 days
    ) to limit the scan size for large tables, unless verifying full history is required.
  • Model Inclusion: Ensure
    test
    files are included in the model file (e.g.,
    include: "/tests/*.test.lkml"
    ).
  • 描述性命名:为测试(如
    orders_pk_is_unique
    )和断言(如
    order_id_is_unique
    )使用信息丰富的名称。
  • 性能优化:使用过滤器(例如
    last 7 days
    )限制大表的扫描范围,除非需要验证完整历史数据。
  • 模型包含:确保
    test
    文件被包含在模型文件中(例如:
    include: "/tests/*.test.lkml"
    )。