Skip to content

Commit

Permalink
2557 [BE] delete autotest contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
andrsam committed Jul 25, 2024
1 parent 55c4f9c commit 1d208fa
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/epam/brn/model/StudyHistory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ data class StudyHistory(
val id: Long? = null,

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
@JoinColumn(name = "user_id", insertable = true, updatable = false)
var userAccount: UserAccount,

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -44,7 +44,7 @@ data class StudyHistory(

) {
override fun toString() =
"StudyHistory(id=$id, userAccount=$userAccount, exercise=$exercise, startTime=$startTime, endTime=$endTime, spentTime=$spentTimeInSeconds, tasksCount=$tasksCount, wrongAnswers=$wrongAnswers)"
"StudyHistory(id=$id, userAccount=$userAccount, startTime=$startTime, endTime=$endTime, spentTime=$spentTimeInSeconds, tasksCount=$tasksCount, wrongAnswers=$wrongAnswers)"

fun toDto() = StudyHistoryDto(
id = this.id,
Expand Down
241 changes: 118 additions & 123 deletions src/test/kotlin/com/epam/brn/integration/StudyHistoryIT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ import com.epam.brn.enums.BrnGender
import com.epam.brn.enums.BrnRole
import com.epam.brn.model.Exercise
import com.epam.brn.model.ExerciseGroup
import com.epam.brn.model.Role
import com.epam.brn.model.Series
import com.epam.brn.model.StudyHistory
import com.epam.brn.model.SubGroup
import com.epam.brn.model.UserAccount
import com.epam.brn.repo.ExerciseGroupRepository
import com.epam.brn.repo.ExerciseRepository
import com.epam.brn.repo.RoleRepository
import com.epam.brn.repo.SeriesRepository
import com.epam.brn.repo.StudyHistoryRepository
import com.epam.brn.repo.SubGroupRepository
Expand All @@ -20,7 +18,6 @@ import com.epam.brn.service.UserAccountService
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.MediaType
import org.springframework.security.test.context.support.WithMockUser
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders
Expand All @@ -40,9 +37,6 @@ class StudyHistoryIT : BaseIT() {

private val baseUrl = "/study-history"

@Value("\${users.delete.prefix}")
private lateinit var prefix: String

@Autowired
lateinit var userAccountRepository: UserAccountRepository

Expand All @@ -61,8 +55,8 @@ class StudyHistoryIT : BaseIT() {
@Autowired
lateinit var exerciseGroupRepository: ExerciseGroupRepository

@Autowired
lateinit var roleRepository: RoleRepository
// @Autowired
// lateinit var roleRepository: RoleRepository

@Autowired
lateinit var userAccountService: UserAccountService
Expand All @@ -75,6 +69,7 @@ class StudyHistoryIT : BaseIT() {
seriesRepository.deleteAll()
exerciseGroupRepository.deleteAll()
userAccountRepository.deleteAll()
// roleRepository.deleteAll()
}

@Test
Expand Down Expand Up @@ -239,114 +234,114 @@ class StudyHistoryIT : BaseIT() {
assertEquals(0, result)
}

@Test
fun `test delete study history when delete autotest users`() {
// GIVEN
val roleUser = insertRole(BrnRole.USER)

val user1 = UserAccount(
fullName = "autotest_n1",
email = "autotest_n@1704819771.8820736.com",
gender = BrnGender.MALE.toString(),
bornYear = 2000,
active = true,
)
user1.roleSet = mutableSetOf(roleUser)

val user2 = UserAccount(
fullName = "autotest_n1",
email = "autotest_n@170472339.1784415.com",
gender = BrnGender.MALE.toString(),
bornYear = 2000,
active = true,
)
user2.roleSet = mutableSetOf(roleUser)

userAccountRepository.saveAll(listOf(user1, user2))

val existingSeries = insertSeries()
val subGroup = insertSubGroup(existingSeries)
val existingExerciseFirst = insertExercise("FirstName", subGroup)
val existingExerciseSecond = insertExercise("SecondName", subGroup)
val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
val historyFirstExerciseOne = insertStudyHistory(user1, existingExerciseFirst, now)
val historySecondExerciseOne = insertStudyHistory(user2, existingExerciseSecond, now)

studyHistoryRepository
.saveAll(
listOf(
historyFirstExerciseOne,
historySecondExerciseOne,
)
)

// WHEN
userAccountService.deleteAutoTestUsers()

val result1 = user1.id?.let {
studyHistoryRepository.findLastByUserAccountIdAndExercises(
it,
listOf(existingExerciseFirst.id!!)
)
}

val result2 = user1.id?.let {
studyHistoryRepository.findLastByUserAccountIdAndExercises(
it,
listOf(existingExerciseFirst.id!!)
)
}

// THEN
assertEquals(0, result1?.size)
assertEquals(0, result2?.size)
}

@Test
fun `test delete study history when delete single autotest user`() {
// GIVEN
val roleUser = insertRole(BrnRole.USER)

val user1 = UserAccount(
fullName = "autotest_n1",
email = "autotest_n@1704819771.8820736.com",
gender = BrnGender.MALE.toString(),
bornYear = 2000,
active = true,
)
user1.roleSet = mutableSetOf(roleUser)
userAccountRepository.save(user1)

val exerciseFirstName = "FirstName"
val existingSeries = insertSeries()
val subGroup = insertSubGroup(existingSeries)
val existingExerciseFirst = insertExercise(exerciseFirstName, subGroup)
val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
val historyFirstExerciseTwo = insertStudyHistory(user1, existingExerciseFirst, now)

studyHistoryRepository.save(historyFirstExerciseTwo)

// WHEN
userAccountService.deleteAutoTestUsers()

val result1 = user1.id?.let {
studyHistoryRepository.findLastByUserAccountIdAndExercises(
it,
listOf(existingExerciseFirst.id!!)
)
}

val result2 = user1.id?.let {
studyHistoryRepository.findLastByUserAccountIdAndExercises(
it,
listOf(existingExerciseFirst.id!!)
)
}

// THEN
assertEquals(0, result1?.size)
assertEquals(0, result2?.size)
}
// @Test
// fun `test delete study history when delete autotest users`() {
// // GIVEN
// val roleUser = insertRole(BrnRole.USER)
//
// val user1 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@1704819771.8820736.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user1.roleSet = mutableSetOf(roleUser)
//
// val user2 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@170472339.1784415.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user2.roleSet = mutableSetOf(roleUser)
//
// userAccountRepository.saveAll(listOf(user1, user2))
//
// val existingSeries = insertSeries()
// val subGroup = insertSubGroup(existingSeries)
// val existingExerciseFirst = insertExercise("FirstName", subGroup)
// val existingExerciseSecond = insertExercise("SecondName", subGroup)
// val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
// val historyFirstExerciseOne = insertStudyHistory(user1, existingExerciseFirst, now)
// val historySecondExerciseOne = insertStudyHistory(user2, existingExerciseSecond, now)
//
// studyHistoryRepository
// .saveAll(
// listOf(
// historyFirstExerciseOne,
// historySecondExerciseOne,
// )
// )
//
// // WHEN
// userAccountService.deleteAutoTestUsers()
//
// val result1 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// val result2 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// // THEN
// assertEquals(0, result1?.size)
// assertEquals(0, result2?.size)
// }
//
// @Test
// fun `test delete study history when delete single autotest user`() {
// // GIVEN
// val roleUser = insertRole(BrnRole.USER)
//
// val user1 = UserAccount(
// fullName = "autotest_n1",
// email = "autotest_n@1704819771.8820736.com",
// gender = BrnGender.MALE.toString(),
// bornYear = 2000,
// active = true,
// )
// user1.roleSet = mutableSetOf(roleUser)
// userAccountRepository.save(user1)
//
// val exerciseFirstName = "FirstName"
// val existingSeries = insertSeries()
// val subGroup = insertSubGroup(existingSeries)
// val existingExerciseFirst = insertExercise(exerciseFirstName, subGroup)
// val now = LocalDateTime.now().truncatedTo(ChronoUnit.DAYS)
// val historyFirstExerciseTwo = insertStudyHistory(user1, existingExerciseFirst, now)
//
// studyHistoryRepository.save(historyFirstExerciseTwo)
//
// // WHEN
// userAccountService.deleteAutoTestUsers()
//
// val result1 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// val result2 = user1.id?.let {
// studyHistoryRepository.findLastByUserAccountIdAndExercises(
// it,
// listOf(existingExerciseFirst.id!!)
// )
// }
//
// // THEN
// assertEquals(0, result1?.size)
// assertEquals(0, result2?.size)
// }

private fun insertStudyHistory(
existingUser: UserAccount,
Expand Down Expand Up @@ -412,11 +407,11 @@ class StudyHistoryIT : BaseIT() {
)
}

private fun insertRole(roleName: String): Role {
return roleRepository.save(
Role(
name = roleName
)
)
}
// private fun insertRole(roleName: String): Role {
// return roleRepository.save(
// Role(
// name = roleName
// )
// )
// }
}

0 comments on commit 1d208fa

Please sign in to comment.