Skip to content

Commit

Permalink
Restructure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
w2sv committed Sep 14, 2022
1 parent 70e077b commit 08ec9bf
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/koala.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library koala;

export 'src/dataframe.dart';
export 'src/column.dart';
export 'src/column.dart';
6 changes: 4 additions & 2 deletions lib/src/dataframe.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ class DataFrame extends ListBase<RecordRow> {
/// [{'col1': 420, 'col2': 69},
/// {'col1': 666, 'col2': 1470}]
DataFrame.fromRowMaps(List<RecordRowMap> rowMaps)
: this._columnNames = ElementPositionTrackingList(rowMaps.first.keys.toList()),
: this._columnNames =
ElementPositionTrackingList(rowMaps.first.keys.toList()),
super(rowMaps.map((e) => e.values.toList()).toList());

/// Returns an empty dataframe.
Expand Down Expand Up @@ -322,7 +323,8 @@ class DataFrame extends ListBase<RecordRow> {
/// Returns a deep copy of the dataframe.
DataFrame copy() => DataFrame._copied(_columnNames, this);

DataFrame._copied(ElementPositionTrackingList<String> columns, DataMatrix data)
DataFrame._copied(
ElementPositionTrackingList<String> columns, DataMatrix data)
: this._columnNames = columns.copy(),
super(ListListExtensions(data).copy());

Expand Down
3 changes: 2 additions & 1 deletion lib/src/utils/element_position_tracking_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class ElementPositionTrackingList<T> extends ListBase<T> {
: _object2Index = elements.asInvertedMap(),
super(elements);

ElementPositionTrackingList<T> copy() => ElementPositionTrackingList(ListExtensions(this).copy());
ElementPositionTrackingList<T> copy() =>
ElementPositionTrackingList(ListExtensions(this).copy());

// *************** overrides *******************

Expand Down
7 changes: 3 additions & 4 deletions lib/src/utils/extensions/list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ extension ListExtensions<E> on List<E> {
Map<E, int> asInvertedMap() => asMap().inverted();
}

extension ListListExtensions<E> on List<List<E>>{
List<List<E>> copy() =>
[for (final sub in this) sub.copy()];
}
extension ListListExtensions<E> on List<List<E>> {
List<List<E>> copy() => [for (final sub in this) sub.copy()];
}
32 changes: 14 additions & 18 deletions test/src/dataframe_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:io';
import 'package:koala/koala.dart';
import 'package:test/test.dart';

String _csvPath(String name) => 'test/data/$name';
import '../test_utils.dart';

extension RecordsExtensions on Records {
Set<Type> _types() => map((e) => e.runtimeType).toSet();
Expand All @@ -15,10 +15,6 @@ DataFrame _getDF() => DataFrame.fromRowMaps([
{'col1': null, 'col2': 8},
]);

final _outputDir = Directory('test/output');

String _outputFilePath(String name) => '${_outputDir.path}/$name';

void main() {
test('fromRowMaps', () async {
final date = DateTime.now();
Expand Down Expand Up @@ -46,7 +42,7 @@ void main() {
group('fromCsv', () {
test('basic parsing', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('with_date.csv'), convertDates: false, eolToken: '\n');
path: csvPath('with_date.csv'), convertDates: false, eolToken: '\n');
expect(df.columnNames, ['symbol', 'date', 'price', 'n']);
expect(df.length, 2);
expect(df('price')._types(), {double});
Expand All @@ -55,15 +51,15 @@ void main() {

test('automatic date conversion', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('iso_date.csv'), eolToken: '\n');
path: csvPath('iso_date.csv'), eolToken: '\n');
expect(df('date')._types(), {DateTime});
expect(df('date').map((el) => el.toString()).toList(),
['2020-04-12 12:16:54.220', '2020-04-12 12:16:54.220']);
});

test('date conversion with specified format', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('with_date.csv'),
path: csvPath('with_date.csv'),
eolToken: '\n',
datePattern: 'MMM d yyyy');

Expand All @@ -72,19 +68,19 @@ void main() {

test('newline at the end of file', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('terminating_newline.csv'), eolToken: '\n');
path: csvPath('terminating_newline.csv'), eolToken: '\n');
expect(df.length, 1);
});

test('max rows', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('stocks.csv'), eolToken: '\n', maxRows: 20);
path: csvPath('stocks.csv'), eolToken: '\n', maxRows: 20);
expect(df.length, 20);
});

test('no header', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('no_header.csv'),
path: csvPath('no_header.csv'),
eolToken: '\n',
containsHeader: false,
columnNames: ['symbol', 'date', 'price', 'n']);
Expand All @@ -94,7 +90,7 @@ void main() {

test('skip columns', () async {
DataFrame df = await DataFrame.fromCsv(
path: _csvPath('with_date.csv'),
path: csvPath('with_date.csv'),
eolToken: '\n',
skipColumns: ['price']);
expect(df.length, 2);
Expand Down Expand Up @@ -150,7 +146,7 @@ void main() {

test('slicing', () async {
DataFrame df =
(await DataFrame.fromCsv(path: _csvPath('stocks.csv'), eolToken: '\n'))
(await DataFrame.fromCsv(path: csvPath('stocks.csv'), eolToken: '\n'))
..slice(0, 30);
expect(df.length, 30);

Expand Down Expand Up @@ -289,7 +285,7 @@ void main() {

group('toCsv', () {
test('default', () async {
final outputCsvPath = _outputFilePath('out.csv');
final outputCsvPath = outputFilePath('out.csv');
final df = DataFrame.fromNamesAndData([
'a',
'b',
Expand All @@ -303,7 +299,7 @@ void main() {
});

test('with null', () async {
final outputCsvPath = _outputFilePath('out1.csv');
final outputCsvPath = outputFilePath('out1.csv');
final df = DataFrame.fromNamesAndData([
'a',
'b',
Expand All @@ -325,7 +321,7 @@ void main() {
// });

test('with single quote including strings', () async {
final outputCsvPath = _outputFilePath('out3.csv');
final outputCsvPath = outputFilePath('out3.csv');
final df = DataFrame.fromNamesAndData([
'a',
'b',
Expand All @@ -339,7 +335,7 @@ void main() {
});

test('without header', () async {
final outputCsvPath = _outputFilePath('out4.csv');
final outputCsvPath = outputFilePath('out4.csv');
final df = DataFrame.fromNamesAndData([
'a',
'b',
Expand All @@ -357,7 +353,7 @@ void main() {
});

tearDownAll(() {
_outputDir.list().forEach((element) => element.delete());
outputDir.list().forEach((element) => element.delete());
});

test('misc', () {
Expand Down
7 changes: 7 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import 'dart:io';

final outputDir = Directory('test/output');

String outputFilePath(String name) => '${outputDir.path}/$name';

String csvPath(String name) => 'test/data/$name';

0 comments on commit 08ec9bf

Please sign in to comment.