Permalink
Browse files

Add to json method to response.

  • Loading branch information...
1 parent 8af3b81 commit fc7345372c20e0a1e494ba292213c4bd5e96856e @jdzak jdzak committed Mar 27, 2012
Showing with 29 additions and 9 deletions.
  1. +1 −0 NUSurveyor/Models/NUResponse.h
  2. +7 −1 NUSurveyor/Models/NUResponse.m
  3. +21 −8 NUSurveyorTests/NUResponseTest.m
@@ -11,5 +11,6 @@
@interface NUResponse : NSManagedObject
- (NSDictionary*) toDict;
+- (NSString*) toJson;
@end
@@ -7,15 +7,21 @@
//
#import "NUResponse.h"
+#import "SBJson.h"
@implementation NUResponse
- (NSDictionary*) toDict {
return [[NSDictionary alloc] initWithObjectsAndKeys:
- [self valueForKey:@"uuid"], @"response_id",
+ [self valueForKey:@"uuid"], @"uuid",
[self valueForKey:@"answer"], @"answer_id",
[self valueForKey:@"question"], @"question_id",
[self valueForKey:@"value"], @"value", nil];
}
+- (NSString*) toJson {
+ SBJsonWriter* w = [[SBJsonWriter alloc] init];
+ return [w stringWithObject:[self toDict]];
+}
+
@end
@@ -14,6 +14,8 @@ @implementation NUResponseTest
@synthesize bundle = _bundle, coord = _coord, ctx = _ctx, model = _model, store = _store;
+NUResponse* response;
+
- (void)setUp
{
[super setUp];
@@ -29,6 +31,12 @@ - (void)setUp
error: NULL];
self.ctx = [[NSManagedObjectContext alloc] init];
[self.ctx setPersistentStoreCoordinator: self.coord];
+
+ /* Test Data */
+ NSDictionary* s = [[NSDictionary alloc] initWithObjectsAndKeys:@"RECT", @"uuid", nil];
+ NUResponseSet* rs = [NUResponseSet newResponseSetForSurvey:s withModel:self.model inContext:self.ctx];
+ response = [rs newResponseForQuestion:@"abc" Answer:@"123" Value:@"foo"];
+ [response setValue:@"OCT" forKey:@"uuid"];
}
@@ -47,6 +55,9 @@ - (void)tearDown
[super tearDown];
}
+- (void)testThatEnvironmentWorks {
+ STAssertNotNil(self.store, @"no persistent store");
+}
- (void)testSanity {
NUResponseSet* rs = [NUResponseSet newResponseSetForSurvey:[NSDictionary dictionary] withModel:self.model inContext:self.ctx];
@@ -57,17 +68,19 @@ - (void)testSanity {
}
- (void)testToDict {
- NSDictionary* s = [[NSDictionary alloc] initWithObjectsAndKeys:@"RECT", @"uuid", nil];
- NUResponseSet* rs = [NUResponseSet newResponseSetForSurvey:s withModel:self.model inContext:self.ctx];
- NUResponse* one = [rs newResponseForQuestion:@"abc" Answer:@"123" Value:@"foo"];
- [one setValue:@"OCT" forKey:@"uuid"];
-
- NSDictionary* actual = [one toDict];
-
+ NSDictionary* actual = [response toDict];
STAssertEqualObjects([actual objectForKey:@"value"], @"foo", @"Wrong value");
STAssertEqualObjects([actual objectForKey:@"answer_id"], @"123", @"Wrong value");
STAssertEqualObjects([actual objectForKey:@"question_id"], @"abc", @"Wrong value");
- STAssertEqualObjects([actual objectForKey:@"response_id"], @"OCT", @"Wrong value");
+ STAssertEqualObjects([actual objectForKey:@"uuid"], @"OCT", @"Wrong value");
+}
+
+- (void)testToJson {
+ NSString* actual = [response toJson];
+ STAssertTrue([actual rangeOfString:@"\"value\":\"foo\""].location != NSNotFound, @"Should exist");
+ STAssertTrue([actual rangeOfString:@"\"answer_id\":\"123\""].location != NSNotFound, @"Should exist");
+ STAssertTrue([actual rangeOfString:@"\"question_id\":\"abc\""].location != NSNotFound, @"Should exist");
+ STAssertTrue([actual rangeOfString:@"\"uuid\":\"OCT\""].location != NSNotFound, @"Should exist");
}
@end

0 comments on commit fc73453

Please sign in to comment.