61 lines
1.8 KiB
Java
61 lines
1.8 KiB
Java
package com.drinkool.entities;
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
|
import io.quarkus.test.junit.QuarkusTest;
|
|
import jakarta.transaction.Transactional;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
@QuarkusTest
|
|
public class MenuItemIngredientEntityTest {
|
|
|
|
@Test
|
|
@Transactional
|
|
void testCreate_ShouldPersistIngredientSuccessfully() {
|
|
// 1. Chuẩn bị dữ liệu giả định
|
|
|
|
String menuItemName = "Milk Tea";
|
|
double menuItemPrice = 40;
|
|
|
|
MenuItemEntity menuItem = MenuItemEntity.create(
|
|
menuItemName,
|
|
menuItemPrice
|
|
);
|
|
|
|
String inventoryItemName = "Water";
|
|
int inventoryItemQuantity = 10; // Đổi thành int cho khớp với kiểu của EateryInventoryItemEntity
|
|
String inventoryItemUnit = "l";
|
|
|
|
// SỬA Ở ĐÂY: Khởi tạo bằng new và dùng hàm add() đúng như cấu trúc class của bạn
|
|
EateryInventoryItemEntity inventoryItem = EateryInventoryItemEntity.create(
|
|
inventoryItemName,
|
|
inventoryItemQuantity,
|
|
inventoryItemUnit
|
|
);
|
|
// Lưu ý: Hàm add() của bạn đã có sẵn lệnh this.persist() bên trong nên không cần gọi lại nữa!
|
|
|
|
double requiredQuantity = 0.5;
|
|
|
|
// 2. Gọi hàm static create cần test
|
|
MenuItemIngredientEntity createdIngredient =
|
|
MenuItemIngredientEntity.create(
|
|
menuItem,
|
|
inventoryItem,
|
|
requiredQuantity
|
|
);
|
|
|
|
// 3. Kiểm tra kết quả
|
|
assertNotNull(createdIngredient.id, "ID không được null sau khi persist");
|
|
|
|
// Truy vấn lại từ Database để đảm bảo dữ liệu đã được lưu đúng
|
|
MenuItemIngredientEntity savedEntity = MenuItemIngredientEntity.findById(
|
|
createdIngredient.id
|
|
);
|
|
|
|
assertNotNull(savedEntity);
|
|
assertEquals(menuItem.id, savedEntity.menuItem.id);
|
|
assertEquals(inventoryItem.id, savedEntity.inventoryItem.id);
|
|
assertEquals(requiredQuantity, savedEntity.requiredQuantity);
|
|
}
|
|
}
|