aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrfoxygmfr <mrfoxygmfr@sch9.ru>2025-03-24 12:22:26 +0300
committermrfoxygmfr <mrfoxygmfr@sch9.ru>2025-03-24 12:22:26 +0300
commit051c17c3103fe3c57c8d9e1b8ab834ccc1b02158 (patch)
tree04315dfc821da5f1115e26a068951a11178aab5e
parent2367109c64d1df1452f302f9a14e45f4c811cac2 (diff)
test(db/dao): productSlot DAO tests
-rw-r--r--src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductSlotDAOTest.java112
1 files changed, 112 insertions, 0 deletions
diff --git a/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductSlotDAOTest.java b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductSlotDAOTest.java
new file mode 100644
index 0000000..85721a1
--- /dev/null
+++ b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductSlotDAOTest.java
@@ -0,0 +1,112 @@
+package ru.mrfoxygmfr.warehouse_accounting.db.dao;
+
+import org.hibernate.Session;
+import org.hibernate.SessionFactory;
+import org.hibernate.Transaction;
+import org.junit.jupiter.api.*;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.TestPropertySource;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.ProductSlotSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.models.*;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.NoSuchElementException;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+@SpringBootTest
+@TestInstance(TestInstance.Lifecycle.PER_CLASS)
+@TestPropertySource(locations="classpath:application.properties")
+public class ProductSlotDAOTest {
+ @Autowired
+ private ProductDAO productDAO;
+
+ @Autowired
+ private StorageSlotDAO storageSlotDAO;
+
+ @Autowired
+ private ProductSlotDAO productSlotDAO;
+
+ @Autowired
+ private SessionFactory sessionFactory;
+
+ @Test
+ void testSimpleManipulations() {
+ assertThrows(
+ NoSuchElementException.class,
+ () -> productSlotDAO.findOne(ProductSlotSpecs.productSlotContainsLike("Десяточка")).orElseThrow()
+ );
+
+ ProductSlot productSlot = assertDoesNotThrow(
+ () -> productSlotDAO.findOne(ProductSlotSpecs.productSlotContainsLike("MAXFUN")).orElseThrow()
+ );
+
+ assertEquals("Alpen Gold MAXFUN с фруктовыми кусочками, 22 шт, коробка", productSlot.getProduct().getName());
+
+ assertThrows(
+ NoSuchElementException.class,
+ () -> productSlotDAO.findOne(ProductSlotSpecs.productStorageStatusEqual(ProductStorageStatus.PROHIBITED)).orElseThrow()
+ );
+
+ ProductSlot ps = assertDoesNotThrow(
+ () -> productSlotDAO.findOne(ProductSlotSpecs.productStorageStatusEqual(ProductStorageStatus.PLACED)).orElseThrow()
+ );
+
+ assertEquals(productSlot.getId(), ps.getId());
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ List<StorageSlot> storageSlots = new ArrayList<>();
+
+ StorageSlot s = new StorageSlot();
+ s.setLocation("Стеллаж 1, полка 3, место 5");
+ s.setHeight(100);
+ s.setWidth(200);
+ s.setDepth(500);
+
+ storageSlots.add(s);
+ storageSlots.add(new StorageSlot("Стеллаж 11, полка 3, место 5", 500, 500, 500));
+ storageSlots.add(new StorageSlot("пом. 4, стеллаж 10, полка 5", 250, 100, 1000));
+ storageSlots.add(new StorageSlot("(раменки, 5) Стеллаж 7", 200, 100, 15));
+ storageSlotDAO.saveAll(storageSlots);
+
+ List<Product> products = new ArrayList<>();
+
+ Product p = new Product();
+ p.setName("Alpen Gold MAXFUN с фруктовыми кусочками, 22 шт, коробка");
+ p.setType(ProductType.FOOD);
+ p.setHeight(100);
+ p.setWidth(200);
+ p.setDepth(500);
+ p.setMaxStorageDuration(java.time.Duration.ofDays(365));
+
+ products.add(p);
+ products.add(new Product("Туалетный Утенок, 9 шт, упаковка", ProductType.HOUSEHOLD_CHEMICALS, 500, 500, 500));
+ products.add(new Product("Цемент м400 строительный 3 кг, уп", ProductType.BUILDING_MATERIALS, 250, 100, 1000));
+ products.add(new Product("iPhone 16 Pro Max", ProductType.ELECTRONICS, 200, 100, 15));
+ productDAO.saveAll(products);
+
+ List<ProductSlot> productSlots = new ArrayList<>();
+ productSlots.add(new ProductSlot(products.getFirst(), storageSlots.getFirst(), 10, ProductStorageStatus.PLACED));
+
+ productSlotDAO.saveAll(productSlots);
+ }
+
+ @BeforeAll
+ @AfterEach
+ void annihilation() {
+ try (Session session = sessionFactory.openSession()) {
+ Transaction tx = session.beginTransaction();
+ session.createNativeQuery("TRUNCATE product_slots RESTART IDENTITY CASCADE;").executeUpdate();
+ session.createNativeQuery("ALTER SEQUENCE product_slots_id_seq RESTART WITH 1;").executeUpdate();
+ session.createNativeQuery("TRUNCATE products RESTART IDENTITY CASCADE;").executeUpdate();
+ session.createNativeQuery("ALTER SEQUENCE products_id_seq RESTART WITH 1;").executeUpdate();
+ session.createNativeQuery("TRUNCATE storage_slots RESTART IDENTITY CASCADE;").executeUpdate();
+ session.createNativeQuery("ALTER SEQUENCE storage_slots_id_seq RESTART WITH 1;").executeUpdate();
+ tx.commit();
+ }
+ }
+}