JXAPIServiceTests.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // JXAPIServiceTests.m
  3. // AICityTests
  4. //
  5. // Feature: 010-ui-ios
  6. // API服务单元测试 - Phase 1 API层
  7. //
  8. #import <XCTest/XCTest.h>
  9. #import "JXAPIService.h"
  10. #import "JXNetworkManager.h"
  11. @interface JXAPIServiceTests : XCTestCase
  12. @property (nonatomic, strong) JXAPIService *apiService;
  13. @end
  14. @implementation JXAPIServiceTests
  15. - (void)setUp {
  16. [super setUp];
  17. self.apiService = [[JXAPIService alloc] init];
  18. }
  19. - (void)tearDown {
  20. self.apiService = nil;
  21. [super tearDown];
  22. }
  23. #pragma mark - 内容相关API测试
  24. - (void)testGetDramaList {
  25. XCTestExpectation *expectation = [self expectationWithDescription:@"Get drama list"];
  26. [self.apiService getDramaListWithCategory:@"romance"
  27. page:1
  28. pageSize:20
  29. success:^(id responseObject) {
  30. XCTAssertNotNil(responseObject);
  31. XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]]);
  32. NSLog(@"✅ 获取短剧列表成功: %@", responseObject);
  33. [expectation fulfill];
  34. } failure:^(NSError *error) {
  35. XCTFail(@"获取短剧列表失败: %@", error.localizedDescription);
  36. [expectation fulfill];
  37. }];
  38. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  39. }
  40. - (void)testGetDramaDetail {
  41. XCTestExpectation *expectation = [self expectationWithDescription:@"Get drama detail"];
  42. [self.apiService getDramaDetailWithDramaId:@"test_drama_001"
  43. success:^(id responseObject) {
  44. XCTAssertNotNil(responseObject);
  45. XCTAssertTrue([responseObject isKindOfClass:[NSDictionary class]]);
  46. NSDictionary *data = responseObject[@"data"];
  47. XCTAssertNotNil(data);
  48. NSLog(@"✅ 获取短剧详情成功: %@", data);
  49. [expectation fulfill];
  50. } failure:^(NSError *error) {
  51. NSLog(@"⚠️ 获取短剧详情失败(预期可能失败): %@", error.localizedDescription);
  52. [expectation fulfill];
  53. }];
  54. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  55. }
  56. - (void)testSearchDramas {
  57. XCTestExpectation *expectation = [self expectationWithDescription:@"Search dramas"];
  58. [self.apiService searchDramasWithKeyword:@"爱情"
  59. page:1
  60. pageSize:20
  61. success:^(id responseObject) {
  62. XCTAssertNotNil(responseObject);
  63. NSLog(@"✅ 搜索短剧成功: %@", responseObject);
  64. [expectation fulfill];
  65. } failure:^(NSError *error) {
  66. NSLog(@"⚠️ 搜索短剧失败: %@", error.localizedDescription);
  67. [expectation fulfill];
  68. }];
  69. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  70. }
  71. #pragma mark - 交互相关API测试
  72. - (void)testLikeDrama {
  73. XCTestExpectation *expectation = [self expectationWithDescription:@"Like drama"];
  74. [self.apiService likeDramaWithDramaId:@"test_drama_001"
  75. success:^(id responseObject) {
  76. XCTAssertNotNil(responseObject);
  77. NSLog(@"✅ 点赞短剧成功: %@", responseObject);
  78. [expectation fulfill];
  79. } failure:^(NSError *error) {
  80. NSLog(@"⚠️ 点赞短剧失败(可能需要登录): %@", error.localizedDescription);
  81. [expectation fulfill];
  82. }];
  83. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  84. }
  85. - (void)testFavoriteDrama {
  86. XCTestExpectation *expectation = [self expectationWithDescription:@"Favorite drama"];
  87. [self.apiService favoriteDramaWithDramaId:@"test_drama_001"
  88. success:^(id responseObject) {
  89. XCTAssertNotNil(responseObject);
  90. NSLog(@"✅ 收藏短剧成功: %@", responseObject);
  91. [expectation fulfill];
  92. } failure:^(NSError *error) {
  93. NSLog(@"⚠️ 收藏短剧失败(可能需要登录): %@", error.localizedDescription);
  94. [expectation fulfill];
  95. }];
  96. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  97. }
  98. #pragma mark - 评论相关API测试
  99. - (void)testGetComments {
  100. XCTestExpectation *expectation = [self expectationWithDescription:@"Get comments"];
  101. [self.apiService getCommentsWithDramaId:@"test_drama_001"
  102. page:1
  103. pageSize:20
  104. success:^(id responseObject) {
  105. XCTAssertNotNil(responseObject);
  106. NSLog(@"✅ 获取评论列表成功: %@", responseObject);
  107. [expectation fulfill];
  108. } failure:^(NSError *error) {
  109. NSLog(@"⚠️ 获取评论列表失败: %@", error.localizedDescription);
  110. [expectation fulfill];
  111. }];
  112. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  113. }
  114. - (void)testSubmitComment {
  115. XCTestExpectation *expectation = [self expectationWithDescription:@"Submit comment"];
  116. [self.apiService submitCommentWithDramaId:@"test_drama_001"
  117. content:@"这部剧太好看了!"
  118. success:^(id responseObject) {
  119. XCTAssertNotNil(responseObject);
  120. NSLog(@"✅ 提交评论成功: %@", responseObject);
  121. [expectation fulfill];
  122. } failure:^(NSError *error) {
  123. NSLog(@"⚠️ 提交评论失败(可能需要登录): %@", error.localizedDescription);
  124. [expectation fulfill];
  125. }];
  126. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  127. }
  128. #pragma mark - 合集相关API测试
  129. - (void)testGetEpisodes {
  130. XCTestExpectation *expectation = [self expectationWithDescription:@"Get episodes"];
  131. [self.apiService getEpisodesWithDramaId:@"test_drama_001"
  132. success:^(id responseObject) {
  133. XCTAssertNotNil(responseObject);
  134. if ([responseObject isKindOfClass:[NSArray class]]) {
  135. NSArray *episodes = (NSArray *)responseObject;
  136. NSLog(@"✅ 获取剧集列表成功,共%lu集", (unsigned long)episodes.count);
  137. }
  138. [expectation fulfill];
  139. } failure:^(NSError *error) {
  140. NSLog(@"⚠️ 获取剧集列表失败: %@", error.localizedDescription);
  141. [expectation fulfill];
  142. }];
  143. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  144. }
  145. #pragma mark - API并发测试
  146. - (void)testConcurrentAPIRequests {
  147. XCTestExpectation *expectation1 = [self expectationWithDescription:@"Request 1"];
  148. XCTestExpectation *expectation2 = [self expectationWithDescription:@"Request 2"];
  149. XCTestExpectation *expectation3 = [self expectationWithDescription:@"Request 3"];
  150. // 同时发起3个API请求
  151. [self.apiService getDramaListWithCategory:@"romance" page:1 pageSize:10 success:^(id responseObject) {
  152. NSLog(@"✅ 并发请求1完成");
  153. [expectation1 fulfill];
  154. } failure:^(NSError *error) {
  155. [expectation1 fulfill];
  156. }];
  157. [self.apiService getDramaListWithCategory:@"urban" page:1 pageSize:10 success:^(id responseObject) {
  158. NSLog(@"✅ 并发请求2完成");
  159. [expectation2 fulfill];
  160. } failure:^(NSError *error) {
  161. [expectation2 fulfill];
  162. }];
  163. [self.apiService searchDramasWithKeyword:@"测试" page:1 pageSize:10 success:^(id responseObject) {
  164. NSLog(@"✅ 并发请求3完成");
  165. [expectation3 fulfill];
  166. } failure:^(NSError *error) {
  167. [expectation3 fulfill];
  168. }];
  169. [self waitForExpectationsWithTimeout:15.0 handler:nil];
  170. }
  171. #pragma mark - 网络错误处理测试
  172. - (void)testInvalidDramaId {
  173. XCTestExpectation *expectation = [self expectationWithDescription:@"Invalid drama ID"];
  174. [self.apiService getDramaDetailWithDramaId:@"invalid_id_999999"
  175. success:^(id responseObject) {
  176. NSLog(@"⚠️ 意外成功(可能返回404或空数据): %@", responseObject);
  177. [expectation fulfill];
  178. } failure:^(NSError *error) {
  179. NSLog(@"✅ 正确处理无效ID: %@", error.localizedDescription);
  180. XCTAssertNotNil(error);
  181. [expectation fulfill];
  182. }];
  183. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  184. }
  185. - (void)testEmptySearchKeyword {
  186. XCTestExpectation *expectation = [self expectationWithDescription:@"Empty keyword"];
  187. [self.apiService searchDramasWithKeyword:@""
  188. page:1
  189. pageSize:20
  190. success:^(id responseObject) {
  191. NSLog(@"✅ 空关键词搜索完成: %@", responseObject);
  192. [expectation fulfill];
  193. } failure:^(NSError *error) {
  194. NSLog(@"✅ 空关键词搜索失败(预期行为): %@", error.localizedDescription);
  195. [expectation fulfill];
  196. }];
  197. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  198. }
  199. #pragma mark - 性能测试
  200. - (void)testPerformanceGetDramaList {
  201. [self measureBlock:^{
  202. XCTestExpectation *expectation = [self expectationWithDescription:@"Performance test"];
  203. [self.apiService getDramaListWithCategory:@"all"
  204. page:1
  205. pageSize:20
  206. success:^(id responseObject) {
  207. [expectation fulfill];
  208. } failure:^(NSError *error) {
  209. [expectation fulfill];
  210. }];
  211. [self waitForExpectationsWithTimeout:5.0 handler:nil];
  212. }];
  213. }
  214. #pragma mark - 参数验证测试
  215. - (void)testPaginationParameters {
  216. XCTestExpectation *expectation = [self expectationWithDescription:@"Pagination test"];
  217. // 测试第2页,每页10条
  218. [self.apiService getDramaListWithCategory:@"all"
  219. page:2
  220. pageSize:10
  221. success:^(id responseObject) {
  222. NSLog(@"✅ 分页参数测试成功");
  223. [expectation fulfill];
  224. } failure:^(NSError *error) {
  225. NSLog(@"⚠️ 分页参数测试失败: %@", error.localizedDescription);
  226. [expectation fulfill];
  227. }];
  228. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  229. }
  230. - (void)testLargePageSize {
  231. XCTestExpectation *expectation = [self expectationWithDescription:@"Large page size"];
  232. // 测试大页码(可能被服务器限制)
  233. [self.apiService getDramaListWithCategory:@"all"
  234. page:1
  235. pageSize:100
  236. success:^(id responseObject) {
  237. NSLog(@"✅ 大页码测试完成: %@", responseObject);
  238. [expectation fulfill];
  239. } failure:^(NSError *error) {
  240. NSLog(@"⚠️ 大页码被服务器拒绝(预期行为): %@", error.localizedDescription);
  241. [expectation fulfill];
  242. }];
  243. [self waitForExpectationsWithTimeout:10.0 handler:nil];
  244. }
  245. @end