Skip to content

Commit

Permalink
repository done
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo0Khaled committed Feb 19, 2022
1 parent eb9adce commit aee6846
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import 'package:clean_arc/core/exceptions/exception.dart';
import 'package:clean_arc/core/platform/networrk_info.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source%20copy.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_remote_data_source.dart';
import 'package:clean_arc/features/number_trivia/data/models/number_trivia_model.dart';
import 'package:clean_arc/features/number_trivia/domain/entities/number_trivia.dart';
import 'package:clean_arc/core/exceptions/failuers.dart';
import 'package:clean_arc/features/number_trivia/domain/repositories/number_trivia_repository.dart';
import 'package:dartz/dartz.dart';

typedef Future<NumberTriviaModel> _ConcreteOrRandomChooser();

class NumberTriviaRepositoryImpl implements NumberTriviaRepository {
final NumberTriviaRemoteDataSource remoteDataSource;
final NumberTriviaLocaleDataSource localeDataSource;
Expand All @@ -21,19 +24,33 @@ class NumberTriviaRepositoryImpl implements NumberTriviaRepository {
@override
Future<Either<Failure, NumberTrivia>> getConcreteNumberTrivia(
int number) async {
networkInfo.isConnected;
try {
final remoteTrivia =
await remoteDataSource.getConcreteNumberTrivia(number);
await localeDataSource.cacheNumberTrivia(remoteTrivia);
return Right(remoteTrivia);
} on ServerException {
return Left(ServerFailure());
}
return (await _getNumberTrivia(
() => remoteDataSource.getConcreteNumberTrivia(number)));
}

@override
Future<Either<Failure, NumberTrivia>> getRandomNumberTrivia() async {
return Right(await remoteDataSource.getRandomNumberTrivia());
return (await _getNumberTrivia(
() => remoteDataSource.getRandomNumberTrivia()));
}

Future<Either<Failure, NumberTrivia>> _getNumberTrivia(
_ConcreteOrRandomChooser getConcreteOrRandomNumber) async {
if (await networkInfo.isConnected) {
try {
final trivia = await getConcreteOrRandomNumber();
await localeDataSource.cacheNumberTrivia(trivia);
return Right(trivia);
} on ServerException {
return Left(ServerFailure());
}
} else {
try {
final localeTrivia = await localeDataSource.getLastNumberTrivia();
return Right(localeTrivia);
} on CacheException {
return Left(CacheFailure());
}
}
}
}
9 changes: 8 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.11"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.3"
meta:
dependency: transitive
description:
Expand Down Expand Up @@ -608,7 +615,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.3"
version: "0.4.8"
timing:
dependency: transitive
description:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:clean_arc/core/exceptions/exception.dart';
import 'package:clean_arc/core/exceptions/failuers.dart';
import 'package:clean_arc/core/platform/networrk_info.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source%20copy.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source.dart';
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_remote_data_source.dart';
import 'package:clean_arc/features/number_trivia/data/models/number_trivia_model.dart';
import 'package:clean_arc/features/number_trivia/data/repositories/number_trivia_repository_impl.dart';
Expand Down Expand Up @@ -32,6 +32,24 @@ void main() {
);
});

void runTestsOnline(Function body) {
group("device is online", () {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => true);
});
body();
});
}

void runTestsOffline(Function body) {
group("device is offline", () {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => false);
});
body();
});
}

group("get concrete number trivia", () {
const tNumber = 1;
const tNumberTriviaModel = NumberTriviaModel(number: tNumber, text: "test");
Expand All @@ -47,11 +65,7 @@ void main() {
verify(mockNetworkInfo.isConnected);
});

group("device is online", () {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => true);
});

runTestsOnline(() {
test(
'should return remote data when the call to remote to data source is success',
() async {
Expand Down Expand Up @@ -99,7 +113,7 @@ void main() {
});
});

group("device is offline", () {
runTestsOffline(() {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => false);
});
Expand All @@ -117,6 +131,119 @@ void main() {
verify(mockNumberTriviaLocaleDataSource.getLastNumberTrivia());
expect(result, const Right(tNumberTrivia));
});

test('should return cache failure when there is not cached data present',
() async {
// arrange
when(mockNumberTriviaLocaleDataSource.getLastNumberTrivia())
.thenThrow(CacheException());
// act
final result = await repositoryImpl.getConcreteNumberTrivia(tNumber);
// assert
verifyZeroInteractions(mockNumberTriviaRemoteDataSource);
verify(mockNumberTriviaLocaleDataSource.getLastNumberTrivia());
expect(result, equals(Left(CacheFailure())));
});
});
});



group("get Random number trivia", () {
const tNumberTriviaModel = NumberTriviaModel(number: 123, text: "test");
const NumberTrivia tNumberTrivia = tNumberTriviaModel;
test('should check if the device is online', () async {
// arrange
when(mockNetworkInfo.isConnected).thenAnswer((_) async => true);
when(mockNumberTriviaRemoteDataSource.getRandomNumberTrivia())
.thenAnswer((_) async => tNumberTriviaModel);
// act
repositoryImpl.getRandomNumberTrivia();
// assert
verify(mockNetworkInfo.isConnected);
});

runTestsOnline(() {
test(
'should return remote data when the call to remote to data source is success',
() async {
// arrange
when(mockNumberTriviaRemoteDataSource.getRandomNumberTrivia())
.thenAnswer((realInvocation) async => tNumberTriviaModel);
// act
final result = await repositoryImpl.getRandomNumberTrivia();
// assert

verify(
mockNumberTriviaRemoteDataSource.getRandomNumberTrivia());
expect(result, equals(const Right(tNumberTrivia)));
});
test(
'should cached the data lacally when the call to remote to data source is success',
() async {
// arrange
when(mockNumberTriviaRemoteDataSource.getRandomNumberTrivia())
.thenAnswer((realInvocation) async => tNumberTriviaModel);
// act
await repositoryImpl.getRandomNumberTrivia();
// assert

verify(
mockNumberTriviaRemoteDataSource.getRandomNumberTrivia());
verify(mockNumberTriviaLocaleDataSource
.cacheNumberTrivia(tNumberTriviaModel));
});

test(
'should return server failure when the call to remote to data source is unsuccess',
() async {
// arrange
when(mockNumberTriviaRemoteDataSource.getRandomNumberTrivia())
.thenThrow(ServerException());
// act
final result = await repositoryImpl.getRandomNumberTrivia();
// assert

verify(
mockNumberTriviaRemoteDataSource.getRandomNumberTrivia());
verifyZeroInteractions(mockNumberTriviaLocaleDataSource);
expect(result, equals(Left(ServerFailure())));
});
});

runTestsOffline(() {
setUp(() {
when(mockNetworkInfo.isConnected).thenAnswer((_) async => false);
});

test(
'should return last cached locally data when the cache data is present',
() async {
// arrange
when(mockNumberTriviaLocaleDataSource.getLastNumberTrivia())
.thenAnswer((realInvocation) async => tNumberTriviaModel);
// act
final result = await repositoryImpl.getRandomNumberTrivia();
// assert
verifyZeroInteractions(mockNumberTriviaRemoteDataSource);
verify(mockNumberTriviaLocaleDataSource.getLastNumberTrivia());
expect(result, const Right(tNumberTrivia));
});

test('should return cache failure when there is not cached data present',
() async {
// arrange
when(mockNumberTriviaLocaleDataSource.getLastNumberTrivia())
.thenThrow(CacheException());
// act
final result = await repositoryImpl.getRandomNumberTrivia();
// assert
verifyZeroInteractions(mockNumberTriviaRemoteDataSource);
verify(mockNumberTriviaLocaleDataSource.getLastNumberTrivia());
expect(result, equals(Left(CacheFailure())));
});
});
});


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'dart:async' as _i4;

import 'package:clean_arc/core/platform/networrk_info.dart' as _i6;
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source%20copy.dart'
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_locale_data_source.dart'
as _i5;
import 'package:clean_arc/features/number_trivia/data/datasources/number_trivia_remote_data_source.dart'
as _i3;
Expand Down

0 comments on commit aee6846

Please sign in to comment.