Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unit testing repositories #202

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
3 changes: 3 additions & 0 deletions .github/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

printf "\e[33;1m%s\e[0m\n" 'Running the Pre-push checks'
./scripts/checks.sh

printf "\e[33;1m%s\e[0m\n" 'Running the Pre-push tests'
fvm flutter test
14 changes: 6 additions & 8 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,16 @@ dart_code_metrics:
allowed-duplicated-chains: 3
- prefer-trailing-comma



# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
analyzer:
exclude:
- '**/*.freezed.dart'
- '**/*.g.dart'
- '**/*.gen.dart'
- '**/*.gr.dart'
- 'bricks'
- 'lib/generated_plugin_registrant.dart'
- "**/*.freezed.dart"
- "**/*.g.dart"
- "**/*.gen.dart"
- "**/*.gr.dart"
- "bricks"
- "lib/generated_plugin_registrant.dart"
errors:
invalid_annotation_target: ignore
unused_element: ignore # https://github.com/dart-lang/sdk/issues/49025
4 changes: 3 additions & 1 deletion lib/core/di/di_repository_module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class RepositoryDiModule {

extension _GetItDiModuleExtensions on GetIt {
void _setupProvidersAndUtils() {
registerLazySingleton(() => HttpServiceDio([AuthInterceptor(get())]));
registerLazySingleton<HttpService>(
() => HttpServiceDio([AuthInterceptor(get())]),
);
}

void _setupRepositories() {
Expand Down
15 changes: 13 additions & 2 deletions lib/core/model/db/repository_db_entity.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import 'package:equatable/equatable.dart';
import 'package:floor/floor.dart';

@Entity(tableName: 'projects')
class ProjectDbEntity {
class ProjectDbEntity extends Equatable {
@primaryKey
final int id;
final String name;
Expand All @@ -10,12 +11,22 @@ class ProjectDbEntity {
final String imageUrl;
final String language;

ProjectDbEntity({
const ProjectDbEntity({
required this.id,
required this.name,
required this.description,
required this.url,
required this.imageUrl,
required this.language,
});

@override
List<Object?> get props => [
id,
name,
description,
url,
imageUrl,
language,
];
}
2 changes: 1 addition & 1 deletion lib/core/source/auth_remote_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:flutter_template/core/model/service/service_response.dart';
import 'package:flutter_template/core/source/common/http_service.dart';

class AuthRemoteSource {
final HttpServiceDio _httpService;
final HttpService _httpService;

static const _urlLogin = 'auth/v1/token';

Expand Down
4 changes: 2 additions & 2 deletions lib/core/source/project_remote_source.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter_template/core/model/service/service_response.dart';
import 'package:flutter_template/core/model/project.dart';
import 'package:flutter_template/core/model/service/service_response.dart';
import 'package:flutter_template/core/source/common/http_service.dart';

class ProjectRemoteSource {
static const _urlGetProjects = 'rest/v1/projects?select=*';

final HttpServiceDio _httpService;
final HttpService _httpService;

ProjectRemoteSource(this._httpService);

Expand Down
108 changes: 106 additions & 2 deletions pubspec.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 12 additions & 10 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: flutter_template
description: A new Flutter project.

publish_to: 'none'
publish_to: "none"

version: 1.0.0+1

environment:
sdk: '>=3.0.0 <4.0.0'
sdk: ">=3.0.0 <4.0.0"
flutter: 3.13.9

dependencies:
Expand Down Expand Up @@ -52,6 +52,8 @@ dev_dependencies:

auto_route_generator: 7.3.1
build_runner: 2.4.6
mocktail: 1.0.1
bloc_test: 9.1.5
fedecor9 marked this conversation as resolved.
Show resolved Hide resolved
dart_code_metrics: 5.7.6
floor_generator: 1.4.2
flutter_flavorizr: 2.2.1
Expand Down Expand Up @@ -79,19 +81,19 @@ flutter_gen:
flutter_launcher_icons:
android: true
ios: true
image_path: 'icons/ic_launcher.png'
image_path_ios: 'icons/ic_launcher_ios.png' # Transparency not supported on IOS
adaptive_icon_foreground: 'icons/ic_launcher_foreground.png'
adaptive_icon_background: '#ee1a64'
image_path: "icons/ic_launcher.png"
image_path_ios: "icons/ic_launcher_ios.png" # Transparency not supported on IOS
adaptive_icon_foreground: "icons/ic_launcher_foreground.png"
adaptive_icon_background: "#ee1a64"
remove_alpha_ios: true
web:
generate: false
windows:
generate: false

flutter_native_splash:
color: '#ffffff'
image: 'icons/splash_logo.png'
color: "#ffffff"
image: "icons/splash_logo.png"
android_12:
image: 'icons/splash_logo_android_12.png'
branding: 'icons/splash_branding.png'
image: "icons/splash_logo_android_12.png"
branding: "icons/splash_branding.png"
4 changes: 4 additions & 0 deletions test/common/general_helpers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import 'package:flutter_template/core/source/common/app_database.dart';

Future<AppDatabase> setupFloorDatabase() =>
$FloorAppDatabase.inMemoryDatabaseBuilder().build();
35 changes: 35 additions & 0 deletions test/common/mocks.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_template/core/repository/project_repository.dart';
import 'package:flutter_template/core/repository/session_repository.dart';
import 'package:flutter_template/core/source/auth_local_source.dart';
import 'package:flutter_template/core/source/auth_remote_source.dart';
import 'package:flutter_template/core/source/common/http_service.dart';
import 'package:flutter_template/core/source/common/local_shared_preferences_storage.dart';
import 'package:flutter_template/core/source/project_local_source.dart';
import 'package:flutter_template/core/source/project_remote_source.dart';
import 'package:flutter_template/ui/section/error_handler/global_event_handler_cubit.dart';
import 'package:mocktail/mocktail.dart';

//* Services
class HttpServiceMock extends Mock implements HttpService {}

//* Data sources
class ProjectLocalSourceMock extends Mock implements ProjectLocalSource {}

class ProjectRemoteSourceMock extends Mock implements ProjectRemoteSource {}

class AuthLocalSourceMock extends Mock implements AuthLocalSource {}

class AuthRemoteSourceMock extends Mock implements AuthRemoteSource {}

//* Repositories
class MockSessionRepository extends Mock implements SessionRepository {}

class MockProjectRepository extends Mock implements ProjectRepository {}

//* Cubits
class MockGlobalEventHandlerCubit extends MockCubit<GlobalEventHandlerState>
implements GlobalEventHandlerCubit {}
fedecor9 marked this conversation as resolved.
Show resolved Hide resolved

class LocalSharedPreferencesStorageMock extends Mock
implements LocalSharedPreferencesStorage {}
Loading