Skip to content

Commit

Permalink
Add some tests to DatabaseBloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Syutkin committed Jan 22, 2025
1 parent 44ab06e commit 8f65ff1
Show file tree
Hide file tree
Showing 12 changed files with 586 additions and 124 deletions.
2 changes: 1 addition & 1 deletion lib/src/common/logger/logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/foundation.dart';
import 'package:logger/logger.dart';

final Logger logger = Logger(
level: kReleaseMode ? Level.error : Level.debug,
level: kReleaseMode ? Level.error : Level.trace,
printer: PrettyPrinter(
colors: false,
noBoxingByDefault: true,
Expand Down
14 changes: 0 additions & 14 deletions lib/src/common/utils/file_picker.dart
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@
import 'package:file_picker/file_picker.dart';

Future<PlatformFile?> pickFile([List<String> extensions = const ['csv']]) async {
final file = (await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: extensions,
withData: true,
))?.files.first;

if (file == null || file.bytes == null) {
return null;
}
return file;
}
24 changes: 20 additions & 4 deletions lib/src/common/utils/file_utils.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,36 @@
import 'dart:io';

import 'package:file_picker/file_picker.dart';
import 'package:path_provider/path_provider.dart';

import '../logger/logger.dart';

/// Save csv to file
Future<File?> saveCsv(String csv, String filename) async {
Future<File?> saveToFile(String content, String filename, [String extension = 'csv']) async {
final directory =
Platform.isAndroid
? await getExternalStorageDirectory()
: await getApplicationDocumentsDirectory();
if (directory == null) {
return null;
}
final file = File('${directory.path}/$filename.csv');
await file.writeAsString(csv);
logger.i('saveCsv -> Saved csv to file ${file.path}');
final file = File('${directory.path}/$filename.$extension');
await file.writeAsString(content);
logger.i('saveToFile -> Saved $extension to file ${file.path}');
return file;
}


/// Pick file from filesystem
Future<PlatformFile?> pickFile([List<String> extensions = const ['csv']]) async {
final file = (await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: extensions,
withData: true,
))?.files.first;

if (file == null || file.bytes == null) {
return null;
}
return file;
}
8 changes: 4 additions & 4 deletions lib/src/constants/pubspec.yaml.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ sealed class Pubspec {
2025,
1,
22,
1,
4,
5,
351,
892,
21,
52,
37,
89,
);

/// Name
Expand Down
2 changes: 1 addition & 1 deletion lib/src/feature/csv/logic/startlist_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:file_picker/file_picker.dart';

import '../../../common/logger/logger.dart';
import '../../../common/utils/csv_utils.dart';
import '../../../common/utils/file_picker.dart';
import '../../../common/utils/file_utils.dart';
import '../model/race_csv.dart';
import '../model/stages_csv.dart';
import '../model/start_item_csv.dart';
Expand Down
8 changes: 5 additions & 3 deletions lib/src/feature/database/bloc/database_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
numbersOnTrace: [],
),
) {

_racesSubscription = _db.getRaces().watch().listen((event) async {
_races = event;
logger.t('DatabaseBloc -> getRaces().watch()');
Expand All @@ -61,6 +60,7 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {

_ridersSubscription = _db.getRiders.watch().listen((event) async {
_riders = event;
logger.t('DatabaseBloc -> getRiders().watch()');
_emitState();
});

Expand Down Expand Up @@ -137,6 +137,7 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
}
},
emitState: (event) {
logger.t('emitState event, participants length: ${_participants.length}');
emit(
DatabaseState(
race: event.race,
Expand Down Expand Up @@ -504,7 +505,7 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
final csv = mapListToCsv(startMap);
if (csv != null) {
final filename = '${race.name}-${stage.name}-start';
final file = await saveCsv(csv, filename);
final file = await saveToFile(csv, filename);
if (file != null) {
await Share.shareXFiles(
[XFile(file.path)],
Expand All @@ -530,7 +531,7 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
final csv = mapListToCsv(finishMap);
if (csv != null) {
final filename = '${race.name}-${stage.name}-finish';
final file = await saveCsv(csv, filename);
final file = await saveToFile(csv, filename);
if (file != null) {
await Share.shareXFiles(
[XFile(file.path)],
Expand Down Expand Up @@ -613,6 +614,7 @@ class DatabaseBloc extends Bloc<DatabaseEvent, DatabaseState> {
int? autoFinishNumber,
bool? updateFinishNumber,
}) {
logger.t('_emitState func, participants length: ${_participants.length}');
add(
DatabaseEvent.emitState(
race: _race,
Expand Down
20 changes: 10 additions & 10 deletions test/helpers/startlist.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
String startlist = '''
Категория;Номер;Имя;Ник;Возраст;Команда;Город;Старт;Старт2
Девушки;2;Алексахина Варвара;Zoenor;35;ТимАк;Занаду;10:00:00;11:00:00
Девушки;7;Берестова Владислава;Marsica;41;ТимАк;Ктесифон;10:00:30;11:01:00
Девушки;14;Григолюк Изабелла;Isopy;31;РысакиТекс;Тикаль;10:01:00;11:02:00
Девушки;21;Игнатьева Розалия;Amsie;34;ТимАльянс;Ктесифон;10:01:30;11:03:00
Девушки;31;Костина Ангелина;Evecy;28;СтройРысаки;Тикаль;10:02:00;11:04:00
Девушки;33;Левицкая Инга;Emirah;30;;Фивы;10:02:30;11:05:00
Девушки;37;Лялюшкина Арина;Emloe;42;ТимАльянс;Занаду;10:03:00;11:06:00
Девушки;48;Рыбалкина Каролина;Frelda;25;;Занаду;10:03:30;11:07:00
Девушки;56;Сухиха Зоя;Lece;45;ГорРысаки;Занаду;10:04:00;11:08:00
Девушки;61;Урусова Евдокия;Ruber;23;;Карфаген;10:04:30;11:09:00
Девушки;2;Алексахина Варвара;Zoenor;35;ТимАк;Занаду;00:00:00;11:00:00
Девушки;7;Берестова Владислава;Marsica;41;ТимАк;Ктесифон;00:00:30;11:01:00
Девушки;14;Григолюк Изабелла;Isopy;31;РысакиТекс;Тикаль;00:01:00;11:02:00
Девушки;21;Игнатьева Розалия;Amsie;34;ТимАльянс;Ктесифон;00:01:30;11:03:00
Девушки;31;Костина Ангелина;Evecy;28;СтройРысаки;Тикаль;00:02:00;11:04:00
Девушки;33;Левицкая Инга;Emirah;30;;Фивы;00:02:30;11:05:00
Девушки;37;Лялюшкина Арина;Emloe;42;ТимАльянс;Занаду;00:03:00;11:06:00
Девушки;48;Рыбалкина Каролина;Frelda;25;;Занаду;00:03:30;11:07:00
Девушки;56;Сухиха Зоя;Lece;45;ГорРысаки;Занаду;00:04:00;11:08:00
Девушки;61;Урусова Евдокия;Ruber;23;;Карфаген;00:04:30;11:09:00
Любители;9;Гадолин Мечислав;Benlix;51;;Фивы;10:05:00;11:10:00
Любители;11;Голов Степан;Arter;55;;Ктесифон;10:05:30;11:11:00
Любители;16;Дуркин Наум;Ausott;28;;Тикаль;10:06:00;11:12:00
Expand Down
4 changes: 2 additions & 2 deletions test/src/common/utils/csv_utils_test.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import 'package:entime/src/common/utils/csv_utils.dart';
import 'package:flutter_test/flutter_test.dart';

import 'startlist_csv.dart';
import '../../../helpers/startlist.dart';

void main() async {
TestWidgetsFlutterBinding.ensureInitialized();
late String csv;

group('csv_utils:', () {
setUpAll(() {
csv = startlistCsv;
csv = startlist;
});

test('CsvToMapConverter and mapListToCsv', () {
Expand Down
83 changes: 0 additions & 83 deletions test/src/common/utils/startlist_csv.dart

This file was deleted.

Loading

0 comments on commit 8f65ff1

Please sign in to comment.