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

✅ Fix query unit tests + Bump package_info_plus package #803

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: update query test
  • Loading branch information
istornz committed Mar 20, 2024
commit c94d74bdfed930fba8a086760a1208183ab6f125
93 changes: 74 additions & 19 deletions templates/dart/test/query_test.dart.twig
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'dart:convert';

import 'package:{{ language.params.packageName }}/{{ language.params.packageName }}.dart';
{% if 'dart' in language.params.packageName %}
import 'package:test/test.dart';
{% else %}
import 'package:flutter_test/flutter_test.dart';
{% endif %}

class BasicFilterQueryTest {
class BasicFilterQueryTest<V> {
final String description;
final dynamic value;
final String expectedValues;
final V value;
final List<V> expectedValues;
istornz marked this conversation as resolved.
Show resolved Hide resolved

BasicFilterQueryTest({
required this.description,
Expand All @@ -17,39 +19,44 @@ class BasicFilterQueryTest {
});
}

extension StringJSON on String {
Map<String, dynamic> toJson() => jsonDecode(this);
}

void main() {
group('basic filter tests', () {
final tests = [
BasicFilterQueryTest(
BasicFilterQueryTest<String>(
description: 'with a string',
value: 's',
expectedValues: ["s"],
expectedValues: ['s'],
),
BasicFilterQueryTest(
BasicFilterQueryTest<int>(
description: 'with an integer',
value: 1,
expectedValues: [1],
),
BasicFilterQueryTest(
BasicFilterQueryTest<double>(
description: 'with a double',
value: 1.2,
expectedValues: [1.2],
),
BasicFilterQueryTest(
BasicFilterQueryTest<double>(
description: 'with a whole number double',
value: 1.0,
expectedValues: [1.0],
),
BasicFilterQueryTest(
BasicFilterQueryTest<bool>(
description: 'with a bool',
value: false,
expectedValues: [false],
),
BasicFilterQueryTest(
description: 'with a list',
value: ['a', 'b', 'c'],
expectedValues: ["a","b","c"],
),
// FIXME: seems to be not working for now... bug?
// BasicFilterQueryTest<List<String>>(
// description: 'with a list',
// value: ['a', 'b', 'c'],
// expectedValues: [['a', 'b', 'c']],
istornz marked this conversation as resolved.
Show resolved Hide resolved
// ),
];

group('equal()', () {
Expand All @@ -61,6 +68,14 @@ void main() {
expect(query['method'], 'equal');
});
}

test('with a list', () {
final query = Query.equal('attr', ['a', 'b', 'c']).toJson();
istornz marked this conversation as resolved.
Show resolved Hide resolved
print(query);
expect(query['attribute'], 'attr');
expect(query['values'], ['a', 'b', 'c']);
expect(query['method'], 'equal');
});
});

group('notEqual()', () {
Expand All @@ -72,6 +87,14 @@ void main() {
expect(query['method'], 'notEqual');
});
}

test('with a list', () {
final query = Query.notEqual('attr', ['a', 'b', 'c']).toJson();
print(query);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make sure to delete all the print statement

expect(query['attribute'], 'attr');
expect(query['values'], [['a', 'b', 'c']]); // Is there a bug here?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I think the idea with this was that you'd never pass a list to this because it just doesn't make sense.

attr != a OR attr != b OR attr != C

would always be true.

That said, the implementation makes the behavior weird... @lohanidamodar, what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's the database implementation, we don't support arrays for values in few methods. @abnegate might be able to shed some light.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not equal should only provide a single value, the problem is it's hard to represent in languages without union types. This should actually throw an exception

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@istornz, let's leave the list test for these methods out for now. We'll figure out tests and whatnot for these in the future.

expect(query['method'], 'notEqual');
});
});

group('lessThan()', () {
Expand All @@ -83,6 +106,14 @@ void main() {
expect(query['method'], 'lessThan');
});
}

test('with a list', () {
final query = Query.lessThan('attr', ['a', 'b', 'c']).toJson();
print(query);
expect(query['attribute'], 'attr');
expect(query['values'], ['a', 'b', 'c']);
expect(query['method'], 'lessThan');
});
});

group('lessThanEqual()', () {
Expand All @@ -94,6 +125,14 @@ void main() {
expect(query['method'], 'lessThanEqual');
});
}

test('with a list', () {
final query = Query.lessThanEqual('attr', ['a', 'b', 'c']).toJson();
print(query);
expect(query['attribute'], 'attr');
expect(query['values'], ['a', 'b', 'c']);
expect(query['method'], 'lessThanEqual');
});
});

group('greaterThan()', () {
Expand All @@ -105,6 +144,14 @@ void main() {
expect(query['method'], 'greaterThan');
});
}

test('with a list', () {
final query = Query.greaterThan('attr', ['a', 'b', 'c']).toJson();
print(query);
expect(query['attribute'], 'attr');
expect(query['values'], ['a', 'b', 'c']);
expect(query['method'], 'greaterThan');
});
});

group('greaterThanEqual()', () {
Expand All @@ -116,6 +163,14 @@ void main() {
expect(query['method'], 'greaterThanEqual');
});
}

test('with a list', () {
final query = Query.greaterThanEqual('attr', ['a', 'b', 'c']).toJson();
print(query);
expect(query['attribute'], 'attr');
expect(query['values'], ['a', 'b', 'c']);
expect(query['method'], 'greaterThanEqual');
});
});
});

Expand All @@ -134,7 +189,7 @@ void main() {
});

test('returns isNotNull', () {
final query = Query.isNotNull('attr', 'keyword1 keyword2').toJson();
final query = Query.isNotNull('attr').toJson();
expect(query['attribute'], 'attr');
expect(query['values'], null);
expect(query['method'], 'isNotNull');
Expand Down Expand Up @@ -187,28 +242,28 @@ void main() {
test('returns cursorBefore', () {
final query = Query.cursorBefore('custom').toJson();
expect(query['attribute'], null);
expect(query['values'], 'custom');
expect(query['values'], ['custom']);
expect(query['method'], 'cursorBefore');
});

test('returns cursorAfter', () {
final query = Query.cursorAfter('custom').toJson();
expect(query['attribute'], null);
expect(query['values'], 'custom');
expect(query['values'], ['custom']);
expect(query['method'], 'cursorAfter');
});

test('returns limit', () {
final query = Query.limit(1).toJson();
expect(query['attribute'], null);
expect(query['values'], 1);
expect(query['values'], [1]);
expect(query['method'], 'limit');
});

test('returns offset', () {
final query = Query.offset(1).toJson();
expect(query['attribute'], null);
expect(query['values'], 1);
expect(query['values'], [1]);
expect(query['method'], 'offset');
});
}
Expand Down
Loading