diff options
author | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-24 12:22:09 +0300 |
---|---|---|
committer | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-24 12:22:09 +0300 |
commit | 2367109c64d1df1452f302f9a14e45f4c811cac2 (patch) | |
tree | 1ff7c89826d2cb09d4cfe9671616d061d490c031 | |
parent | 2404935491aee52355e84fd7a051c6ffe8b821ed (diff) |
test(db/dao): operationDAO tests
-rw-r--r-- | src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/OperationDAOTest.java | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/OperationDAOTest.java b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/OperationDAOTest.java new file mode 100644 index 0000000..54a21b9 --- /dev/null +++ b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/OperationDAOTest.java @@ -0,0 +1,135 @@ +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.OperationSpecs; +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 OperationDAOTest { + @Autowired + private PartnerDAO partnerDAO; + + @Autowired + private ProductDAO productDAO; + + @Autowired + private OperationDAO operationDAO; + + @Autowired + private SessionFactory sessionFactory; + + @Test + void testSimpleManipulations() { + assertThrows( + NoSuchElementException.class, + () -> operationDAO.findOne(OperationSpecs.operationPartnerNameLike("Десяточка")).orElseThrow() + ); + + Operation operation = assertDoesNotThrow( + () -> operationDAO.findOne(OperationSpecs.operationPartnerNameLike("Рога и копыта")).orElseThrow() + ); + + assertEquals("1036300882672", operation.getPartner().getTaxNumber()); + assertEquals(2, operation.getProducts().size()); + + assertThrows( + NoSuchElementException.class, + () -> operationDAO.findOne(OperationSpecs.operationProductsContainsLike("Десяточка")).orElseThrow() + ); + + Operation o = assertDoesNotThrow( + () -> operationDAO.findOne(OperationSpecs.operationProductsContainsLike("MAXFUN")).orElseThrow() + ); + + assertEquals(operation.getId(), o.getId()); + + assertThrows( + NoSuchElementException.class, + () -> operationDAO.findOne(OperationSpecs.operationStatusEqual(OperationStatus.EXECUTED)).orElseThrow() + ); + + assertThrows( + NoSuchElementException.class, + () -> operationDAO.findOne(OperationSpecs.operationTypeEqual(OperationType.SUPPLY)).orElseThrow() + ); + } + + @BeforeEach + void beforeEach() { + List<Partner> partners = new ArrayList<>(); + partners.add(new Partner("ООО Рога и копыта", "1036300882672", PartnerType.ISSUER, "Россия, г. Город, ул. Улица, д. 1")); + PartnerContact pc = new PartnerContact(partners.getFirst(), "Иванов", "Иван", "Исп. директор", "+7 800 555-35-35", "ivanov@roga-kopita.ru"); + partners.getFirst().addContact(pc); + partners.add(new Partner("ООО Поставки из Китая", "1036300882671", PartnerType.SUPPLIER, "Россия, г. Город, ул. Улица, д. 2")); + partners.add(new Partner("Десяточка", "7825706086", PartnerType.ISSUER, "Россия, г. Санкт-Петербург, пр. Невский, д. 90/92")); + partnerDAO.saveAll(partners); + + 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); + + Operation op = new Operation(); + op.setPartner(partners.getFirst()); + op.setStatus(OperationStatus.CHECKOUT); + op.setType(OperationType.ISSUE); + op.setAddress("address"); + op.setResponsible(pc); + + OperationProduct opProduct = new OperationProduct(); + opProduct.setProduct(products.getFirst()); + opProduct.setAmount(5); + op.updateProduct(opProduct); + + OperationProduct opProduct2 = new OperationProduct(); + opProduct2.setProduct(products.get(2)); + opProduct2.setAmount(50); + op.updateProduct(opProduct2); + + operationDAO.save(op); + } + + @BeforeAll + @AfterEach + void annihilation() { + try (Session session = sessionFactory.openSession()) { + Transaction tx = session.beginTransaction(); + session.createNativeQuery("TRUNCATE operation_products RESTART IDENTITY CASCADE;").executeUpdate(); + session.createNativeQuery("ALTER SEQUENCE operation_products_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 partner_contacts RESTART IDENTITY CASCADE;").executeUpdate(); + session.createNativeQuery("ALTER SEQUENCE partner_contacts_id_seq RESTART WITH 1;").executeUpdate(); + session.createNativeQuery("TRUNCATE partners RESTART IDENTITY CASCADE;").executeUpdate(); + session.createNativeQuery("ALTER SEQUENCE partners_id_seq RESTART WITH 1;").executeUpdate(); + session.createNativeQuery("TRUNCATE operations RESTART IDENTITY CASCADE;").executeUpdate(); + session.createNativeQuery("ALTER SEQUENCE operations_id_seq RESTART WITH 1;").executeUpdate(); + tx.commit(); + } + } +} |