|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:parse_server_sdk/parse_server_sdk.dart'; |
| 4 | +import 'package:test/test.dart'; |
| 5 | + |
| 6 | +import '../../../test_utils.dart'; |
| 7 | + |
| 8 | +void main() { |
| 9 | + group('handleResponse()', () { |
| 10 | + setUp(() async { |
| 11 | + await initializeParse(); |
| 12 | + }); |
| 13 | + |
| 14 | + group('when batch', () { |
| 15 | + test( |
| 16 | + 'should return a ParseResponse holds a list of created/updated ParseObjects', |
| 17 | + () { |
| 18 | + // arrange |
| 19 | + final object1 = ParseObject("object1"); |
| 20 | + |
| 21 | + final object2 = ParseObject("object2")..objectId = "GYfSRRRXbL"; |
| 22 | + |
| 23 | + final objectsToBatch = [object1, object2]; |
| 24 | + |
| 25 | + const object1ResultFromServerObjectId = "YAfSAWwXbL"; |
| 26 | + const object1ResultFromServerCreatedAt = "2023-03-19T00:20:37.187Z"; |
| 27 | + |
| 28 | + const object2ResultFromServerUpdatedAt = "2023-03-19T00:20:37.187Z"; |
| 29 | + |
| 30 | + final resultFromServerForBatch = json.encode( |
| 31 | + [ |
| 32 | + { |
| 33 | + "success": { |
| 34 | + keyVarObjectId: object1ResultFromServerObjectId, |
| 35 | + keyVarCreatedAt: object1ResultFromServerCreatedAt, |
| 36 | + } |
| 37 | + }, |
| 38 | + { |
| 39 | + "success": { |
| 40 | + keyVarUpdatedAt: object2ResultFromServerUpdatedAt, |
| 41 | + } |
| 42 | + } |
| 43 | + ], |
| 44 | + ); |
| 45 | + |
| 46 | + final result = ParseNetworkResponse( |
| 47 | + data: resultFromServerForBatch, |
| 48 | + statusCode: 200, |
| 49 | + ); |
| 50 | + |
| 51 | + // act |
| 52 | + final response = handleResponse( |
| 53 | + objectsToBatch, |
| 54 | + result, |
| 55 | + ParseApiRQ.batch, |
| 56 | + true, |
| 57 | + 'test_batch', |
| 58 | + ); |
| 59 | + |
| 60 | + // assert |
| 61 | + expect(response.success, isTrue); |
| 62 | + |
| 63 | + expect(response.count, equals(2)); |
| 64 | + |
| 65 | + expect(response.error, isNull); |
| 66 | + |
| 67 | + final resultsObjectsList = List<ParseObject>.from(response.results!); |
| 68 | + |
| 69 | + expect(resultsObjectsList.length, equals(2)); |
| 70 | + |
| 71 | + final firstObject = resultsObjectsList[0]; |
| 72 | + final secondObject = resultsObjectsList[1]; |
| 73 | + |
| 74 | + expect(firstObject.objectId, equals(object1ResultFromServerObjectId)); |
| 75 | + |
| 76 | + expect( |
| 77 | + firstObject.createdAt!.toIso8601String(), |
| 78 | + equals(object1ResultFromServerCreatedAt), |
| 79 | + ); |
| 80 | + |
| 81 | + expect( |
| 82 | + secondObject.updatedAt!.toIso8601String(), |
| 83 | + equals(object2ResultFromServerUpdatedAt), |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + test( |
| 88 | + 'should return a ParseResponse holds a list of ParseObject and ParseError', |
| 89 | + () { |
| 90 | + // arrange |
| 91 | + final object1 = ParseObject("object1"); |
| 92 | + final object2 = ParseObject("object2"); |
| 93 | + |
| 94 | + final objectsToBatch = [object1, object2]; |
| 95 | + |
| 96 | + const object1ResultFromServerObjectId = "YAfSAWwXbL"; |
| 97 | + const object1ResultFromServerCreatedAt = "2023-03-19T00:20:37.187Z"; |
| 98 | + |
| 99 | + final resultFromServerForBatch = json.encode( |
| 100 | + [ |
| 101 | + { |
| 102 | + "success": { |
| 103 | + keyVarObjectId: object1ResultFromServerObjectId, |
| 104 | + keyVarCreatedAt: object1ResultFromServerCreatedAt, |
| 105 | + } |
| 106 | + }, |
| 107 | + // error while saving the second object |
| 108 | + { |
| 109 | + "error": { |
| 110 | + "code": ParseError.internalServerError, |
| 111 | + "error": "internal server error", |
| 112 | + } |
| 113 | + } |
| 114 | + ], |
| 115 | + ); |
| 116 | + |
| 117 | + final result = ParseNetworkResponse( |
| 118 | + data: resultFromServerForBatch, |
| 119 | + statusCode: 200, |
| 120 | + ); |
| 121 | + |
| 122 | + // act |
| 123 | + final response = handleResponse( |
| 124 | + objectsToBatch, |
| 125 | + result, |
| 126 | + ParseApiRQ.batch, |
| 127 | + true, |
| 128 | + 'test_batch', |
| 129 | + ); |
| 130 | + |
| 131 | + // assert |
| 132 | + expect(response.success, isTrue); |
| 133 | + |
| 134 | + expect(response.count, equals(2)); |
| 135 | + |
| 136 | + expect(response.error, isNull); |
| 137 | + |
| 138 | + expect(response.results, isNotNull); |
| 139 | + |
| 140 | + final resultsList = response.results!; |
| 141 | + |
| 142 | + expect(resultsList.length, equals(2)); |
| 143 | + |
| 144 | + final ParseObject firstObject = resultsList[0]; |
| 145 | + |
| 146 | + final ParseError secondObjectError = resultsList[1]; |
| 147 | + |
| 148 | + expect(firstObject.objectId, equals(object1ResultFromServerObjectId)); |
| 149 | + |
| 150 | + expect( |
| 151 | + firstObject.createdAt!.toIso8601String(), |
| 152 | + equals(object1ResultFromServerCreatedAt), |
| 153 | + ); |
| 154 | + |
| 155 | + expect( |
| 156 | + secondObjectError.code, |
| 157 | + equals(ParseError.internalServerError), |
| 158 | + ); |
| 159 | + |
| 160 | + expect(object2.objectId, isNull); |
| 161 | + }); |
| 162 | + }); |
| 163 | + }); |
| 164 | +} |
0 commit comments