move repositories to separate db package

replace jacoco with kover
This commit is contained in:
Savosin Denis
2025-03-28 14:38:57 +07:00
parent 4f9ad14767
commit d7c051746d
37 changed files with 603 additions and 34 deletions

View File

@@ -0,0 +1,14 @@
package com.github.dannecron.demo.db
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories
import org.springframework.test.context.ActiveProfiles
import org.testcontainers.junit.jupiter.Testcontainers
@ActiveProfiles("db")
@DataJdbcTest
@Testcontainers(disabledWithoutDocker = false)
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@EnableJdbcRepositories
class BaseDbTest

View File

@@ -0,0 +1,49 @@
package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.City
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.jdbc.Sql
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@ContextConfiguration(classes = [CityRepository::class])
class CityRepositoryTest : BaseDbTest() {
@Autowired
private lateinit var cityRepository: CityRepository
private val cityGuid = UUID.fromString("21a1a3a8-621a-40f7-b64f-7e118aa241b9")
private val city = City(
id = 1000,
guid = cityGuid,
name = "Tokyo",
createdAt = OffsetDateTime.parse("2025-01-01T12:10:05+00:00"),
updatedAt = null,
deletedAt = null,
)
@Test
@Sql(scripts = ["/sql/insert_city.sql"])
fun findByGuid() {
val result = cityRepository.findByGuid(cityGuid)
assertEquals(city, result)
val emptyResult = cityRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult)
}
@Test
@Sql(scripts = ["/sql/insert_city.sql"])
fun softDelete() {
val deletedAt = OffsetDateTime.parse("2025-01-02T12:10:05+00:00")
val expectedCity = city.copy(deletedAt = deletedAt)
val result = cityRepository.softDelete(cityGuid, deletedAt)
assertEquals(expectedCity, result)
}
}

View File

@@ -0,0 +1,39 @@
package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.Customer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.jdbc.Sql
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@ContextConfiguration(classes = [CustomerRepository::class])
class CustomerRepositoryTest : BaseDbTest() {
@Autowired
private lateinit var customerRepository: CustomerRepository
private val customerGuid = UUID.fromString("823c50de-4c81-49bd-a69a-2d52be42b728")
private val customer = Customer(
id = 1000,
guid = customerGuid,
name = "Customer",
cityId = 1000,
createdAt = OffsetDateTime.parse("2025-01-01T12:10:05+00:00"),
updatedAt = null,
)
@Test
@Sql(scripts = ["/sql/insert_city.sql", "/sql/insert_customer.sql"])
fun findByGuid() {
val result = customerRepository.findByGuid(customerGuid)
assertEquals(customer, result)
val emptyResult = customerRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult)
}
}

View File

@@ -0,0 +1,43 @@
package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.order.OrderProduct
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.jdbc.Sql
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
@ContextConfiguration(classes = [OrderProductRepository::class])
class OrderProductRepositoryTest : BaseDbTest() {
@Autowired
private lateinit var orderProductRepository: OrderProductRepository
private val orderId = 1000L
private val orderProduct = OrderProduct(
guid = UUID.fromString("930f54e2-c60d-448e-83b1-0d259ff2c2d3"),
orderId = orderId,
productId = 1000,
createdAt = OffsetDateTime.parse("2025-01-01T12:10:05+00:00"),
updatedAt = null,
)
@Test
@Sql(
scripts = [
"/sql/insert_city.sql",
"/sql/insert_customer.sql",
"/sql/insert_order.sql",
"/sql/insert_product.sql",
"/sql/insert_order_product.sql",
]
)
fun findByOrderId() {
val result = orderProductRepository.findByOrderId(orderId)
assertEquals(1, result.size)
assertEquals(orderProduct, result[0])
}
}

View File

@@ -0,0 +1,43 @@
package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.order.Order
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.jdbc.Sql
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
@ContextConfiguration(classes = [OrderRepository::class])
class OrderRepositoryTest : BaseDbTest() {
@Autowired
private lateinit var orderRepository: OrderRepository
private val orderGuid = UUID.fromString("2c960a08-7187-4e91-9ef3-275c91b1342c")
private val customerId = 1000L
private val order = Order(
id = 1000,
guid = orderGuid,
customerId = customerId,
deliveredAt = null,
createdAt = OffsetDateTime.parse("2025-01-01T12:10:05+00:00"),
updatedAt = null,
)
@Test
@Sql(
scripts = [
"/sql/insert_city.sql",
"/sql/insert_customer.sql",
"/sql/insert_order.sql",
]
)
fun findByGuid() {
val result = orderRepository.findByCustomerId(customerId)
assertEquals(1, result.size)
assertEquals(order, result[0])
}
}

View File

@@ -0,0 +1,41 @@
package com.github.dannecron.demo.db.repository
import com.github.dannecron.demo.db.BaseDbTest
import com.github.dannecron.demo.db.entity.Product
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.jdbc.Sql
import java.time.OffsetDateTime
import java.util.UUID
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
@ContextConfiguration(classes = [ProductRepository::class])
class ProductRepositoryTest : BaseDbTest() {
@Autowired
private lateinit var productRepository: ProductRepository
private val productGuid = UUID.fromString("1fb5c7e4-8ce2-43b8-8ca7-1089b04959b9")
private val product = Product(
id = 1000,
guid = productGuid,
name = "product",
description = "description",
price = 10000,
createdAt = OffsetDateTime.parse("2025-01-01T12:10:05+00:00"),
updatedAt = null,
deletedAt = null,
)
@Test
@Sql(scripts = ["/sql/insert_product.sql"])
fun findByGuid() {
val result = productRepository.findByGuid(productGuid)
assertEquals(product, result)
val emptyResult = productRepository.findByGuid(UUID.randomUUID())
assertNull(emptyResult)
}
}

View File

@@ -0,0 +1,10 @@
---
spring:
datasource:
url: jdbc:tc:postgresql:14-alpine:///test
driver-class-name: org.testcontainers.jdbc.ContainerDatabaseDriver
hikari:
maximum-pool-size: 2
flyway:
enabled: true
locations: classpath:migration/structure, classpath:migration/data

View File

@@ -0,0 +1,3 @@
insert into "city" (id, guid, name, created_at) values
(1000, '21a1a3a8-621a-40f7-b64f-7e118aa241b9', 'Tokyo', '2025-01-01 12:10:05 +00:00')
;

View File

@@ -0,0 +1,3 @@
insert into "customer" (id, guid, name, city_id, created_at) values
(1000, '823c50de-4c81-49bd-a69a-2d52be42b728', 'Customer', 1000, '2025-01-01 12:10:05 +00:00')
;

View File

@@ -0,0 +1,3 @@
insert into "order" (id, guid, customer_id, created_at) values
(1000, '2c960a08-7187-4e91-9ef3-275c91b1342c', 1000, '2025-01-01 12:10:05 +00:00')
;

View File

@@ -0,0 +1,3 @@
insert into "order_product" (guid, order_id, product_id, created_at) values
('930f54e2-c60d-448e-83b1-0d259ff2c2d3', 1000, 1000, '2025-01-01 12:10:05 +00:00')
;

View File

@@ -0,0 +1,3 @@
insert into "product" (id, guid, name, description, price, created_at) values
(1000, '1fb5c7e4-8ce2-43b8-8ca7-1089b04959b9', 'product', 'description', 10000, '2025-01-01 12:10:05 +00:00')
;