Commit 098d1243 authored by 井庆林's avatar 井庆林

添加单元测试

parent dfe48e19
......@@ -27,6 +27,10 @@ NSString *const MockCityId = @"beijing";
- (void)setUp {
[super setUp];
_client = [Phobos clientWithAppName:MockAppName channelId:MockChannelId];
_client.serverAPI = @"http://log.test.igengmei.com/log/collect";
[_client setLogEnabled:NO]; // 调试打Log模式,看情况开启
_client.signingType = PhobosSigningTypeDebug;
_client.userId = @"";
[Phobos setSharedClient:_client];
}
......@@ -34,6 +38,7 @@ NSString *const MockCityId = @"beijing";
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
[GMCache removeObjectAtDocumentPathWithkey:PhobosTempCacheKey];
}
/**
......@@ -107,10 +112,11 @@ NSString *const MockCityId = @"beijing";
- (void)testTrackEventWithAttrAndSendNowDonotClearNormal {
// Given
NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
// When
[Phobos track:MockEventId attributes:attr sendNow:NO];
// When
[Phobos track:MockEventId attributes:attr sendNow:YES];
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count != 0, @"array shouldn't be empty");
......@@ -122,21 +128,23 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithDoubleAttrAndSendNow {
// Given
NSDictionary *attr = @{@"attr":@"track_attr"};
// When
[Phobos track:MockEventId attributes:attr sendNow:YES];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
// When
[Phobos track:MockEventId attributes:attr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
// 因为实时埋点是异步删除,所以这个位置暂时延时取数据,待优化 TODO
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr sendNow:YES];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosTempCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
});
});
}
/**
......@@ -145,8 +153,6 @@ NSString *const MockCityId = @"beijing";
* @since 1.1.4
*/
- (void)testTrackEventWithDoubleAttrAndNoSendNow {
// remove 保证PhobosCacheKey起始数据为空
[GMCache removeObjectAtDocumentPathWithkey:PhobosCacheKey];
NSDictionary *attr = @{@"attr":@"track_attr"};
//
[Phobos track:MockEventId attributes:attr];
......@@ -166,6 +172,103 @@ NSString *const MockCityId = @"beijing";
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
}
/**
* @brief 测试不实时和实时穿插 1
*
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowOne {
NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
for (int i = 0; i < 30; i++) {
[Phobos track:MockEventId attributes:attr];
}
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
// PhobosCacheKey超过50条数据会自动上报,模拟此情况
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 30, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 31, @"array shouldn't be empty");
}
/**
* @brief 测试不实时和实时穿插 2
*
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowTwo {
NSDictionary *attr = @{@"attr":@"track_attr"};
NSDictionary *sendNowAttr = @{@"attr":@"track_attr_send_now"};
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:sendNowAttr sendNow:YES];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 2, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 3, @"array shouldn't be empty");
}
/**
* @brief 测试不实时和实时穿插 3
*
* @since 1.1.4
*/
- (void)testTrackEventWithSendNowAndNoSendNowThree {
NSDictionary *attr = @{@"attr":@"track_attr"};
//
[Phobos track:MockEventId attributes:attr];
NSArray *array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
// PhobosCacheKey超过50条数据会自动上报,模拟此情况
for (int i = 0; i < 50; i++) {
[Phobos track:MockEventId attributes:attr];
}
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 0, @"array shouldn't be empty");
[Phobos track:MockEventId attributes:attr];
array = [GMCache fetchObjectAtDocumentPathWithkey:PhobosCacheKey];
XCTAssertTrue(array.count == 1, @"array shouldn't be empty");
}
- (void)verfiyDict:(NSDictionary *)dict{
NSArray *keys = [dict allKeys];
XCTAssertTrue([keys containsObject:@"type"], @"Missing type");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment