Skip to content

Commit

Permalink
Fix filtering bug of number.
Browse files Browse the repository at this point in the history
  • Loading branch information
bosskmk committed Feb 2, 2023
1 parent 68eaefc commit 5c952cd
Show file tree
Hide file tree
Showing 4 changed files with 265 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/src/model/pluto_column_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,11 @@ mixin PlutoColumnTypeWithNumberFormat {
}

int compare(dynamic a, dynamic b) {
return _compareWithNull(a, b, () => a.compareTo(b));
return _compareWithNull(
a,
b,
() => toNumber(a.toString()).compareTo(toNumber(b.toString())),
);
}

dynamic makeCompareValue(dynamic v) {
Expand Down
144 changes: 144 additions & 0 deletions test/scenario/filtering/currency_filtering_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pluto_grid/pluto_grid.dart';
import 'package:pluto_grid/src/ui/ui.dart';

void main() {
late PlutoGridStateManager stateManager;

Widget buildGrid({
required List<PlutoColumn> columns,
required List<PlutoRow> rows,
}) {
return MaterialApp(
home: Material(
child: PlutoGrid(
columns: columns,
rows: rows,
onLoaded: (e) {
stateManager = e.stateManager;
stateManager.setShowColumnFilter(true);
},
),
),
);
}

Finder findFilterTextField() {
return find.descendant(
of: find.byType(PlutoColumnFilter),
matching: find.byType(TextField),
);
}

Future<void> tapAndEnterTextColumnFilter(
WidgetTester tester,
String? enterText,
) async {
final textField = findFilterTextField();

// 텍스트 박스가 최초에 포커스를 받으려면 두번 탭.
await tester.tap(textField);
await tester.tap(textField);

if (enterText != null) {
await tester.enterText(textField, enterText);
}
}

group('기본 숫자 컬럼 테스트.', () {
final columns = [
PlutoColumn(
title: 'column',
field: 'column',
type: PlutoColumnType.currency(),
),
];

final rows = [
PlutoRow(cells: {'column': PlutoCell(value: 0)}),
PlutoRow(cells: {'column': PlutoCell(value: -100)}),
PlutoRow(cells: {'column': PlutoCell(value: -123000)}),
PlutoRow(cells: {'column': PlutoCell(value: 123000)}),
PlutoRow(cells: {'column': PlutoCell(value: 1)}),
PlutoRow(cells: {'column': PlutoCell(value: -1)}),
PlutoRow(cells: {'column': PlutoCell(value: 300)}),
PlutoRow(cells: {'column': PlutoCell(value: 311)}),
PlutoRow(cells: {'column': PlutoCell(value: -999)}),
PlutoRow(cells: {'column': PlutoCell(value: 3133)}),
];

testWidgets('렌더링 테스트', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));

expect(find.text('USD0.00'), findsOneWidget);
expect(find.text('-USD100.00'), findsOneWidget);
expect(find.text('-USD123,000.00'), findsOneWidget);
expect(find.text('USD123,000.00'), findsOneWidget);
expect(find.text('USD1.00'), findsOneWidget);
expect(find.text('-USD1.00'), findsOneWidget);
expect(find.text('USD300.00'), findsOneWidget);
expect(find.text('USD311.00'), findsOneWidget);
expect(find.text('-USD999.00'), findsOneWidget);
expect(find.text('USD3,133.00'), findsOneWidget);
});

testWidgets('300 보다 큰 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(const PlutoFilterTypeGreaterThan());

await tapAndEnterTextColumnFilter(tester, '300');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 311, 3133]);
});

testWidgets('300 보다 크거나 같은 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(
const PlutoFilterTypeGreaterThanOrEqualTo(),
);

await tapAndEnterTextColumnFilter(tester, '300');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 300, 311, 3133]);
});

testWidgets('300.01 보다 큰 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(const PlutoFilterTypeGreaterThan());

await tapAndEnterTextColumnFilter(tester, '300.01');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 311, 3133]);
});

testWidgets('USD300.01 보다 큰 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(const PlutoFilterTypeGreaterThan());

await tapAndEnterTextColumnFilter(tester, 'USD300.01');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 311, 3133]);
});
});
}
116 changes: 116 additions & 0 deletions test/scenario/filtering/number_filtering_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:pluto_grid/pluto_grid.dart';
import 'package:pluto_grid/src/ui/ui.dart';

void main() {
late PlutoGridStateManager stateManager;

Widget buildGrid({
required List<PlutoColumn> columns,
required List<PlutoRow> rows,
}) {
return MaterialApp(
home: Material(
child: PlutoGrid(
columns: columns,
rows: rows,
onLoaded: (e) {
stateManager = e.stateManager;
stateManager.setShowColumnFilter(true);
},
),
),
);
}

Finder findFilterTextField() {
return find.descendant(
of: find.byType(PlutoColumnFilter),
matching: find.byType(TextField),
);
}

Future<void> tapAndEnterTextColumnFilter(
WidgetTester tester,
String? enterText,
) async {
final textField = findFilterTextField();

// 텍스트 박스가 최초에 포커스를 받으려면 두번 탭.
await tester.tap(textField);
await tester.tap(textField);

if (enterText != null) {
await tester.enterText(textField, enterText);
}
}

group('기본 숫자 컬럼 테스트.', () {
final columns = [
PlutoColumn(
title: 'column',
field: 'column',
type: PlutoColumnType.number(),
),
];

final rows = [
PlutoRow(cells: {'column': PlutoCell(value: 0)}),
PlutoRow(cells: {'column': PlutoCell(value: -100)}),
PlutoRow(cells: {'column': PlutoCell(value: -123000)}),
PlutoRow(cells: {'column': PlutoCell(value: 123000)}),
PlutoRow(cells: {'column': PlutoCell(value: 1)}),
PlutoRow(cells: {'column': PlutoCell(value: -1)}),
PlutoRow(cells: {'column': PlutoCell(value: 300)}),
PlutoRow(cells: {'column': PlutoCell(value: 311)}),
PlutoRow(cells: {'column': PlutoCell(value: -999)}),
PlutoRow(cells: {'column': PlutoCell(value: 3133)}),
];

testWidgets('렌더링 테스트', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));

expect(find.text('0'), findsOneWidget);
expect(find.text('-100'), findsOneWidget);
expect(find.text('-123,000'), findsOneWidget);
expect(find.text('123,000'), findsOneWidget);
expect(find.text('1'), findsOneWidget);
expect(find.text('-1'), findsOneWidget);
expect(find.text('300'), findsOneWidget);
expect(find.text('311'), findsOneWidget);
expect(find.text('-999'), findsOneWidget);
expect(find.text('3,133'), findsOneWidget);
});

testWidgets('300 보다 큰 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(const PlutoFilterTypeGreaterThan());

await tapAndEnterTextColumnFilter(tester, '300');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 311, 3133]);
});

testWidgets('300 보다 크거나 같은 수 필터링.', (tester) async {
await tester.pumpWidget(buildGrid(columns: columns, rows: rows));
await tester.pump();

columns.first.setDefaultFilter(
const PlutoFilterTypeGreaterThanOrEqualTo(),
);

await tapAndEnterTextColumnFilter(tester, '300');
await tester.pumpAndSettle(const Duration(seconds: 1));

final values = stateManager.refRows.map((e) => e.cells['column']!.value);

expect(values, [123000, 300, 311, 3133]);
});
});
}
File renamed without changes.

0 comments on commit 5c952cd

Please sign in to comment.