-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// Created by Helge Heß. | ||
// Copyright © 2024 ZeeZide GmbH. | ||
// | ||
|
||
import XCTest | ||
import Foundation | ||
import CoreData | ||
@testable import ManagedModels | ||
|
||
final class CodablePropertiesTests: XCTestCase { | ||
|
||
private lazy var container = try? ModelContainer( | ||
for: Fixtures.CodablePropertiesSchema.managedObjectModel, | ||
configurations: ModelConfiguration(isStoredInMemoryOnly: true) | ||
) | ||
|
||
func testEntityName() throws { | ||
let entityType = Fixtures.CodablePropertiesSchema.StoredAccess.self | ||
XCTAssertEqual(entityType.entity().name, "StoredAccess") | ||
} | ||
|
||
func testPropertySetup() throws { | ||
let valueType = Fixtures.CodablePropertiesSchema.AccessSIP.self | ||
let attribute = CoreData.NSAttributeDescription( | ||
name: "sip", | ||
valueType: valueType, | ||
defaultValue: nil | ||
) | ||
XCTAssertEqual(attribute.name, "sip") | ||
XCTAssertEqual(attribute.attributeType, .transformableAttributeType) | ||
|
||
let transformerName = try XCTUnwrap( | ||
ValueTransformer.valueTransformerNames().first(where: { | ||
$0.rawValue.range(of: "CodableBox11TransformerVOO17ManagedModelTests8") | ||
!= nil | ||
}) | ||
) | ||
let transformer = try XCTUnwrap(ValueTransformer(forName: transformerName)) | ||
_ = transformer // to clear unused-wraning | ||
|
||
XCTAssertTrue(attribute.valueType == | ||
CodableBox<Fixtures.CodablePropertiesSchema.AccessSIP>.self) | ||
XCTAssertNotNil(attribute.valueTransformerName) | ||
XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue) | ||
} | ||
|
||
func testCodablePropertyEntity() throws { | ||
let entity = try XCTUnwrap( | ||
container?.managedObjectModel.entitiesByName["StoredAccess"] | ||
) | ||
|
||
// Creating the entity should have registered the transformer for the | ||
// CodableBox. | ||
let transformerName = try XCTUnwrap( | ||
ValueTransformer.valueTransformerNames().first(where: { | ||
$0.rawValue.range(of: "CodableBox11TransformerVOO17ManagedModelTests8") | ||
!= nil | ||
}) | ||
) | ||
let transformer = try XCTUnwrap(ValueTransformer(forName: transformerName)) | ||
_ = transformer // to clear unused-wraning | ||
|
||
let attribute = try XCTUnwrap(entity.attributesByName["sip"]) | ||
XCTAssertEqual(attribute.name, "sip") | ||
XCTAssertTrue(attribute.valueType == | ||
CodableBox<Fixtures.CodablePropertiesSchema.AccessSIP>.self) | ||
XCTAssertNotNil(attribute.valueTransformerName) | ||
XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue) | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// Created by Helge Heß. | ||
// Copyright © 2024 ZeeZide GmbH. | ||
// | ||
|
||
import ManagedModels | ||
|
||
extension Fixtures { | ||
// https://github.com/Data-swift/ManagedModels/issues/27 | ||
|
||
enum CodablePropertiesSchema: VersionedSchema { | ||
static var models : [ any PersistentModel.Type ] = [ | ||
StoredAccess.self | ||
] | ||
|
||
public static let versionIdentifier = Schema.Version(0, 1, 0) | ||
|
||
@Model | ||
final class StoredAccess: NSManagedObject { | ||
var token : String | ||
var expires : Date | ||
var sip : AccessSIP | ||
} | ||
|
||
struct AccessSIP: Codable { | ||
var username : String | ||
var password : String | ||
var realm : String | ||
} | ||
} | ||
} |