Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

3.0 KB
Raw Blame
import {
  doesOperatorExist,
  getOperatorByTypeAndName,
  isEqualsOperator,
  isFuzzyOperator,
} from "metabase-lib/v1/operators/utils/index";
import {
  COORDINATE,
  FOREIGN_KEY,
  NUMBER,
  PRIMARY_KEY,
  STRING,
} from "metabase-lib/v1/types/constants";

describe("metabase-lib/v1/operators/utils", () => {
  describe("doesOperatorExist", () => {
    it("should return a boolean indicating existence of operator with given name", () => {
      expect(doesOperatorExist("foo")).toBe(false);
      expect(doesOperatorExist("contains")).toBe(true);
      expect(doesOperatorExist("between")).toBe(true);
    });
  });

  describe("isEqualsOperator", () => {
    it("should evaluate whether it is an equals operator", () => {
      expect(isEqualsOperator()).toBe(false);
      expect(
        isEqualsOperator(getOperatorByTypeAndName(NUMBER, "between")),
      ).toBe(false);
      expect(isEqualsOperator(getOperatorByTypeAndName(NUMBER, "="))).toBe(
        true,
      );
    });
  });

  describe("getOperatorByTypeAndName", () => {
    it("should return undefined if operator does not exist", () => {
      expect(getOperatorByTypeAndName(NUMBER, "contains")).toBe(undefined);
    });

    it("should return a metadata object for specific operator type/name", () => {
      expect(getOperatorByTypeAndName(NUMBER, "between")).toEqual({
        name: "between",
        numFields: 2,
        validArgumentsFilters: [expect.any(Function), expect.any(Function)],
        verboseName: "Between",
      });
    });

    it("should have 'between' filter operator for the coordinate type", () => {
      expect(getOperatorByTypeAndName(COORDINATE, "between")).toEqual({
        name: "between",
        numFields: 2,
        validArgumentsFilters: [expect.any(Function), expect.any(Function)],
        verboseName: "Between",
      });
    });

    it("should return a metadata object for primary key", () => {
      expect(getOperatorByTypeAndName(PRIMARY_KEY, "=")).toEqual({
        multi: true,
        name: "=",
        numFields: 1,
        validArgumentsFilters: [expect.any(Function)],
        verboseName: "Is",
      });
    });

    it("should return a metadata object for foreign key", () => {
      expect(getOperatorByTypeAndName(FOREIGN_KEY, "between")).toEqual({
        name: "between",
        numFields: 2,
        validArgumentsFilters: [expect.any(Function), expect.any(Function)],
        verboseName: "Between",
      });
    });
  });

  describe("isFuzzyOperator", () => {
    it("should return false for operators that expect an exact match", () => {
      expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "="))).toBe(
        false,
      );
      expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "!="))).toBe(
        false,
      );
    });

    it("should return true for operators that are not exact", () => {
      expect(
        isFuzzyOperator(getOperatorByTypeAndName(STRING, "contains")),
      ).toBe(true);
      expect(isFuzzyOperator(getOperatorByTypeAndName(NUMBER, "between"))).toBe(
        true,
      );
    });
  });
});