<commit_before>//
//  RadonTests.swift
//  RadonTests
//
//  Created by mhaddl on 06/05/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import XCTest
import CloudKit
@testable import Radon_iOS

class RadonTests: XCTestCase {
    
    var mockInterface: MockCloudKitInterface!
    var radon: Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>!
    var store: ExampleRadonStore!
    
    override func setUp() {
        store = ExampleRadonStore()
        mockInterface = MockCloudKitInterface()
        radon = Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>(store: store, interface: mockInterface, recordZoneErrorBlock: nil)
        radon.defaultsStoreable = MockDefaultsStoreable()
    }
    
    func testDateExtensionEalier() {
        let date1 = Date(timeIntervalSince1970: 0)
        let date2 = Date()
        
        XCTAssert(date1.isEarlierThan(date2))
        XCTAssert(!date2.isEarlierThan(date1))
        XCTAssert(!date1.isEarlierThan(date1))
    }
    
    func testDateExtensionNil() {
        let date1: Date? = nil
        let date2 = Date()
        
        XCTAssert(!date2.isEarlierThan(date1))
    }
    
    func testUserDefaultsExtension() {
        let userDefaults = UserDefaults()
        let key = "key"
        let testString = "1234Test"
        userDefaults.saveObject(testString, forKey: key)
        XCTAssert(testString == userDefaults.loadObjectForKey(key) as? String)
    }
    
    func testFullInit() {
        
        mockInterface.failsSaveRecordZone = true
        _ = Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>(store: ExampleRadonStore(), interface: mockInterface) { (error) in
            XCTAssert(error != nil)
        }
        
        XCTAssert(true)
    }
    
    func testCreateObjectSuccess() {
        radon.createObject({ (newObject) -> TestClass in
            newObject.string = "MockString"
            newObject.int = 12345
            newObject.double = 2
            return newObject
        }) { (error) in
            if let object = self.store.objectWithIdentifier("Mock") {
                XCTAssert(object.string == "MockString")
            } else {
                XCTFail()
            }
        }
    }
    
    func testCreateObjectFail() {
        mockInterface.failsCreateRecord = true
        
        radon.createObject({ (newObject) -> TestClass in
            newObject.string = "MockString"
            newObject.int = 12345
            newObject.double = 2
            return newObject
        }) { (error) in
            if let _ = self.store.objectWithIdentifier("Mock") {
                XCTFail()
            } else {
                XCTAssert(error != nil)
            }
        }
    }
    
    func testUpdateObjectSuccess() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        store.setRecordName("Mock", forObject: testObject)
        XCTAssert(testObject.internSyncStatus == false)
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error == nil)
            XCTAssert(testObject.internSyncStatus == true)
            expectation.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
        
    }
    
    func testUpdateObjectError() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        XCTAssert(testObject.internSyncStatus == false)
        //Fail because object could not be found in the store by its record name
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation.fulfill()
        }
        
        store.setRecordName("Mock", forObject: testObject)
        mockInterface.failsModifyRecord = true
        //Fail because modify failed
        let expectation2 = self.expectation(description: "Update Object fail 2")
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation2.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
        
        let expectation3 = self.expectation(description: "Update Object fail 3")
        mockInterface.failsFetchRecord = true
        //Fail because fetch failed
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation3.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    func testUpdateObjectForNotYetSyncedObject() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        store.setRecordName(nil, forObject: testObject)
        XCTAssert(testObject.internSyncStatus == false)
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    
    func testDeleteRecordSuccess() {
        let expectation = self.expectation(description: "Delete Object")
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        radon.deleteObject(testObject) { (error) in
            XCTAssert(error == nil)
            XCTAssert(self.store.objectWithIdentifier(testObject.internRecordID) == nil)
            expectation.fulfill()
        }
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    func testHandleQueryNotificationReasonUpdated() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        let recordID = CKRecordID(recordName: "123")
        radon.handleQueryNotificationReason(.recordUpdated, forRecordID: recordID)
        XCTAssert(testObject.string == "Mock")
    }
    
    func testHandleQueryNotificationReasonUpdatedRecordNotFound() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        let recordID = CKRecordID(recordName: "123")
        mockInterface.failsFetchRecord = true
        radon.handleQueryNotificationReason(.recordUpdated, forRecordID: recordID)
        XCTAssert(testObject.string == "hi")
    }
    
    func testHandleQueryNotificationReasonDelete() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        let recordID = CKRecordID(recordName: "123")
        radon.handleQueryNotificationReason(.recordDeleted, forRecordID: recordID)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) == nil)
    }
    
    func testHandleQueryNotificationReasonRecordCreated() {
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        let recordID = CKRecordID(recordName: "Mock")
        radon.handleQueryNotificationReason(.recordCreated, forRecordID: recordID)
        XCTAssert(store.objectWithIdentifier("Mock") != nil)
    }
    
    func testHandleQueryNotificationReasonRecordCreatedRecordNotFound() {
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        mockInterface.failsFetchRecord = true
        let recordID = CKRecordID(recordName: "Mock")
        radon.handleQueryNotificationReason(.recordCreated, forRecordID: recordID)
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
    }
    
    func testSyncWithNewObjectToFetch() {
        let expectation = self.expectation(description: "New object from sync")
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        mockInterface.syncRecordChangeHasNewObject = true
        radon.sync({ (error) in
            
        }) { (error) in<commit_msg>Update tests<commit_after>//
//  RadonTests.swift
//  RadonTests
//
//  Created by mhaddl on 06/05/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import XCTest
import CloudKit
@testable import Radon_iOS

class RadonTests: XCTestCase {
    
    var mockInterface: MockCloudKitInterface!
    var radon: Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>!
    var store: ExampleRadonStore!
    
    override func setUp() {
        store = ExampleRadonStore()
        mockInterface = MockCloudKitInterface()
        radon = Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>(store: store, interface: mockInterface, recordZoneErrorBlock: nil)
        radon.defaultsStoreable = MockDefaultsStoreable()
    }
    
    func testDateExtensionEalier() {
        let date1 = Date(timeIntervalSince1970: 0)
        let date2 = Date()
        
        XCTAssert(date1.isEarlierThan(date2))
        XCTAssert(!date2.isEarlierThan(date1))
        XCTAssert(!date1.isEarlierThan(date1))
    }
    
    func testDateExtensionNil() {
        let date1: Date? = nil
        let date2 = Date()
        
        XCTAssert(!date2.isEarlierThan(date1))
    }
    
    func testUserDefaultsExtension() {
        let userDefaults = UserDefaults()
        let key = "key"
        let testString = "<PASSWORD>"
        userDefaults.saveObject(testString, forKey: key)
        XCTAssert(testString == userDefaults.loadObjectForKey(key) as? String)
    }
    
    func testFullInit() {
        
        mockInterface.failsSaveRecordZone = true
        _ = Radon<ExampleRadonStore, TestClass, MockCloudKitInterface>(store: ExampleRadonStore(), interface: mockInterface) { (error) in
            XCTAssert(error != nil)
        }
        
        XCTAssert(true)
    }
    
    func testCreateObjectSuccess() {
        radon.createObject({ (newObject) -> TestClass in
            newObject.string = "MockString"
            newObject.int = 12345
            newObject.double = 2
            return newObject
        }) { (error) in
            if let object = self.store.objectWithIdentifier("Mock") {
                XCTAssert(object.string == "MockString")
            } else {
                XCTFail()
            }
        }
    }
    
    func testCreateObjectFail() {
        mockInterface.failsCreateRecord = true
        
        radon.createObject({ (newObject) -> TestClass in
            newObject.string = "MockString"
            newObject.int = 12345
            newObject.double = 2
            return newObject
        }) { (error) in
            if let _ = self.store.objectWithIdentifier("Mock") {
                XCTFail()
            } else {
                XCTAssert(error != nil)
            }
        }
    }
    
    func testUpdateObjectSuccess() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        store.setRecordName("Mock", forObject: testObject)
        XCTAssert(testObject.internSyncStatus == false)
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error == nil)
            XCTAssert(testObject.internSyncStatus == true)
            expectation.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
        
    }
    
    func testUpdateObjectError() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        XCTAssert(testObject.internSyncStatus == false)
        //Fail because object could not be found in the store by its record name
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation.fulfill()
        }
        
        store.setRecordName("Mock", forObject: testObject)
        mockInterface.failsModifyRecord = true
        //Fail because modify failed
        let expectation2 = self.expectation(description: "Update Object fail 2")
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation2.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
        
        let expectation3 = self.expectation(description: "Update Object fail 3")
        mockInterface.failsFetchRecord = true
        //Fail because fetch failed
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation3.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    func testUpdateObjectForNotYetSyncedObject() {
        let expectation = self.expectation(description: "Update Object")
        
        mockInterface.failsCreateRecord = true
        let testObject = TestClass(string: "", int: 0, double: 0)
        store.addObject(testObject)
        store.setRecordName(nil, forObject: testObject)
        XCTAssert(testObject.internSyncStatus == false)
        radon.updateObject({ testClass in
            testObject.string = "Update"
            return testObject
        }) { (error) in
            XCTAssert(error != nil)
            XCTAssert(testObject.internSyncStatus == false)
            expectation.fulfill()
        }
        
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    
    func testDeleteRecordSuccess() {
        let expectation = self.expectation(description: "Delete Object")
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        radon.deleteObject(testObject) { (error) in
            XCTAssert(error == nil)
            XCTAssert(self.store.objectWithIdentifier(testObject.internRecordID) == nil)
            expectation.fulfill()
        }
        waitForExpectations(timeout: 5, handler: nil)
    }
    
    func testHandleQueryNotificationReasonUpdated() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        radon.handleQueryNotificationReason(.recordUpdated, forRecordName: "123")
        XCTAssert(testObject.string == "Mock")
    }
    
    func testHandleQueryNotificationReasonUpdatedRecordNotFound() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        mockInterface.failsFetchRecord = true
        radon.handleQueryNotificationReason(.recordUpdated, forRecordName: "123")
        XCTAssert(testObject.string == "hi")
    }
    
    func testHandleQueryNotificationReasonDelete() {
        let testObject = TestClass(string: "hi", int: 1, double: 1)
        testObject.internRecordID = "123"
        store.addObject(testObject)
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) != nil)
        radon.handleQueryNotificationReason(.recordDeleted, forRecordName: "123")
        XCTAssert(store.objectWithIdentifier(testObject.internRecordID) == nil)
    }
    
    func testHandleQueryNotificationReasonRecordCreated() {
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        radon.handleQueryNotificationReason(.recordCreated, forRecordName: "123")
        XCTAssert(store.objectWithIdentifier("Mock") != nil)
    }
    
    func testHandleQueryNotificationReasonRecordCreatedRecordNotFound() {
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        mockInterface.failsFetchRecord = true
        radon.handleQueryNotificationReason(.recordCreated, forRecordName: "123")
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
    }
    
    func testSyncWithNewObjectToFetch() {
        let expectation = self.expectation(description: "New object from sync")
        XCTAssert(store.objectWithIdentifier("Mock") == nil)
        mockInterface.syncRecordChangeHasNewObject = true
        radon.sync({ (error) in
            
        }) { (error) in
{'fraction_non_alphanumeric': 0.09256491169794608, 'fraction_numerical': 0.009965122072745391, 'mean_word_length': 2.374556323556884, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2993438', 'n_tokens_mistral': 5012, 'n_tokens_neox': 4853, 'n_words': 984}