Skip to content

Commit

Permalink
Added update method on TelepatBaseObjects
Browse files Browse the repository at this point in the history
  • Loading branch information
Ovidiu Nitan committed Sep 25, 2015
1 parent 0ae6a0a commit 37deab5
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 78 deletions.
7 changes: 7 additions & 0 deletions Telepat/RemoteObjects/TelepatBaseObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
//

#import "JSONModel.h"
#define kTelepatNoChannelError @"NoChannelError"

@class TelepatChannel, TelepatResponse;
typedef void (^TelepatResponseBlock)(TelepatResponse *response);

@interface TelepatBaseObject : JSONModel

@property (nonatomic) NSString *object_id;
@property (nonatomic) NSString<Optional> *uuid;
@property (nonatomic, weak) TelepatChannel <Ignore>*channel;

- (NSDictionary *) patchAgainst:(TelepatBaseObject *)updatedObject;
- (NSArray *) propertiesList;
- (void) update;
- (void) updateWithBlock:(TelepatResponseBlock)block;

@end
29 changes: 9 additions & 20 deletions Telepat/RemoteObjects/TelepatBaseObject.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,6 @@ - (BOOL) isEqual:(id)object {
}

- (NSDictionary *) patchAgainst:(TelepatBaseObject *)updatedObject {
// if (![updatedObject isMemberOfClass:[self class]]) @throw([NSException exceptionWithName:kTelepatInvalidClass reason:@"The received object is not the same as the current one" userInfo:nil]);
// NSMutableArray *patch = [NSMutableArray array];
//
// NSMutableArray *patches = [NSMutableArray array];
// for (NSString *property in [updatedObject propertiesList]) {
// if (![[updatedObject valueForKey:property] isEqual:[self valueForKey:property]]) {
// NSMutableDictionary *patchDict = [NSMutableDictionary dictionary];
// patchDict[@"path"] = [NSString stringWithFormat:@"%@/%ld/%@", self.modelName, (long)object.object_id, property];
//
// if ([object valueForKey:property] == nil) {
// patchDict[@"op"] = @"delete";
// } else {
// patchDict[@"op"] = @"replace";
// patchDict[@"value"] = [object valueForKey:property];
// }
//
// [patches addObject:patchDict];
// }
// }

return @{};
}

Expand All @@ -73,4 +53,13 @@ - (NSArray *) propertiesList {
return rv;
}

- (void) update {
[self updateWithBlock:nil];
}

- (void) updateWithBlock:(TelepatResponseBlock)block {
if (!self.channel) @throw [NSException exceptionWithName:kTelepatNoChannelError reason:[NSString stringWithFormat:@"You tried to update object %@ but it's channel is null", self] userInfo:nil];
[self.channel patch:self withBlock:block];
}

@end
1 change: 1 addition & 0 deletions Telepat/TelepatChannel.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ - (NSString *) add:(TelepatBaseObject *)object {
}

- (NSString *) add:(TelepatBaseObject *)object withBlock:(void (^)(TelepatResponse *response))block {
[object setChannel:self];
[object setUuid:[[NSUUID UUID] UUIDString]];
[_waitingForCreation setObject:object forKey:object.uuid];
[[KRRest sharedClient] create:@{@"model": self.modelName,
Expand Down
17 changes: 3 additions & 14 deletions TelepatProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
objects = {

/* Begin PBXBuildFile section */
A507315D1BB1A54600A41090 /* TelepatProjectTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A507315C1BB1A54600A41090 /* TelepatProjectTests.m */; };
A5330CDF1BB1959100271E08 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A5330CDE1BB1959100271E08 /* main.m */; };
A5330CE21BB1959100271E08 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = A5330CE11BB1959100271E08 /* AppDelegate.m */; };
A5330CE51BB1959100271E08 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = A5330CE41BB1959100271E08 /* ViewController.m */; };
A5330CE81BB1959100271E08 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A5330CE61BB1959100271E08 /* Main.storyboard */; };
A5330CEA1BB1959100271E08 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A5330CE91BB1959100271E08 /* Assets.xcassets */; };
A5330CED1BB1959100271E08 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A5330CEB1BB1959100271E08 /* LaunchScreen.storyboard */; };
A5E259E71BB588940058E978 /* TelepatProjectTests.m in Sources */ = {isa = PBXBuildFile; fileRef = A5330CF71BB1959100271E08 /* TelepatProjectTests.m */; settings = {ASSET_TAGS = (); }; };
B25BA84BA8ECF85DE4B14B2F /* libPods.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 9649BD2FB4B1D6828B2F6393 /* libPods.a */; };
/* End PBXBuildFile section */

Expand All @@ -32,8 +32,6 @@
9649BD2FB4B1D6828B2F6393 /* libPods.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libPods.a; sourceTree = BUILT_PRODUCTS_DIR; };
A3F210B6D5A2F24BA29D0622 /* Pods.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = Pods.release.xcconfig; path = "Pods/Target Support Files/Pods/Pods.release.xcconfig"; sourceTree = "<group>"; };
A507315A1BB1A54600A41090 /* TelepatProjectTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TelepatProjectTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A507315C1BB1A54600A41090 /* TelepatProjectTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TelepatProjectTests.m; sourceTree = "<group>"; };
A507315E1BB1A54600A41090 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A5330CDA1BB1959100271E08 /* TelepatProject.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TelepatProject.app; sourceTree = BUILT_PRODUCTS_DIR; };
A5330CDE1BB1959100271E08 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
A5330CE01BB1959100271E08 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -78,22 +76,12 @@
name = Pods;
sourceTree = "<group>";
};
A507315B1BB1A54600A41090 /* TelepatProjectTests */ = {
isa = PBXGroup;
children = (
A507315C1BB1A54600A41090 /* TelepatProjectTests.m */,
A507315E1BB1A54600A41090 /* Info.plist */,
);
path = TelepatProjectTests;
sourceTree = "<group>";
};
A5330CD11BB1959100271E08 = {
isa = PBXGroup;
children = (
A5330CDC1BB1959100271E08 /* TelepatProject */,
A5330CF61BB1959100271E08 /* TelepatProjectTests */,
A5330D011BB1959100271E08 /* TelepatProjectUITests */,
A507315B1BB1A54600A41090 /* TelepatProjectTests */,
A5330CDB1BB1959100271E08 /* Products */,
1818248381CF6C079265BDA2 /* Pods */,
DE6F44041BE0DD8032EFB3B5 /* Frameworks */,
Expand Down Expand Up @@ -294,7 +282,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A507315D1BB1A54600A41090 /* TelepatProjectTests.m in Sources */,
A5E259E71BB588940058E978 /* TelepatProjectTests.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -481,6 +469,7 @@
A50731631BB1A54700A41090 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
A5330CD51BB1959100271E08 /* Build configuration list for PBXProject "TelepatProject" */ = {
isa = XCConfigurationList;
Expand Down
Binary file not shown.
73 changes: 73 additions & 0 deletions TelepatProject/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,78 @@
<array>
<string>fbauth</string>
</array>
<key>kTestAppSchema</key>
<dict>
<key>appId</key>
<string>REPLACE_THIS</string>
<key>schema</key>
<dict>
<key>comments</key>
<dict>
<key>namespace</key>
<string>comments</string>
<key>type</key>
<string>comments</string>
<key>properties</key>
<dict>
<key>text</key>
<dict>
<key>type</key>
<string>string</string>
</dict>
</dict>
<key>belongsTo</key>
<array>
<dict>
<key>parentModel</key>
<string>events</string>
<key>relationType</key>
<string>hasMany</string>
</dict>
</array>
<key>read_acl</key>
<integer>6</integer>
<key>write_acl</key>
<integer>6</integer>
<key>meta_read_acl</key>
<integer>6</integer>
</dict>
<key>events</key>
<dict>
<key>namespace</key>
<string>events</string>
<key>type</key>
<string>events</string>
<key>properties</key>
<dict>
<key>text</key>
<dict>
<key>type</key>
<string>string</string>
</dict>
<key>image</key>
<dict>
<key>type</key>
<string>string</string>
</dict>
<key>options</key>
<dict>
<key>type</key>
<string>object</string>
</dict>
</dict>
<key>hasMany</key>
<array>
<string>comments</string>
</array>
<key>read_acl</key>
<integer>7</integer>
<key>write_acl</key>
<integer>7</integer>
<key>meta_read_acl</key>
<integer>4</integer>
</dict>
</dict>
</dict>
</dict>
</plist>
42 changes: 31 additions & 11 deletions TelepatProject/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@

#import "ViewController.h"

@interface Event : TelepatBaseObject

@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *image;

@end

@implementation Event

@end

@interface ViewController ()

@end
Expand All @@ -18,18 +29,27 @@ - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

// [[Telepat client] adminAdd:@"[email protected]" password:@"abracadabra" name:@"Ovidiu N." withBlock:^(TelepatResponse *response) {
// NSLog(@"add re: %@", response);
// }];

// [[Telepat client] adminLogin:@"[email protected]" password:@"abracadabra" withBlock:^(TelepatResponse *response) {
// // [[Telepat client] createAppWithName:@"iOS Test Application" fields:@{@"testdield": @"abcd"} block:^(TelepatResponse *response) {
// // NSLog(@"createAppWithName response: %@", response);
// // }];
//
// [[Telepat client] listAppsWithBlock:^(TelepatResponse *response) {
// NSLog(@"listAppsWithBlock response: %@", [response getObjectOfType:[TelepatApp class]]);
// Set the API key and the current application ID
// [[Telepat client] setApiKey:@"API_KEY"];
// [[Telepat client] setAppId:@"APP_ID"];
//
// [[Telepat client] login:@"[email protected]" password:@"mysecurepassword" withBlock:^(TelepatResponse *response) {
// [[Telepat client] getContextsWithBlock:^(TelepatResponse *response) {
// NSArray *contexts = [response getObjectOfType:[TelepatContext class]];
// TelepatContext *firstContext = contexts[0];
// TelepatChannel *firstChannel = [[Telepat client] subscribe:firstContext modelName:@"events" classType:[Event class] withBlock:^(TelepatResponse *response) {
// NSLog(@"Subscribed to %@", firstContext.name);
// }];
//
// Event *newEvent = [[Event alloc] init];
// newEvent.text = @"Hello world!";
// newEvent.image = @"telepat_image.png";
//
// [firstChannel add:newEvent];
//
// Event *modifiedEvent = [[Event alloc] initWithDi];
// }];
//
// }];
}

Expand Down
2 changes: 2 additions & 0 deletions TelepatProjectTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>kTestAppSchema</key>
<dict><key>appId</key><string>REPLACE_THIS</string><key>schema</key><dict><key>comments</key><dict><key>namespace</key><string>comments</string><key>type</key><string>comments</string><key>properties</key><dict><key>text</key><dict><key>type</key><string>string</string></dict></dict><key>belongsTo</key><array><dict><key>parentModel</key><string>events</string><key>relationType</key><string>hasMany</string></dict></array><key>read_acl</key><integer>6</integer><key>write_acl</key><integer>6</integer><key>meta_read_acl</key><integer>6</integer></dict><key>events</key><dict><key>namespace</key><string>events</string><key>type</key><string>events</string><key>properties</key><dict><key>text</key><dict><key>type</key><string>string</string></dict><key>image</key><dict><key>type</key><string>string</string></dict><key>options</key><dict><key>type</key><string>object</string></dict></dict><key>hasMany</key><array><string>comments</string></array><key>read_acl</key><integer>7</integer><key>write_acl</key><integer>7</integer><key>meta_read_acl</key><integer>4</integer></dict></dict></dict>
<key>CFBundleVersion</key>
<string>1</string>
</dict>
Expand Down
81 changes: 48 additions & 33 deletions TelepatProjectTests/TelepatProjectTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
static NSString *contextInfo = @"A super context";
static NSString *apiKey = @"3406870085495689e34d878f09faf52c";

@interface Event : TelepatBaseObject

@property (nonatomic, strong) NSString *text;
@property (nonatomic, strong) NSString *image;

@end

@implementation Event

@end

@interface TelepatProjectTests : XCTestCase

@end
Expand Down Expand Up @@ -85,43 +96,42 @@ - (void)testAdminLogin {
- (void) testCreateRemoveApp {
XCTestExpectation *expectation = [self expectationWithDescription:@"Testing application adding"];

[[Telepat client] createAppWithName:appName keys:@[apiKey] customFields:@{} block:^(TelepatResponse *response) {
[[Telepat client] setApiKey:apiKey];
TelepatApp *resultedApp = [response getObjectOfType:[TelepatApp class]];
XCTAssertNotNil(resultedApp, @"createAppWithName didn't returned a valid application");
XCTAssertNotNil(resultedApp.app_id, @"createAppWithName didn't returned a valid app_id");
[[Telepat client] setAppId:resultedApp.app_id];
[[Telepat client] listAppsWithBlock:^(TelepatResponse *response) {
NSArray *apps = [response getObjectOfType:[TelepatApp class]];
XCTAssertGreaterThanOrEqual([apps count], 1, @"listAppsWithBlock: %lu applications found, expected at least 1", (unsigned long)[apps count]);
XCTAssertEqualObjects(resultedApp, [apps lastObject], @"listAppsWithBlock: received app is not the same like the created one");
TelepatApp *updatedApp = [[TelepatApp alloc] initWithDictionary:[resultedApp toDictionary] error:nil];
updatedApp.name = updatedAppName;
[[Telepat client] updateApp:resultedApp withApp:updatedApp andBlock:^(TelepatResponse *response) {
XCTAssertEqual(response.status, 200, @"updateApp: app update error");
[[Telepat client] createContextWithName:@"TestContext" meta:@{@"info": contextInfo} withBlock:^(TelepatResponse *response) {
_createdContext = [response getObjectOfType:[TelepatContext class]];
XCTAssertNotNil([response getObjectOfType:[TelepatContext class]], @"createContextWithName: The server returned an invalid context");
XCTAssertEqualObjects(_createdContext.meta[@"info"], contextInfo, @"createContextWithName: context info doesn't match");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[[Telepat client] getContextsWithBlock:^(TelepatResponse *response) {
XCTAssertGreaterThanOrEqual([[response getObjectOfType:[TelepatContext class]] count], 1, @"getContextsWithBlock: there should be at least one context");
[[Telepat client] getContext:_createdContext.context_id withBlock:^(TelepatResponse *response) {
TelepatContext *retrievedContext = [response getObjectOfType:[TelepatContext class]];
XCTAssertEqualObjects(_createdContext.meta[@"info"], retrievedContext.meta[@"info"]);
[[Telepat client] removeAppWithBlock:^(TelepatResponse *response) {
XCTAssertEqual(response.status, 200);
NSLog(@"Registering device...");
[[Telepat client] registerDeviceForWebsocketsWithBlock:^(TelepatResponse *response) {
[[Telepat client] createAppWithName:appName keys:@[apiKey] customFields:@{} block:^(TelepatResponse *response) {
[[Telepat client] setApiKey:apiKey];
TelepatApp *resultedApp = [response getObjectOfType:[TelepatApp class]];
XCTAssertNotNil(resultedApp, @"createAppWithName didn't returned a valid application");
XCTAssertNotNil(resultedApp.app_id, @"createAppWithName didn't returned a valid app_id");
[[Telepat client] setAppId:resultedApp.app_id];
[[Telepat client] listAppsWithBlock:^(TelepatResponse *response) {
NSArray *apps = [response getObjectOfType:[TelepatApp class]];
XCTAssertGreaterThanOrEqual([apps count], 1, @"listAppsWithBlock: %lu applications found, expected at least 1", (unsigned long)[apps count]);
XCTAssertEqualObjects(resultedApp, [apps lastObject], @"listAppsWithBlock: received app is not the same like the created one");
TelepatApp *updatedApp = [[TelepatApp alloc] initWithDictionary:[resultedApp toDictionary] error:nil];
updatedApp.name = updatedAppName;
[[Telepat client] updateApp:resultedApp withApp:updatedApp andBlock:^(TelepatResponse *response) {
XCTAssertEqual(response.status, 200, @"updateApp: app update error");
[[Telepat client] createContextWithName:@"TestContext" meta:@{@"info": contextInfo} withBlock:^(TelepatResponse *response) {
_createdContext = [response getObjectOfType:[TelepatContext class]];
XCTAssertNotNil([response getObjectOfType:[TelepatContext class]], @"createContextWithName: The server returned an invalid context");
XCTAssertEqualObjects(_createdContext.meta[@"info"], contextInfo, @"createContextWithName: context info doesn't match");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[[Telepat client] updateSchema:[[NSBundle mainBundle] objectForInfoDictionaryKey:@"kTestAppSchema"] withBlock:^(TelepatResponse *response) {
XCTAssertEqual(response.status, 200, @"updateSchema: server returned %d, expected 200", response.status);
[[Telepat client] getSchemasWithBlock:^(TelepatResponse *response) {
XCTAssertNotNil(response.content, @"getSchemasWithBlock returned an invalid response");
[expectation fulfill];
}];
}];
}];
});
});
}];
}];
}];
}];
}];
} shouldUpdateBackend:NO];

[self waitForExpectationsWithTimeout:15.0 handler:^(NSError *error) {
[self waitForExpectationsWithTimeout:25.0 handler:^(NSError *error) {
if(error) {
XCTFail(@"Expectation Failed with error: %@", error);
}
Expand All @@ -144,9 +154,14 @@ - (void) testRegisterUser {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[[Telepat client] listAppUsersWithBlock:^(TelepatResponse *response) {
XCTAssertGreaterThanOrEqual([[response getObjectOfType:[TelepatUser class]] count], 1, @"listAppUsersWithBlock: at least one user should be returned");
[[Telepat client] deleteUser:username withBlock:^(TelepatResponse *response) {
[[Telepat client] removeAppWithBlock:^(TelepatResponse *response) {
[expectation fulfill];
[[Telepat client] login:username password:password withBlock:^(TelepatResponse *response) {
TelepatUser *loggedInUser = [response getObjectOfType:[TelepatUser class]];
XCTAssertNotNil(loggedInUser, @"login: invalid user returned");

[[Telepat client] deleteUser:username withBlock:^(TelepatResponse *response) {
[[Telepat client] removeAppWithBlock:^(TelepatResponse *response) {
[expectation fulfill];
}];
}];
}];
}];
Expand Down

0 comments on commit 37deab5

Please sign in to comment.