refactor(test): update paths and mocks

This commit is contained in:
Mauricio Siu
2024-09-29 18:44:07 -06:00
parent 7bd6b66551
commit 98aa474975
6 changed files with 93 additions and 89 deletions

View File

@@ -81,14 +81,17 @@ const baseApp: ApplicationNested = {
username: null,
dockerContextPath: null,
};
//
vi.mock("@/server/constants", () => ({
paths: () => ({
APPLICATIONS_PATH: "./__test__/drop/zips/output",
}),
// APPLICATIONS_PATH: "./__test__/drop/zips/output",
}));
vi.mock("@dokploy/builders", async (importOriginal) => {
const actual = await importOriginal();
return {
...actual,
unzipDrop: actual.unzipDrop, // Asegura que unzipDrop esté presente en el mock
paths: () => ({
APPLICATIONS_PATH: "./__test__/drop/zips/output",
}),
};
});
describe("unzipDrop using real zip files", () => {
// const { APPLICATIONS_PATH } = paths();
beforeAll(async () => {
@@ -102,15 +105,19 @@ describe("unzipDrop using real zip files", () => {
it("should correctly extract a zip with a single root folder", async () => {
baseApp.appName = "single-file";
// const appName = "single-file";
const outputPath = path.join(APPLICATIONS_PATH, baseApp.appName, "code");
const zip = new AdmZip("./__test__/drop/zips/single-file.zip");
const zipBuffer = zip.toBuffer();
const file = new File([zipBuffer], "single.zip");
await unzipDrop(file, baseApp);
const files = await fs.readdir(outputPath, { withFileTypes: true });
expect(files.some((f) => f.name === "test.txt")).toBe(true);
try {
const outputPath = path.join(APPLICATIONS_PATH, baseApp.appName, "code");
const zip = new AdmZip("./__test__/drop/zips/single-file.zip");
console.log(`Output Path: ${outputPath}`);
const zipBuffer = zip.toBuffer();
const file = new File([zipBuffer], "single.zip");
await unzipDrop(file, baseApp);
const files = await fs.readdir(outputPath, { withFileTypes: true });
expect(files.some((f) => f.name === "test.txt")).toBe(true);
} catch (err) {
console.log(err);
} finally {
}
});
it("should correctly extract a zip with a single root folder and a subfolder", async () => {

View File

@@ -13,4 +13,9 @@ export default defineConfig({
exclude: ["**/node_modules/**", "**/dist/**", "**/.docker/**"],
pool: "forks",
},
define: {
"process.env": {
NODE: "test",
},
},
});

View File

@@ -1,15 +1,7 @@
import { type PostgresJsDatabase, drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
// import { sc } from "@dokploy/schema";
import * as schema from "./schema";
// schema
// import * as schema from "@dokploy/schema";
// type Schema = typeof schema;
// type Schema = typeof schema;
declare global {
// eslint-disable-next-line no-var -- only var works here
var db: PostgresJsDatabase<typeof schema> | undefined;
}

View File

@@ -1,21 +1,21 @@
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
// import { drizzle } from "drizzle-orm/postgres-js";
// import { migrate } from "drizzle-orm/postgres-js/migrator";
// import postgres from "postgres";
const connectionString = process.env.DATABASE_URL || "";
// const connectionString = process.env.DATABASE_URL || "";
const sql = postgres(connectionString, { max: 1 });
const db = drizzle(sql);
// const sql = postgres(connectionString, { max: 1 });
// const db = drizzle(sql);
export const migration = async () =>
await migrate(db, { migrationsFolder: "drizzle" })
.then(() => {
console.log("Migration complete");
sql.end();
})
.catch((error) => {
console.log("Migration failed", error);
})
.finally(() => {
sql.end();
});
// export const migration = async () =>
// await migrate(db, { migrationsFolder: "drizzle" })
// .then(() => {
// console.log("Migration complete");
// sql.end();
// })
// .catch((error) => {
// console.log("Migration failed", error);
// })
// .finally(() => {
// sql.end();
// });

View File

@@ -1,23 +1,23 @@
import { sql } from "drizzle-orm";
// Credits to Louistiti from Drizzle Discord: https://discord.com/channels/1043890932593987624/1130802621750448160/1143083373535973406
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
// import { sql } from "drizzle-orm";
// // Credits to Louistiti from Drizzle Discord: https://discord.com/channels/1043890932593987624/1130802621750448160/1143083373535973406
// import { drizzle } from "drizzle-orm/postgres-js";
// import postgres from "postgres";
const connectionString = process.env.DATABASE_URL || "";
// const connectionString = process.env.DATABASE_URL || "";
const pg = postgres(connectionString, { max: 1 });
const db = drizzle(pg);
// const pg = postgres(connectionString, { max: 1 });
// const db = drizzle(pg);
const clearDb = async (): Promise<void> => {
try {
const tablesQuery = sql<string>`DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP schema drizzle CASCADE;`;
const tables = await db.execute(tablesQuery);
console.log(tables);
await pg.end();
} catch (error) {
console.error("Error to clean database", error);
} finally {
}
};
// const clearDb = async (): Promise<void> => {
// try {
// const tablesQuery = sql<string>`DROP SCHEMA public CASCADE; CREATE SCHEMA public; DROP schema drizzle CASCADE;`;
// const tables = await db.execute(tablesQuery);
// console.log(tables);
// await pg.end();
// } catch (error) {
// console.error("Error to clean database", error);
// } finally {
// }
// };
clearDb();
// clearDb();

View File

@@ -1,35 +1,35 @@
import bc from "bcrypt";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import { users } from "./schema";
// import bc from "bcrypt";
// import { drizzle } from "drizzle-orm/postgres-js";
// import postgres from "postgres";
// import { users } from "./schema";
const connectionString = process.env.DATABASE_URL || "";
// const connectionString = process.env.DATABASE_URL || "";
const pg = postgres(connectionString, { max: 1 });
const db = drizzle(pg);
// const pg = postgres(connectionString, { max: 1 });
// const db = drizzle(pg);
function password(txt: string) {
return bc.hashSync(txt, 10);
}
// function password(txt: string) {
// return bc.hashSync(txt, 10);
// }
async function seed() {
console.log("> Seed:", process.env.DATABASE_PATH, "\n");
// async function seed() {
// console.log("> Seed:", process.env.DATABASE_PATH, "\n");
// const authenticationR = await db
// .insert(users)
// .values([
// {
// email: "user1@hotmail.com",
// password: password("12345671"),
// },
// ])
// .onConflictDoNothing()
// .returning();
// // const authenticationR = await db
// // .insert(users)
// // .values([
// // {
// // email: "user1@hotmail.com",
// // password: password("12345671"),
// // },
// // ])
// // .onConflictDoNothing()
// // .returning();
// console.log("\nSemillas Update:", authenticationR.length);
}
// // console.log("\nSemillas Update:", authenticationR.length);
// }
seed().catch((e) => {
console.error(e);
process.exit(1);
});
// seed().catch((e) => {
// console.error(e);
// process.exit(1);
// });