feat: implement VirtualEntity and DispatchLog models with corresponding tests and configuration
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
package com.condado.newsletter.model
|
||||
|
||||
import org.assertj.core.api.Assertions.assertThat
|
||||
import org.junit.jupiter.api.Test
|
||||
import org.junit.jupiter.api.assertThrows
|
||||
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
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@DataJpaTest
|
||||
class EntityMappingTest {
|
||||
|
||||
@Autowired
|
||||
lateinit var entityManager: TestEntityManager
|
||||
|
||||
@Test
|
||||
fun should_persistVirtualEntity_when_allFieldsProvided() {
|
||||
val entity = VirtualEntity(
|
||||
name = "João Silva",
|
||||
email = "joao@condado.com",
|
||||
jobTitle = "Chief Nonsense Officer",
|
||||
personality = "Extremely formal but talks about cats",
|
||||
scheduleCron = "0 9 * * 1",
|
||||
contextWindowDays = 3
|
||||
)
|
||||
|
||||
val saved = entityManager.persistAndFlush(entity)
|
||||
|
||||
assertThat(saved.id).isNotNull()
|
||||
assertThat(saved.name).isEqualTo("João Silva")
|
||||
assertThat(saved.email).isEqualTo("joao@condado.com")
|
||||
assertThat(saved.jobTitle).isEqualTo("Chief Nonsense Officer")
|
||||
assertThat(saved.personality).isEqualTo("Extremely formal but talks about cats")
|
||||
assertThat(saved.scheduleCron).isEqualTo("0 9 * * 1")
|
||||
assertThat(saved.contextWindowDays).isEqualTo(3)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_enforceUniqueEmail_when_duplicateEmailInserted() {
|
||||
val entity1 = VirtualEntity(name = "First", email = "dup@condado.com", jobTitle = "Dev")
|
||||
val entity2 = VirtualEntity(name = "Second", email = "dup@condado.com", jobTitle = "Dev")
|
||||
|
||||
entityManager.persistAndFlush(entity1)
|
||||
|
||||
assertThrows<Exception> {
|
||||
entityManager.persistAndFlush(entity2)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_persistDispatchLog_when_linkedToVirtualEntity() {
|
||||
val entity = VirtualEntity(name = "Maria Santos", email = "maria@condado.com", jobTitle = "COO")
|
||||
val savedEntity = entityManager.persistAndFlush(entity)
|
||||
entityManager.clear()
|
||||
|
||||
val log = DispatchLog(
|
||||
virtualEntity = entityManager.find(VirtualEntity::class.java, savedEntity.id),
|
||||
promptSent = "Test prompt content",
|
||||
aiResponse = "SUBJECT: Test\nBODY:\nTest body",
|
||||
emailSubject = "Test Subject",
|
||||
emailBody = "Test body content",
|
||||
status = DispatchStatus.SENT
|
||||
)
|
||||
|
||||
val savedLog = entityManager.persistAndFlush(log)
|
||||
|
||||
assertThat(savedLog.id).isNotNull()
|
||||
assertThat(savedLog.virtualEntity.id).isEqualTo(savedEntity.id)
|
||||
assertThat(savedLog.status).isEqualTo(DispatchStatus.SENT)
|
||||
assertThat(savedLog.promptSent).isEqualTo("Test prompt content")
|
||||
assertThat(savedLog.emailSubject).isEqualTo("Test Subject")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_setCreatedAtAutomatically_when_virtualEntitySaved() {
|
||||
val before = LocalDateTime.now().minusSeconds(1)
|
||||
|
||||
val entity = VirtualEntity(name = "Auto Time", email = "time@condado.com", jobTitle = "Tester")
|
||||
val saved = entityManager.persistAndFlush(entity)
|
||||
|
||||
val after = LocalDateTime.now().plusSeconds(1)
|
||||
|
||||
assertThat(saved.createdAt).isNotNull()
|
||||
assertThat(saved.createdAt).isAfterOrEqualTo(before)
|
||||
assertThat(saved.createdAt).isBeforeOrEqualTo(after)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun should_defaultActiveToTrue_when_virtualEntityCreated() {
|
||||
val entity = VirtualEntity(name = "Default Active", email = "active@condado.com", jobTitle = "CEO")
|
||||
val saved = entityManager.persistAndFlush(entity)
|
||||
|
||||
assertThat(saved.active).isTrue()
|
||||
}
|
||||
}
|
||||
40
backend/src/test/resources/application.yml
Normal file
40
backend/src/test/resources/application.yml
Normal file
@@ -0,0 +1,40 @@
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
driver-class-name: org.h2.Driver
|
||||
username: sa
|
||||
password:
|
||||
jpa:
|
||||
hibernate:
|
||||
ddl-auto: create-drop
|
||||
show-sql: false
|
||||
properties:
|
||||
hibernate:
|
||||
dialect: org.hibernate.dialect.H2Dialect
|
||||
mail:
|
||||
host: localhost
|
||||
port: 25
|
||||
username: test
|
||||
password: test
|
||||
properties:
|
||||
mail:
|
||||
smtp:
|
||||
auth: false
|
||||
starttls:
|
||||
enable: false
|
||||
|
||||
app:
|
||||
password: testpassword
|
||||
recipients: test@test.com
|
||||
jwt:
|
||||
secret: test-secret-key-for-testing-only-must-be-at-least-32-characters
|
||||
expiration-ms: 86400000
|
||||
|
||||
imap:
|
||||
host: localhost
|
||||
port: 993
|
||||
inbox-folder: INBOX
|
||||
|
||||
openai:
|
||||
api-key: test-api-key
|
||||
model: gpt-4o
|
||||
Reference in New Issue
Block a user