Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

3.0 KB
Raw Blame
/**
 * Editing and deleting a data quality check from the Models overview.
 */
import { expect } from '@playwright/test'

import { test } from '../utils/workspace-test-base'

const CHECK_NAME = 'orders_has_rows'
const SUBJECT_NAME = 'orders_e2e'

test('edits and deletes a check from Models', async ({ page, playwrightSetup }) => {
    const workspace = await playwrightSetup.createWorkspace({ skip_onboarding: true, no_demo_data: true })
    const auth = {
        headers: {
            Authorization: `Bearer ${workspace.personal_api_key}`,
            'Content-Type': 'application/json',
        },
    }

    const savedQuery = await page.request.post(`/api/projects/${workspace.team_id}/warehouse_saved_queries/`, {
        ...auth,
        data: { name: SUBJECT_NAME, query: { kind: 'HogQLQuery', query: 'SELECT 1 AS id' } },
    })
    expect(savedQuery.ok()).toBe(true)
    const savedQueryId = (await savedQuery.json()).id

    const created = await page.request.post(`/api/projects/${workspace.team_id}/data_quality_checks/`, {
        ...auth,
        data: {
            name: CHECK_NAME,
            check_type: 'custom_sql',
            column_name: '',
            config: { query: 'SELECT id FROM orders_e2e WHERE id < 0' },
            subject_type: 'view',
            subject_uuid: savedQueryId,
        },
    })
    expect(created.ok()).toBe(true)
    const check = await created.json()

    await playwrightSetup.loginAndNavigateToTeam(page, workspace)
    await page.goto('/models?tab=data-quality')

    await page.getByLabel(`Expand checks for ${SUBJECT_NAME}`).click({ timeout: 30000 })
    await expect(page.getByText(CHECK_NAME)).toBeVisible()

    await page.getByLabel(`Actions for check ${CHECK_NAME}`).click()
    const metadataResponse = page.waitForResponse(
        (response) =>
            response.url().includes('/query/HogQLMetadata/') && response.request().method() === 'POST' && response.ok()
    )
    await page.getByRole('menuitem', { name: 'Edit' }).click()
    await metadataResponse
    await page.getByLabel('Description').fill('Every order keeps a positive id')
    const saveButton = page.getByTestId('data-quality-check-save')
    await expect(saveButton).toBeEnabled()
    await saveButton.click()

    await expect(page.getByText('Check saved')).toBeVisible()
    const edited = await page.request.get(`/api/projects/${workspace.team_id}/data_quality_checks/${check.id}/`, auth)
    const editedCheck = await edited.json()
    // The point of the whole change: an edit refines the check rather than replacing it.
    expect(editedCheck.id).toEqual(check.id)
    expect(editedCheck.description).toEqual('Every order keeps a positive id')
    expect(editedCheck.last_status).toEqual(check.last_status)
    expect(editedCheck.last_run_at).toEqual(check.last_run_at)

    await page.getByLabel(`Actions for check ${CHECK_NAME}`).click()
    await page.getByRole('menuitem', { name: 'Delete' }).click()
    await page.getByRole('button', { name: 'Delete' }).click()

    await expect(page.getByText(CHECK_NAME)).toHaveCount(0)
})