test(playwright): additional dataset list playwright tests (#36684)

Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Joe Li
2026-02-05 16:42:07 -08:00
committed by GitHub
parent ef4f7afa90
commit 5040db859c
30 changed files with 2828 additions and 125 deletions

View File

@@ -0,0 +1,61 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import type { Response, APIResponse } from '@playwright/test';
import { expect } from '@playwright/test';
/**
* Common interface for response types with status() method.
* Supports both Response (network interception) and APIResponse (page.request API).
*/
type ResponseLike = Response | APIResponse;
/**
* Verify response has exact status code
* @param response - Playwright Response or APIResponse object
* @param expected - Expected status code
* @returns The response for chaining
*/
export function expectStatus<T extends ResponseLike>(
response: T,
expected: number,
): T {
expect(
response.status(),
`Expected status ${expected}, got ${response.status()}`,
).toBe(expected);
return response;
}
/**
* Verify response status code is one of the expected values
* @param response - Playwright Response or APIResponse object
* @param expected - Array of acceptable status codes
* @returns The response for chaining
*/
export function expectStatusOneOf<T extends ResponseLike>(
response: T,
expected: number[],
): T {
expect(
expected,
`Expected status to be one of ${expected.join(', ')}, got ${response.status()}`,
).toContain(response.status());
return response;
}

View File

@@ -18,12 +18,33 @@
*/
import { Page, APIResponse } from '@playwright/test';
import { apiPost, apiDelete, ApiRequestOptions } from './requests';
import rison from 'rison';
import { apiGet, apiPost, apiDelete, ApiRequestOptions } from './requests';
const ENDPOINTS = {
DATABASE: 'api/v1/database/',
} as const;
/**
* TypeScript interface for database API response
*/
export interface DatabaseResult {
id: number;
database_name: string;
/** Optional - list API masks this for security, only detail API returns it */
sqlalchemy_uri?: string;
backend?: string;
engine_information?: {
disable_ssh_tunneling?: boolean;
supports_dynamic_catalog?: boolean;
supports_file_upload?: boolean;
supports_oauth2?: boolean;
};
extra?: string;
expose_in_sqllab?: boolean;
impersonate_user?: boolean;
}
/**
* TypeScript interface for database creation API payload
* Provides compile-time safety for required fields
@@ -31,6 +52,7 @@ const ENDPOINTS = {
export interface DatabaseCreatePayload {
database_name: string;
engine: string;
sqlalchemy_uri?: string;
configuration_method?: string;
engine_information?: {
disable_ssh_tunneling?: boolean;
@@ -77,3 +99,53 @@ export async function apiDeleteDatabase(
): Promise<APIResponse> {
return apiDelete(page, `${ENDPOINTS.DATABASE}${databaseId}`, options);
}
/**
* GET request to fetch a database's details
* @param page - Playwright page instance (provides authentication context)
* @param databaseId - ID of the database to fetch
* @returns API response with database details
*/
export async function apiGetDatabase(
page: Page,
databaseId: number,
options?: ApiRequestOptions,
): Promise<APIResponse> {
return apiGet(page, `${ENDPOINTS.DATABASE}${databaseId}`, options);
}
/**
* Get a database by its name
* @param page - Playwright page instance (provides authentication context)
* @param databaseName - The database_name to search for
* @returns Database object if found, null if not found
*/
export async function getDatabaseByName(
page: Page,
databaseName: string,
): Promise<DatabaseResult | null> {
const filter = {
filters: [
{
col: 'database_name',
opr: 'eq',
value: databaseName,
},
],
};
const queryParam = rison.encode(filter);
const response = await apiGet(page, `${ENDPOINTS.DATABASE}?q=${queryParam}`, {
failOnStatusCode: false,
});
if (!response.ok()) {
return null;
}
const body = await response.json();
if (body.result && body.result.length > 0) {
return body.result[0] as DatabaseResult;
}
return null;
}

View File

@@ -20,9 +20,13 @@
import { Page, APIResponse } from '@playwright/test';
import rison from 'rison';
import { apiGet, apiPost, apiDelete, ApiRequestOptions } from './requests';
import { getDatabaseByName } from './database';
export const ENDPOINTS = {
DATASET: 'api/v1/dataset/',
DATASET_EXPORT: 'api/v1/dataset/export/',
DATASET_DUPLICATE: 'api/v1/dataset/duplicate',
DATASET_IMPORT: 'api/v1/dataset/import/',
} as const;
/**
@@ -37,12 +41,12 @@ export interface DatasetCreatePayload {
}
/**
* TypeScript interface for virtual dataset creation API payload
* Virtual datasets are SQL-based and support the Duplicate action in UI
* TypeScript interface for virtual dataset creation API payload.
* Virtual datasets are defined by SQL queries rather than physical tables.
*/
export interface VirtualDatasetCreatePayload {
database: number;
schema: string;
schema: string | null;
table_name: string;
sql: string;
owners?: number[];
@@ -55,8 +59,8 @@ export interface VirtualDatasetCreatePayload {
export interface DatasetResult {
id: number;
table_name: string;
sql?: string;
schema?: string;
sql?: string | null;
schema?: string | null;
database: {
id: number;
database_name: string;
@@ -79,11 +83,11 @@ export async function apiPostDataset(
}
/**
* POST request to create a virtual (SQL-based) dataset
* Virtual datasets support the Duplicate action in the UI
* POST request to create a virtual dataset with SQL.
* Use expectStatusOneOf() on the response and handle both result.id and id shapes.
* @param page - Playwright page instance (provides authentication context)
* @param requestBody - Virtual dataset config (database, schema, table_name, sql)
* @returns API response from dataset creation
* @param requestBody - Virtual dataset configuration (database, schema, table_name, sql)
* @returns API response from virtual dataset creation
*/
export async function apiPostVirtualDataset(
page: Page,
@@ -96,16 +100,27 @@ export async function apiPostVirtualDataset(
* Creates a simple virtual dataset for testing purposes
* @param page - Playwright page instance
* @param name - Name for the virtual dataset
* @param databaseId - ID of the database to use (defaults to 1 for examples db)
* @param databaseId - ID of the database to use (looks up 'examples' DB if not provided)
* @returns The created dataset ID, or null on failure
*/
export async function createTestVirtualDataset(
page: Page,
name: string,
databaseId = 1,
databaseId?: number,
): Promise<number | null> {
// Look up examples database if no ID provided
let dbId = databaseId;
if (dbId === undefined) {
const examplesDb = await getDatabaseByName(page, 'examples');
if (!examplesDb?.id) {
console.warn('Failed to find examples database');
return null;
}
dbId = examplesDb.id;
}
const response = await apiPostVirtualDataset(page, {
database: databaseId,
database: dbId,
schema: '',
table_name: name,
sql: "SELECT 1 as id, 'test' as name",
@@ -118,7 +133,8 @@ export async function createTestVirtualDataset(
}
const body = await response.json();
return body.id ?? null;
// Handle both response shapes: { id } or { result: { id } }
return body.result?.id ?? body.id ?? null;
}
/**
@@ -186,3 +202,30 @@ export async function apiDeleteDataset(
): Promise<APIResponse> {
return apiDelete(page, `${ENDPOINTS.DATASET}${datasetId}`, options);
}
/**
* Duplicate a dataset via the API
* @param page - Playwright page instance (provides authentication context)
* @param datasetId - ID of the dataset to duplicate
* @param newName - Name for the duplicated dataset
* @returns Object containing the new dataset's ID (use apiGetDataset for full details)
*/
export async function duplicateDataset(
page: Page,
datasetId: number,
newName: string,
): Promise<{ id: number }> {
const response = await apiPost(page, `${ENDPOINTS.DATASET}duplicate`, {
base_model_id: datasetId,
table_name: newName,
});
const body = await response.json();
// Normalize: API may return id at top level or inside result
const resolvedId = body.result?.id ?? body.id;
if (!resolvedId) {
throw new Error(
`Duplicate dataset API returned no id. Response: ${JSON.stringify(body)}`,
);
}
return { id: resolvedId };
}

View File

@@ -0,0 +1,145 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import type { Page, Response } from '@playwright/test';
/**
* HTTP methods enum for consistency
*/
export const HTTP_METHODS = {
GET: 'GET',
POST: 'POST',
PUT: 'PUT',
DELETE: 'DELETE',
PATCH: 'PATCH',
} as const;
type HttpMethod = (typeof HTTP_METHODS)[keyof typeof HTTP_METHODS];
/**
* Options for waitFor* functions
*/
interface WaitForResponseOptions {
/** Optional timeout in milliseconds */
timeout?: number;
/** Match against URL pathname suffix instead of full URL includes (default: false) */
pathMatch?: boolean;
}
/**
* Normalize a path by removing trailing slashes
*/
function normalizePath(path: string): string {
return path.replace(/\/+$/, '');
}
/**
* Check if a URL matches a pattern
* - String + pathMatch: pathname.endsWith(pattern) with trailing slash normalization
* - String: url.includes(pattern)
* - RegExp: pattern.test(url)
*/
function matchUrl(
url: string,
pattern: string | RegExp,
pathMatch?: boolean,
): boolean {
if (typeof pattern === 'string') {
if (pathMatch) {
const pathname = normalizePath(new URL(url).pathname);
const normalizedPattern = normalizePath(pattern);
return pathname.endsWith(normalizedPattern);
}
return url.includes(pattern);
}
return pattern.test(url);
}
/**
* Generic helper to wait for a response matching URL pattern and HTTP method
*/
function waitForResponse(
page: Page,
urlPattern: string | RegExp,
method: HttpMethod,
options?: WaitForResponseOptions,
): Promise<Response> {
const { pathMatch, ...waitOptions } = options ?? {};
return page.waitForResponse(
response =>
matchUrl(response.url(), urlPattern, pathMatch) &&
response.request().method() === method,
waitOptions,
);
}
/**
* Wait for a GET response matching the URL pattern
*/
export function waitForGet(
page: Page,
urlPattern: string | RegExp,
options?: WaitForResponseOptions,
): Promise<Response> {
return waitForResponse(page, urlPattern, HTTP_METHODS.GET, options);
}
/**
* Wait for a POST response matching the URL pattern
*/
export function waitForPost(
page: Page,
urlPattern: string | RegExp,
options?: WaitForResponseOptions,
): Promise<Response> {
return waitForResponse(page, urlPattern, HTTP_METHODS.POST, options);
}
/**
* Wait for a PUT response matching the URL pattern
*/
export function waitForPut(
page: Page,
urlPattern: string | RegExp,
options?: WaitForResponseOptions,
): Promise<Response> {
return waitForResponse(page, urlPattern, HTTP_METHODS.PUT, options);
}
/**
* Wait for a DELETE response matching the URL pattern
*/
export function waitForDelete(
page: Page,
urlPattern: string | RegExp,
options?: WaitForResponseOptions,
): Promise<Response> {
return waitForResponse(page, urlPattern, HTTP_METHODS.DELETE, options);
}
/**
* Wait for a PATCH response matching the URL pattern
*/
export function waitForPatch(
page: Page,
urlPattern: string | RegExp,
options?: WaitForResponseOptions,
): Promise<Response> {
return waitForResponse(page, urlPattern, HTTP_METHODS.PATCH, options);
}