Skip to content
This repository was archived by the owner on Apr 18, 2023. It is now read-only.

Commit 686c8ed

Browse files
author
Vikas Dadheech
committed
Add unit tests for models and fix for bugs found during unit tests
1 parent 0437d64 commit 686c8ed

File tree

11 files changed

+462
-13
lines changed

11 files changed

+462
-13
lines changed

MSGraphCoreSDK/MSGraphCoreSDK.xcodeproj/project.pbxproj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@
3232
E52AD9F22133C8F9005FED47 /* MSURLSessionDataTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E52AD9F12133C8F9005FED47 /* MSURLSessionDataTaskTests.m */; };
3333
E52AD9F42133E171005FED47 /* MSURLSessionDownloadTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E52AD9F32133E171005FED47 /* MSURLSessionDownloadTaskTests.m */; };
3434
E52AD9F62133E740005FED47 /* MSURLSessionUploadTaskTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E52AD9F52133E73F005FED47 /* MSURLSessionUploadTaskTests.m */; };
35+
E547CE2321DC8551008D0925 /* MSDateTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E547CE2021DC8551008D0925 /* MSDateTests.m */; };
36+
E547CE2421DC8551008D0925 /* MSCollectionTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E547CE2121DC8551008D0925 /* MSCollectionTests.m */; };
37+
E547CE2521DC8551008D0925 /* MSObjectTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E547CE2221DC8551008D0925 /* MSObjectTests.m */; };
38+
E547CE2721DC9CF6008D0925 /* MSTimeOfDayTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E547CE2621DC9CF6008D0925 /* MSTimeOfDayTests.m */; };
39+
E55DFCBE21DCD21C00DE305D /* NSDate+MSSerializationTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E55DFCBD21DCD21C00DE305D /* NSDate+MSSerializationTests.m */; };
3540
E566782E2150BC4F00C6B7AE /* MSRedirectHandlerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E566782D2150BC4F00C6B7AE /* MSRedirectHandlerTests.m */; };
3641
E56678302150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m in Sources */ = {isa = PBXBuildFile; fileRef = E566782F2150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m */; };
3742
E56FDB9321D36A98001DFE68 /* MSURLSessionDownloadTask.h in Headers */ = {isa = PBXBuildFile; fileRef = E56FDB8B21D36A98001DFE68 /* MSURLSessionDownloadTask.h */; settings = {ATTRIBUTES = (Public, ); }; };
@@ -116,6 +121,11 @@
116121
E52AD9F12133C8F9005FED47 /* MSURLSessionDataTaskTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSURLSessionDataTaskTests.m; sourceTree = "<group>"; };
117122
E52AD9F32133E171005FED47 /* MSURLSessionDownloadTaskTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSURLSessionDownloadTaskTests.m; sourceTree = "<group>"; };
118123
E52AD9F52133E73F005FED47 /* MSURLSessionUploadTaskTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSURLSessionUploadTaskTests.m; sourceTree = "<group>"; };
124+
E547CE2021DC8551008D0925 /* MSDateTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSDateTests.m; sourceTree = "<group>"; };
125+
E547CE2121DC8551008D0925 /* MSCollectionTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSCollectionTests.m; sourceTree = "<group>"; };
126+
E547CE2221DC8551008D0925 /* MSObjectTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MSObjectTests.m; sourceTree = "<group>"; };
127+
E547CE2621DC9CF6008D0925 /* MSTimeOfDayTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSTimeOfDayTests.m; sourceTree = "<group>"; };
128+
E55DFCBD21DCD21C00DE305D /* NSDate+MSSerializationTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSDate+MSSerializationTests.m"; sourceTree = "<group>"; };
119129
E566782D2150BC4F00C6B7AE /* MSRedirectHandlerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSRedirectHandlerTests.m; sourceTree = "<group>"; };
120130
E566782F2150E1AF00C6B7AE /* MSAuthenticationHandlerTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MSAuthenticationHandlerTests.m; sourceTree = "<group>"; };
121131
E56FDB8B21D36A98001DFE68 /* MSURLSessionDownloadTask.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MSURLSessionDownloadTask.h; sourceTree = "<group>"; };
@@ -238,6 +248,7 @@
238248
E5069AD7211C1AD500432372 /* MSGraphCoreSDKTests */ = {
239249
isa = PBXGroup;
240250
children = (
251+
E547CE1F21D61561008D0925 /* Models */,
241252
E5204FFC2173A79100E64160 /* GraphContent */,
242253
E5B6DB6921497A3300836E1D /* Frameworks */,
243254
E573EE1121367A4000C5E884 /* HTTPClient */,
@@ -319,6 +330,18 @@
319330
path = Middleware;
320331
sourceTree = "<group>";
321332
};
333+
E547CE1F21D61561008D0925 /* Models */ = {
334+
isa = PBXGroup;
335+
children = (
336+
E547CE2121DC8551008D0925 /* MSCollectionTests.m */,
337+
E547CE2021DC8551008D0925 /* MSDateTests.m */,
338+
E547CE2221DC8551008D0925 /* MSObjectTests.m */,
339+
E547CE2621DC9CF6008D0925 /* MSTimeOfDayTests.m */,
340+
E55DFCBD21DCD21C00DE305D /* NSDate+MSSerializationTests.m */,
341+
);
342+
path = Models;
343+
sourceTree = "<group>";
344+
};
322345
E56FDB8A21D36A98001DFE68 /* Session Tasks */ = {
323346
isa = PBXGroup;
324347
children = (
@@ -648,11 +671,16 @@
648671
E573EE092134167400C5E884 /* MSClientFactoryTests.m in Sources */,
649672
E566782E2150BC4F00C6B7AE /* MSRedirectHandlerTests.m in Sources */,
650673
E573EE0B213427ED00C5E884 /* MSURLSessionTaskDelegateTests.m in Sources */,
674+
E547CE2321DC8551008D0925 /* MSDateTests.m in Sources */,
651675
E5ECE09E2142992F00A289C2 /* MSMiddlewareFactoryTests.m in Sources */,
652676
E5069AE2211C323D00432372 /* MSURLSessionManagerTests.m in Sources */,
653677
E52AD9F62133E740005FED47 /* MSURLSessionUploadTaskTests.m in Sources */,
678+
E547CE2721DC9CF6008D0925 /* MSTimeOfDayTests.m in Sources */,
654679
E5069AD9211C1AD500432372 /* MSGraphCoreSDKTests.m in Sources */,
680+
E547CE2421DC8551008D0925 /* MSCollectionTests.m in Sources */,
655681
E52AD9F02133C766005FED47 /* MSURLSessionTaskTests.m in Sources */,
682+
E547CE2521DC8551008D0925 /* MSObjectTests.m in Sources */,
683+
E55DFCBE21DCD21C00DE305D /* NSDate+MSSerializationTests.m in Sources */,
656684
E52AD9F22133C8F9005FED47 /* MSURLSessionDataTaskTests.m in Sources */,
657685
);
658686
runOnlyForDeploymentPostprocessing = 0;

MSGraphCoreSDK/MSGraphCoreSDK/Models/MSCollection.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ - (instancetype)initWithDictionary:(NSDictionary *)dictionary
3636
self = [super initWithDictionary:dictionary];
3737
if(self)
3838
{
39-
self.value = [[self getDictionary] objectForKey:@"values"];
39+
self.value = [[self getDictionary] objectForKey:@"value"];
4040
self.nextLink = [NSURL URLWithString:[[self getDictionary] objectForKey:@"@odata.nextLink"]];
4141
self.additionalData = [self getDictionary];
4242
}

MSGraphCoreSDK/MSGraphCoreSDK/Models/MSObject.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
/*
88
Creates an MSObject with the given data.
99
@param data The NSData instance from the response.
10-
@error If there are any errors during the conversion of data then it will be assigned to this.
10+
@param error If an error occurs, the error parameter will be set and the return value will be nil.
1111
@warning This method will return nil if the data is nil.
1212
*/
1313
-(instancetype)initWithData:(NSData *)data error:(NSError **)error;;
@@ -27,7 +27,7 @@ This method returns the underlying dictionary which is used to construct the mod
2727

2828
/*
2929
This method converts the internal dictionary to serialized data.
30-
@param error If there are any errors during the conversion of internal dictionary then it will be assigned to this.
30+
@param error If an error occurs, the error parameter will be set and the return value will be nil.
3131
@return NSData representation of the internal dictionary,
3232
*/
3333
- (NSData *)getSerializedDataWithError:(NSError **)error;;

MSGraphCoreSDK/MSGraphCoreSDK/Models/MSTimeOfDay.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
@param minute Integer value denoting the minutes of that hour
1818
@param second Integer value denoting the seconds of that minute
1919
*/
20-
+ (instancetype) timeWithSeconds:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;
20+
+ (instancetype) timeWithHour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;
2121

2222
/*
2323
Instance method to create an instance of MSTimeOfDay
2424
@param hour Integer value denoting the hour of day
2525
@param minute Integer value denoting the minutes of that hour
2626
@param second Integer value denoting the seconds of that minute
2727
*/
28-
- (id) initWithSeconds:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;
28+
- (id) initWithHour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;
2929

3030
/*
3131
This method generates a string equivalent of MSTimeOfDay instance.

MSGraphCoreSDK/MSGraphCoreSDK/Models/MSTimeOfDay.m

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#import "MSTimeOfDay.h"
44

5-
static NSString *dateFormat = @"HH:mm:SS";
5+
static NSString *dateFormat = @"HH:mm:ss";
66

77
@interface MSTimeOfDay ()
88

@@ -14,9 +14,9 @@ @implementation MSTimeOfDay
1414

1515
#pragma mark - Initializers
1616

17-
+ (instancetype) timeWithSeconds:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
17+
+ (instancetype) timeWithHour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
1818
{
19-
return [[MSTimeOfDay alloc] initWithSeconds:hour minute:minute second:second];
19+
return [[MSTimeOfDay alloc] initWithHour:hour minute:minute second:second];
2020
}
2121

2222
- (id) initWithNSDate:(NSDate*)date
@@ -28,7 +28,7 @@ - (id) initWithNSDate:(NSDate*)date
2828
return self;
2929
}
3030

31-
- (id) initWithSeconds:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
31+
- (id) initWithHour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second
3232
{
3333
if (self = [super init])
3434
{
@@ -46,19 +46,19 @@ - (id) initWithSeconds:(NSInteger)hour minute:(NSInteger)minute second:(NSIntege
4646

4747
- (NSInteger) hour
4848
{
49-
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitYear fromDate:self.date];
49+
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitHour fromDate:self.date];
5050
return components.hour;
5151
}
5252

5353
- (NSInteger) minute
5454
{
55-
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitMonth fromDate:self.date];
55+
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitMinute fromDate:self.date];
5656
return components.minute;
5757
}
5858

5959
- (NSInteger) second
6060
{
61-
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitDay fromDate:self.date];
61+
NSDateComponents *components = [[MSTimeOfDay gregorianCalendar] components:NSCalendarUnitSecond fromDate:self.date];
6262
return components.second;
6363
}
6464

MSGraphCoreSDK/MSGraphCoreSDK/Models/NSDate+MSSerialization.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@implementation NSDate (Serialization)
66

77
static NSString *dateFormatWithMillis = @"yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ";
8-
static NSString *dateFormatWithoutMillis = @"yyyy-MM-dd'T'HH:mm:ssZ";
8+
static NSString *dateFormatWithoutMillis = @"yyyy-MM-dd'T'HH:mm:ssZZZZZ";
99

1010
- (NSString *)ms_toString
1111
{
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
//
4+
5+
#import <XCTest/XCTest.h>
6+
#import "MSCollection.h"
7+
#import "MSConstants.h"
8+
9+
@interface MSCollectionTests : XCTestCase
10+
@end
11+
12+
@implementation MSCollectionTests {
13+
NSDictionary *collectionDictionary;
14+
NSArray *valueArray;
15+
NSURL *nextLink;
16+
}
17+
18+
- (void)setUp {
19+
[super setUp];
20+
NSDictionary *userItemDict1 = @{
21+
@"@odata.context": @"https://graph.microsoft.com/v1.0/$metadata#users/$entity",
22+
@"displayName": @"Tester 1",
23+
@"mail": @"[email protected]"};
24+
NSDictionary *userItemDict2 = @{
25+
@"@odata.context": @"https://graph.microsoft.com/v1.0/$metadata#users/$entity",
26+
@"displayName": @"Tester 2",
27+
@"mail": @"[email protected]"};
28+
nextLink = [NSURL URLWithString:MSGraphBaseURL];
29+
valueArray = @[userItemDict1, userItemDict2];
30+
collectionDictionary = @{
31+
@"@odata.context": @"https://graph.microsoft.com/v1.0/$metadata#users",
32+
@"@odata.nextLink": [nextLink absoluteString],
33+
@"value": valueArray
34+
};
35+
}
36+
37+
- (void)tearDown {
38+
[super tearDown];
39+
}
40+
41+
- (void)testInitWithArray {
42+
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"testDict",@"dictKey1", nil];
43+
44+
MSCollection *msCollection = [[MSCollection alloc] initWithArray:valueArray nextLink:[nextLink absoluteString] additionalData:dic];
45+
XCTAssertNotNil(msCollection);
46+
XCTAssertEqualObjects(msCollection.value, valueArray);
47+
XCTAssertTrue([msCollection.nextLink.absoluteString isEqualToString:[nextLink absoluteString]]);
48+
XCTAssertEqualObjects(msCollection.additionalData, dic);
49+
50+
}
51+
52+
- (void)testInitWithData {
53+
NSError *error;
54+
NSData *collectionData = [NSJSONSerialization dataWithJSONObject:collectionDictionary options:kNilOptions error:&error];
55+
XCTAssertNil(error);
56+
57+
MSCollection *msCollection = [[MSCollection alloc] initWithData:collectionData error:&error];
58+
XCTAssertNil(error);
59+
XCTAssertNotNil(msCollection);
60+
XCTAssertEqualObjects(msCollection.value, valueArray);
61+
XCTAssertTrue([msCollection.nextLink.absoluteString isEqualToString:[nextLink absoluteString]]);
62+
XCTAssertEqualObjects(msCollection.additionalData, collectionDictionary);
63+
64+
}
65+
66+
- (void)testInitWithDictionary {
67+
NSError *error;
68+
MSCollection *msCollection = [[MSCollection alloc] initWithDictionary:collectionDictionary];
69+
XCTAssertNil(error);
70+
XCTAssertNotNil(msCollection);
71+
XCTAssertEqualObjects(msCollection.value, valueArray);
72+
XCTAssertTrue([msCollection.nextLink.absoluteString isEqualToString:[nextLink absoluteString]]);
73+
XCTAssertEqualObjects(msCollection.additionalData, collectionDictionary);
74+
}
75+
76+
77+
@end
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. See License in the project root for license information.
3+
//
4+
5+
#import <XCTest/XCTest.h>
6+
#import <Foundation/Foundation.h>
7+
#import "MSDate.h"
8+
9+
@interface MSDate (Test)
10+
11+
@property (nonatomic, strong) NSDate *date;
12+
13+
@end
14+
15+
@interface MSDateTests : XCTestCase
16+
@property NSInteger testYear;
17+
@property NSInteger testMonth;
18+
@property NSInteger testDay;
19+
@property NSDateComponents *dateComponents;
20+
@end
21+
22+
@implementation MSDateTests
23+
24+
- (void)setUp {
25+
[super setUp];
26+
self.testYear = 2016;
27+
self.testMonth = 6;
28+
self.testDay = 11;
29+
self.dateComponents = [[NSDateComponents alloc] init];
30+
self.dateComponents.year = _testYear;
31+
self.dateComponents.month = _testMonth;
32+
self.dateComponents.day = _testDay;
33+
self.dateComponents.calendar = [NSCalendar currentCalendar];
34+
}
35+
36+
- (void)tearDown {
37+
// Put teardown code here. This method is called after the invocation of each test method in the class.
38+
[super tearDown];
39+
}
40+
-(void)testInit{
41+
MSDate *msdate = [MSDate date];
42+
XCTAssertNotNil(msdate);
43+
44+
msdate = [MSDate dateWithYear:_testYear month:_testMonth day:_testDay];
45+
XCTAssertNotNil(msdate);
46+
47+
XCTAssertNoThrow([MSDate dateWithYear:0 month:0 day:0]);
48+
}
49+
50+
-(void)testInitWithNSDate{
51+
NSDate *nowData = [NSDate date];
52+
MSDate *msdate = [[MSDate alloc] initWithNSDate: nowData];
53+
XCTAssertNotNil(msdate);
54+
XCTAssertEqualObjects(msdate.date, nowData);
55+
}
56+
-(void)testInitWithNilNSDate{
57+
MSDate *msdate = [[MSDate alloc] initWithNSDate: nil];
58+
XCTAssertNotNil(msdate);
59+
XCTAssertNil(msdate.date);
60+
}
61+
-(void)testInitWithYear{
62+
MSDate *msdate = [[MSDate alloc] initWithYear:_testYear month:_testMonth day:_testDay];
63+
XCTAssertEqualObjects(msdate.date, [_dateComponents date]);
64+
}
65+
66+
-(void)testMSDatePropertiesFromInitWithYear{
67+
MSDate *msdate = [[MSDate alloc] initWithYear:_testYear month:_testMonth day:_testDay];
68+
XCTAssertEqual([msdate year], _testYear);
69+
XCTAssertEqual(msdate.month, _testMonth);
70+
XCTAssertEqual(msdate.day, _testDay);
71+
}
72+
-(void)testMSDateProertiesFromInitWithDate{
73+
74+
NSDate *nowData = [NSDate date];
75+
MSDate *msdate = [[MSDate alloc] initWithNSDate: nowData];
76+
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
77+
XCTAssertEqual(msdate.year, components.year);
78+
XCTAssertEqual(msdate.month, components.month);
79+
XCTAssertEqual(msdate.day, components.day);
80+
81+
}
82+
83+
-(void)testMS_toString{
84+
MSDate *msdate = [[MSDate alloc] initWithYear:_testYear month:_testMonth day:_testDay];
85+
NSString *dateString = [msdate ms_toString];
86+
NSString *expectedString =[NSString stringWithFormat:@"%@-0%@-%@",[@(_testYear) stringValue],[@(_testMonth) stringValue],[@(_testDay) stringValue]];
87+
XCTAssertEqualObjects(dateString, expectedString);
88+
}
89+
90+
-(void)testMS_dateFromString{
91+
MSDate *msdate = [MSDate ms_dateFromString:[NSString stringWithFormat:@"%@-0%@-%@",[@(_testYear) stringValue],[@(_testMonth) stringValue],[@(_testDay) stringValue]]];
92+
93+
XCTAssertNotNil(msdate);
94+
XCTAssertEqualObjects(msdate.date, [_dateComponents date]);
95+
}
96+
-(void)testMS_dateFromNilString{
97+
MSDate *msdate = [MSDate ms_dateFromString:nil];
98+
XCTAssertNotNil(msdate);
99+
XCTAssertNil(msdate.date);
100+
}
101+
-(void)testMS_dateFromInvalidFormatterString{
102+
MSDate *msdate = [MSDate ms_dateFromString:@"test"];
103+
XCTAssertNotNil(msdate);
104+
XCTAssertNil(msdate.date);
105+
}
106+
107+
@end

0 commit comments

Comments
 (0)