mirror of
https://github.com/hmalik144/easy_cc_flutter.git
synced 2025-12-10 03:05:34 +00:00
Unit tests added
This commit is contained in:
@@ -34,7 +34,7 @@ class HomePage extends BaseStatelessWidget<MainViewModel> {
|
|||||||
model.setConversionPair(selected1, selected2);
|
model.setConversionPair(selected1, selected2);
|
||||||
}),
|
}),
|
||||||
ConverterEditText("Enter conversion from", controller1, (input) => {
|
ConverterEditText("Enter conversion from", controller1, (input) => {
|
||||||
controller2.text = model.convertInput(input, SelectionType.conversionFrom)
|
controller2.text = model.convertInput(input?.trim(), SelectionType.conversionFrom)
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -48,7 +48,7 @@ class HomePage extends BaseStatelessWidget<MainViewModel> {
|
|||||||
model.setConversionPair(selected1, selected2);
|
model.setConversionPair(selected1, selected2);
|
||||||
}),
|
}),
|
||||||
ConverterEditText("Enter conversion from", controller2, (input) => {
|
ConverterEditText("Enter conversion from", controller2, (input) => {
|
||||||
controller1.text = model.convertInput(input, SelectionType.conversionTo)
|
controller1.text = model.convertInput(input?.trim(), SelectionType.conversionTo)
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ GetIt locator = GetIt.instance;
|
|||||||
|
|
||||||
void setupLocator() {
|
void setupLocator() {
|
||||||
final dio = Dio();
|
final dio = Dio();
|
||||||
|
|
||||||
locator.registerLazySingleton(() => PreferenceProvider());
|
locator.registerLazySingleton(() => PreferenceProvider());
|
||||||
locator.registerLazySingleton(() => CurrencyApi(dio));
|
locator.registerLazySingleton(() => CurrencyApi(dio));
|
||||||
locator.registerLazySingleton(() => BackupCurrencyApi(dio));
|
locator.registerLazySingleton(() => BackupCurrencyApi(dio));
|
||||||
|
|||||||
@@ -366,6 +366,13 @@ packages:
|
|||||||
url: "https://pub.dartlang.org"
|
url: "https://pub.dartlang.org"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "1.0.2"
|
version: "1.0.2"
|
||||||
|
mockito:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: mockito
|
||||||
|
url: "https://pub.dartlang.org"
|
||||||
|
source: hosted
|
||||||
|
version: "5.3.2"
|
||||||
nested:
|
nested:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ dependencies:
|
|||||||
retrofit: ^3.0.1+1
|
retrofit: ^3.0.1+1
|
||||||
logger: ^1.1.0
|
logger: ^1.1.0
|
||||||
flutter_dotenv: ^5.0.2
|
flutter_dotenv: ^5.0.2
|
||||||
|
mockito: ^5.3.2
|
||||||
|
|
||||||
dev_dependencies:
|
dev_dependencies:
|
||||||
flutter_test:
|
flutter_test:
|
||||||
@@ -76,6 +77,7 @@ flutter:
|
|||||||
# To add assets to your application, add an assets section, like this:
|
# To add assets to your application, add an assets section, like this:
|
||||||
assets:
|
assets:
|
||||||
- .env
|
- .env
|
||||||
|
- test/resources/
|
||||||
# - images/a_dot_burr.jpeg
|
# - images/a_dot_burr.jpeg
|
||||||
# - images/a_dot_ham.jpeg
|
# - images/a_dot_ham.jpeg
|
||||||
|
|
||||||
|
|||||||
13
test/resources/success_call_api.json
Normal file
13
test/resources/success_call_api.json
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"query": {
|
||||||
|
"count": 1
|
||||||
|
},
|
||||||
|
"results": {
|
||||||
|
"AUD_GBP": {
|
||||||
|
"id": "AUD_GBP",
|
||||||
|
"fr": "AUD",
|
||||||
|
"to": "GBP",
|
||||||
|
"val": 0.601188
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
8
test/resources/success_call_backup_api.json
Normal file
8
test/resources/success_call_backup_api.json
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"amount": 1.0,
|
||||||
|
"base": "AUD",
|
||||||
|
"date": "2022-09-23",
|
||||||
|
"rates": {
|
||||||
|
"GBP": 0.59483
|
||||||
|
}
|
||||||
|
}
|
||||||
2
test/resources/test_res.env
Normal file
2
test/resources/test_res.env
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
// freecurrencyapi api key
|
||||||
|
apiKey=12121
|
||||||
8
test/test_utils/test_utils.dart
Normal file
8
test/test_utils/test_utils.dart
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:flutter/services.dart';
|
||||||
|
|
||||||
|
Future<Map<String, dynamic>> readJson(String filePath) async {
|
||||||
|
final String response = await rootBundle.loadString('$filePath.json');
|
||||||
|
return await json.decode(response);
|
||||||
|
}
|
||||||
124
test/unit_test/repository_test.dart
Normal file
124
test/unit_test/repository_test.dart
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/model/Currency.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/network/backupCurrencyApi.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/network/currencyApi.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/prefs/CurrencyPair.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/prefs/PreferenceProvider.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/repository/RepositoryImpl.dart';
|
||||||
|
import 'package:easy_cc_flutter/locator.dart';
|
||||||
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mockito/annotations.dart';
|
||||||
|
import 'package:mockito/mockito.dart';
|
||||||
|
import 'package:retrofit/dio.dart' as http;
|
||||||
|
|
||||||
|
import '../test_utils/test_utils.dart';
|
||||||
|
import 'repository_test.mocks.dart';
|
||||||
|
|
||||||
|
@GenerateMocks([
|
||||||
|
PreferenceProvider,
|
||||||
|
CurrencyApi,
|
||||||
|
BackupCurrencyApi
|
||||||
|
], customMocks: [
|
||||||
|
MockSpec<http.HttpResponse<ResponseObject>>(
|
||||||
|
onMissingStub: OnMissingStub.returnDefault),
|
||||||
|
MockSpec<http.HttpResponse<CurrencyResponse>>(
|
||||||
|
as: #MockCurrencyResponse, onMissingStub: OnMissingStub.returnDefault),
|
||||||
|
MockSpec<DioError>(onMissingStub: OnMissingStub.returnDefault)
|
||||||
|
])
|
||||||
|
void main() {
|
||||||
|
late RepositoryImpl repository;
|
||||||
|
late PreferenceProvider preferenceProvider;
|
||||||
|
late CurrencyApi currencyApi;
|
||||||
|
late BackupCurrencyApi backupCurrencyApi;
|
||||||
|
|
||||||
|
const String fromCurrency = "AUD - Australian Dollar";
|
||||||
|
const String toCurrency = "GBP - British Pound Sterling";
|
||||||
|
|
||||||
|
setUpAll(() async {
|
||||||
|
// Setup
|
||||||
|
await dotenv.load(fileName: "test/resources/test_res.env");
|
||||||
|
// Create mock object.
|
||||||
|
preferenceProvider = MockPreferenceProvider();
|
||||||
|
currencyApi = MockCurrencyApi();
|
||||||
|
backupCurrencyApi = MockBackupCurrencyApi();
|
||||||
|
|
||||||
|
locator.registerLazySingleton(() => preferenceProvider);
|
||||||
|
locator.registerLazySingleton(() => currencyApi);
|
||||||
|
locator.registerLazySingleton(() => backupCurrencyApi);
|
||||||
|
|
||||||
|
repository = RepositoryImpl();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get currency pair from prefs', () {
|
||||||
|
// Given
|
||||||
|
CurrencyPair pair = CurrencyPair(fromCurrency, toCurrency);
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(preferenceProvider.getConversionPair()).thenReturn(pair);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(repository.getConversionPair(), pair);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get currency rate from API', () async {
|
||||||
|
// Given
|
||||||
|
http.HttpResponse<ResponseObject> mockResponse = MockHttpResponse();
|
||||||
|
ResponseObject responseObject = ResponseObject.fromJson(
|
||||||
|
await readJson("test/resources/success_call_api"));
|
||||||
|
Currency currencyObject = Currency("AUD", "GBP", 0.601188);
|
||||||
|
String currency = "AUD_GBP";
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(mockResponse.data).thenReturn(responseObject);
|
||||||
|
when(currencyApi.getConversion(dotenv.env['apiKey']!, currency))
|
||||||
|
.thenAnswer((_) async => mockResponse);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Currency retrieved =
|
||||||
|
await repository.getConversationRateFromApi(fromCurrency, toCurrency);
|
||||||
|
expect(retrieved.toString(), currencyObject.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get currency rate from backup API', () async {
|
||||||
|
// Given
|
||||||
|
http.HttpResponse<CurrencyResponse> mockResponse = MockCurrencyResponse();
|
||||||
|
CurrencyResponse currencyResponse = CurrencyResponse.fromJson(
|
||||||
|
await readJson("test/resources/success_call_backup_api"));
|
||||||
|
Currency currencyObject = Currency("AUD", "GBP", 0.601188);
|
||||||
|
String currency = "AUD_GBP";
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(currencyApi.getConversion(dotenv.env['apiKey']!, currency))
|
||||||
|
.thenAnswer((_) async => Future.error(MockDioError()));
|
||||||
|
when(mockResponse.data).thenReturn(currencyResponse);
|
||||||
|
when(backupCurrencyApi.getCurrencyRate("AUD", "GBP"))
|
||||||
|
.thenAnswer((_) async => mockResponse);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
Currency retrieved =
|
||||||
|
await repository.getConversationRateFromApi(fromCurrency, toCurrency);
|
||||||
|
expect(retrieved.toString(), currencyObject.toString());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('unable to retrieve rate from both APIs', () async {
|
||||||
|
// Given
|
||||||
|
String currency = "AUD_GBP";
|
||||||
|
DioError backUpError = MockDioError();
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(backUpError.error).thenReturn("Error message");
|
||||||
|
when(currencyApi.getConversion(dotenv.env['apiKey']!, currency))
|
||||||
|
.thenAnswer((_) async => Future.error(MockDioError()));
|
||||||
|
when(backupCurrencyApi.getCurrencyRate("AUD", "GBP"))
|
||||||
|
.thenAnswer((_) async => Future.error(backUpError));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(() async => await repository.getConversationRateFromApi(fromCurrency, toCurrency),
|
||||||
|
throwsA(predicate((e) =>
|
||||||
|
e is HttpException &&
|
||||||
|
e.message == 'Error message')));
|
||||||
|
});
|
||||||
|
}
|
||||||
132
test/unit_test/viewmodel_test.dart
Normal file
132
test/unit_test/viewmodel_test.dart
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:easy_cc_flutter/MainViewModel.dart';
|
||||||
|
import 'package:easy_cc_flutter/Utils/SelectionType.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/model/Currency.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/prefs/CurrencyPair.dart';
|
||||||
|
import 'package:easy_cc_flutter/data/repository/RepositoryImpl.dart';
|
||||||
|
import 'package:easy_cc_flutter/locator.dart';
|
||||||
|
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:mockito/annotations.dart';
|
||||||
|
import 'package:mockito/mockito.dart';
|
||||||
|
import 'package:easy_cc_flutter/Utils/ViewState.dart';
|
||||||
|
|
||||||
|
import 'viewmodel_test.mocks.dart';
|
||||||
|
|
||||||
|
@GenerateMocks([
|
||||||
|
RepositoryImpl
|
||||||
|
])
|
||||||
|
void main() {
|
||||||
|
late MainViewModel mainViewModel;
|
||||||
|
late RepositoryImpl repository;
|
||||||
|
|
||||||
|
const String fromCurrency = "AUD - Australian Dollar";
|
||||||
|
const String toCurrency = "GBP - British Pound Sterling";
|
||||||
|
|
||||||
|
setUpAll(() async {
|
||||||
|
// Setup
|
||||||
|
await dotenv.load(fileName: "test/resources/test_res.env");
|
||||||
|
// Create mock object.
|
||||||
|
repository = MockRepositoryImpl();
|
||||||
|
|
||||||
|
locator.registerLazySingleton(() => repository);
|
||||||
|
|
||||||
|
mainViewModel = MainViewModel();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get currency pair from prefs', () {
|
||||||
|
// Given
|
||||||
|
CurrencyPair pair = CurrencyPair(fromCurrency, toCurrency);
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(repository.getConversionPair()).thenReturn(pair);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
String fromSelection = mainViewModel.getConversionPair(SelectionType.conversionFrom);
|
||||||
|
String toSelection = mainViewModel.getConversionPair(SelectionType.conversionTo);
|
||||||
|
expect(fromSelection, fromCurrency);
|
||||||
|
expect(toSelection, toCurrency);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('get currency pair from prefs nothing stored', () {
|
||||||
|
// Given
|
||||||
|
CurrencyPair pair = CurrencyPair(null, null);
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(repository.getConversionPair()).thenReturn(pair);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
String fromSelection = mainViewModel.getConversionPair(SelectionType.conversionFrom);
|
||||||
|
String toSelection = mainViewModel.getConversionPair(SelectionType.conversionTo);
|
||||||
|
expect(fromSelection, "ALL - Albanian Lek");
|
||||||
|
expect(toSelection, "ALL - Albanian Lek");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('set the currency rate from API', () async{
|
||||||
|
// Given
|
||||||
|
Currency currency = Currency(fromCurrency, toCurrency, 0.6);
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(repository.getConversationRateFromApi(fromCurrency, toCurrency))
|
||||||
|
.thenAnswer((_) async => currency);
|
||||||
|
|
||||||
|
// Then
|
||||||
|
mainViewModel.setCurrencyRate(fromCurrency, toCurrency);
|
||||||
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
|
|
||||||
|
expect((mainViewModel.viewState as HasData).data, currency);
|
||||||
|
expect(mainViewModel.conversionRate, currency.rate);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('currency rate api fails to retrieve data', () async{
|
||||||
|
// Given
|
||||||
|
String errorMessage = "failed to retrieve data";
|
||||||
|
|
||||||
|
// When
|
||||||
|
when(repository.getConversationRateFromApi(fromCurrency, toCurrency))
|
||||||
|
.thenAnswer((_) async => Future.error(HttpException(errorMessage)));
|
||||||
|
|
||||||
|
// Then
|
||||||
|
mainViewModel.setCurrencyRate(fromCurrency, toCurrency);
|
||||||
|
await Future.delayed(const Duration(milliseconds: 100));
|
||||||
|
|
||||||
|
expect((mainViewModel.viewState as HasError).error, errorMessage);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert input with correct format', () async{
|
||||||
|
// Given
|
||||||
|
String input = "43";
|
||||||
|
mainViewModel.conversionRate = 6.34;
|
||||||
|
|
||||||
|
// When
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionFrom), "272.62");
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionTo), "6.78");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert input with empty input', () async{
|
||||||
|
// Given
|
||||||
|
String input = "";
|
||||||
|
mainViewModel.conversionRate = 6.34;
|
||||||
|
|
||||||
|
// When
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionFrom), "");
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionTo), "");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('convert input with empty input', () async{
|
||||||
|
// Given
|
||||||
|
String input = "45.45564";
|
||||||
|
mainViewModel.conversionRate = 6.34;
|
||||||
|
|
||||||
|
// When
|
||||||
|
|
||||||
|
// Then
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionFrom), "288.19");
|
||||||
|
expect(mainViewModel.convertInput(input, SelectionType.conversionTo), "7.17");
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user