Skip to content

Commit

Permalink
fix: fix column.minValue and column.maxValue
Browse files Browse the repository at this point in the history
Mminimum and maximum values for exact fractional numbers are not possible with native JS types.
  • Loading branch information
ozum committed Mar 1, 2021
1 parent 224c734 commit 80222d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 38 deletions.
14 changes: 4 additions & 10 deletions src/pg-structure/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,8 @@ export default class Column extends DbObject {
this.arrayDimension = args.arrayDimension || 0;
this.defaultWithTypeCast = args.defaultWithTypeCast;
this.attributeNumber = args.attributeNumber;

if (this.type.numericType !== undefined) {
/* istanbul ignore next */
const maxLimit = this.precision ? 10 ** (this.precision - (this.scale ?? 0)) : undefined;
const minLimit = maxLimit === undefined ? undefined : -maxLimit;
this.minValue = this.isSerial ? 1 : NUMERIC_BOUNDRIES?.[this.type.name]?.min ?? minLimit;
this.maxValue = NUMERIC_BOUNDRIES?.[this.type.name]?.max ?? maxLimit;
}
this.minValue = this.isSerial ? 1 : NUMERIC_BOUNDRIES?.[this.type.name]?.min;
this.maxValue = NUMERIC_BOUNDRIES?.[this.type.name]?.max;
}

/**
Expand Down Expand Up @@ -241,12 +235,12 @@ export default class Column extends DbObject {
public readonly length?: number;

/**
* For integer and exact numeric columns with scale minimum value of the column.
* Minimum value of the column (only for integer types).
*/
public readonly minValue?: number;

/**
* For integer and exact numeric columns with scale maximum value of the column.
* Maximum value of the column (only for integer types).
*/
public readonly maxValue?: number;

Expand Down
32 changes: 4 additions & 28 deletions test/column.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,36 +145,12 @@ describe("Column", () => {
expect(accountTable.get("id").maxValue).toBe(2147483647);
});

it("should not have minimum value for numeric column without precision.", () => {
expect(typeTable.get("field2_a").minValue).toBe(undefined);
it("should have minimum value for big integer.", () => {
expect(typeTable.get("field12").minValue).toBe(-9223372036854775808);
});

it("should not have maximum value for numeric column without precision.", () => {
expect(typeTable.get("field2_a").maxValue).toBe(undefined);
});

it("should have minimum value for numeric column.", () => {
expect(typeTable.get("field2_b").minValue).toBe(-10);
});

it("should have maximum value for numeric column.", () => {
expect(typeTable.get("field2_b").maxValue).toBe(10);
});

it("should have minimum value for numeric column with only scale but without scale.", () => {
expect(typeTable.get("field2_c").minValue).toBe(-1000);
});

it("should have maximum value for numeric column with only scale but without scale.", () => {
expect(typeTable.get("field2_c").maxValue).toBe(1000);
});

it("should not have minimum value for floating column.", () => {
expect(typeTable.get("field21").minValue).toBe(undefined);
});

it("should nt have maximum value for floating column.", () => {
expect(typeTable.get("field21").maxValue).toBe(undefined);
it("should have maximum value for big integer.", () => {
expect(typeTable.get("field12").maxValue).toBe(9223372036854775807);
});

it("should have referencedColumns.", () => {
Expand Down

0 comments on commit 80222d1

Please sign in to comment.