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

[bugfix/aggregate_test] #2819

Merged
merged 4 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ docs/**/*.g.dart
*/build/
drift/extension/devtools/build
**/pubspec_overrides.yaml
**.history/
14 changes: 14 additions & 0 deletions drift/lib/src/runtime/query_builder/expressions/aggregate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ extension BaseAggregate<DT extends Object> on Expression<DT> {
filter: filter, distinct: distinct);
}

/// Return the maximum of all non-null values in this group.
///
/// If there are no non-null values in the group, returns null.
/// {@macro drift_aggregate_filter}
Expression<DT> max({Expression<bool>? filter}) =>
_AggregateExpression('MAX', [this], filter: filter);

/// Return the minimum of all non-null values in this group.
///
/// If there are no non-null values in the group, returns null.
/// {@macro drift_aggregate_filter}
Expression<DT> min({Expression<bool>? filter}) =>
_AggregateExpression('MIN', [this], filter: filter);

/// Returns the concatenation of all non-null values in the current group,
/// joined by the [separator].
///
Expand Down
148 changes: 148 additions & 0 deletions drift/test/database/expressions/aggregate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import '../../test_utils/test_utils.dart';

void main() {
const foo = CustomExpression<int>('foo', precedence: Precedence.primary);
const b1 = CustomExpression<BigInt>('b1', precedence: Precedence.primary);
const s1 = CustomExpression<String>('s1', precedence: Precedence.primary);
const p1 = CustomExpression<bool>('p1', precedence: Precedence.primary);

group('count', () {
test('all', () {
Expand All @@ -16,72 +19,157 @@ void main() {
countAll(filter: foo.isBiggerOrEqualValue(3)),
generates('COUNT(*) FILTER (WHERE foo >= ?)', [3]),
);
expect(
countAll(filter: b1.isBiggerOrEqualValue(BigInt.from(3))),
generates('COUNT(*) FILTER (WHERE b1 >= ?)', [BigInt.from(3)]),
);
expect(
countAll(filter: s1.equals('STRING_VALUE')),
generates('COUNT(*) FILTER (WHERE s1 = ?)', ['STRING_VALUE']),
);
expect(
countAll(filter: p1.equals(true)),
generates('COUNT(*) FILTER (WHERE p1 = ?)', [1]),
);
});

test('single', () {
expect(foo.count(), generates('COUNT(foo)'));
expect(b1.count(), generates('COUNT(b1)'));
expect(s1.count(), generates('COUNT(s1)'));
expect(p1.count(), generates('COUNT(p1)'));
});

test('single - distinct', () {
expect(foo.count(distinct: true), generates('COUNT(DISTINCT foo)'));
expect(b1.count(distinct: true), generates('COUNT(DISTINCT b1)'));
expect(s1.count(distinct: true), generates('COUNT(DISTINCT s1)'));
expect(p1.count(distinct: true), generates('COUNT(DISTINCT p1)'));
});

test('single - filter', () {
expect(
foo.count(filter: foo.isBiggerOrEqualValue(3)),
generates('COUNT(foo) FILTER (WHERE foo >= ?)', [3]),
);
expect(
b1.count(filter: b1.isBiggerOrEqualValue(BigInt.from(3))),
generates('COUNT(b1) FILTER (WHERE b1 >= ?)', [BigInt.from(3)]),
);
expect(
s1.count(filter: s1.equals('STRING_VALUE')),
generates('COUNT(s1) FILTER (WHERE s1 = ?)', ['STRING_VALUE']),
);
expect(
p1.count(filter: p1.equals(true)),
generates('COUNT(p1) FILTER (WHERE p1 = ?)', [1]),
);
});

test('single - distinct and filter', () {
expect(
foo.count(distinct: true, filter: foo.isBiggerOrEqualValue(3)),
generates('COUNT(DISTINCT foo) FILTER (WHERE foo >= ?)', [3]),
);
expect(
b1.count(
distinct: true, filter: b1.isBiggerOrEqualValue(BigInt.from(3))),
generates(
'COUNT(DISTINCT b1) FILTER (WHERE b1 >= ?)', [BigInt.from(3)]),
);
expect(
s1.count(distinct: true, filter: s1.equals('STRING_VALUE')),
generates('COUNT(DISTINCT s1) FILTER (WHERE s1 = ?)', ['STRING_VALUE']),
);
expect(
p1.count(distinct: true, filter: p1.equals(true)),
generates('COUNT(DISTINCT p1) FILTER (WHERE p1 = ?)', [1]),
);
});
});

test('avg', () {
expect(foo.avg(), generates('AVG(foo)'));
expect(b1.avg(), generates('AVG(b1)'));

expect(foo.avg(filter: foo.isBiggerOrEqualValue(3)),
generates('AVG(foo) FILTER (WHERE foo >= ?)', [3]));
expect(b1.avg(filter: b1.isBiggerOrEqualValue(BigInt.from(3))),
generates('AVG(b1) FILTER (WHERE b1 >= ?)', [BigInt.from(3)]));
});

test('max', () {
expect(foo.max(), generates('MAX(foo)'));
expect(b1.max(), generates('MAX(b1)'));
expect(s1.max(), generates('MAX(s1)'));
expect(p1.max(), generates('MAX(p1)'));
});

test('min', () {
expect(foo.min(), generates('MIN(foo)'));
expect(b1.min(), generates('MIN(b1)'));
expect(s1.min(), generates('MIN(s1)'));
expect(p1.min(), generates('MIN(p1)'));
});

test('sum', () {
expect(foo.sum(), generates('SUM(foo)'));
expect(b1.sum(), generates('SUM(b1)'));
});

test('total', () {
expect(foo.total(), generates('TOTAL(foo)'));
expect(b1.total(), generates('TOTAL(b1)'));
});

group('group_concat', () {
test('with the default separator', () {
expect(foo.groupConcat(), generates('GROUP_CONCAT(foo)'));
expect(b1.groupConcat(), generates('GROUP_CONCAT(b1)'));
expect(s1.groupConcat(), generates('GROUP_CONCAT(s1)'));
expect(p1.groupConcat(), generates('GROUP_CONCAT(p1)'));

expect(foo.groupConcat(separator: ','), generates('GROUP_CONCAT(foo)'));
expect(b1.groupConcat(separator: ','), generates('GROUP_CONCAT(b1)'));
expect(s1.groupConcat(separator: ','), generates('GROUP_CONCAT(s1)'));
expect(p1.groupConcat(separator: ','), generates('GROUP_CONCAT(p1)'));
});

test('with a custom separator', () {
expect(foo.groupConcat(separator: ' and '),
generates('GROUP_CONCAT(foo, ?)', [' and ']));
expect(b1.groupConcat(separator: ' and '),
generates('GROUP_CONCAT(b1, ?)', [' and ']));
expect(s1.groupConcat(separator: ' and '),
generates('GROUP_CONCAT(s1, ?)', [' and ']));
expect(p1.groupConcat(separator: ' and '),
generates('GROUP_CONCAT(p1, ?)', [' and ']));
});

test('with a filter', () {
expect(foo.groupConcat(filter: foo.isSmallerThan(const Variable(3))),
generates('GROUP_CONCAT(foo) FILTER (WHERE foo < ?)', [3]));
expect(
b1.groupConcat(filter: b1.isSmallerThan(Variable(BigInt.from(3)))),
generates(
'GROUP_CONCAT(b1) FILTER (WHERE b1 < ?)', [BigInt.from(3)]));
expect(
s1.groupConcat(filter: s1.isSmallerThan(Variable('STRING_VALUE'))),
generates(
'GROUP_CONCAT(s1) FILTER (WHERE s1 < ?)', ['STRING_VALUE']));
expect(p1.groupConcat(filter: p1.equals(true)),
generates('GROUP_CONCAT(p1) FILTER (WHERE p1 = ?)', [1]));
});

test('with distinct', () {
expect(foo.groupConcat(distinct: true),
generates('GROUP_CONCAT(DISTINCT foo)', isEmpty));
expect(b1.groupConcat(distinct: true),
generates('GROUP_CONCAT(DISTINCT b1)', isEmpty));
expect(s1.groupConcat(distinct: true),
generates('GROUP_CONCAT(DISTINCT s1)', isEmpty));
expect(p1.groupConcat(distinct: true),
generates('GROUP_CONCAT(DISTINCT p1)', isEmpty));

expect(
foo.groupConcat(
Expand All @@ -93,11 +181,47 @@ void main() {
[3],
),
);
expect(
b1.groupConcat(
distinct: true,
filter: b1.isSmallerThan(Variable(BigInt.from(3))),
),
generates(
'GROUP_CONCAT(DISTINCT b1) FILTER (WHERE b1 < ?)',
[BigInt.from(3)],
),
);
expect(
s1.groupConcat(
distinct: true,
filter: s1.isSmallerThan(Variable('STRING_VALUE')),
),
generates(
'GROUP_CONCAT(DISTINCT s1) FILTER (WHERE s1 < ?)',
['STRING_VALUE'],
),
);
expect(
p1.groupConcat(
distinct: true,
filter: p1.equals(true),
),
generates(
'GROUP_CONCAT(DISTINCT p1) FILTER (WHERE p1 = ?)',
[1],
),
);
});

test('does not allow distinct with a custom separator', () {
expect(() => foo.groupConcat(distinct: true, separator: ' and '),
throwsArgumentError);
expect(() => b1.groupConcat(distinct: true, separator: ' and '),
throwsArgumentError);
expect(() => s1.groupConcat(distinct: true, separator: ' and '),
throwsArgumentError);
expect(() => p1.groupConcat(distinct: true, separator: ' and '),
throwsArgumentError);

expect(
() => foo.groupConcat(
Expand All @@ -107,6 +231,30 @@ void main() {
),
throwsArgumentError,
);
expect(
() => b1.groupConcat(
distinct: true,
separator: ' and ',
filter: b1.isSmallerThan(Variable(BigInt.from(3))),
),
throwsArgumentError,
);
expect(
() => s1.groupConcat(
distinct: true,
separator: ' and ',
filter: s1.isSmallerThan(Variable('STRING_VALUE')),
),
throwsArgumentError,
);
expect(
() => p1.groupConcat(
distinct: true,
separator: ' and ',
filter: p1.equals(true),
),
throwsArgumentError,
);
});
});
}
Loading