Skip to content

Commit

Permalink
chore: clean up tests
Browse files Browse the repository at this point in the history
Signed-off-by: Jan Guzman <[email protected]>
  • Loading branch information
Krystian19 committed Jan 24, 2025
1 parent 8aa6fc4 commit 186c26d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/server/src/service/model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe("service/model", () => {
sinon.restore();
});

it("should create a Model instance successfully", async () => {
it("should create a Model instance", async () => {
sinon.stub(Model, "getModelRuntime").resolves({
runtime: sinon.createStubInstance(Runtime),
modelURL: new URL("file://mockModelPath"),
Expand Down
192 changes: 106 additions & 86 deletions packages/server/src/service/package.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,105 +21,125 @@ describe("service/package", () => {
await fs.rm(testPackageDirectory, { recursive: true });
});

describe("create", () => {
it("should throw PackageNotFoundError if the package manifest does not exist", async () => {
sinon.stub(fs, "stat").rejects(new Error("File not found"));

await expect(Package.create("testPackage")).to.be.rejectedWith(
PackageNotFoundError,
"Package manifest for testPackage does not exist.",
);
});

it("should return a Package object if the package exists", async () => {
sinon.stub(fs, "stat").resolves();
sinon
.stub(fs, "readFile")
.resolves(
Buffer.from(JSON.stringify({ description: "Test package" })),
);

sinon
.stub(Model, "create")
.resolves({ getPath: () => "model1.model" } as any);

sinon.stub(Scheduler, "create").returns({
list: () => [],
} as any);

const packageInstance = await Package.create("testPackage");

expect(packageInstance).to.be.an.instanceOf(Package);
expect(packageInstance.getPackageName()).to.equal("testPackage");
expect(packageInstance.getPackageMetadata().description).to.equal(
"Test package",
);
expect(packageInstance.listDatabases()).to.be.empty;
expect(packageInstance.listModels()).to.be.empty;
expect(packageInstance.listSchedules()).to.be.empty;
});
it("should create a package instance", async () => {
const pkg = new Package(
"testPackage",
{ name: "testPackage", description: "Test package" },
[],
new Map([
["model1.malloy", { getPath: () => "model1.malloy" } as any],
["model2.malloynb", { getPath: () => "model2.malloynb" } as any],
]),
undefined,
);

expect(pkg).to.be.an.instanceOf(Package);
});

describe("listModels", () => {
it("should return a list of models with their paths and types", async () => {
const packageInstance = new Package(
"testPackage",
{ name: "testPackage", description: "Test package" },
[],
new Map([
["model1.malloy", { getPath: () => "model1.malloy" } as any],
["model2.malloynb", { getPath: () => "model2.malloynb" } as any],
]),
undefined,
);

const models = packageInstance.listModels();

expect(models).to.deep.equal([
{ path: "model1.malloy", type: "source" },
{ path: "model2.malloynb", type: "notebook" },
]);
describe("instance methods", () => {
describe("create", () => {
it("should throw PackageNotFoundError if the package manifest does not exist", async () => {
sinon.stub(fs, "stat").rejects(new Error("File not found"));

await expect(Package.create("testPackage")).to.be.rejectedWith(
PackageNotFoundError,
"Package manifest for testPackage does not exist.",
);
});

it("should return a Package object if the package exists", async () => {
sinon.stub(fs, "stat").resolves();
sinon
.stub(fs, "readFile")
.resolves(
Buffer.from(JSON.stringify({ description: "Test package" })),
);

sinon
.stub(Model, "create")
.resolves({ getPath: () => "model1.model" } as any);

sinon.stub(Scheduler, "create").returns({
list: () => [],
} as any);

const packageInstance = await Package.create("testPackage");

expect(packageInstance).to.be.an.instanceOf(Package);
expect(packageInstance.getPackageName()).to.equal("testPackage");
expect(packageInstance.getPackageMetadata().description).to.equal(
"Test package",
);
expect(packageInstance.listDatabases()).to.be.empty;
expect(packageInstance.listModels()).to.be.empty;
expect(packageInstance.listSchedules()).to.be.empty;
});
});
});

describe("getDatabaseSize", () => {
it("should return the size of the database file", async () => {
sinon.stub(fs, "stat").resolves({ size: 1024 } as any);
describe("listModels", () => {
it("should return a list of models with their paths and types", async () => {
const packageInstance = new Package(
"testPackage",
{ name: "testPackage", description: "Test package" },
[],
new Map([
["model1.malloy", { getPath: () => "model1.malloy" } as any],
[
"model2.malloynb",
{ getPath: () => "model2.malloynb" } as any,
],
]),
undefined,
);

const size = await (Package as any).getDatabaseSize(
"testPackage",
"database.parquet",
);
const models = packageInstance.listModels();

expect(size).to.equal(1024);
expect(models).to.deep.equal([
{ path: "model1.malloy", type: "source" },
{ path: "model2.malloynb", type: "notebook" },
]);
});
});
});

describe("readConnectionConfig", () => {
it("should return an empty array if the connection manifest does not exist", async () => {
sinon.stub(fs, "stat").rejects(new Error("File not found"));
describe("getDatabaseSize", () => {
it("should return the size of the database file", async () => {
sinon.stub(fs, "stat").resolves({ size: 1024 } as any);

const config = await Package.readConnectionConfig("testPackage");
expect(config).to.be.an("array").that.is.empty;
const size = await (Package as any).getDatabaseSize(
"testPackage",
"database.parquet",
);

expect(size).to.equal(1024);
});
});

it("should return the parsed connection config if it exists", async () => {
sinon.stub(fs, "stat").resolves();
sinon.stub(fs, "readFile").resolves(
Buffer.from(
JSON.stringify([
{ id: "conn1", type: "database" },
{ id: "conn2", type: "api" },
]),
),
);
describe("readConnectionConfig", () => {
it("should return an empty array if the connection manifest does not exist", async () => {
sinon.stub(fs, "stat").rejects(new Error("File not found"));

const config = await Package.readConnectionConfig("testPackage");
expect(config).to.be.an("array").that.is.empty;
});

it("should return the parsed connection config if it exists", async () => {
sinon.stub(fs, "stat").resolves();
sinon.stub(fs, "readFile").resolves(
Buffer.from(
JSON.stringify([
{ id: "conn1", type: "database" },
{ id: "conn2", type: "api" },
]),
),
);

const config = await Package.readConnectionConfig("testPackage");
const config = await Package.readConnectionConfig("testPackage");

expect(config).to.deep.equal([
{ id: "conn1", type: "database" },
{ id: "conn2", type: "api" },
]);
expect(config).to.deep.equal([
{ id: "conn1", type: "database" },
{ id: "conn2", type: "api" },
]);
});
});
});
});

0 comments on commit 186c26d

Please sign in to comment.