12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- import XCTest
- import RealmSwift
- private var expectedTotalBytesBefore = 0
- private let expectedUsedBytesBeforeMin = 50000
- private var count = 1000
- private func fileSize(path: String) -> Int {
- let attributes = try! FileManager.default.attributesOfItem(atPath: path)
- return attributes[.size] as! Int
- }
- class CompactionTests: TestCase {
- override func setUp() {
- super.setUp()
- autoreleasepool {
-
- let realm = realmWithTestPath()
- let uuid = UUID().uuidString
- try! realm.write {
- realm.create(SwiftStringObject.self, value: ["A"])
- for _ in 0..<count {
- realm.create(SwiftStringObject.self, value: [uuid])
- }
- realm.create(SwiftStringObject.self, value: ["B"])
- }
- }
- expectedTotalBytesBefore = fileSize(path: testRealmURL().path)
- }
- func testSuccessfulCompactOnLaunch() {
-
- let config = Realm.Configuration(fileURL: testRealmURL(),
- shouldCompactOnLaunch: { totalBytes, usedBytes in
-
- XCTAssertEqual(totalBytes, expectedTotalBytesBefore)
- XCTAssert((usedBytes < totalBytes) && (usedBytes > expectedUsedBytesBeforeMin))
-
-
- let fiveHundredKB = 500 * 1024
- return (totalBytes > fiveHundredKB) && (Double(usedBytes) / Double(totalBytes)) < 0.2
- })
-
- XCTAssertEqual(fileSize(path: config.fileURL!.path), expectedTotalBytesBefore)
- let realm = try! Realm(configuration: config)
- XCTAssertLessThan(fileSize(path: config.fileURL!.path), expectedTotalBytesBefore)
-
- XCTAssertEqual(realm.objects(SwiftStringObject.self).count, count + 2)
- XCTAssertEqual("A", realm.objects(SwiftStringObject.self).first?.stringCol)
- XCTAssertEqual("B", realm.objects(SwiftStringObject.self).last?.stringCol)
- }
- }
|