Skip to content

Commit

Permalink
fix(admin/performance): Fix default time bug in Schedule policy (meta…
Browse files Browse the repository at this point in the history
  • Loading branch information
rafpaf authored Jul 25, 2024
1 parent 63b483b commit 6b5ce3b
Show file tree
Hide file tree
Showing 13 changed files with 522 additions and 189 deletions.
21 changes: 20 additions & 1 deletion e2e/support/config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from "node:path";

import {
removeDirectory,
verifyDownloadTasks,
Expand All @@ -21,6 +23,23 @@ const targetVersion = process.env["CROSS_VERSION_TARGET"];

const feHealthcheckEnabled = process.env["CYPRESS_FE_HEALTHCHECK"] === "true";

// docs say that tsconfig paths should handle aliases, but they don't
const assetsResolverPlugin = {
name: "assetsResolver",
setup(build) {
// Redirect all paths starting with "assets/" to "resources/"
build.onResolve({ filter: /^assets\// }, args => {
return {
path: path.join(
__dirname,
"../../resources/frontend_client/app",
args.path,
),
};
});
},
};

const defaultConfig = {
// This is the functionality of the old cypress-plugins.js file
setupNodeEvents(on, config) {
Expand All @@ -36,7 +55,7 @@ const defaultConfig = {
loader: {
".svg": "text",
},
plugins: [NodeModulesPolyfillPlugin()],
plugins: [NodeModulesPolyfillPlugin(), assetsResolverPlugin],
sourcemap: "inline",
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import dayjs from "dayjs";
import duration from "dayjs/plugin/duration";
import relativeTime from "dayjs/plugin/relativeTime";

dayjs.extend(duration);
dayjs.extend(relativeTime);

/** Intercept routes for caching tests */
export const interceptRoutes = () => {
cy.intercept("POST", "/api/dataset").as("dataset");
cy.intercept("POST", "/api/card/*/query").as("cardQuery");
cy.intercept("PUT", "/api/cache").as("putCacheConfig");
cy.intercept("DELETE", "/api/cache").as("deleteCacheConfig");
cy.intercept("GET", "/api/cache?model=*&id=*").as("getCacheConfig");
cy.intercept("POST", "/api/dashboard/*/dashcard/*/card/*/query").as(
"dashcardQuery",
);
cy.intercept("POST", "/api/persist/enable").as("enablePersistence");
cy.intercept("POST", "/api/persist/disable").as("disablePersistence");
cy.intercept("POST", "/api/cache/invalidate?include=overrides&database=*").as(
"invalidateCacheForSampleDatabase",
);
};

/** Cypress log messages sometimes occur out of order so it is helpful to log to the console as well. */
export const log = (message: string) => {
cy.log(message);
console.log(message);
};

export const databaseCachingSettingsPage = () =>
cy.findByRole("main", { name: "Database caching settings" });
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {
type ScheduleComponentType,
getScheduleComponentLabel,
} from "metabase/components/Schedule/constants";
import type { CacheStrategyType, CacheableModel } from "metabase-types/api";

import { databaseCachingSettingsPage } from "./e2e-performance-helpers";

/** Save the cache strategy form and wait for a response from the relevant endpoint */
export const saveCacheStrategyForm = (options?: {
strategyType?: CacheStrategyType;
/** 'Model' as in 'type of object' */
model?: CacheableModel;
}) => {
let expectedRoute: string;
if (options?.strategyType === "nocache" && options?.model === "root") {
// When setting the default policy to "Don't cache", we delete the policy in the BE
expectedRoute = "@deleteCacheConfig";
} else if (options?.strategyType === "inherit") {
// When setting a database's policy to "Use default", we delete the policy in the BE
expectedRoute = "@deleteCacheConfig";
} else {
// Otherwise we update the cache config
expectedRoute = "@putCacheConfig";
}
cy.log("Save the cache strategy form");
cacheStrategyForm().button(/Save/).click();
return cy.wait(expectedRoute);
};

export const cacheStrategyForm = () =>
cy.findByLabelText("Select the cache invalidation policy");

export const cacheStrategyRadioButton = (name: RegExp) =>
cacheStrategyForm().findByRole("radio", { name });

export const durationRadioButton = () => cacheStrategyRadioButton(/Duration/);
export const adaptiveRadioButton = () => cacheStrategyRadioButton(/Adaptive/);
export const scheduleRadioButton = () => cacheStrategyRadioButton(/Schedule/);
export const dontCacheResultsRadioButton = () =>
cacheStrategyRadioButton(/Don.t cache results/);
export const useDefaultRadioButton = () =>
cacheStrategyRadioButton(/Use default/);

export const formLauncher = (
itemName: string,
preface:
| "currently"
| "currently inheriting"
| "currently inheriting the default policy",
strategyLabel = "",
) => {
databaseCachingSettingsPage().should("exist");
const regExp = new RegExp(`Edit.*${itemName}.*${preface}.*${strategyLabel}`);
cy.log(`Finding strategy for launcher for regular expression: ${regExp}`);
const launcher = databaseCachingSettingsPage().findByLabelText(regExp);
launcher.should("exist");
return launcher;
};

export const openStrategyFormForDatabaseOrDefaultPolicy = (
/** To open the form for the default policy, set this parameter to "default policy" */
databaseNameOrDefaultPolicy: string,
currentStrategyLabel?: string,
) => {
cy.visit("/admin/performance");
cy.findByRole("tab", { name: "Database caching settings" }).click();
cy.log(`Open strategy form for ${databaseNameOrDefaultPolicy}`);
formLauncher(
databaseNameOrDefaultPolicy,
"currently",
currentStrategyLabel,
).click();
};

export const getScheduleComponent = (componentType: ScheduleComponentType) =>
cacheStrategyForm().findByLabelText(getScheduleComponentLabel(componentType));

export const openSidebar = () => {
cy.findByLabelText("info icon").click();
};
export const closeSidebar = () => {
cy.findByLabelText("info icon").click();
};

/** Open the sidebar form that lets you set the caching strategy.
* This works on dashboards and questions */
export const openSidebarCacheStrategyForm = () => {
cy.log("Open the cache strategy form in the sidebar");
openSidebar();
cy.wait("@getCacheConfig");
cy.findByLabelText("Caching policy").click();
};
Loading

0 comments on commit 6b5ce3b

Please sign in to comment.