Skip to content

Instantly share code, notes, and snippets.

@sgrimm
Last active December 8, 2023 16:46
Show Gist options
  • Save sgrimm/e242f6534d7670b049f5e9e47e72e768 to your computer and use it in GitHub Desktop.
Save sgrimm/e242f6534d7670b049f5e9e47e72e768 to your computer and use it in GitHub Desktop.
Two attempts to use IntelliJ's AI assistant to write a test case
// Attempt 1: Just the function name as the prompt. Totally wrong approach, plus neither of
// the reportStore methods exists, plus reportStore is a dependency anyway, not
// the class under test.
//
// fun `deletes unsubmitted project-level reports when project is deleted`() {
// val projectReports = reportStore.findReportsByProjectId(projectId)
// projectReports.forEach { report ->
// if (!report.isSubmitted) {
// reportStore.deleteReport(report.reportId)
// }
// }
// }
// Attempt 2: Write a description of the desired implementation as a comment. This gets closer,
// but it tries to use self-generated UUIDs rather than letting the insert helper
// functions return database-generated IDs of the correct types. The assertions don't
// have descriptions to distinguish them. Also, reportStore.fetchOneById() throws an
// exception if the report doesn't exist, rather than returning null.
//
// fun `deletes unsubmitted project-level reports when project is deleted`() {
// // Insert a project that will be deleted and another project that will be kept. Insert an
// // organization-level report and a project-level report for each project, all in "New" state.
// // Create a ProjectDeletionStartedEvent and pass it to the service's "on" method. Verify
// // that only the project-level report for the deleted project was deleted.
//
// val deletedProjectId = UUID.randomUUID()
// val keptProjectId = UUID.randomUUID()
// val organizationId = UUID.randomUUID()
// val organizationReportId = insertReport(organizationId = organizationId)
// val deletedProjectReportId = insertReport(projectId = deletedProjectId)
// val keptProjectReportId = insertReport(projectId = keptProjectId)
//
// val event = ProjectDeletionStartedEvent(deletedProjectId)
// service.on(event)
//
// val deletedProjectReport = reportStore.fetchOneById(deletedProjectReportId)
// val keptProjectReport = reportStore.fetchOneById(keptProjectReportId)
// val organizationReport = reportStore.fetchOneById(organizationReportId)
//
// assertNull(deletedProjectReport)
// assertNotNull(keptProjectReport)
// assertNotNull(organizationReport)
// }
// Finally: write it by hand. This took only slightly longer than writing the description in
// attempt 2.
@Test
fun `deletes unsubmitted project-level reports when project is deleted`() {
val deletedProjectId = insertProject()
val keptProjectId = insertProject()
val orgLevelReportId = insertReport()
val keptProjectReportId = insertReport(projectId = keptProjectId)
val deletedProjectReportId = insertReport(projectId = deletedProjectId)
service.on(ProjectDeletionStartedEvent(deletedProjectId))
assertFalse(
reportsDao.existsById(deletedProjectReportId),
"Should have deleted report for deleted project")
assertTrue(
reportsDao.existsById(keptProjectReportId),
"Should not have deleted report for non-deleted project")
assertTrue(
reportsDao.existsById(orgLevelReportId), "Should not have deleted org-level report")
}
@sgrimm
Copy link
Author

sgrimm commented Dec 7, 2023

Seeing the AI-generated code did end up influencing my handwritten code. I probably wouldn't have called the variable keptProjectId if I'd written the test from the get-go, but that name is no worse than what I probably would have come up with, so I didn't feel the need to pick a different one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment