diff options
author | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-23 15:15:32 +0300 |
---|---|---|
committer | mrfoxygmfr <mrfoxygmfr@sch9.ru> | 2025-03-23 15:15:32 +0300 |
commit | f91d1c94258e9c309b0324d47b7a7ad99eebe973 (patch) | |
tree | 8c5217fb6f24c928c5c0d71cda1c1a3e41f85f44 | |
parent | dd941eb5d594eef99e921151fb9fbb4793757229 (diff) |
test(db/dao): add PartnersDAO test
-rw-r--r-- | src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/PartnerDAOTest.java | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/PartnerDAOTest.java b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/PartnerDAOTest.java new file mode 100644 index 0000000..7102025 --- /dev/null +++ b/src/test/java/ru/mrfoxygmfr/warehouse_accounting/db/dao/PartnerDAOTest.java @@ -0,0 +1,97 @@ +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.PartnerSpecs; +import ru.mrfoxygmfr.warehouse_accounting.db.models.Partner; +import ru.mrfoxygmfr.warehouse_accounting.db.models.PartnerType; + +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 PartnerDAOTest { + @Autowired + private PartnerDAO partnerDAO; + + @Autowired + private SessionFactory sessionFactory; + + @Test + void testSimpleManipulations() { + List<Partner> partners = partnerDAO.findAll(); + assertEquals(3, partners.size()); + + Partner queryPartnerNameLike = assertDoesNotThrow( + () -> partnerDAO.findOne(PartnerSpecs.partnerNameLike("Рога и копыта")).orElseThrow() + ); + assertEquals("1036300882672", queryPartnerNameLike.getTaxNumber()); + + Partner queryPartnerTaxNumberLike = assertDoesNotThrow( + () -> partnerDAO.findOne(PartnerSpecs.partnerTaxNumberLike("7825706086")).orElseThrow() + ); + assertEquals("Десяточка", queryPartnerTaxNumberLike.getName()); + + List<Partner> queryPartnerTypeEqual = partnerDAO.findAll(PartnerSpecs.partnerTypeEqual(PartnerType.ISSUER)); + assertEquals(2, queryPartnerTypeEqual.size()); + + Partner partnerId1 = assertDoesNotThrow(() -> partnerDAO.findById(1).orElseThrow()); + assertEquals(1, partnerId1.getId()); + + assertThrows(NoSuchElementException.class, () -> partnerDAO.findById(100).orElseThrow()); + } + + @Test + void testUpdate() { + String name = "ООО Поставки из Китая оптом", address = "Китай"; + + Partner updatePartner = partnerDAO.findById(1).orElseThrow(); + updatePartner.setName(name); + updatePartner.setAddress(address); + partnerDAO.save(updatePartner); + + Partner partner = partnerDAO.findById(1).orElseThrow(); + assertEquals(name, partner.getName()); + assertEquals(address, partner.getAddress()); + } + + @Test + void testDelete() { + Partner deletePartner = partnerDAO.findById(1).orElseThrow(); + partnerDAO.delete(deletePartner); + + assertTrue(partnerDAO.findById(1).isEmpty()); + } + + @BeforeEach + void beforeEach() { + List<Partner> partners = new ArrayList<>(); + partners.add(new Partner("ООО Рога и копыта", "1036300882672", PartnerType.ISSUER, "Россия, г. Город, ул. Улица, д. 1")); + partners.add(new Partner("ООО Поставки из Китая", "1036300882671", PartnerType.SUPPLIER, "Россия, г. Город, ул. Улица, д. 2")); + partners.add(new Partner("Десяточка", "7825706086", PartnerType.ISSUER, "Россия, г. Санкт-Петербург, пр. Невский, д. 90/92")); + partnerDAO.saveAll(partners); + System.out.println(partnerDAO.findAll()); + } + + @BeforeAll + @AfterEach + void annihilation() { + try (Session session = sessionFactory.openSession()) { + Transaction tx = session.beginTransaction(); + session.createNativeQuery("TRUNCATE partners RESTART IDENTITY CASCADE;").executeUpdate(); + session.createNativeQuery("ALTER SEQUENCE partners_id_seq RESTART WITH 1;").executeUpdate(); + tx.commit(); + } + } +}
\ No newline at end of file |