Skip to content
Snippets Groups Projects
Commit bb4ed0ad authored by cramakri's avatar cramakri
Browse files

Added implementation and test of persistence of entities

SVN: 26958
parent 7f54eacb
No related branches found
No related tags found
No related merge requests found
......@@ -25,6 +25,10 @@
#import <CoreData/CoreData.h>
/**
* \brief A persistent version of an entity from the iPad server.
*/
@class CISDOBIpadRawEntity;
@interface CISDOBIpadEntity : NSManagedObject
@property (nonatomic, retain) NSString * summaryHeader;
......@@ -35,4 +39,8 @@
@property (nonatomic, retain) NSString * entityType;
@property (nonatomic, retain) NSString * propertiesJson;
// Actions
//! Take the values from the raw entity.
- (void)initializeFromRawEntity:(CISDOBIpadRawEntity *)rawEntity;
@end
/*
* Copyright 2012 ETH Zuerich, CISD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//
// CISDOBIpadEntity.m
// BisMac
//
// Created by Ramakrishnan Chandrasekhar on 10/1/12.
//
//
#import "CISDOBIpadEntity.h"
#import "CISDOBIpadService.h"
@implementation CISDOBIpadEntity
@dynamic summaryHeader;
@dynamic summary;
@dynamic identifier;
@dynamic permId;
@dynamic entityKind;
@dynamic entityType;
@dynamic propertiesJson;
- (void)initializeFromRawEntity:(CISDOBIpadRawEntity *)rawEntity
{
self.summaryHeader = rawEntity.summaryHeader;
self.summary = rawEntity.summary;
self.identifier = rawEntity.identifier;
self.permId = rawEntity.permId;
self.entityKind = rawEntity.entityKind;
self.entityType = rawEntity.entityType;
self.propertiesJson = rawEntity.properties;
}
@end
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model name="" userDefinedModelVersionIdentifier="" type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1810" systemVersion="11G56" minimumToolsVersion="Xcode 4.3" macOSVersion="Automatic" iOSVersion="Automatic">
<entity name="CISDOBIpadEntity" representedClassName="CIDOBIpadEntity" syncable="YES">
<entity name="CISDOBIpadEntity" representedClassName="CISDOBIpadEntity" syncable="YES">
<attribute name="entityKind" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="entityType" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="identifier" optional="YES" attributeType="String" syncable="YES"/>
......@@ -10,6 +10,6 @@
<attribute name="summaryHeader" optional="YES" attributeType="String" syncable="YES"/>
</entity>
<elements>
<element name="CISDOBIpadEntity" positionX="0" positionY="0" width="0" height="0"/>
<element name="CISDOBIpadEntity" positionX="160" positionY="192" width="128" height="150"/>
</elements>
</model>
\ No newline at end of file
......@@ -20,11 +20,11 @@
// Created by Ramakrishnan Chandrasekhar on 10/2/12.
//
#import <SenTestingKit/SenTestingKit.h>
#import "CISDOBAsyncTest.h"
// Test the persistence of iPad entities.
@class CISDOBIpadService;
@interface CISDOBIpadEntityTest : SenTestCase
@interface CISDOBIpadEntityTest : CISDOBAsyncTest
@property(strong) CISDOBIpadService *service;
@property(strong) NSURL *databaseUrl;
......
......@@ -27,13 +27,10 @@
NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeURL, NSError** error)
{
NSManagedObjectModel* mom = [NSManagedObjectModel mergedModelFromBundles: nil];
// Explicitly specify which db schema we want to use
// NSBundle* bundle = [NSBundle bundleForClass: [CISDOBIpadEntity class]];
// NSString* momPath = [bundle pathForResource: @"persistent-data-model" ofType: @"mom"];
// NSLog(@"Mom %@", momPath);
// NSManagedObjectModel* mom = [[NSManagedObjectModel alloc] initWithContentsOfURL: [NSURL fileURLWithPath: momPath]];
NSBundle* bundle = [NSBundle bundleForClass: [CISDOBIpadEntity class]];
NSString* momPath = [bundle pathForResource: @"persistent-data-model" ofType: @"momd"];
NSManagedObjectModel* mom = [[NSManagedObjectModel alloc] initWithContentsOfURL: [NSURL fileURLWithPath: momPath]];
NSManagedObjectContext* moc = [[NSManagedObjectContext alloc] init];
NSPersistentStoreCoordinator* coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: mom];
......@@ -76,4 +73,55 @@ NSManagedObjectContext* GetDatabaseManagedObjectContext(NSURL* storeURL, NSError
[super tearDown];
}
- (void)configureAndRunCallSynchronously:(CISDOBAsyncCall *)call
{
[self configureCall: call];
// The ipad service may make multiple calls, so take that into account.
int waitTime = ((int) _service.connection.timeoutInterval) * 2;
[self waitSeconds: waitTime forCallToComplete: call];
}
- (void)checkPersistRawEntities:(NSArray *)rawEntities
{
STAssertTrue([rawEntities count] > 0, @"The Pad service should have returned some entities.");
for (CISDOBIpadRawEntity *rawEntity in rawEntities) {
// Create new entities in the moc, and store them.
CISDOBIpadEntity *entity = [NSEntityDescription insertNewObjectForEntityForName: @"CISDOBIpadEntity" inManagedObjectContext: _moc];
[entity initializeFromRawEntity: rawEntity];
}
NSError *error;
STAssertTrue([_moc save: &error], @"Could not save data %@", error);
}
- (void)checkEntityCardnalityEquals:(NSUInteger)count
{
NSError* error;
NSFetchRequest* request = [[NSFetchRequest alloc] init];
NSEntityDescription* entity = [NSEntityDescription entityForName: @"CISDOBIpadEntity" inManagedObjectContext: _moc];
[request setEntity: entity];
NSArray* elements = [_moc executeFetchRequest: request error: &error];
STAssertEquals([elements count], count, @"%llu in db != %llu from server", [elements count], count);
}
- (void)testPersistEntities
{
CISDOBAsyncCall *call;
call = [_service loginUser: GetDefaultUserName() password: GetDefaultUserPassword()];
[self configureAndRunCallSynchronously: call];
call = [_service listAllEntities];
[self configureAndRunCallSynchronously: call];
STAssertNotNil(_callResult, @"The iPad service should have returned some entities.");
// Check that we can persist the entities
[self checkPersistRawEntities: _callResult];
// Check that the cardnality is equal
[self checkEntityCardnalityEquals: [_callResult count]];
}
@end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment