0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

dataform-assertions をさくっと試す

Posted at

概要

自分が所属する組織では dataform の assertion の実装が割と後回しにされがち。
なかなかキレイに書けないのでモチベーションが沸かないのかもしれないが、品質を保証する大事な作業なのでなんとかしたい。
と思って調べたら dataform-assertions という良いものを見つけたので軽く試す。

ref. https://github.com/devoteamgcloud/dataform-assertions

試す

dataform の version 3 以降は workflow_settings.yaml で dataform core の version を指定してやればコンパイルするときに勝手に dataform core もインストールされるのだが、他に依存するライブラリがあれば package.json を使う必要がある。

ref. https://github.com/dataform-co/dataform/releases/tag/3.0.0

workflow_settings.yaml から「dataformCoreVersion: 3.0.8」の行を消し、version 3 にする際に削除した package.json を復活させる。

workflow_settings.yaml
defaultLocation: asia-northeast1
defaultDataset: dataform
defaultAssertionDataset: dataform_assertions

dataform core の version 通常は workflow_settings.yaml に記述するのだが、package.json を使う場合はそちらに記述する。

package.json
{
    "name": "data-analytics-dataform",
    "dependencies": {
        "@dataform/core": "3.0.8",
        "dataform-assertions": "https://github.com/devoteamgcloud/dataform-assertions/archive/refs/tags/v2.0.0.tar.gz"
    }
}

そしてインストール。

$ dataform install
 
Installing NPM dependencies...
 
Project dependencies successfully installed.

以下のコードは fact_orders というテーブルに対して、order_date が前日以降のデータを対象に、order_id のカラムに null が含まれていないかテストする内容。

globalAssertionsParams.database / schema は assertion 実行時に作成される view が格納される先を設定する箇所で、default_database / schema を置き換えるものではないことに注意。

assertions.js
const commonAssertions = require("dataform-assertions");
 
const commonAssertionsResult = commonAssertions({
    globalAssertionsParams: {
        // If not provided, the default Dataform project config will be used
        "database": "data-sandbox",
        "schema": "dataform_assertions",
        // "location": "bigquery-location",
        "tags": ["global-assertions-tag"],
        // "disabledInEnvs": ["dv"] // Check match with 'dataform.projectConfig.vars.env' value
    },
    config: {
        "data_component": {
            "fact_orders": {
                "where": "order_date >= current_date() - 1"
            }
        }
    },
    rowConditions: {
        "data_component": {
            "fact_orders": {
                "order_id_not_null": "order_id is not null",
            }
        }
    }
});

実行してみる。

$ dataform run --tags global-assertions-tag --default-database data-sandbox
Compiling...
 
Compiled successfully.
 
Running...
 
Assertion passed:  dataform_assertions.assert_order_id_not_nulldata_component_fact_orders

すばらしい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?