-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
test/entities/ColumnOptionsEntity4.ts → test/entities/TransformerOptionsEntity3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,21 @@ | ||
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
import { ExtendedColumnOptions } from '../../src/options'; | ||
import { EncryptionTransformer } from '../../src/transformer'; | ||
|
||
@Entity() | ||
export default class ColumnOptionsEntity4 extends BaseEntity { | ||
export default class TransformerOptionsEntity3 extends BaseEntity { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column(<ExtendedColumnOptions>{ | ||
type: 'varchar', | ||
nullable: false, | ||
encrypt: { | ||
transformer: new EncryptionTransformer({ | ||
key: 'e41c966f21f9e1577802463f8924e6a3fe3e9751f201304213b2f845d8841d61', | ||
algorithm: 'aes-256-cbc', | ||
ivLength: 16, | ||
iv: 'ff5ac19190424b1d88f9419ef949ae56' | ||
} | ||
}) | ||
}) | ||
secret: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { expect } from "chai"; | ||
import { Connection, In, Not, IsNull, Equal, Like, LessThan } from "typeorm"; | ||
import { getConnection } from "./utils"; | ||
import TransformerOptionsEntity3 from "./entities/TransformerOptionsEntity3"; | ||
|
||
describe("Column Options - Data Mapper", function () { | ||
let connection: Connection; | ||
|
||
this.timeout(10000); | ||
|
||
before(async function () { | ||
connection = await getConnection(); | ||
}); | ||
it("should find by supported FindOperator", async function () { | ||
const repo = connection.getRepository(TransformerOptionsEntity3); | ||
|
||
try { | ||
const secret1 = "test1"; | ||
const secret2 = "test2"; | ||
await repo.save([{ secret: secret1 }, { secret: secret2 }]); | ||
// Where in | ||
const whereIn = await repo.find({ | ||
where: { | ||
secret: In([secret1, secret2]) | ||
} | ||
}); | ||
expect(whereIn.length).to.equal(2); | ||
expect(whereIn[0].secret).to.equal(secret1); | ||
expect(whereIn[1].secret).to.equal(secret2); | ||
// Where not | ||
const whereNot = await repo.find({ | ||
where: { | ||
secret: Not(secret2) | ||
} | ||
}); | ||
expect(whereNot.length).to.equal(1); | ||
expect(whereNot[0].secret).to.equal(secret1); | ||
// Where equal | ||
const whereEqual = await repo.find({ | ||
where: { | ||
secret: Equal(secret1) | ||
} | ||
}); | ||
expect(whereEqual.length).to.equal(1); | ||
expect(whereEqual[0].secret).to.equal(secret1); | ||
// Where not in | ||
const whereNotIn = await repo.find({ | ||
where: { | ||
secret: Not(In([secret2])) | ||
} | ||
}); | ||
expect(whereNotIn.length).to.equal(1); | ||
expect(whereNotIn[0].secret).to.equal(secret1); | ||
// Where IsNull | ||
const whereIsNull = await repo.find({ | ||
where: { | ||
secret: IsNull() | ||
} | ||
}); | ||
expect(whereIsNull.length).to.equal(0); | ||
} finally { | ||
await repo.clear(); | ||
} | ||
}); | ||
it("should throw error by not supported FindOperator", async function () { | ||
const repo = connection.getRepository(TransformerOptionsEntity3); | ||
|
||
try { | ||
const secret1 = "test3"; | ||
const secret2 = "test4"; | ||
await repo.save([{ secret: secret1 }, { secret: secret2 }]); | ||
// Can't use FindOperator except supported ones | ||
for (const notSupportedOperator of [ | ||
LessThan(secret1), | ||
Like(secret1)]) { | ||
await repo.find({ | ||
where: { | ||
secret: notSupportedOperator | ||
} | ||
}).then( () => { | ||
throw new Error("Never resolved") | ||
}, (reason) => { | ||
expect(reason.message).to.equal( | ||
'Only "Equal","In", "Not", and "IsNull" are supported for FindOperator' | ||
) | ||
}); | ||
} | ||
} finally { | ||
await repo.clear(); | ||
} | ||
}); | ||
}); |