如何在 Node js 中模拟 AWS QLDB

How to mock AWS QLDB in Node js

我想在 QLDB 上做 Jest 测试用例,以尽可能多地覆盖我的代码行。

有什么方法可以用我们的代码进行 QLDB 模拟 (开玩笑)

实现这一目标的一种方法是利用不进行远程调用的本地 QLDB 实例。例如,DynamoDB 具有用于这些目的的本地 DynamoDB:https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html. QLDB however currently doesn’t support a local instance. An alternative to this would be the utilisation of a third party service that enables the development and testing of cloud services offline. One such service is LocalStack: https://localstack.cloud/. LocalStack currently has support for the QLDB APIs: https://localstack.cloud/features/.

成功了!

目前,这是在 QLDB(模拟数据库)上进行 Jest 测试的唯一方法。

Sample Code - Ref this article -

var chai = require('chai');
var qldb = require('amazon-qldb-driver-nodejs');
var sinon = require("sinon");

class VehicleRegistration {
    constructor() {
        var serviceConfigurationOptions = {
            region: "us-east-1",
            httpOptions: {
                maxSockets: 10
            }
        };
        this.qldbDriver = new qldb.QldbDriver("vehicle-registration", serviceConfigurationOptions)
    }

    async getPerson(firstName) {
        return await this.qldbDriver.executeLambda("SELECT * FROM People WHERE FirstName = ?", firstName);
    }
}

describe("VehicleRegistration", () => {
    const sandbox = sinon.createSandbox();
    let vehicleRegistration = new VehicleRegistration();

    it("should return person when calling getPerson()", async () => { 
        const testPersonResult = {"FirstName": "John", "LastName": "Doe"};
        const executeStub = sandbox.stub(vehicleRegistration.qldbDriver, "executeLambda");
        executeStub.returns(Promise.resolve(testPersonResult));

        let result = await vehicleRegistration.getPerson("John");
        chai.assert.equal(result, testPersonResult);
    });
});