Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions src/main/kotlin/database/model/MatchResult.kt
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package database.model

sealed class MatchResult {

data class CustomerAndPet(
val filteredCustomers: Map<String, model.Customer>,
val filteredPets: Map<String, model.Pet>
) : MatchResult()

data class Customer(
val filteredCustomers: Map<String, model.Customer>
) : MatchResult()

data class Pet(
val filteredPets: Map<String, model.Pet>
) : MatchResult()

object NoMatch : MatchResult()
}
9 changes: 1 addition & 8 deletions src/main/kotlin/database/model/SearchQueryResult.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,8 @@ import database.entity.CustomerEntity
import database.entity.PetEntity

sealed class SearchQueryResult {
data class CustomerSuccess(
val customers: List<CustomerEntity>
) : SearchQueryResult()

data class PetSuccess(
val pets: List<PetEntity>
) : SearchQueryResult()

data class CustomerAndPetSuccess(
data class Success(
val customers: List<CustomerEntity>,
val pets: List<PetEntity>
) : SearchQueryResult()
Expand Down
26 changes: 4 additions & 22 deletions src/main/kotlin/database/search/SearchDatabase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@ class SearchDatabase(
is MatchResult.CustomerAndPet -> {
val customers = getAllMatchingCustomerEntities(result.filteredCustomers)
val pets = getAllMatchingPetEntities(result.filteredPets)
SearchQueryResult.CustomerAndPetSuccess(customers, pets)
}
is MatchResult.Customer -> {
SearchQueryResult.CustomerSuccess(getAllMatchingCustomerEntities(result.filteredCustomers))
}
is MatchResult.Pet -> {
SearchQueryResult.PetSuccess(getAllMatchingPetEntities(result.filteredPets))
SearchQueryResult.Success(customers, pets)
}
is MatchResult.NoMatch -> {
SearchQueryResult.Error("no match found")
Expand All @@ -45,27 +39,15 @@ class SearchDatabase(
}

private fun checkTablesForAnyMatch(query: String): MatchResult {
val doesAnyCustomersMatch = vetPracticeDatabase.getAllCustomers().entries.any {
it.value.name.lowercase() == query
}
val doesAnyPetsMatch = vetPracticeDatabase.getAllPets().entries.any {
it.value.name.lowercase() == query
}
val doesAnyCustomersMatch = getMatchingResultsForCustomers(query)
val doesAnyPetsMatch = getMatchingResultsForPets(query)

return when {
doesAnyCustomersMatch && doesAnyPetsMatch -> {
doesAnyCustomersMatch.isNotEmpty() || doesAnyPetsMatch.isNotEmpty() -> {
val filteredCustomers = getMatchingResultsForCustomers(query)
val filteredPets = getMatchingResultsForPets(query)
MatchResult.CustomerAndPet(filteredCustomers, filteredPets)
}
doesAnyCustomersMatch -> {
val filteredCustomers = getMatchingResultsForCustomers(query)
MatchResult.Customer(filteredCustomers)
}
doesAnyPetsMatch -> {
val filteredPets = getMatchingResultsForPets(query)
MatchResult.Pet(filteredPets)
}
else -> {
MatchResult.NoMatch
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/presenter/VeterinaryPracticePresenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ class VeterinaryPracticePresenter(

override fun search(query: String) {
when (val result = vetPracticeDatabase.query(query)) {
is SearchQueryResult.CustomerAndPetSuccess -> view.showCustomersAndPets(result.customers, result.pets)
is SearchQueryResult.CustomerSuccess -> view.showCustomers(customers = result.customers)
is SearchQueryResult.PetSuccess -> view.showPets(result.pets)
is SearchQueryResult.Success -> view.showQueryResult(result.customers, result.pets)
is SearchQueryResult.Error -> view.showError(result.message)
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/view/ConsoleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import database.entity.PetEntity
interface ConsoleView {
fun startProgram(fileName: String)
fun searchProgram(searchQuery: String)
fun showCustomers(customers: List<CustomerEntity>)
fun showPets(pets: List<PetEntity>)
fun showCustomersAndPets(customers: List<CustomerEntity>, pets: List<PetEntity>)
fun showQueryResult(customers: List<CustomerEntity>, pets: List<PetEntity>)
fun showInvalidErrorMessage(lineNumber: Int)
fun showErrorOnLineWithMessage(lineNumber: Int, message: String)
fun showError(message: String)
Expand Down
21 changes: 1 addition & 20 deletions src/main/kotlin/view/VeterinaryPracticeConsoleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ class VeterinaryPracticeConsoleView(private val presenter: Presenter) : ConsoleV
presenter.search(searchQuery)
}

override fun showCustomers(customers: List<CustomerEntity>) {
override fun showQueryResult(customers: List<CustomerEntity>, pets: List<PetEntity>) {
customers.forEach { customer ->
println("Customer: ${customer.customer}")
println("Identifier: ${customer.identifier}")
println("Pets: ${customer.pets}")
println()
}
}

override fun showPets(pets: List<PetEntity>) {
pets.forEach { pet ->
println("Pet: ${pet.pet}")
println("Type: ${pet.type}")
Expand All @@ -35,22 +32,6 @@ class VeterinaryPracticeConsoleView(private val presenter: Presenter) : ConsoleV
}
}

override fun showCustomersAndPets(customers: List<CustomerEntity>, pets: List<PetEntity>) {
customers.forEach { customer ->
println("Customer: ${customer.customer}")
println("Identifier: ${customer.identifier}")
println("Pets: ${customer.pets}")
println()
}
pets.forEach { pet ->
println("Pet: ${pet.pet}")
println("Type: ${pet.type}")
println("Identifier: ${pet.identifier}")
println("Owner: ${pet.owner}")
println("Owner ID: ${pet.ownerId}")
}
}

override fun showInvalidErrorMessage(lineNumber: Int) {
println("Error on line ${lineNumber}: Invalid syntax")
}
Expand Down
14 changes: 8 additions & 6 deletions src/test/kotlin/database/search/SearchDatabaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class SearchDatabaseTest {
}

@Test
fun `queryAllDatabaseTable() - given query has a customer and pet match in the database then SearchQueryResultCustomerAndPetSuccess is returned with customer and pet`() {
fun `queryAllDatabaseTable() - given query has a customer and pet match in the database then SearchQueryResultSuccess is returned with customer and pet`() {
//given
every { mockLocalDatabase.getAllCustomers() } returns hashMapOf(
Pair("1", Customer("1", "Albert")),
Expand All @@ -84,37 +84,39 @@ class SearchDatabaseTest {
//then
assertThat(
result
).isEqualTo(SearchQueryResult.CustomerAndPetSuccess(listOf(secondCustomerEntity), listOf(secondPetEntity)))
).isEqualTo(SearchQueryResult.Success(customers = listOf(secondCustomerEntity), pets = listOf(secondPetEntity)))
}

@Test
fun `queryAllDatabaseTable() - given query has a customer match only in the database then SearchQueryResultCustomerSuccess is returned with customer entity`() {
fun `queryAllDatabaseTable() - given query has a customer match only in the database then SearchQueryResultSuccess is returned with customer entity`() {
//given
val query = "Albert"
every { mockCustomerEntityMapper.map(any(), any()) } returns listOf(customerEntity)
every { mockPetEntityMapper.map(any(), any()) } returns emptyList()

//when
val result = subject.queryAllDatabaseTable(query)

//then
assertThat(
result
).isEqualTo(SearchQueryResult.CustomerSuccess(listOf(customerEntity)))
).isEqualTo(SearchQueryResult.Success(customers = listOf(customerEntity), pets = emptyList()))
}

@Test
fun `queryAllDatabaseTable() - given query has a pet match only in the database then SearchQueryResultPetSuccess is returned with pet entity`() {
fun `queryAllDatabaseTable() - given query has a pet match only in the database then SearchQueryResultSuccess is returned with pet entity`() {
//given
val query = "Johnny"
every { mockPetEntityMapper.map(any(), any()) } returns listOf(petEntity)
every { mockCustomerEntityMapper.map(any(), any()) } returns emptyList()

//when
val result = subject.queryAllDatabaseTable(query)

//then
assertThat(
result
).isEqualTo(SearchQueryResult.PetSuccess(listOf(petEntity)))
).isEqualTo(SearchQueryResult.Success(customers = emptyList(), pets = listOf(petEntity)))
}

@Test
Expand Down
12 changes: 6 additions & 6 deletions src/test/kotlin/presenter/VeterinaryPracticePresenterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,40 +141,40 @@ class VeterinaryPracticePresenterTest {
every {
mockLocalDatabase.query(searchQuery)
}.returns(
SearchQueryResult.CustomerAndPetSuccess(customerEntities, petEntities)
SearchQueryResult.Success(customerEntities, petEntities)
)

//when
subject.search(searchQuery)

//then
verify { mockView.showCustomersAndPets(customerEntities, petEntities) }
verify { mockView.showQueryResult(customerEntities, petEntities) }
}

@Test
fun `search() - given query that matches a customer in the database then view customer is printed`() {
//given
val searchQuery = "sam"
every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.CustomerSuccess(customerEntities))
every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.Success(customerEntities, emptyList()))

//when
subject.search(searchQuery)

//then
verify { mockView.showCustomers(customerEntities) }
verify { mockView.showQueryResult(customers = customerEntities, pets = emptyList()) }
}

@Test
fun `search() - given query that matches a pet in the database then pet is printed`() {
//given
val searchQuery = "sam"
every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.PetSuccess(petEntities))
every { mockLocalDatabase.query(searchQuery) }.returns(SearchQueryResult.Success(emptyList(), petEntities))

//when
subject.search(searchQuery)

//then
verify { mockView.showPets(petEntities) }
verify { mockView.showQueryResult(emptyList(), petEntities) }
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions src/test/kotlin/view/VeterinaryPracticeConsoleViewTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class VeterinaryPracticeConsoleViewTest {
@Test
fun `showCustomers() - given customer information then customer details are printed`() {
//when
subject.showCustomers(listOf(singleCustomerEntity))
subject.showQueryResult(customers = listOf(singleCustomerEntity), pets = emptyList())

//then
assertThat(
Expand All @@ -75,7 +75,7 @@ class VeterinaryPracticeConsoleViewTest {
@Test
fun `showCustomers() - given list of customer information then customer details are printed`() {
//when
subject.showCustomers(customerEntities)
subject.showQueryResult(customers = customerEntities, pets = emptyList())

//then
assertThat(
Expand All @@ -94,7 +94,7 @@ class VeterinaryPracticeConsoleViewTest {
@Test
fun `showPets() - given pet information then the pet details are printed`() {
//when
subject.showPets(listOf(singlePetEntity))
subject.showQueryResult(customers = emptyList(), pets = listOf(singlePetEntity))

//then
assertThat(
Expand All @@ -111,7 +111,7 @@ class VeterinaryPracticeConsoleViewTest {
@Test
fun `showPets() - given list of pet information then pet details are printed`() {
//when
subject.showPets(petEntities)
subject.showQueryResult(customers = emptyList(), pets = petEntities)

//then
assertThat(
Expand All @@ -134,7 +134,7 @@ class VeterinaryPracticeConsoleViewTest {
@Test
fun `showCustomersAndPets() - given customer and pet information then customer and pet details are printed`() {
//when
subject.showCustomersAndPets(listOf(singleCustomerEntity), listOf(singlePetEntity))
subject.showQueryResult(listOf(singleCustomerEntity), listOf(singlePetEntity))

//then
assertThat(
Expand Down