-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Test for DB queries #127
Test for DB queries #127
Changes from all commits
7ecaa53
5a7788d
f627c7d
7f275d7
82e0348
7422e9a
78a3b99
4ffc9e1
65803d2
7e23559
ec8c22a
15e9423
6d995c9
ed31a9f
bcb32a8
7781cb8
448c373
db25f12
cd456dd
48d079f
940ac00
c334d60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,16 +104,21 @@ public static Set<Contest> forCounty(final County the_county) { | |
/** | ||
* Deletes all the contests for the county with the specified ID. | ||
* | ||
* @param the_id The county ID. | ||
* @param the_county_id The county ID. | ||
* @return the number of contests that were deleted, or -1 if an error occured. | ||
*/ | ||
public static void deleteForCounty(final Long the_county_id) { | ||
public static int deleteForCounty(final Long the_county_id) { | ||
final Set<Contest> contests = | ||
forCounty(Persistence.getByID(the_county_id, County.class)); | ||
|
||
int retval = -1; | ||
if (contests != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I really like the idea of going through everything and changing it from something/null with null checks to Optional(something or nothing) with .ispresent checks. I'm just a little unsure about this particular case - are you sure you changed forCounty so that it can never return null? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You are completely right, it definitely can. I even have a test for it higher up! 🤦 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have now put this back, and also changed the function to return a count of how many contests it deletes so transient DB errors can be detected. |
||
retval = contests.size(); | ||
for (final Contest c : contests) { | ||
Persistence.delete(c); | ||
} | ||
} | ||
Persistence.flush(); | ||
return retval; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package us.freeandfair.corla.query; | ||
|
||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testng.annotations.*; | ||
import us.freeandfair.corla.model.Administrator; | ||
import us.freeandfair.corla.model.County; | ||
import us.freeandfair.corla.persistence.Persistence; | ||
import us.freeandfair.corla.util.TestClassWithDatabase; | ||
|
||
import java.util.Properties; | ||
|
||
import static org.testng.AssertJUnit.assertEquals; | ||
import static org.testng.AssertJUnit.assertNull; | ||
|
||
@Test | ||
public class AdministratorQueriesTest extends TestClassWithDatabase { | ||
|
||
@Test | ||
public void testByUsernameState() { | ||
|
||
Administrator admin = new Administrator("testname", | ||
Administrator.AdministratorType.STATE, | ||
"fulltestname", | ||
null); | ||
|
||
Persistence.saveOrUpdate(admin); | ||
assertEquals(AdministratorQueries.byUsername("testname"), admin); | ||
} | ||
|
||
|
||
@Test | ||
public void testByUsernameCounty() { | ||
|
||
County county = new County("test", 1L); | ||
Persistence.saveOrUpdate(county); | ||
Administrator admin = new Administrator("testname", | ||
Administrator.AdministratorType.COUNTY, | ||
"fulltestname", | ||
county | ||
); | ||
|
||
Persistence.saveOrUpdate(admin); | ||
assertEquals(AdministratorQueries.byUsername("testname"), admin); | ||
} | ||
|
||
@Test | ||
public void testByUsernameStateAndCounty() { | ||
County county = new County("test", 1L); | ||
Persistence.saveOrUpdate(county); | ||
Administrator countyadmin = new Administrator("county", | ||
Administrator.AdministratorType.COUNTY, | ||
"countyfull", | ||
county); | ||
|
||
Persistence.saveOrUpdate(countyadmin); | ||
|
||
Administrator stateadmin = new Administrator("state", | ||
Administrator.AdministratorType.STATE, | ||
"statefull", | ||
null); | ||
|
||
Persistence.saveOrUpdate(stateadmin); | ||
|
||
assertEquals(AdministratorQueries.byUsername("county"),countyadmin); | ||
assertEquals(AdministratorQueries.byUsername("state"), stateadmin); | ||
} | ||
|
||
@Test | ||
public void testSameName() { | ||
|
||
Administrator first = new Administrator("state", | ||
Administrator.AdministratorType.STATE, | ||
"first", | ||
null); | ||
|
||
Administrator second = new Administrator("state", | ||
Administrator.AdministratorType.STATE, | ||
"second", | ||
null); | ||
Persistence.saveOrUpdate(first); | ||
Persistence.saveOrUpdate(second); | ||
|
||
assertNull(AdministratorQueries.byUsername("state")); | ||
} | ||
|
||
@Test | ||
public void testDBError() { | ||
// Close the DB | ||
Persistence.commitTransaction(); | ||
assertNull(AdministratorQueries.byUsername("username")); | ||
Persistence.beginTransaction(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think e is still final.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ope, I missed it. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.