Skip to content

Commit 361eeb0

Browse files
committed
WIP: more streamlined spec code for RaceUtil
1 parent 8a0a5a1 commit 361eeb0

File tree

1 file changed

+92
-100
lines changed

1 file changed

+92
-100
lines changed

publication/src/test/scala/hmda/publication/reports/RaceUtilSpec.scala

Lines changed: 92 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ class RaceUtilSpec extends AsyncWordSpec with MustMatchers with LarGenerators wi
2222
implicit val ec = system.dispatcher
2323
implicit val materializer = ActorMaterializer()
2424

25-
def larCollection(transformation: (LoanApplicationRegister => LoanApplicationRegister)): List[LoanApplicationRegister] = {
26-
lar100ListGen.sample.get.map(transformation)
25+
def larCollectionWithApplicant(transformation: (Applicant => Applicant)): List[LoanApplicationRegister] = {
26+
lar100ListGen.sample.get.map { lar =>
27+
val newApplicant = transformation(lar.applicant)
28+
lar.copy(applicant = newApplicant)
29+
}
2730
}
2831

2932
def source(lars: List[LoanApplicationRegister]): Source[LoanApplicationRegisterQuery, NotUsed] = Source
@@ -40,158 +43,147 @@ class RaceUtilSpec extends AsyncWordSpec with MustMatchers with LarGenerators wi
4043

4144
"'American Indian or Alaska Native' race filter" must {
4245
"include applications that meet 'American Indian or Alaska Native' criteria" in {
43-
val lars = larCollection { lar =>
44-
val app = lar.applicant
45-
val withQualifyingRace = applicantRace3to5Blank(app.copy(race1 = 1, race2 = whiteOrBlank))
46-
val withCoApplicant = coApplicantNotWhite(withQualifyingRace)
47-
lar.copy(applicant = withCoApplicant)
46+
val lars = larCollectionWithApplicant { app =>
47+
val withQualifyingCoApp = coApplicantNotWhite(app)
48+
applicantRace3to5Blank(withQualifyingCoApp.copy(race1 = 1, race2 = whiteOrBlank))
4849
}
4950
val nativeLars = filterRace(source(lars), AmericanIndianOrAlaskaNative)
5051
count(nativeLars).map(_ mustBe 100)
5152
}
52-
"exclude applications that do not meet 'American Indian or Alaska Native' criteria" in {
53-
val larsExcludedByApplicant = larCollection { lar =>
54-
val applicant = lar.applicant.copy(race1 = 1, race2 = "2")
55-
lar.copy(applicant = applicant)
56-
}
57-
val larsExcludedByCoApplicant = larCollection { lar =>
58-
val applicant = lar.applicant.copy(race1 = 1, coRace1 = 5)
59-
lar.copy(applicant = applicant)
60-
}
61-
val nonNativeLars1 = filterRace(source(larsExcludedByApplicant), AmericanIndianOrAlaskaNative)
62-
val nonNativeLars2 = filterRace(source(larsExcludedByCoApplicant), AmericanIndianOrAlaskaNative)
63-
count(nonNativeLars1).map(_ mustBe 0)
64-
count(nonNativeLars2).map(_ mustBe 0)
53+
"exclude lars where applicant does not meet criteria" in {
54+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 1, race2 = "2"))
55+
val nonNativeLars = filterRace(source(excludedLars), AmericanIndianOrAlaskaNative)
56+
count(nonNativeLars).map(_ mustBe 0)
57+
}
58+
"exclude lars where coApplicant does not meet criteria" in {
59+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 1, coRace1 = 5))
60+
val nonNativeLars = filterRace(source(excludedLars), AmericanIndianOrAlaskaNative)
61+
count(nonNativeLars).map(_ mustBe 0)
6562
}
6663
}
6764

6865
"'Asian' race filter" must {
6966
"include applications that meet 'Asian' criteria" in {
70-
val lars = larCollection { lar =>
71-
val app = lar.applicant
72-
val withQualifyingRace = applicantRace3to5Blank(app.copy(race1 = 2, race2 = whiteOrBlank))
73-
val withCoApplicant = coApplicantNotWhite(withQualifyingRace)
74-
lar.copy(applicant = withCoApplicant)
67+
val lars = larCollectionWithApplicant { app =>
68+
val withQualifyingCoApp = coApplicantNotWhite(app)
69+
applicantRace3to5Blank(withQualifyingCoApp.copy(race1 = 2, race2 = whiteOrBlank))
7570
}
7671
val asianLars = filterRace(source(lars), Asian)
7772
count(asianLars).map(_ mustBe 100)
7873
}
79-
"exclude applications that do not meet 'Asian' criteria" in {
80-
val larsExcludedByApplicant = larCollection { lar =>
81-
val applicant = lar.applicant.copy(race1 = 2, race2 = "3")
82-
lar.copy(applicant = applicant)
83-
}
84-
val larsExcludedByCoApplicant = larCollection { lar =>
85-
val applicant = lar.applicant.copy(race1 = 2, coRace1 = 5)
86-
lar.copy(applicant = applicant)
87-
}
88-
val nonAsianLars1 = filterRace(source(larsExcludedByApplicant), Asian)
89-
val nonAsianLars2 = filterRace(source(larsExcludedByCoApplicant), Asian)
90-
count(nonAsianLars1).map(_ mustBe 0)
91-
count(nonAsianLars2).map(_ mustBe 0)
74+
"exclude lars where applicant does not meet criteria" in {
75+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 2, race2 = "3"))
76+
val nonAsianLars = filterRace(source(excludedLars), Asian)
77+
count(nonAsianLars).map(_ mustBe 0)
78+
}
79+
"exclude lars where coApplicant does not meet criteria" in {
80+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 2, coRace1 = 5))
81+
val nonAsianLars = filterRace(source(excludedLars), Asian)
82+
count(nonAsianLars).map(_ mustBe 0)
9283
}
9384
}
94-
/*
85+
9586
"'Black or African American' race filter" must {
9687
"include applications that meet 'Black or African American' criteria" in {
97-
val lars = larCollection { lar =>
88+
val lars = larCollectionWithApplicant { app =>
89+
val withQualifyingCoApp = coApplicantNotWhite(app)
90+
applicantRace3to5Blank(withQualifyingCoApp.copy(race1 = 3, race2 = whiteOrBlank))
9891
}
99-
val nativeLars = filterRace(source(lars), BlackOrAfricanAmerican)
100-
count(nativeLars).map(_ mustBe 100)
92+
val blackLars = filterRace(source(lars), BlackOrAfricanAmerican)
93+
count(blackLars).map(_ mustBe 100)
10194
}
102-
"exclude applications that do not meet 'Black or African American' criteria" in {
103-
val larsExcludedByApplicant = larCollection { lar =>
104-
val applicant = lar.applicant.copy(ethnicity = 2, coEthnicity = 3)
105-
lar.copy(applicant = applicant)
106-
}
107-
val larsExcludedByCoApplicant = larCollection { lar =>
108-
val applicant = lar.applicant.copy(ethnicity = 1, coEthnicity = 2)
109-
lar.copy(applicant = applicant)
110-
}
111-
val nonBlackLars = filterRace(source(lars), BlackOrAfricanAmerican)
95+
"exclude lars where applicant does not meet criteria" in {
96+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 3, race2 = "4"))
97+
val nonBlackLars = filterRace(source(excludedLars), BlackOrAfricanAmerican)
98+
count(nonBlackLars).map(_ mustBe 0)
99+
}
100+
"exclude lars where coApplicant does not meet criteria" in {
101+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 3, coRace1 = 5))
102+
val nonBlackLars = filterRace(source(excludedLars), BlackOrAfricanAmerican)
112103
count(nonBlackLars).map(_ mustBe 0)
113104
}
114105
}
106+
115107
"'Hawaiian or Pacific Islander' race filter" must {
116108
"include applications that meet 'Hawaiian or Pacific Islander' criteria" in {
117-
val lars = larCollection { lar =>
109+
val lars = larCollectionWithApplicant { app =>
110+
val withQualifyingCoApp = coApplicantNotWhite(app)
111+
applicantRace3to5Blank(withQualifyingCoApp.copy(race1 = 4, race2 = whiteOrBlank))
118112
}
119-
val hawaiianLars = filterRace(source(lars), HawaiianOrPacific)
120-
count(hawaiianLars).map(_ mustBe 100)
113+
val blackLars = filterRace(source(lars), HawaiianOrPacific)
114+
count(blackLars).map(_ mustBe 100)
121115
}
122-
"exclude applications that do not meet 'Hawaiian or Pacific Islander' criteria" in {
123-
val larsExcludedByApplicant = larCollection { lar =>
124-
val applicant = lar.applicant.copy(ethnicity = 2, coEthnicity = 3)
125-
lar.copy(applicant = applicant)
126-
}
127-
val larsExcludedByCoApplicant = larCollection { lar =>
128-
val applicant = lar.applicant.copy(ethnicity = 1, coEthnicity = 2)
129-
lar.copy(applicant = applicant)
130-
}
131-
val hawaiianLars = filterRace(source(lars), HawaiianOrPacific)
132-
count(hawaiianLars).map(_ mustBe 0)
116+
"exclude lars where applicant does not meet criteria" in {
117+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 4, race2 = "3"))
118+
val nonBlackLars = filterRace(source(excludedLars), HawaiianOrPacific)
119+
count(nonBlackLars).map(_ mustBe 0)
120+
}
121+
"exclude lars where coApplicant does not meet criteria" in {
122+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 4, coRace1 = 5))
123+
val nonBlackLars = filterRace(source(excludedLars), HawaiianOrPacific)
124+
count(nonBlackLars).map(_ mustBe 0)
133125
}
134126
}
127+
135128
"'White' race filter" must {
136129
"include applications that meet 'White' criteria" in {
137-
val lars = larCollection { lar =>
130+
def nonWhiteCoApp = Gen.oneOf(5, 6, 7, 8).sample.get
131+
val lars = larCollectionWithApplicant { app =>
132+
val whiteApp = app.copy(race1 = 5, race2 = "", race3 = "", race4 = "", race5 = "")
133+
whiteApp.copy(coRace1 = nonWhiteCoApp, coRace2 = "", coRace3 = "", coRace4 = "", coRace5 = "")
138134
}
139135
val whiteLars = filterRace(source(lars), White)
140136
count(whiteLars).map(_ mustBe 100)
141137
}
142-
"exclude applications that do not meet 'White' criteria" in {
143-
val larsExcludedByApplicant = larCollection { lar =>
144-
val applicant = lar.applicant.copy(ethnicity = 2, coEthnicity = 3)
145-
lar.copy(applicant = applicant)
146-
}
147-
val larsExcludedByCoApplicant = larCollection { lar =>
148-
val applicant = lar.applicant.copy(ethnicity = 1, coEthnicity = 2)
149-
lar.copy(applicant = applicant)
150-
}
151-
val nonWhiteLars = filterRace(source(lars), White)
138+
"exclude lars where applicant does not meet criteria" in {
139+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 5, race2 = "3"))
140+
val nonWhiteLars = filterRace(source(excludedLars), White)
141+
count(nonWhiteLars).map(_ mustBe 0)
142+
}
143+
"exclude lars where coApplicant does not meet criteria" in {
144+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 5, coRace1 = 3))
145+
val nonWhiteLars = filterRace(source(excludedLars), White)
152146
count(nonWhiteLars).map(_ mustBe 0)
153147
}
154148
}
149+
155150
"'Not Provided' race filter" must {
156151
"include applications that meet 'Not Provided' criteria" in {
157-
val lars = larCollection { lar =>
158-
}
152+
def notProvided = Gen.oneOf(6, 7).sample.get
153+
val lars = larCollectionWithApplicant(_.copy(race1 = notProvided))
159154
val notProvidedLars = filterRace(source(lars), NotProvided)
160155
count(notProvidedLars).map(_ mustBe 100)
161156
}
162157
"exclude applications that do not meet 'Not Provided' criteria" in {
163-
val larsExcludedByApplicant = larCollection { lar =>
164-
val applicant = lar.applicant.copy(ethnicity = 2, coEthnicity = 3)
165-
lar.copy(applicant = applicant)
166-
}
167-
val larsExcludedByCoApplicant = larCollection { lar =>
168-
val applicant = lar.applicant.copy(ethnicity = 1, coEthnicity = 2)
169-
lar.copy(applicant = applicant)
170-
}
171-
val otherLars = filterRace(source(lars), NotProvided)
158+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 4))
159+
val otherLars = filterRace(source(excludedLars), NotProvided)
172160
count(otherLars).map(_ mustBe 0)
173161
}
174162
}
163+
175164
"'Two Or More Minority' race filter" must {
165+
def minority = Gen.oneOf(1, 2, 3, 4).sample.get
166+
176167
"include applications that meet 'Two Or More Minority' criteria" in {
177-
val lars = larCollection { lar =>
168+
val lars = larCollectionWithApplicant { app =>
169+
val withQualifyingCoApp = coApplicantNotWhite(app)
170+
withQualifyingCoApp.copy(race1 = minority, race2 = minority.toString, race3 = "", race4 = "", race5 = "")
178171
}
179172
val multiMinorityLars = filterRace(source(lars), TwoOrMoreMinority)
180173
count(multiMinorityLars).map(_ mustBe 100)
181174
}
182-
"exclude applications that do not meet 'Two Or More Minority' criteria" in {
183-
val larsExcludedByApplicant = larCollection { lar =>
184-
val applicant = lar.applicant.copy(ethnicity = 2, coEthnicity = 3)
185-
lar.copy(applicant = applicant)
186-
}
187-
val larsExcludedByCoApplicant = larCollection { lar =>
188-
val applicant = lar.applicant.copy(ethnicity = 1, coEthnicity = 2)
189-
lar.copy(applicant = applicant)
190-
}
191-
val nonMultiMinority = filterRace(source(lars), TwoOrMoreMinority)
192-
count(nonMultiMinority).map(_ mustBe 0)
175+
"exclude lars where applicant does not meet criteria" in {
176+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 2, race2 = "5"))
177+
val otherLars = filterRace(source(excludedLars), TwoOrMoreMinority)
178+
count(otherLars).map(_ mustBe 0)
179+
}
180+
"exclude lars where coApplicant does not meet criteria" in {
181+
val excludedLars = larCollectionWithApplicant(_.copy(race1 = 1, race2 = "2", coRace1 = 5))
182+
val otherLars = filterRace(source(excludedLars), TwoOrMoreMinority)
183+
count(otherLars).map(_ mustBe 0)
193184
}
194185
}
186+
/*
195187
"'Joint' race filter" must {
196188
"include applications that meet 'Joint' criteria" in {
197189
val lars = larCollection { lar =>

0 commit comments

Comments
 (0)