1414
1515import 'package:firebase_core/firebase_core.dart' ;
1616import 'package:firebase_data_connect/firebase_data_connect.dart' ;
17+ import 'package:firebase_data_connect/src/core/ref.dart' ;
18+ import 'package:firebase_data_connect/src/network/rest_library.dart' ;
1719import 'package:firebase_data_connect/src/common/common_library.dart' ;
1820import 'package:firebase_data_connect/src/cache/cache_data_types.dart' ;
1921import 'package:firebase_data_connect/src/cache/cache_manager.dart' ;
@@ -22,6 +24,8 @@ import 'package:firebase_data_connect/src/cache/in_memory_cache_provider.dart';
2224import 'package:firebase_data_connect/src/cache/sqlite_cache_provider.dart' ;
2325import 'package:flutter/foundation.dart' ;
2426
27+ import 'package:http/http.dart' as http;
28+
2529import 'package:flutter_test/flutter_test.dart' ;
2630import 'dart:convert' ;
2731
@@ -31,6 +35,7 @@ import 'package:mockito/mockito.dart';
3135import '../core/ref_test.dart' ;
3236@GenerateNiceMocks ([MockSpec <FirebaseApp >(), MockSpec <ConnectorConfig >()])
3337import '../firebase_data_connect_test.mocks.dart' ;
38+ import '../network/rest_transport_test.mocks.dart' ;
3439
3540class MockTransportOptions extends Mock implements TransportOptions {}
3641
@@ -41,6 +46,9 @@ void main() {
4146 late MockFirebaseAuth mockAuth;
4247 late MockConnectorConfig mockConnectorConfig;
4348 late FirebaseDataConnect dataConnect;
49+ late MockClient mockHttpClient;
50+ late RestTransport transport;
51+ const Duration maxAgeSeconds = Duration (milliseconds: 200 );
4452
4553 const String entityObject = '''
4654 {"desc":"itemDesc1","name":"itemOne", "cacheId":"123","price":4}
@@ -86,14 +94,31 @@ void main() {
8694 projectId: 'fake_project_id' ,
8795 ),
8896 );
89- when (mockConnectorConfig.location).thenReturn ('us-central1' );
90- when (mockConnectorConfig.connector).thenReturn ('connector' );
91- when (mockConnectorConfig.serviceId).thenReturn ('serviceId' );
97+ when (mockConnectorConfig.location).thenReturn ('testLocation' );
98+ when (mockConnectorConfig.connector).thenReturn ('testConnector' );
99+ when (mockConnectorConfig.serviceId).thenReturn ('testService' );
100+
101+ mockHttpClient = MockClient ();
102+ transport = RestTransport (
103+ TransportOptions ('testhost' , 443 , true ),
104+ DataConnectOptions (
105+ 'testProject' ,
106+ 'testLocation' ,
107+ 'testConnector' ,
108+ 'testService' ,
109+ ),
110+ 'testAppId' ,
111+ CallerSDKType .core,
112+ null ,
113+ );
114+ transport.setHttp (mockHttpClient);
92115
93116 dataConnect = FirebaseDataConnect (
94117 app: mockApp,
95118 connectorConfig: mockConnectorConfig,
96- cacheSettings: CacheSettings (storage: CacheStorage .memory));
119+ cacheSettings: CacheSettings (
120+ storage: CacheStorage .memory, maxAge: maxAgeSeconds));
121+ dataConnect.transport = transport;
97122 dataConnect.checkTransport ();
98123 dataConnect.checkAndInitializeCache ();
99124 });
@@ -189,5 +214,51 @@ void main() {
189214
190215 expect (testValue, value);
191216 });
217+
218+ test ('maxAge conformance' , () async {
219+ final deserializer = (String data) => 'Deserialized Data' ;
220+ final mockResponseSuccess = http.Response ('{"success": true}' , 200 );
221+
222+ if (dataConnect.cacheManager == null ) {
223+ fail ('No cacheManager available' );
224+ }
225+
226+ Cache cache = dataConnect.cacheManager! ;
227+
228+ Map <String , dynamic > jsonData =
229+ jsonDecode (simpleQueryResponse) as Map <String , dynamic >;
230+ await cache.update ('itemsSimple' , ServerResponse (jsonData));
231+
232+ QueryRef ref = QueryRef (
233+ dataConnect,
234+ 'operation' ,
235+ transport,
236+ deserializer,
237+ QueryManager (dataConnect),
238+ emptySerializer,
239+ null ,
240+ );
241+ when (
242+ mockHttpClient.post (
243+ any,
244+ headers: anyNamed ('headers' ),
245+ body: anyNamed ('body' ),
246+ ),
247+ ).thenAnswer ((_) async => mockResponseSuccess);
248+
249+ QueryResult result = await ref.execute ();
250+ expect (result.source, DataSource .server);
251+
252+ // call execute immediately. Should be within maxAge so source should be cache
253+ QueryResult result2 = await ref.execute ();
254+ expect (result2.source, DataSource .cache);
255+
256+ // now lets add delay beyond maxAge and result source should be server
257+ await Future .delayed (
258+ Duration (milliseconds: maxAgeSeconds.inMilliseconds + 100 ), () async {
259+ QueryResult resultDelayed = await ref.execute ();
260+ expect (resultDelayed.source, DataSource .server);
261+ });
262+ });
192263 }); // test group
193264} //main
0 commit comments