Skip to content

Commit

Permalink
Revert "Fix: Data source changed for testing purposes"
Browse files Browse the repository at this point in the history
This reverts commit c1affb6.
  • Loading branch information
Villarley committed Dec 22, 2024
1 parent 512648a commit c824aa0
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 45 deletions.
26 changes: 13 additions & 13 deletions src/tests/integration/services/productType.integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import testDataSource from "../../../config/ormconfig.test";
import AppDataSource from "../../../config/ormconfig";
import { ProductVariant } from "../../../entities/ProductVariant";
import { Product } from "../../../entities/Product";
import { ProductVariantService } from "../../../services/productVariant.service";
Expand All @@ -7,30 +7,30 @@ describe("ProductVariantService Integration Tests", () => {
let service: ProductVariantService;

beforeAll(async () => {
if (!testDataSource.isInitialized) {
await testDataSource.initialize();
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
await testDataSource.synchronize(true);
await AppDataSource.synchronize(true);

service = new ProductVariantService();
});

afterEach(async () => {
const entities = testDataSource.entityMetadatas;
const entities = AppDataSource.entityMetadatas;
for (const entity of entities) {
const repository = testDataSource.getRepository(entity.name);
const repository = AppDataSource.getRepository(entity.name);
await repository.clear();
}
});

afterAll(async () => {
if (testDataSource.isInitialized) {
await testDataSource.destroy();
if (AppDataSource.isInitialized) {
await AppDataSource.destroy();
}
});

it("should create a ProductVariant with Product", async () => {
const productRepo = testDataSource.getRepository(Product);
const productRepo = AppDataSource.getRepository(Product);

const product = productRepo.create({
name: "Laptop",
Expand All @@ -53,11 +53,11 @@ describe("ProductVariantService Integration Tests", () => {
});

it("should fetch all ProductVariants with their Products", async () => {
const productRepo = testDataSource.getRepository(Product);
const productRepo = AppDataSource.getRepository(Product);
const product = productRepo.create({ name: "Laptop" });
const savedProduct = await productRepo.save(product);

const variantRepo = testDataSource.getRepository(ProductVariant);
const variantRepo = AppDataSource.getRepository(ProductVariant);
const variant1 = variantRepo.create({ sku: "LAP123", price: 999.99, stock: 10, product: savedProduct });
const variant2 = variantRepo.create({ sku: "LAP456", price: 899.99, stock: 5, product: savedProduct });

Expand All @@ -76,11 +76,11 @@ describe("ProductVariantService Integration Tests", () => {
});

it("should delete a ProductVariant", async () => {
const productRepo = testDataSource.getRepository(Product);
const productRepo = AppDataSource.getRepository(Product);
const product = productRepo.create({ name: "Laptop" });
const savedProduct = await productRepo.save(product);

const variantRepo = testDataSource.getRepository(ProductVariant);
const variantRepo = AppDataSource.getRepository(ProductVariant);
const variant = variantRepo.create({ sku: "LAP123", price: 999.99, stock: 10, product: savedProduct });
const savedVariant = await variantRepo.save(variant);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import testDataSource from "../../../config/ormconfig.test";
import AppDataSource from "../../../config/ormconfig";
import { Product } from "../../../entities/Product";
import { ProductType } from "../../../entities/ProductType";
import { ProductVariant } from "../../../entities/ProductVariant";
Expand All @@ -8,31 +8,31 @@ describe("ProductVariantService Integration Tests", () => {
let service: ProductVariantService;

beforeAll(async () => {
if (!testDataSource.isInitialized) {
await testDataSource.initialize();
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}
await testDataSource.synchronize(true);
await AppDataSource.synchronize(true);

service = new ProductVariantService();
});

afterAll(async () => {
if (testDataSource.isInitialized) {
await testDataSource.destroy();
if (AppDataSource.isInitialized) {
await AppDataSource.destroy();
}
});

afterEach(async () => {
const entities = testDataSource.entityMetadatas;
const entities = AppDataSource.entityMetadatas;
for (const entity of entities) {
const repository = testDataSource.getRepository(entity.name);
const repository = AppDataSource.getRepository(entity.name);
await repository.clear();
}
});

it("should create a Product with a ProductType", async () => {
const productTypeRepo = testDataSource.getRepository(ProductType);
const productRepo = testDataSource.getRepository(Product);
const productTypeRepo = AppDataSource.getRepository(ProductType);
const productRepo = AppDataSource.getRepository(Product);

const productType = await productTypeRepo.save({ name: "Electronics", description: "..." });
expect(productType.id).toBeDefined();
Expand All @@ -44,8 +44,8 @@ describe("ProductVariantService Integration Tests", () => {


it("should create a ProductVariant with Product", async () => {
const productTypeRepo = testDataSource.getRepository(ProductType);
const productRepo = testDataSource.getRepository(Product);
const productTypeRepo = AppDataSource.getRepository(ProductType);
const productRepo = AppDataSource.getRepository(Product);

const productType = productTypeRepo.create({
name: "Electronics",
Expand Down Expand Up @@ -80,8 +80,8 @@ describe("ProductVariantService Integration Tests", () => {
);
});
it("should fetch ProductVariants associated with a Product", async () => {
const productRepo = testDataSource.getRepository(Product);
const variantRepo = testDataSource.getRepository(ProductVariant);
const productRepo = AppDataSource.getRepository(Product);
const variantRepo = AppDataSource.getRepository(ProductVariant);

const product = productRepo.create({ name: "Laptop" });
const savedProduct = await productRepo.save(product);
Expand Down
6 changes: 3 additions & 3 deletions src/tests/services/productService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ProductService } from '../../services/product.service';
import { Product } from '../../entities/Product';
import { Repository } from 'typeorm';
import { ProductType } from '../../entities/ProductType';
import testDataSource from '../../config/ormconfig.test';
import AppDataSource from '../../config/ormconfig';

jest.mock('../../config/ormconfig', () => ({
getRepository: jest.fn(),
Expand All @@ -22,7 +22,7 @@ jest.mock('../../config/ormconfig', () => ({
create: jest.fn(),
} as unknown as jest.Mocked<Repository<Product>>;

(testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);
(AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);

service = new ProductService();
});
Expand All @@ -39,7 +39,7 @@ jest.mock('../../config/ormconfig', () => ({
};

const productTypeRepo = { findOne: jest.fn().mockResolvedValue(productType) };
(testDataSource.getRepository as jest.Mock)
(AppDataSource.getRepository as jest.Mock)
.mockReturnValueOnce(productTypeRepo)
.mockReturnValueOnce(mockRepo);

Expand Down
4 changes: 2 additions & 2 deletions src/tests/services/productTypeService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProductTypeService } from '../../services/productType.service';
import { ProductType } from '../../entities/ProductType';
import { Repository } from 'typeorm';
import testDataSource from '../../config/ormconfig.test';
import AppDataSource from '../../config/ormconfig';

jest.mock('../../config/ormconfig', () => ({
getRepository: jest.fn(),
Expand All @@ -21,7 +21,7 @@ jest.mock('../../config/ormconfig', () => ({
create: jest.fn(),
} as unknown as jest.Mocked<Repository<ProductType>>;

(testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);
(AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);

service = new ProductTypeService();
});
Expand Down
6 changes: 3 additions & 3 deletions src/tests/services/productVariantService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ProductVariantService } from '../../services/productVariant.service';
import { ProductVariant } from '../../entities/ProductVariant';
import { Repository } from 'typeorm';
import testDataSource from '../../config/ormconfig.test';
import AppDataSource from '../../config/ormconfig';
import { ProductType } from '../../entities/ProductType';
import { Product } from '../../entities/Product';

Expand All @@ -28,7 +28,7 @@ describe('ProductVariantService', () => {
} as any,
} as unknown as jest.Mocked<Repository<ProductVariant>>;

(testDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);
(AppDataSource.getRepository as jest.Mock).mockReturnValue(mockRepo);

service = new ProductVariantService();
(service as any).repository = mockRepo;
Expand All @@ -42,7 +42,7 @@ describe('ProductVariantService', () => {
const savedVariant = { id: 1, ...variantData, product: productData, createdAt: new Date() };

const productRepo = { findOne: jest.fn().mockResolvedValue(productData) };
(testDataSource.getRepository as jest.Mock)
(AppDataSource.getRepository as jest.Mock)
.mockReturnValueOnce(productRepo)
.mockReturnValueOnce(mockRepo);

Expand Down
2 changes: 1 addition & 1 deletion src/tests/services/testService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupTestDB, teardownTestDB } from "../../utils/test-utils";
import testDataSource from "../../config/ormconfig.test";
import { testDataSource } from "../../config/ormconfig.test";
import { TestService } from "../../services/test.service";
import { TestEntity } from "../../entities/testEntity";

Expand Down
2 changes: 1 addition & 1 deletion src/tests/services/userService.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupTestDB, teardownTestDB } from "../../utils/test-utils";
import testDataSource from "../../config/ormconfig.test";
import { testDataSource } from "../../config/ormconfig.test";
import { UserService } from "../../services/User.service";
import { User } from "../../entities/User";

Expand Down
22 changes: 14 additions & 8 deletions src/tests/setup.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import testDataSource from "../config/ormconfig.test";
import AppDataSource from "../config/ormconfig";

beforeAll(async () => {
if (!testDataSource.isInitialized) {
await testDataSource.initialize();
if (!AppDataSource.isInitialized) {
await AppDataSource.initialize();
}

await testDataSource.synchronize();
if (process.env.NODE_ENV === "test") {
await AppDataSource.synchronize();

const queryRunner = AppDataSource.createQueryRunner();
const tables = await queryRunner.getTables();
await queryRunner.release();
}
});

afterEach(async () => {
const entities = testDataSource.entityMetadatas;
const entities = AppDataSource.entityMetadatas;
for (const entity of entities) {
const repository = testDataSource.getRepository(entity.name);
const repository = AppDataSource.getRepository(entity.name);
await repository.clear();
}
});

afterAll(async () => {
if (testDataSource.isInitialized) {
await testDataSource.destroy();
if (AppDataSource.isInitialized) {
await AppDataSource.destroy();
}
});

0 comments on commit c824aa0

Please sign in to comment.