From 387f70742910b30714da7ff01cad761d17595817 Mon Sep 17 00:00:00 2001 From: Gabriel Sancho Date: Thu, 26 Mar 2026 18:41:15 -0300 Subject: [PATCH] =?UTF-8?q?test(backend):=20add=20failing=20tests=20for=20?= =?UTF-8?q?step=203=20=E2=80=94=20repositories?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../newsletter/repository/RepositoryTest.kt | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 backend/src/test/kotlin/com/condado/newsletter/repository/RepositoryTest.kt diff --git a/backend/src/test/kotlin/com/condado/newsletter/repository/RepositoryTest.kt b/backend/src/test/kotlin/com/condado/newsletter/repository/RepositoryTest.kt new file mode 100644 index 0000000..f253abd --- /dev/null +++ b/backend/src/test/kotlin/com/condado/newsletter/repository/RepositoryTest.kt @@ -0,0 +1,91 @@ +package com.condado.newsletter.repository + +import com.condado.newsletter.model.DispatchLog +import com.condado.newsletter.model.DispatchStatus +import com.condado.newsletter.model.VirtualEntity +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager + +@DataJpaTest +class RepositoryTest { + + @Autowired + lateinit var entityManager: TestEntityManager + + @Autowired + lateinit var virtualEntityRepository: VirtualEntityRepository + + @Autowired + lateinit var dispatchLogRepository: DispatchLogRepository + + private lateinit var activeEntity: VirtualEntity + private lateinit var inactiveEntity: VirtualEntity + + @BeforeEach + fun setUp() { + activeEntity = entityManager.persistAndFlush( + VirtualEntity(name = "Active One", email = "active@condado.com", jobTitle = "CEO", active = true) + ) + inactiveEntity = entityManager.persistAndFlush( + VirtualEntity(name = "Inactive One", email = "inactive@condado.com", jobTitle = "CTO", active = false) + ) + entityManager.clear() + } + + @Test + fun should_returnOnlyActiveEntities_when_findAllByActiveTrueCalled() { + val result = virtualEntityRepository.findAllByActiveTrue() + + assertThat(result).hasSize(1) + assertThat(result[0].email).isEqualTo("active@condado.com") + } + + @Test + fun should_findEntityByEmail_when_emailExists() { + val result = virtualEntityRepository.findByEmail("active@condado.com") + + assertThat(result).isPresent + assertThat(result.get().name).isEqualTo("Active One") + } + + @Test + fun should_returnEmptyOptional_when_emailNotFound() { + val result = virtualEntityRepository.findByEmail("nobody@condado.com") + + assertThat(result).isEmpty + } + + @Test + fun should_returnAllLogsForEntity_when_findAllByVirtualEntityCalled() { + val entity = entityManager.find(VirtualEntity::class.java, activeEntity.id) + entityManager.persistAndFlush(DispatchLog(virtualEntity = entity, status = DispatchStatus.SENT)) + entityManager.persistAndFlush(DispatchLog(virtualEntity = entity, status = DispatchStatus.FAILED)) + entityManager.clear() + + val result = dispatchLogRepository.findAllByVirtualEntity(entity) + + assertThat(result).hasSize(2) + } + + @Test + fun should_returnMostRecentLog_when_findTopByVirtualEntityOrderByDispatchedAtDescCalled() { + val entity = entityManager.find(VirtualEntity::class.java, activeEntity.id) + val firstLog = entityManager.persistAndFlush( + DispatchLog(virtualEntity = entity, status = DispatchStatus.SENT) + ) + Thread.sleep(10) + val latestLog = entityManager.persistAndFlush( + DispatchLog(virtualEntity = entity, status = DispatchStatus.FAILED) + ) + entityManager.clear() + + val result = dispatchLogRepository.findTopByVirtualEntityOrderByDispatchedAtDesc(entity) + + assertThat(result).isPresent + assertThat(result.get().id).isEqualTo(latestLog.id) + } +}