BUYObjectTests.m 5.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
//
//  BUYObjectTests.m
//  Mobile Buy SDK
//
//  Created by Shopify.
//  Copyright (c) 2015 Shopify Inc. All rights reserved.
//
//  Permission is hereby granted, free of charge, to any person obtaining a copy
//  of this software and associated documentation files (the "Software"), to deal
//  in the Software without restriction, including without limitation the rights
//  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//  copies of the Software, and to permit persons to whom the Software is
//  furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included in
//  all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//  THE SOFTWARE.
//

@import UIKit;
@import XCTest;

#import <Buy/Buy.h>

@interface BUYDirtyTracked : BUYObject

@property (nonatomic, copy) NSString *dirtyObjectValue;
@property (nonatomic, copy) NSString *s;
@property (nonatomic, assign) BOOL dirtyBooleanValue;
@property (nonatomic, assign) char dirtyCharacterValue;
@property (nonatomic, assign) unsigned char dirtyUnsignedCharValue;
@property (nonatomic, assign) int dirtyIntegerValue;
@property (nonatomic, assign) unsigned int dirtyUnsignedIntegerValue;
@property (nonatomic, assign) short dirtyShortValue;
@property (nonatomic, assign) unsigned short dirtyUnsignedShortValue;
@property (nonatomic, assign) long dirtyLongValue;
@property (nonatomic, assign) unsigned long dirtyUnsignedLongValue;
@property (nonatomic, assign) long long dirtyLongLongValue;
@property (nonatomic, assign) unsigned long long dirtyUnsignedLongLongValue;
@property (nonatomic, assign) float dirtyFloatValue;
@property (nonatomic, assign) double dirtyDoubleValue;

@property (nonatomic, readonly) NSArray *array;
@property (nonatomic, readonly) NSMutableArray *mutableArray;

53 54
- (NSArray *)propertyNames;

55 56 57
@end

@interface BUYObjectSubclass : BUYObject
58
@property (nonatomic, strong) NSNumber *identifier;
59 60 61
@end

@interface BUYObjectTests : XCTestCase
62
@property (nonatomic, strong) BUYModelManager *modelManager;
63 64
@end

65 66 67
@interface BUYModelManager (BUYDirtyTracked)
+ (BUYModelManager *)testModelManager;
@end
68

69
@implementation BUYObjectTests
70

71
- (void)setUp
72
{
73 74
	[super setUp];
	self.modelManager = [BUYModelManager testModelManager];
75 76 77 78
}

- (void)testDirtyTracking
{
79
	BUYDirtyTracked *object = [self dirtyTrackedObject];
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	object.s = @"short property name test";
	object.dirtyObjectValue = @"Banana";
	object.dirtyBooleanValue = true;
	object.dirtyCharacterValue = 'c';
	object.dirtyUnsignedCharValue = 'c';
	object.dirtyIntegerValue = 1234;
	object.dirtyUnsignedIntegerValue = 123;
	object.dirtyShortValue = 1;
	object.dirtyUnsignedShortValue = 2;
	object.dirtyLongValue = -4;
	object.dirtyUnsignedLongValue = 4;
	object.dirtyLongLongValue = 1234123412341234;
	object.dirtyUnsignedLongLongValue = 1234123412341234;
	object.dirtyFloatValue = 0.5f;
	object.dirtyDoubleValue = 0.5;
95
	NSSet *expected = [NSSet setWithArray:[object propertyNames]];
96
	NSSet *actual = [object dirtyProperties];
97
	XCTAssertEqualObjects(actual, expected);
98 99 100 101
}

- (void)testIsDirtyReturnsFalseWhenClean
{
102
	BUYDirtyTracked *object = [self dirtyTrackedObject];
103 104 105 106 107 108 109 110
	XCTAssert([object isDirty] == NO);
	object.dirtyObjectValue = @"Banana";
	[object markAsClean];
	XCTAssert([object isDirty] == NO);
}

- (void)testIsDirtyReturnsTrueWhenDirty
{
111
	BUYDirtyTracked *object = [self dirtyTrackedObject];
112 113 114 115 116 117
	object.dirtyObjectValue = @"Banana";
	XCTAssert([object isDirty]);
}

- (void)testMarkAsClean
{
118
	BUYDirtyTracked *object = [self dirtyTrackedObject];
119 120 121 122
	object.dirtyObjectValue = @"Banana";
	XCTAssert([object dirtyProperties]);
}

123 124
- (BUYDirtyTracked *)dirtyTrackedObject
{
125
	return [[BUYDirtyTracked alloc] initWithModelManager:[BUYModelManager modelManager] JSONDictionary:nil];
126 127
}

128 129 130 131 132 133
@end

#pragma mark - Helper Impls

@implementation BUYDirtyTracked

134
+ (BOOL)tracksDirtyProperties
135
{
136
	return YES;
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
+ (NSString *)entityName
{
	return nil;
}

// Overriding private BUYObject class method to avoid need for entity
- (NSArray *)propertyNames
{
	return @[@"s", @"dirtyObjectValue", @"dirtyBooleanValue", @"dirtyCharacterValue",
			 @"dirtyUnsignedCharValue", @"dirtyIntegerValue", @"dirtyUnsignedIntegerValue",
			 @"dirtyShortValue", @"dirtyUnsignedShortValue", @"dirtyLongValue",
			 @"dirtyUnsignedLongValue", @"dirtyLongLongValue", @"dirtyUnsignedLongLongValue",
			 @"dirtyFloatValue", @"dirtyDoubleValue"];
}

154 155 156
@end

@implementation BUYObjectSubclass
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172

+ (NSString *)entityName
{
	return @"ObjectSubclass";
}

@end

@implementation BUYModelManager (BUYDirtyTracked)

+ (BUYModelManager *)testModelManager
{
	static dispatch_once_t onceToken;
	static BUYModelManager *modelManager;
	dispatch_once(&onceToken, ^{
		NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:@[[NSBundle bundleForClass:self]]];
173
		modelManager = [[BUYModelManager alloc] initWithManagedObjectModel:model];
174 175 176 177 178
	});
	
	return modelManager;
}

179
@end