Skip to content

Commit deb0998

Browse files
committed
testing suggestion mapper
1 parent 11beeb2 commit deb0998

File tree

2 files changed

+251
-13
lines changed

2 files changed

+251
-13
lines changed

Diff for: features/finances/impl/src/test/kotlin/br/com/mob1st/features/finances/impl/data/repositories/suggestions/SelectSuggestionsMapperTest.kt

+242-12
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import br.com.mob1st.features.finances.impl.domain.entities.Category
88
import br.com.mob1st.features.finances.impl.domain.entities.CategorySuggestion
99
import br.com.mob1st.features.finances.impl.domain.entities.Recurrences
1010
import br.com.mob1st.features.finances.impl.domain.values.DayOfMonth
11+
import br.com.mob1st.features.finances.impl.domain.values.DayOfWeek
1112
import br.com.mob1st.features.finances.impl.utils.moduleFixture
1213
import br.com.mob1st.features.finances.publicapi.domain.entities.CategoryType
1314
import br.com.mob1st.tests.featuresutils.TestTimberTree
1415
import com.appmattus.kotlinfixture.Fixture
16+
import org.junit.jupiter.api.AfterEach
1517
import org.junit.jupiter.api.BeforeEach
1618
import org.junit.jupiter.api.Test
1719
import org.junit.jupiter.api.extension.ExtensionContext
@@ -22,16 +24,18 @@ import org.junit.jupiter.params.provider.ArgumentsSource
2224
import timber.log.Timber
2325
import java.util.stream.Stream
2426
import kotlin.test.assertEquals
27+
import kotlin.test.assertTrue
2528

2629
class SelectSuggestionsMapperTest {
2730
private lateinit var mapper: SelectSuggestionsMapper
2831

2932
private lateinit var fixture: Fixture
33+
private lateinit var timberTree: TestTimberTree
3034
private lateinit var selectCategoryViewsMapper: SelectCategoryViewsMapper
3135

3236
@BeforeEach
3337
fun setUp() {
34-
Timber.plant(TestTimberTree())
38+
timberTree = TestTimberTree()
3539
fixture = moduleFixture.new {
3640
factory<SelectSuggestions> {
3741
val id = moduleFixture<Long>()
@@ -53,6 +57,12 @@ class SelectSuggestionsMapperTest {
5357
}
5458
selectCategoryViewsMapper = SelectCategoryViewsMapper
5559
mapper = SelectSuggestionsMapper(selectCategoryViewsMapper)
60+
Timber.plant(timberTree)
61+
}
62+
63+
@AfterEach
64+
fun tearDown() {
65+
Timber.uproot(timberTree)
5666
}
5767

5868
@Test
@@ -94,57 +104,229 @@ class SelectSuggestionsMapperTest {
94104
fun `GIVEN a list of suggestions And a linked category with nullable name WHEN map THEN assert suggestion is discarded And error is logged`() {
95105
val discardedSuggestion = fixture<SelectSuggestions>().copy(
96106
sug_id = 1,
97-
sug_name = "night_clubs",
107+
sug_name = allSuggestionsNames.random().first,
98108
cat_name = null,
99109
cat_id = 1,
100110
cat_is_expense = true,
101111
cat_amount = 300_000,
102112
cat_linked_suggestion_id = 1,
103113
vrc_day_of_week = 3,
104114
)
115+
val name = allSuggestionsNames.random()
105116
val persistedSuggestion = fixture<SelectSuggestions>().copy(
106117
sug_id = 2,
107-
sug_name = "bars",
118+
sug_name = name.first,
108119
cat_id = null,
109120
)
110121
val actual = mapper.map(CategoryType.Variable, listOf(discardedSuggestion, persistedSuggestion))
111122
val expected = listOf(
112123
CategorySuggestion(
113124
id = RowId(2),
114-
name = CategorySuggestion.Name.Bars,
125+
name = name.second,
115126
linkedCategory = null,
116127
),
117128
)
118129
assertEquals(expected, actual)
130+
assertTrue(timberTree.logs[0].isError)
119131
}
120132

121133
@Test
122134
fun `GIVEN a list of suggestions And a linked category with nullable is_expense WHEN map THEN assert suggestion is discarded And error is logged`() {
135+
val discardedSuggestion = fixture<SelectSuggestions>().copy(
136+
sug_id = 1,
137+
sug_name = allSuggestionsNames.random().first,
138+
cat_id = 1,
139+
cat_is_expense = null,
140+
cat_amount = 1000_00,
141+
cat_linked_suggestion_id = 1,
142+
src_month = 1,
143+
src_day = 12,
144+
)
145+
val name = allSuggestionsNames.random()
146+
val persistedSuggestion = fixture<SelectSuggestions>().copy(
147+
sug_id = 2,
148+
sug_name = name.first,
149+
cat_id = null,
150+
)
151+
val actual = mapper.map(CategoryType.Seasonal, listOf(discardedSuggestion, persistedSuggestion))
152+
val expected = listOf(
153+
CategorySuggestion(
154+
id = RowId(2),
155+
name = name.second,
156+
linkedCategory = null,
157+
),
158+
)
159+
assertEquals(expected, actual)
160+
assertTrue(timberTree.logs[0].isError)
123161
}
124162

125163
@Test
126164
fun `GIVEN a list of suggestions And a linked category with nullable amount WHEN map THEN assert suggestion is discarded And error is logged`() {
127-
}
128-
129-
@Test
130-
fun `GIVEN a list of suggestions And a linked category with nullable created_at WHEN map THEN assert empty is used as placeholder for created_at`() {
165+
val discardedSuggestion = fixture<SelectSuggestions>().copy(
166+
sug_id = 1,
167+
sug_name = allSuggestionsNames.random().first,
168+
cat_id = 1,
169+
cat_is_expense = true,
170+
cat_amount = null,
171+
cat_linked_suggestion_id = 1,
172+
vrc_day_of_week = 3,
173+
)
174+
val name = allSuggestionsNames.random()
175+
val persistedSuggestion = fixture<SelectSuggestions>().copy(
176+
sug_id = 2,
177+
sug_name = name.first,
178+
cat_id = null,
179+
)
180+
val actual = mapper.map(CategoryType.Variable, listOf(discardedSuggestion, persistedSuggestion))
181+
val expected = listOf(
182+
CategorySuggestion(
183+
id = RowId(2),
184+
name = name.second,
185+
linkedCategory = null,
186+
),
187+
)
188+
assertEquals(expected, actual)
189+
assertTrue(timberTree.logs[0].isError)
131190
}
132191

133192
@Test
134193
fun `GIVEN a list of suggestions And a linked category with different linked suggestion WHEN map THEN assert suggestion is discarded And error is logged`() {
194+
val discardedSuggestion = fixture<SelectSuggestions>().copy(
195+
sug_id = 1,
196+
sug_name = allSuggestionsNames.random().first,
197+
cat_id = 1,
198+
cat_is_expense = true,
199+
cat_amount = 1000_00,
200+
cat_linked_suggestion_id = 2,
201+
src_month = 1,
202+
src_day = 12,
203+
)
204+
val name = allSuggestionsNames.random()
205+
val persistedSuggestion = fixture<SelectSuggestions>().copy(
206+
sug_id = 2,
207+
sug_name = name.first,
208+
cat_id = null,
209+
)
210+
val actual = mapper.map(CategoryType.Seasonal, listOf(discardedSuggestion, persistedSuggestion))
211+
val expected = listOf(
212+
CategorySuggestion(
213+
id = RowId(2),
214+
name = name.second,
215+
linkedCategory = null,
216+
),
217+
)
218+
assertEquals(expected, actual)
219+
assertTrue(timberTree.logs[0].isError)
135220
}
136221

137222
@Test
138-
fun `GIVEN a list of suggestions And a unknown name WHEN map THEN assert suggestion is discarded And message is logged`() {
223+
fun `GIVEN a list of suggestions And a unknown name WHEN map THEN assert suggestion is discarded And warning is logged`() {
224+
val discardedSuggestion = fixture<SelectSuggestions>().copy(
225+
sug_id = 1,
226+
sug_name = "unknown_name",
227+
cat_id = 1,
228+
cat_is_expense = true,
229+
cat_amount = 1000_00,
230+
cat_linked_suggestion_id = 1,
231+
src_month = 1,
232+
src_day = 12,
233+
)
234+
val name = allSuggestionsNames.random()
235+
val persistedSuggestion = fixture<SelectSuggestions>().copy(
236+
sug_id = 2,
237+
sug_name = name.first,
238+
cat_id = null,
239+
)
240+
val actual = mapper.map(CategoryType.Seasonal, listOf(discardedSuggestion, persistedSuggestion))
241+
val expected = listOf(
242+
CategorySuggestion(
243+
id = RowId(2),
244+
name = name.second,
245+
linkedCategory = null,
246+
),
247+
)
248+
assertEquals(expected, actual)
249+
assertTrue(timberTree.logs[0].isWarning)
139250
}
140251

141252
@Test
142-
fun `GIVEN a list of suggestions And two categories are linked to the same suggestion WHEN map THEN assert that only the first category is used And a log message is added`() {
253+
fun `GIVEN a list of suggestions And two categories are linked to the same suggestion WHEN map THEN assert that only the first category is used And a warning is logged`() {
254+
val firstSuggestionName = allSuggestionsNames.random()
255+
val secondSuggestionName = allSuggestionsNames.random()
256+
val firstSuggestionWithCategory = fixture<SelectSuggestions>().copy(
257+
sug_id = 1,
258+
sug_name = firstSuggestionName.first,
259+
cat_id = 1,
260+
cat_name = "any1",
261+
cat_is_expense = true,
262+
cat_amount = 1000_00,
263+
cat_linked_suggestion_id = 1,
264+
vrc_day_of_week = 4,
265+
)
266+
val secondSuggestionWithCategory = fixture<SelectSuggestions>().copy(
267+
sug_id = 1,
268+
sug_name = firstSuggestionName.first,
269+
cat_id = 2,
270+
cat_name = "any2",
271+
cat_is_expense = true,
272+
cat_amount = 2000_00,
273+
cat_linked_suggestion_id = 1,
274+
vrc_day_of_week = 3,
275+
)
276+
val suggestionWithoutLinkedCategory = fixture<SelectSuggestions>().copy(
277+
sug_id = 2,
278+
sug_name = secondSuggestionName.first,
279+
cat_id = null,
280+
)
281+
val actual = mapper.map(
282+
CategoryType.Variable,
283+
listOf(firstSuggestionWithCategory, secondSuggestionWithCategory, suggestionWithoutLinkedCategory),
284+
)
285+
val expected = listOf(
286+
CategorySuggestion(
287+
id = RowId(1),
288+
name = firstSuggestionName.second,
289+
linkedCategory = Category(
290+
id = RowId(1),
291+
name = "any1",
292+
isExpense = true,
293+
amount = Money(1000_00),
294+
recurrences = Recurrences.Variable(
295+
daysOfWeek = listOf(DayOfWeek(4)),
296+
),
297+
),
298+
),
299+
CategorySuggestion(
300+
id = RowId(2),
301+
name = secondSuggestionName.second,
302+
linkedCategory = null,
303+
),
304+
)
305+
assertEquals(expected, actual)
306+
assertTrue(timberTree.logs[0].isWarning)
143307
}
144308

145309
@ParameterizedTest
146310
@ArgumentsSource(SuggestionsNameProvider::class)
147-
fun `GIVEN a list of suggestions WHEN map THEN assert the names are mapped correctly`() {
311+
fun `GIVEN suggestion with a name as string WHEN map THEN assert the name are mapped to enum`(
312+
stringName: String,
313+
enumName: CategorySuggestion.Name,
314+
) {
315+
val suggestion = fixture<SelectSuggestions>().copy(
316+
sug_id = 1,
317+
sug_name = stringName,
318+
cat_id = 1,
319+
cat_name = "any",
320+
cat_is_expense = false,
321+
cat_amount = 1000_00,
322+
cat_linked_suggestion_id = 1,
323+
frc_day_of_month = 2,
324+
)
325+
val actual = mapper.map(CategoryType.Fixed, listOf(suggestion))
326+
assertEquals(
327+
enumName,
328+
actual.first().name,
329+
)
148330
}
149331

150332
private fun linkedFixedCategories(): List<Category> {
@@ -222,10 +404,58 @@ class SelectSuggestionsMapperTest {
222404

223405
object SuggestionsNameProvider : ArgumentsProvider {
224406
override fun provideArguments(context: ExtensionContext?): Stream<out Arguments> {
225-
TODO("Not yet implemented")
407+
return allSuggestionsNames.map { (stringName, enumName) ->
408+
Arguments.of(stringName, enumName)
409+
}.stream()
226410
}
227411
}
228412

229413
companion object {
414+
val allSuggestionsNames = listOf(
415+
"rent_or_mortgage" to CategorySuggestion.Name.RentOrMortgage,
416+
"property_taxes" to CategorySuggestion.Name.PropertyTaxes,
417+
"health_insurance" to CategorySuggestion.Name.HealthInsurance,
418+
"car_insurance" to CategorySuggestion.Name.CarInsurance,
419+
"public_transport" to CategorySuggestion.Name.PublicTransport,
420+
"home_insurance" to CategorySuggestion.Name.HomeInsurance,
421+
"loan_payments" to CategorySuggestion.Name.LoanPayments,
422+
"internet_subscription" to CategorySuggestion.Name.InternetSubscription,
423+
"cell_phone_plan" to CategorySuggestion.Name.CellPhonePlan,
424+
"cable_or_streaming_services" to CategorySuggestion.Name.CableOrStreamingServices,
425+
"music_streaming_services" to CategorySuggestion.Name.MusicStreamingServices,
426+
"magazine_or_newspaper_subscriptions" to CategorySuggestion.Name.MagazineOrNewspaperSubscriptions,
427+
"association_fees" to CategorySuggestion.Name.AssociationFees,
428+
"private_retirement_plans" to CategorySuggestion.Name.PrivateRetirementPlans,
429+
"personal_education" to CategorySuggestion.Name.PersonalEducation,
430+
"children_school" to CategorySuggestion.Name.ChildrenSchool,
431+
"childcare" to CategorySuggestion.Name.Childcare,
432+
"groceries" to CategorySuggestion.Name.Groceries,
433+
"dining_out" to CategorySuggestion.Name.DiningOut,
434+
"food_delivery" to CategorySuggestion.Name.FoodDelivery,
435+
"weekday_lunch" to CategorySuggestion.Name.WeekdayLunch,
436+
"coffee_snacks" to CategorySuggestion.Name.CoffeeSnacks,
437+
"transportation_fuel" to CategorySuggestion.Name.TransportationFuel,
438+
"public_transport_tickets" to CategorySuggestion.Name.PublicTransportTickets,
439+
"cinema" to CategorySuggestion.Name.Cinema,
440+
"sports_tickets" to CategorySuggestion.Name.SportsTickets,
441+
"electronic_games" to CategorySuggestion.Name.ElectronicGames,
442+
"bars" to CategorySuggestion.Name.Bars,
443+
"night_clubs" to CategorySuggestion.Name.NightClubs,
444+
"household_supplies" to CategorySuggestion.Name.HouseholdSupplies,
445+
"fitness_recreation" to CategorySuggestion.Name.FitnessRecreation,
446+
"holiday_gifts" to CategorySuggestion.Name.HolidayGifts,
447+
"vacation_travel" to CategorySuggestion.Name.VacationTravel,
448+
"back_to_school_supplies" to CategorySuggestion.Name.BackToSchoolSupplies,
449+
"winter_clothing" to CategorySuggestion.Name.WinterClothing,
450+
"summer_activities" to CategorySuggestion.Name.SummerActivities,
451+
"garden_supplies" to CategorySuggestion.Name.GardenSupplies,
452+
"home_heating" to CategorySuggestion.Name.HomeHeating,
453+
"holiday_decorations" to CategorySuggestion.Name.HolidayDecorations,
454+
"tax_preparation_fees" to CategorySuggestion.Name.TaxPreparationFees,
455+
"spring_cleaning" to CategorySuggestion.Name.SpringCleaning,
456+
"salary" to CategorySuggestion.Name.Salary,
457+
"pension" to CategorySuggestion.Name.Pension,
458+
"rental_income" to CategorySuggestion.Name.RentalIncome,
459+
)
230460
}
231461
}

Diff for: tests/features-utils/src/main/kotlin/br/com/mob1st/tests/featuresutils/TestTimberTree.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ class TestTimberTree : Timber.Tree() {
1111
_logs.add(Log(priority, tag, message, t))
1212
}
1313

14-
data class Log(val priority: Int, val tag: String?, val message: String, val t: Throwable?)
14+
data class Log(val priority: Int, val tag: String?, val message: String, val t: Throwable?) {
15+
val isError get() = priority == ERROR_PRIORITY
16+
val isWarning get() = priority == WARNING_PRIORITY
17+
}
18+
19+
companion object {
20+
private const val ERROR_PRIORITY = 6
21+
private const val WARNING_PRIORITY = 5
22+
}
1523
}

0 commit comments

Comments
 (0)