forked from eatvio/eatvio_app
added dev tools
This commit is contained in:
156
lib/features/dev_tools/presentation/dev_tools_screen.dart
Normal file
156
lib/features/dev_tools/presentation/dev_tools_screen.dart
Normal file
@@ -0,0 +1,156 @@
|
||||
import 'package:eatvio_app/core/const.dart';
|
||||
import 'package:eatvio_app/core/utils/toasts.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_outlined_button.dart';
|
||||
import 'package:eatvio_app/core/widgets/custom_text_form_field.dart';
|
||||
import 'package:eatvio_app/features/dev_tools/test_cases.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
class DevToolsScreen extends StatelessWidget {
|
||||
const DevToolsScreen({super.key});
|
||||
static const name = 'devToolsScreen';
|
||||
static const path = '/dev-tools-screen';
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return const DevToolsScreenContent();
|
||||
}
|
||||
}
|
||||
|
||||
class DevToolsScreenContent extends StatefulWidget {
|
||||
const DevToolsScreenContent({super.key});
|
||||
|
||||
@override
|
||||
State<DevToolsScreenContent> createState() => _DevToolsScreenContentState();
|
||||
}
|
||||
|
||||
class _DevToolsScreenContentState extends State<DevToolsScreenContent> {
|
||||
String baseUrlForTest = '';
|
||||
final List<TestCases> testCases = [Test850task()];
|
||||
final controller = TextEditingController();
|
||||
late final List<TestState> testsStates;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
setState(() {
|
||||
this.baseUrlForTest = baseUrl;
|
||||
});
|
||||
testsStates = List.generate(testCases.length, (index) => TestState.notRun);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: SingleChildScrollView(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10),
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Row(
|
||||
children: [
|
||||
Text('BaseUrl: ${baseUrlForTest}'),
|
||||
],
|
||||
),
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: CustomTextFormField(
|
||||
controller: controller,
|
||||
hintText: 'type new base url',
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
baseUrlForTest = controller.text;
|
||||
controller.clear();
|
||||
});
|
||||
},
|
||||
icon: const Icon(Icons.done),
|
||||
),
|
||||
],
|
||||
),
|
||||
SizedBox(
|
||||
height: 10,
|
||||
),
|
||||
if (baseUrlForTest != 'https://eatvio.com')
|
||||
ListView.builder(
|
||||
shrinkWrap: true,
|
||||
itemCount: testCases.length,
|
||||
itemBuilder: (context, index) => Row(
|
||||
children: [
|
||||
Text('${index + 1}. ${testCases[index].name}'),
|
||||
SizedBox(
|
||||
width: 50,
|
||||
),
|
||||
Expanded(
|
||||
child: CustomOutlinedButton(
|
||||
text: 'run',
|
||||
height: 20,
|
||||
width: 60,
|
||||
onPressed: () async {
|
||||
if (testsStates[index] == TestState.loading) return;
|
||||
|
||||
setState(() {
|
||||
testsStates[index] = TestState.loading;
|
||||
});
|
||||
final result = await testCases[index].run(baseUrl: baseUrlForTest);
|
||||
if (result) {
|
||||
setState(() {
|
||||
testsStates[index] = TestState.success;
|
||||
});
|
||||
showSuccessToast('${testCases[index].name} passed');
|
||||
} else {
|
||||
setState(() {
|
||||
testsStates[index] = TestState.fail;
|
||||
});
|
||||
showErrorToast('${testCases[index].name} failed');
|
||||
}
|
||||
},
|
||||
buttonStyle: ButtonStyle(),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
width: 30,
|
||||
),
|
||||
switch (testsStates[index]) {
|
||||
TestState.success => Expanded(
|
||||
child: Container(
|
||||
height: 20,
|
||||
color: Colors.green,
|
||||
),
|
||||
),
|
||||
TestState.fail => Expanded(
|
||||
child: Container(
|
||||
height: 20,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
TestState.notRun => Expanded(
|
||||
child: Container(
|
||||
height: 20,
|
||||
color: Colors.grey,
|
||||
),
|
||||
),
|
||||
TestState.loading => Expanded(
|
||||
child: Container(
|
||||
height: 20,
|
||||
child: SizedBox(height: 20, width: 20, child: Center(child: CircularProgressIndicator())),
|
||||
),
|
||||
),
|
||||
},
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
enum TestState { success, fail, notRun, loading }
|
||||
115
lib/features/dev_tools/test_cases.dart
Normal file
115
lib/features/dev_tools/test_cases.dart
Normal file
@@ -0,0 +1,115 @@
|
||||
import 'dart:async';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:eatvio_app/core/locator_service.dart';
|
||||
import 'package:eatvio_app/core/utils/date_time_utils.dart';
|
||||
import 'package:eatvio_app/features/diets/services/diet_service.dart';
|
||||
import 'package:eatvio_app/features/feed/services/feed_service.dart';
|
||||
import 'package:eatvio_app/features/profile/services/profile_service.dart';
|
||||
|
||||
sealed class TestCases {
|
||||
String get name => throw UnimplementedError();
|
||||
|
||||
Future<bool> run({required String baseUrl}) async => throw UnimplementedError();
|
||||
}
|
||||
|
||||
class Test850task implements TestCases {
|
||||
@override
|
||||
String get name => 'task #850';
|
||||
|
||||
@override
|
||||
Future<bool> run({required String baseUrl}) async {
|
||||
try {
|
||||
// get all of needed services
|
||||
final dio = sl<Dio>();
|
||||
final feedService = FeedService(dio, baseUrl: baseUrl);
|
||||
final profileService = ProfileService(dio, baseUrl: baseUrl);
|
||||
final dietsService = DietService(dio, baseUrl: baseUrl);
|
||||
|
||||
// get all of needed data
|
||||
final dietsPaginationModel = await feedService.getDiets(page: 1);
|
||||
final chosenDietsPaginationModel = await profileService.getChosenDiets();
|
||||
final lastChosenDiet = chosenDietsPaginationModel.diets.firstOrNull;
|
||||
final today = DateTime.now();
|
||||
final yesterday = today.subtract(const Duration(days: 1));
|
||||
final tomorrow = today.add(const Duration(days: 1));
|
||||
|
||||
// run test case
|
||||
for (final diet in dietsPaginationModel.data) {
|
||||
if (!diet.author.authorIsMe) {
|
||||
if (lastChosenDiet?.state == 'started' || lastChosenDiet?.state == 'inited') {
|
||||
try {
|
||||
await dietsService.cancelDiet(id: lastChosenDiet?.dietId ?? 0);
|
||||
} catch (e) {
|
||||
log('unexpected error7: $e');
|
||||
return false;
|
||||
}
|
||||
await Future.delayed(Duration(seconds: 3));
|
||||
}
|
||||
try {
|
||||
await dietsService.startDiet(
|
||||
id: diet.id,
|
||||
body: {
|
||||
'date': yesterday.format('yyyy-MM-dd'),
|
||||
},
|
||||
);
|
||||
|
||||
return false;
|
||||
} catch (e) {
|
||||
log('expect ot have error: $e');
|
||||
}
|
||||
|
||||
// test start diet with correct date
|
||||
try {
|
||||
await dietsService.startDiet(
|
||||
id: diet.id,
|
||||
body: {
|
||||
'date': today.format('yyyy-MM-dd'),
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
log('unexpected error2: $e');
|
||||
return false;
|
||||
}
|
||||
|
||||
// test cancelation of diet
|
||||
try {
|
||||
await dietsService.cancelDiet(id: diet.id);
|
||||
} catch (e) {
|
||||
log('unexpected error3: $e');
|
||||
return false;
|
||||
}
|
||||
|
||||
await Future.delayed(Duration(seconds: 3));
|
||||
|
||||
// test start diet with correct date
|
||||
try {
|
||||
await dietsService.startDiet(
|
||||
id: diet.id,
|
||||
body: {
|
||||
'date': tomorrow.format('yyyy-MM-dd'),
|
||||
},
|
||||
);
|
||||
} catch (e) {
|
||||
log('unexpected error4: $e');
|
||||
return false;
|
||||
}
|
||||
|
||||
// test cancelation of diet
|
||||
try {
|
||||
await dietsService.cancelDiet(id: diet.id);
|
||||
} catch (e) {
|
||||
log('unexpected error5: $e');
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (e) {
|
||||
log('unexpected error6: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user