aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormrfoxygmfr <mrfoxygmfr@sch9.ru>2025-03-24 01:33:11 +0300
committermrfoxygmfr <mrfoxygmfr@sch9.ru>2025-03-24 01:33:11 +0300
commit610aacf4a2b230f26a780c289131d6a2198957db (patch)
tree1b269afed1b7a4d3e8d33c34f5e440d2ae255853
parent768d605845c6479373a51975b48eab7807c5a29c (diff)
test(db): add ProductDAO and StorageDAO tests
-rw-r--r--src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductDAOTest.java105
-rw-r--r--src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/StorageSlotDAOTest.java96
2 files changed, 201 insertions, 0 deletions
diff --git a/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductDAOTest.java b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductDAOTest.java
new file mode 100644
index 0000000..dfd0b25
--- /dev/null
+++ b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/ProductDAOTest.java
@@ -0,0 +1,105 @@
+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.data.crossstore.ChangeSetPersister;
+import org.springframework.test.context.TestPropertySource;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.ProductSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.models.Product;
+import ru.mrfoxygmfr.warehouse_accounting.db.models.ProductType;
+
+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 ProductDAOTest {
+ @Autowired
+ private ProductDAO productDAO;
+
+ @Autowired
+ private SessionFactory sessionFactory;
+
+ @Test
+ void testSimpleManipulations() {
+ List<Product> products = productDAO.findAll();
+ assertEquals(4, products.size());
+
+ Product p1 = assertDoesNotThrow(
+ () -> productDAO.findOne(ProductSpecs.productNameLike("iPhone")).orElseThrow()
+ );
+ assertEquals("iPhone 16 Pro Max", p1.getName());
+
+ List<Product> p2 = productDAO.findAll(ProductSpecs.productHeightGreater(200).and(ProductSpecs.productDepthLess(500)));
+ assertEquals(2, p2.size());
+ assertEquals(2, p2.getFirst().getId());
+
+ List<Product> p3 = productDAO.findAll(ProductSpecs.productHeightLess(250).and(ProductSpecs.productWidthGreater(150)));
+ assertEquals(1, p3.size());
+ assertEquals(1, p3.getFirst().getId());
+
+ List<Product> p4 = productDAO.findAll(ProductSpecs.productDepthGreater(100).and(ProductSpecs.productWidthLess(200)));
+ System.out.println(p4);
+ assertEquals(2, p4.size());
+ assertEquals(1, p4.getFirst().getId());
+ }
+
+ @Test
+ void testUpdate() {
+ String name = "Poco M3 Pro";
+
+ Product p = productDAO.findById(4).orElseThrow();
+ p.setName(name);
+ productDAO.save(p);
+
+ Product product = productDAO.findById(4).orElseThrow();
+ assertEquals(name, product.getName());
+ assertEquals(ProductType.ELECTRONICS, product.getType());
+ }
+
+ @Test
+ void testDelete() {
+ Product deleteProduct = productDAO.findById(3).orElseThrow();
+ productDAO.delete(deleteProduct);
+
+ assertTrue(productDAO.findById(3).isEmpty());
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ 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);
+ }
+
+ @BeforeAll
+ @AfterEach
+ void annihilation() {
+ try (Session session = sessionFactory.openSession()) {
+ Transaction tx = session.beginTransaction();
+ session.createNativeQuery("TRUNCATE products RESTART IDENTITY CASCADE;").executeUpdate();
+ session.createNativeQuery("ALTER SEQUENCE products_id_seq RESTART WITH 1;").executeUpdate();
+ tx.commit();
+ }
+ }
+}
diff --git a/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/StorageSlotDAOTest.java b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/StorageSlotDAOTest.java
new file mode 100644
index 0000000..eb22860
--- /dev/null
+++ b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/StorageSlotDAOTest.java
@@ -0,0 +1,96 @@
+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.data.crossstore.ChangeSetPersister;
+import org.springframework.test.context.TestPropertySource;
+import ru.mrfoxygmfr.warehouse_accounting.db.dao.specs.StorageSlotSpecs;
+import ru.mrfoxygmfr.warehouse_accounting.db.models.StorageSlot;
+
+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 StorageSlotDAOTest {
+ @Autowired
+ private StorageSlotDAO storageSlotDAO;
+
+ @Autowired
+ private SessionFactory sessionFactory;
+
+ @Test
+ void testSimpleManipulations() {
+ List<StorageSlot> storageSlots = storageSlotDAO.findAll();
+ assertEquals(4, storageSlots.size());
+
+ List<StorageSlot> slot1 = storageSlotDAO.findAll(StorageSlotSpecs.storageSlotHeightGreater(200).and(StorageSlotSpecs.storageSlotDepthLess(500)));
+ assertEquals(2, slot1.size());
+ assertEquals(2, slot1.getFirst().getId());
+
+ List<StorageSlot> slot2 = storageSlotDAO.findAll(StorageSlotSpecs.storageSlotHeightLess(250).and(StorageSlotSpecs.storageSlotWidthGreater(150)));
+ assertEquals(1, slot2.size());
+ assertEquals(1, slot2.getFirst().getId());
+
+ List<StorageSlot> slot3 = storageSlotDAO.findAll(StorageSlotSpecs.storageSlotDepthGreater(100).and(StorageSlotSpecs.storageSlotWidthLess(200)));
+ System.out.println(slot3);
+ assertEquals(2, slot3.size());
+ assertEquals(1, slot3.getFirst().getId());
+ }
+
+ @Test
+ void testUpdate() {
+ String location = "За границей";
+
+ StorageSlot p = storageSlotDAO.findById(4).orElseThrow();
+ p.setLocation(location);
+ storageSlotDAO.save(p);
+
+ StorageSlot storageSlot = storageSlotDAO.findById(4).orElseThrow();
+ assertEquals(location, storageSlot.getLocation());
+ }
+
+ @Test
+ void testDelete() {
+ StorageSlot deleteStorageSlot = storageSlotDAO.findById(3).orElseThrow();
+ storageSlotDAO.delete(deleteStorageSlot);
+
+ assertTrue(storageSlotDAO.findById(3).isEmpty());
+ }
+
+ @BeforeEach
+ void beforeEach() {
+ List<StorageSlot> storageSlots = new ArrayList<>();
+
+ StorageSlot p = new StorageSlot();
+ p.setLocation("Стеллаж 1, полка 3, место 5");
+ p.setHeight(100);
+ p.setWidth(200);
+ p.setDepth(500);
+
+ storageSlots.add(p);
+ 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);
+ }
+
+ @BeforeAll
+ @AfterEach
+ void annihilation() {
+ try (Session session = sessionFactory.openSession()) {
+ Transaction tx = session.beginTransaction();
+ session.createNativeQuery("TRUNCATE storage_slots RESTART IDENTITY CASCADE;").executeUpdate();
+ session.createNativeQuery("ALTER SEQUENCE storage_slots_id_seq RESTART WITH 1;").executeUpdate();
+ tx.commit();
+ }
+ }
+}