Make tables implement equals and hashCode

Fixes #74
This commit is contained in:
Simon Binder 2019-07-11 22:02:46 +02:00
parent 256b91f03f
commit f0e5ed9a87
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 47 additions and 0 deletions

View File

@ -49,4 +49,16 @@ mixin TableInfo<TableDsl extends Table, D extends DataClass> {
D map(Map<String, dynamic> data, {String tablePrefix});
TableInfo<TableDsl, D> createAlias(String alias);
@override
bool operator ==(other) {
// tables are singleton instances except for aliases
if (other is TableInfo) {
return other.runtimeType == runtimeType && other.$tableName == $tableName;
}
return false;
}
@override
int get hashCode => $mrjf($mrjc(runtimeType.hashCode, $tableName.hashCode));
}

View File

@ -0,0 +1,35 @@
import 'package:test_api/test_api.dart';
import 'data/tables/todos.dart';
import 'data/utils/mocks.dart';
void main() {
TodoDb db;
MockExecutor executor;
setUp(() {
executor = MockExecutor();
db = TodoDb(executor);
});
test('aliased tables implement equals correctly', () {
final first = db.users;
final aliasA = db.alias(db.users, 'a');
final anotherA = db.alias(db.categories, 'a');
expect(first == aliasA, isFalse);
// ignore: unrelated_type_equality_checks
expect(anotherA == aliasA, isFalse);
expect(aliasA == db.alias(db.users, 'a'), isTrue);
});
test('aliased table implement hashCode correctly', () {
final first = db.users;
final aliasA = db.alias(db.users, 'a');
final anotherA = db.alias(db.categories, 'a');
expect(first.hashCode == aliasA.hashCode, isFalse);
expect(anotherA.hashCode == aliasA.hashCode, isFalse);
expect(aliasA.hashCode == db.alias(db.users, 'a').hashCode, isTrue);
});
}