<commit_before>import CoreData
import XCTest
import Sync

class ExportTests: XCTestCase {
    let sampleSnakeCaseJSON = [
        "description": "reserved",
        "inflection_binary_data": ["one", "two"],
        "inflection_date": "1970-01-01",
        "custom_remote_key": "randomRemoteKey",
        "inflection_id": 1,
        "inflection_string": "string",
        "inflection_integer": 1,
        "ignored_parameter": "ignored",
        "ignore_transformable": "string",
        "inflection_uuid": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
        "inflection_uri": "https://www.apple.com/"
        ] as [String : Any]

    func testExportDictionaryWithSnakeCase() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSON)
        try! dataStack.mainContext.save()

        let compared = [
            "description": "reserved",
            "inflection_binary_data": NSKeyedArchiver.archivedData(withRootObject: ["one", "two"]) as NSData,
            "inflection_date": "1970-01-01",
            "randomRemoteKey": "randomRemoteKey",
            "inflection_id": 1,
            "inflection_string": "string",
            "inflection_integer": 1,
            "inflection_uuid": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
            "inflection_uri": "https://www.apple.com/"
            ] as [String : Any]

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = TimeZone(identifier: "GMT")

        var exportOptions = ExportOptions()
        exportOptions.dateFormatter = formatter
        let result = user.export(using: exportOptions)

        for (key, value) in compared {
            if let comparedValue = result[key] {
                XCTAssertEqual(value as? NSObject, comparedValue as? NSObject)
            }
        }

        dataStack.drop()
    }

    func testExportDictionaryWithCamelCase() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSON)
        try! dataStack.mainContext.save()

        let compared = [
            "description": "reserved",
            "inflectionBinaryData": NSKeyedArchiver.archivedData(withRootObject: ["one", "two"]) as NSData,
            "inflectionDate": "1970-01-01",
            "randomRemoteKey": "randomRemoteKey",
            "inflectionID": 1,
            "inflectionString": "string",
            "inflectionInteger": 1,
            "inflectionUUID": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
            "inflectionURI": "https://www.apple.com/"
            ] as [String : Any]

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = TimeZone(identifier: "GMT")

        var exportOptions = ExportOptions()
        exportOptions.dateFormatter = formatter
        exportOptions.inflectionType = .camelCase
        let result = user.export(using: exportOptions)
        XCTAssertEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    let sampleSnakeCaseJSONWithRelationship = ["inflection_id": 1] as [String : Any]

    func testExportDictionaryWithSnakeCaseRelationshipArray() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")

        try! dataStack.mainContext.save()

        let compared = [
            "inflection_binary_data": NSNull(),
            "inflection_date": NSNull(),
            "inflection_id": 1,
            "inflection_integer": NSNull(),
            "inflection_string": NSNull(),
            "randomRemoteKey": NSNull(),
            "description": NSNull(),
            "inflection_uuid": NSNull(),
            "inflection_uri": NSNull(),
            "camel_case_company": [
                "inflection_id": 1
            ]
            ] as [String : Any]

        let result = user.export()
        XCTAssertEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    func testExportDictionaryWithCamelCaseRelationshipArray() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")

        try! dataStack.mainContext.save()

        let compared = [
            "inflectionBinaryData": NSNull(),
            "inflectionDate": NSNull(),
            "inflectionID": 1,
            "inflectionInteger": NSNull(),
            "inflectionString": NSNull(),
            "randomRemoteKey": NSNull(),
            "description": NSNull(),
            "inflectionUUID": NSNull(),
            "inflectionURI": NSNull(),
            "camelCaseCompany": [
                "inflectionID": 1
            ]
            ] as [String : Any]


        let result = user.export(using: .camelCase)
        XCTAssertEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    func testExportDictionaryWithSnakeCaseRelationshipNested() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")
<commit_msg>Break one test to try<commit_after>import CoreData
import XCTest
import Sync

class ExportTests: XCTestCase {
    let sampleSnakeCaseJSON = [
        "description": "reserved",
        "inflection_binary_data": ["one", "two"],
        "inflection_date": "1970-01-01",
        "custom_remote_key": "randomRemoteKey",
        "inflection_id": 1,
        "inflection_string": "string",
        "inflection_integer": 1,
        "ignored_parameter": "ignored",
        "ignore_transformable": "string",
        "inflection_uuid": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
        "inflection_uri": "https://www.apple.com/"
        ] as [String : Any]

    func testExportDictionaryWithSnakeCase() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSON)
        try! dataStack.mainContext.save()

        let compared = [
            "description": "reserved",
            "inflection_binary_data": NSKeyedArchiver.archivedData(withRootObject: ["one", "two"]) as NSData,
            "inflection_date": "1970-01-01",
            "randomRemoteKey": "randomRemoteKey",
            "inflection_id": 1,
            "inflection_string": "string",
            "inflection_integer": 1,
            "inflection_uuid": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
            "inflection_uri": "https://www.apple.com/"
            ] as [String : Any]

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = TimeZone(identifier: "GMT")

        var exportOptions = ExportOptions()
        exportOptions.dateFormatter = formatter
        let result = user.export(using: exportOptions)

        for (key, value) in compared {
            if let comparedValue = result[key] {
                XCTAssertEqual(value as? NSObject, comparedValue as? NSObject)
            }
        }

        dataStack.drop()
    }

    func testExportDictionaryWithCamelCase() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSON)
        try! dataStack.mainContext.save()

        let compared = [
            "description": "reserved",
            "inflectionBinaryData": NSKeyedArchiver.archivedData(withRootObject: ["one", "two"]) as NSData,
            "inflectionDate": "1970-01-01",
            "randomRemoteKey": "randomRemoteKey",
            "inflectionID": 1,
            "inflectionString": "string",
            "inflectionInteger": 1,
            "inflectionUUID": "E621E1F8-C36C-495A-93FC-0C247A3E6E5F",
            "inflectionURI": "https://www.apple.com/"
            ] as [String : Any]

        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        formatter.timeZone = TimeZone(identifier: "GMT")

        var exportOptions = ExportOptions()
        exportOptions.dateFormatter = formatter
        exportOptions.inflectionType = .camelCase
        let result = user.export(using: exportOptions)
        XCTAssertEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    let sampleSnakeCaseJSONWithRelationship = ["inflection_id": 1] as [String : Any]

    func testExportDictionaryWithSnakeCaseRelationshipArray() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")

        try! dataStack.mainContext.save()

        let compared = [
            "inflection_binary_data": NSNull(),
            "inflection_date": NSNull(),
            "inflection_id": 1,
            "inflection_integer": NSNull(),
            "inflection_string": NSNull(),
            "randomRemoteKey": NSNull(),
            "description": NSNull(),
            "inflection_uuid": NSNull(),
            "inflection_uri": NSNull(),
            "camel_case_company": [
                "inflection_id": 1
            ]
            ] as [String : Any]

        let result = user.export()
        XCTAssertEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    func testExportDictionaryWithCamelCaseRelationshipArray() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")

        try! dataStack.mainContext.save()

        let compared = [
            "inflectionBinaryData": NSNull(),
            "inflectionDate": NSNull(),
            "inflectionID": 1,
            "inflectionInteger": NSNull(),
            "inflectionString": NSNull(),
            "randomRemoteKey": NSNull(),
            "description": NSNull(),
            "inflectionUUID": NSNull(),
            "inflectionURI": NSNull(),
            "camelCaseCompany": [
                "inflectionID": 1
            ]
            ] as [String : Any]


        let result = user.export(using: .camelCase)
        XCTAssertNotEqual(compared as NSDictionary, result as NSDictionary)

        dataStack.drop()
    }

    func testExportDictionaryWithSnakeCaseRelationshipNested() {
        // Fill in transformable attributes is not supported in Swift 3. Crashes when saving the context.
        let dataStack = Helper.dataStackWithModelName("137")
        let user = NSEntityDescription.insertNewObject(forEntityName: "InflectionUser", into: dataStack.mainContext)
        user.fill(with: self.sampleSnakeCaseJSONWithRelationship)

        let company = NSEntityDescription.insertNewObject(forEntityName: "InflectionCompany", into: dataStack.mainContext)
        company.setValue(NSNumber(value: 1), forKey: "inflectionID")
        user.setValue(company, forKey: "camelCaseCompany")

{'fraction_non_alphanumeric': 0.10394802598700649, 'fraction_numerical': 0.016420361247947456, 'mean_word_length': 2.7444533547179897, 'pattern_counts': {'":': 104, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 6, 'lorem ipsum': 0, 'www.': 6, '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': '959759', 'n_tokens_mistral': 4092, 'n_tokens_neox': 3893, 'n_words': 838}