EntityDestroyerTests.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2019-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. import XCTest
  21. import CoreData
  22. class EntityDestroyerTests: XCTestCase {
  23. var objCnx: NSManagedObjectContext!
  24. override func setUp() {
  25. super.setUp()
  26. self.objCnx = TestDatabasePersistentManager.devNullContext()
  27. // Setup DB for testing, insert 10 video messages
  28. let testEntityManager = TestDatabaseEntityManager(context: self.objCnx)
  29. testEntityManager.save {
  30. let conversation = testEntityManager.createConversation()
  31. let thumbnail = testEntityManager.createImageData()
  32. let userCalendar = Calendar.current
  33. let toDate = Date()
  34. for index in 1...10 {
  35. var addDate = DateComponents()
  36. addDate.day = index * -1
  37. addDate.hour = 1
  38. let date = userCalendar.date(byAdding: addDate, to: toDate)
  39. testEntityManager.createVideoMessage(conversation: conversation, thumbnail: thumbnail, videoData: testEntityManager.createVideoData(), date: date)
  40. }
  41. }
  42. // necessary for ValidationLogger
  43. AppGroup.setGroupId("group.ch.threema") //THREEMA_GROUP_IDENTIFIER @"group.ch.threema"
  44. }
  45. override func tearDown() {
  46. // Put teardown code here. This method is called after the invocation of each test method in the class.
  47. }
  48. func testDeleteMedias() {
  49. let deleteTests = [
  50. // days diff, expected count of deleted media
  51. [-2, 8],
  52. [nil, 2]
  53. ]
  54. let userCalendar = Calendar.current
  55. for deleteTest in deleteTests {
  56. var olderThan: Date? = nil
  57. if let daysAdd = deleteTest[0] {
  58. olderThan = userCalendar.date(byAdding: .day, value: daysAdd, to: Date())
  59. }
  60. let ed = EntityDestroyer(managedObjectContext: self.objCnx)
  61. let count = ed.deleteMedias(olderThan: olderThan)
  62. XCTAssertEqual(count, deleteTest[1]!, "not expected count of deleted medias")
  63. }
  64. }
  65. func testDeleteMessages() {
  66. let deleteTests = [
  67. // days diff, expected count of deleted media
  68. [-2, 8],
  69. [nil, 2]
  70. ]
  71. let userCalendar = Calendar.current
  72. for deleteTest in deleteTests {
  73. var olderThan: Date? = nil
  74. if let daysAdd = deleteTest[0] {
  75. olderThan = userCalendar.date(byAdding: .day, value: daysAdd, to: Date())
  76. }
  77. let ed = EntityDestroyer(managedObjectContext: self.objCnx)
  78. let count = ed.deleteMessages(olderThan: olderThan)
  79. XCTAssertEqual(count, deleteTest[1]!, "not expected count of deleted messages")
  80. }
  81. }
  82. class TestDatabasePersistentManager {
  83. /**
  84. Context in memory, doesn't work with NSBatch... commands (use devNullContext)
  85. - Returns:
  86. DB context for testing
  87. */
  88. static func inMemoryContext() -> NSManagedObjectContext {
  89. let modelURL = BundleUtil.url(forResource:"ThreemaData", withExtension: "momd")
  90. let managedObjectContext = NSManagedObjectModel(contentsOf: modelURL!)
  91. let persistentStoreCoordinator = NSPersistentStoreCoordinator(managedObjectModel: managedObjectContext!)
  92. do {
  93. try persistentStoreCoordinator.addPersistentStore(ofType: NSInMemoryStoreType, configurationName: nil, at: nil, options: nil)
  94. }
  95. catch {
  96. fatalError("Adding in memory persistent store failed")
  97. }
  98. let context = NSManagedObjectContext(concurrencyType: NSManagedObjectContextConcurrencyType.mainQueueConcurrencyType)
  99. context.persistentStoreCoordinator = persistentStoreCoordinator
  100. return context
  101. }
  102. /**
  103. Context stored data to /dev/null, works with NSBatch... commands
  104. - Returns:
  105. DB context for testing
  106. */
  107. static func devNullContext() -> NSManagedObjectContext {
  108. let modelURL = BundleUtil.url(forResource:"ThreemaData", withExtension: "momd")
  109. let managedObjectModel = NSManagedObjectModel(contentsOf: modelURL!)
  110. let container = NSPersistentContainer(name: "TestData", managedObjectModel: managedObjectModel!)
  111. container.persistentStoreDescriptions[0].url = URL(fileURLWithPath: "/dev/null")
  112. container.loadPersistentStores { (description, error) in
  113. XCTAssertNil(error)
  114. }
  115. return container.viewContext
  116. }
  117. }
  118. class TestDatabaseEntityManager {
  119. private let objCnx: NSManagedObjectContext
  120. required init(context: NSManagedObjectContext) {
  121. self.objCnx = context
  122. }
  123. /**
  124. Save data modifications on DB.
  125. - Parameters:
  126. - dbModificationAction: Closure with data modifications
  127. */
  128. func save(dbModificationAction: () -> Void) {
  129. do {
  130. dbModificationAction()
  131. try self.objCnx.save()
  132. }
  133. catch {
  134. print(error)
  135. XCTFail("Could not generate test data.")
  136. }
  137. }
  138. func createConversation() -> Conversation {
  139. let conversation = createEntity(objectType: Conversation.self)
  140. conversation.marked = false
  141. conversation.typing = false
  142. conversation.unreadMessageCount = 0
  143. return conversation
  144. }
  145. func createImageData() -> ImageData {
  146. let imageData = createEntity(objectType: ImageData.self)
  147. imageData.data = Data([22])
  148. imageData.height = 22
  149. imageData.width = 22
  150. return imageData
  151. }
  152. func createVideoData() -> VideoData {
  153. let videoData = createEntity(objectType: VideoData.self)
  154. videoData.data = Data([1])
  155. return videoData
  156. }
  157. func createVideoMessage(conversation: Conversation, thumbnail: ImageData, videoData: VideoData?, date: Date?) -> VideoMessage {
  158. let videoMessage = createEntity(objectType: VideoMessage.self)
  159. videoMessage.date = date
  160. videoMessage.delivered = 1
  161. videoMessage.id = Data([11])
  162. videoMessage.isOwn = true
  163. videoMessage.read = true
  164. videoMessage.sent = true
  165. videoMessage.userack = false
  166. videoMessage.conversation = conversation
  167. videoMessage.thumbnail = thumbnail
  168. videoMessage.duration = 10
  169. videoMessage.video = videoData
  170. videoMessage.remoteSentDate = Date()
  171. return videoMessage
  172. }
  173. private func createEntity<T: NSManagedObject>(objectType: T.Type) -> T {
  174. var entityName: String
  175. if objectType is Conversation.Type {
  176. entityName = "Conversation"
  177. }
  178. else if objectType is ImageData.Type {
  179. entityName = "ImageData"
  180. }
  181. else if objectType is VideoData.Type {
  182. entityName = "VideoData"
  183. }
  184. else if objectType is VideoMessage.Type {
  185. entityName = "VideoMessage"
  186. }
  187. else {
  188. fatalError("objects type not defined")
  189. }
  190. return NSEntityDescription.insertNewObject(forEntityName: entityName, into: self.objCnx) as! T
  191. }
  192. }
  193. }