유닛 테스트 지원 데이터 형식

유닛 테스트 지원 데이터 형식 (data formats)

dbt 유닛 테스트의 목 데이터(mock data)는 dict, csv, sql 세 가지 형식을 지원해요. format을 지정하지 않으면 기본값은 dict예요. 테스트 대상에 맞는 가장 편리한 형식을 골라 쓰면 돼요.

출처: 문서

본문

현재 dbt에서 유닛 테스트용 목 데이터는 세 가지 형식을 지원해요:

  • dict (기본값): 인라인 딕셔너리 값.
  • csv: 인라인 CSV 값 또는 CSV 파일.
  • sql: 인라인 SQL 쿼리 또는 SQL 파일. 주의: 이 형식에서는 모든 컬럼 에 대해 목 데이터를 제공해야 해요.

dict

dict 데이터 형식은 format이 정의되지 않았을 때의 기본값이에요. dictrows에 인라인 딕셔너리를 요구해요: models/schema.yml

unit_tests:
  - name: test_my_model
    model: my_model
    given:
      - input: ref('my_model_a')
        format: dict
        rows:
          - {id: 1, name: gerda}
          - {id: 2, name: michelle}    

CSV

csv 형식을 사용하면 rows에 인라인 CSV 문자열을 쓸 수 있어요: models/schema.yml

unit_tests:
  - name: test_my_model
    model: my_model
    given:
      - input: ref('my_model_a')
        format: csv
        rows: |
          id,name
          1,gerda
          2,michelle

또는 프로젝트의 tests/fixtures 디렉터리(또는 설정된 test-paths 위치)에 있는 CSV 파일의 이름을 fixture로 제공할 수도 있어요: models/schema.yml

unit_tests:
  - name: test_my_model
    model: my_model
    given:
      - input: ref('my_model_a')
        format: csv
        fixture: my_model_a_fixture

sql

이 형식을 사용하면:

  • 유닛 테스트할 수 있는 데이터 유형에 더 많은 유연성이 생겨요
  • ephemeral 모델에 의존하는 모델도 유닛 테스트할 수 있어요

다만 format: sql을 사용할 때는 모든 행 에 대해 목 데이터를 제공해야 해요. sql 형식을 사용하면 rows에 인라인 SQL 쿼리를 쓸 수 있어요: models/schema.yml

unit_tests:
  - name: test_my_model
    model: my_model
    given:
      - input: ref('my_model_a')
        format: sql
        rows: |
          select 1 as id, 'gerda' as name, null as loaded_at union all
          select 2 as id, 'michelle', null as loaded_at as name

또는 프로젝트의 tests/fixtures 디렉터리(또는 설정된 test-paths 위치)에 있는 SQL 파일의 이름을 fixture로 제공할 수도 있어요: models/schema.yml

unit_tests:
  - name: test_my_model
    model: my_model
    given:
      - input: ref('my_model_a')
        format: sql
        fixture: my_model_a_fixture

참고: 유닛 테스트용 SQL 픽스처에서는 Jinja가 지원되지 않아요.

더 알아보기 (Learn more)