<commit_before>//
//  CoreDataAttributeJSONCompatibilityTests.swift
//  NetworkObjects
//
//  Created by <NAME> on 2/8/15.
//  Copyright (c) 2015 ColemanCDA. All rights reserved.
//

import Foundation
import XCTest
import CoreData

class CoreDataAttributeJSONCompatibilityTests: XCTestCase {
    
    // MARK: - Properties
    
    var testModel: NSManagedObjectModel!
    
    var testAttributesEntity: NSEntityDescription!
    
    // MARK: - Setup
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
        
        // create model
        self.testModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle(identifier: "com.ColemanCDA.NetworkObjectsTests")!])!
        
        self.testAttributesEntity = self.testModel.entitiesByName["TestAttributes"] as NSEntityDescription
        
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
        
    }
    
    // MARK: - Convert JSON to CoreData Test Cases
    
    func testConvertJSONNullToCoreDataNil() {
        
        let null = NSNull()
        
        let (newValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(null, forAttribute: "stringAttribute")
        
        XCTAssert(valid, "Conversion should be valid")
        
        XCTAssert(newValue == nil, "Converted value should be nil")
    }
    
    func testConvertJSONTranformedValueToCoreDataTranformedValue() {
        
        let archivedDictionary = ["key": "value"] as NSDictionary
        
        let data = NSKeyedArchiver.archivedDataWithRootObject(archivedDictionary)
        
        let base64String = data.base64EncodedStringWithOptions(CoreDataAttributeJSONCompatibilityOptions.defaultOptions.base64EncodingOptions)
        
        let (newValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(base64String, forAttribute: "defaultTransformer")
        
        XCTAssert(valid, "Conversion should be valid")
        
        XCTAssert(newValue as? NSDictionary == archivedDictionary, "Converted Value should be original archived dictionary")
    }
    
    func testConvertGarbageJSONTranformedValueToCoreDataTranformedValue() {
        
        let data = "GarbageData".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
        
        let (newValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(data, forAttribute: "defaultTransformer")
        
        XCTAssert(!valid, "Conversion should be invalid")
        
        XCTAssert(newValue == nil, "Converted Value should be nil")
    }
    
    // MARK: - Convert CoreData to JSON Cases
    
    func testConvertCoreDataNilToJSONNull() {
        
        let nilValue: AnyObject? = nil
        
        let jsonValue: AnyObject? = testAttributesEntity.JSONCompatibleValueForAttributeValue(nilValue, forAttribute: "stringAttribute")
        
        XCTAssert(jsonValue === NSNull(), "Converted value should be NSNull singleton")
    }
}<commit_msg>added more test cases for #63<commit_after>//
//  CoreDataAttributeJSONCompatibilityTests.swift
//  NetworkObjects
//
//  Created by <NAME> on 2/8/15.
//  Copyright (c) 2015 ColemanCDA. All rights reserved.
//

import Foundation
import XCTest
import CoreData
import ExSwift

class CoreDataAttributeJSONCompatibilityTests: XCTestCase {
    
    // MARK: - Properties
    
    var testModel: NSManagedObjectModel!
    
    var testAttributesEntity: NSEntityDescription!
    
    // MARK: - Setup
    
    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.
        
        // create model
        self.testModel = NSManagedObjectModel.mergedModelFromBundles([NSBundle(identifier: "com.ColemanCDA.NetworkObjectsTests")!])!
        
        self.testAttributesEntity = self.testModel.entitiesByName["TestAttributes"] as NSEntityDescription
        
    }
    
    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
        
    }
    
    // MARK: - Convert JSON to CoreData Test Cases
    
    func testConvertJSONNullToCoreDataNil() {
        
        let null = NSNull()
        
        let (convertedValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(null, forAttribute: "stringAttribute")
        
        XCTAssert(valid, "Conversion should be valid")
        
        XCTAssert(convertedValue == nil, "Converted value should be nil")
    }
    
    func testConvertJSONDefaultTransformerTranformedValueToCoreDataTranformedValue() {
        
        let archivedDictionary = ["key": "value"] as NSDictionary
        
        let data = NSKeyedArchiver.archivedDataWithRootObject(archivedDictionary)
        
        let base64String = data.base64EncodedStringWithOptions(CoreDataAttributeJSONCompatibilityOptions.defaultOptions.base64EncodingOptions)
        
        let (convertedValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(base64String, forAttribute: "defaultTransformer")
        
        XCTAssert(valid, "Conversion should be valid")
        
        XCTAssert(convertedValue as? NSDictionary == archivedDictionary, "Converted Value should be original archived dictionary")
    }
    
    func testConvertJSONDateToCoreDataDate() {
        
        let date = NSDate()
        
        let jsonDateString = CoreDataAttributeJSONCompatibilityOptions.defaultOptions.dateFormatter.stringFromDate(date)
        
        let (convertedValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(jsonDateString, forAttribute: "dateAttribute")
        
        XCTAssert(valid, "Conversion should be valid")
        
        let convertedDate = convertedValue as? NSDate
        
        XCTAssert(convertedDate != nil, "Converted value \(convertedValue) should be NSDate instance")
        
        let truncatedOriginalTimeIntervalSince1970 = Double(UInt(date.timeIntervalSince1970))
        
        XCTAssert(convertedDate!.timeIntervalSince1970 == truncatedOriginalTimeIntervalSince1970, "Converted Value \(convertedDate!) should equal original date \(date)")
    }
    
    func testConvertJSONBinaryDataToCoreDataBinaryData() {
        
        let data = "SampleData".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)!
        
        let base64String = data.base64EncodedStringWithOptions(CoreDataAttributeJSONCompatibilityOptions.defaultOptions.base64EncodingOptions)
        
        let (convertedValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(base64String, forAttribute: "dataAttribute")
        
        XCTAssert(valid, "Conversion should be valid")
        
        XCTAssert(convertedValue as? NSData == data, "Converted value should equal original value")
    }
    
    // MARK: - Convert Garbage JSON to CoreData Test Cases
    
    func testConvertGarbageJSONDefaultTransformerTranformedValueToCoreDataTranformedValue() {
        
        let data = "GarbageData".dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true)!
        
        let (convertedValue: AnyObject?, valid) = testAttributesEntity.attributeValueForJSONCompatibleValue(data, forAttribute: "defaultTransformer")
        
        XCTAssert(!valid, "Conversion should be invalid")
        
        XCTAssert(convertedValue == nil, "Converted Value should be nil")
    }
    
    // MARK: - Convert CoreData to JSON Cases
    
    func testConvertCoreDataNilToJSONNull() {
        
        let nilValue: AnyObject? = nil
        
        let jsonValue: AnyObject? = testAttributesEntity.JSONCompatibleValueForAttributeValue(nilValue, forAttribute: "stringAttribute")
        
        XCTAssert(jsonValue === NSNull(), "Converted value should be NSNull singleton")
    }
}
{'fraction_non_alphanumeric': 0.07392423685178375, 'fraction_numerical': 0.007478239548853746, 'mean_word_length': 2.981454367984383, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, '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': '7153843', 'n_tokens_mistral': 2194, 'n_tokens_neox': 2120, 'n_words': 508}