chore: Move the drift_offline_first to legacy in favor of Serverpod implementation (#32)
This commit is contained in:
parent
027aec5ed0
commit
cbc472523b
6 changed files with 13575 additions and 284 deletions
133
legacy/drift_offline_sync/test/hlc/hlc_comparison_test.dart
Normal file
133
legacy/drift_offline_sync/test/hlc/hlc_comparison_test.dart
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
final _dateTime = DateTime.parse('2001-09-09T01:46:40.000Z');
|
||||
|
||||
void main() {
|
||||
group('Given two Hlcs with same dateTime, counter and nodeId when comparing ', () {
|
||||
final hlc1 = Hlc(_dateTime, 17, 'abc');
|
||||
final hlc2 = Hlc(_dateTime, 17, 'abc');
|
||||
|
||||
test('then they are equal.', () {
|
||||
expect(hlc1, hlc2);
|
||||
expect(hlc1 == hlc2, isTrue);
|
||||
expect(hlc1, equals(hlc2));
|
||||
});
|
||||
|
||||
test('then compareTo returns 0.', () {
|
||||
expect(hlc1.compareTo(hlc2), 0);
|
||||
});
|
||||
|
||||
test('then <= is true.', () {
|
||||
expect(hlc1 <= hlc2, isTrue);
|
||||
expect(hlc1, lessThanOrEqualTo(hlc2));
|
||||
});
|
||||
|
||||
test('then >= is true.', () {
|
||||
expect(hlc1 >= hlc2, isTrue);
|
||||
expect(hlc1, greaterThanOrEqualTo(hlc2));
|
||||
});
|
||||
});
|
||||
|
||||
group(
|
||||
'Given two Hlcs with same dateTime and counter and different nodeId when comparing ',
|
||||
() {
|
||||
final hlc1 = Hlc(_dateTime, 17, 'abc');
|
||||
final hlc2 = Hlc(_dateTime, 17, 'abb');
|
||||
|
||||
test('then they are not equal.', () {
|
||||
expect(hlc1, isNot(hlc2));
|
||||
});
|
||||
|
||||
test('then compareTo returns 1.', () {
|
||||
expect(hlc1.compareTo(hlc2), 1);
|
||||
});
|
||||
|
||||
test('then lexicographically greater nodeId is greater.', () {
|
||||
expect(hlc1 > hlc2, isTrue);
|
||||
expect(hlc1, greaterThan(hlc2));
|
||||
});
|
||||
|
||||
test('then >= is true.', () {
|
||||
expect(hlc1 >= hlc2, isTrue);
|
||||
expect(hlc1, greaterThanOrEqualTo(hlc2));
|
||||
});
|
||||
|
||||
test('then < is false.', () {
|
||||
expect(hlc1 < hlc2, isFalse);
|
||||
});
|
||||
|
||||
test('then <= is false.', () {
|
||||
expect(hlc1 <= hlc2, isFalse);
|
||||
expect(hlc1, isNot(lessThanOrEqualTo(hlc2)));
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
group('Given two Hlcs with same dateTime and different counter when comparing ', () {
|
||||
final hlc1 = Hlc(_dateTime, 17, 'abc');
|
||||
final hlc2 = Hlc(_dateTime, 18, 'abc');
|
||||
|
||||
test('then they are not equal.', () {
|
||||
expect(hlc1, isNot(hlc2));
|
||||
});
|
||||
|
||||
test('then compareTo returns -1.', () {
|
||||
expect(hlc1.compareTo(hlc2), -1);
|
||||
});
|
||||
|
||||
test('then < is true.', () {
|
||||
expect(hlc1 < hlc2, isTrue);
|
||||
expect(hlc1, lessThan(hlc2));
|
||||
});
|
||||
|
||||
test('then <= is true.', () {
|
||||
expect(hlc1 <= hlc2, isTrue);
|
||||
expect(hlc1, lessThanOrEqualTo(hlc2));
|
||||
});
|
||||
|
||||
test('then > is false.', () {
|
||||
expect(hlc1 > hlc2, isFalse);
|
||||
});
|
||||
|
||||
test('then >= is false.', () {
|
||||
expect(hlc1 >= hlc2, isFalse);
|
||||
expect(hlc1, isNot(greaterThanOrEqualTo(hlc2)));
|
||||
});
|
||||
});
|
||||
|
||||
group(
|
||||
'Given two Hlcs with different dateTime when comparing ',
|
||||
() {
|
||||
final hlc1 = Hlc(_dateTime, 0, 'abc');
|
||||
final hlc2 = Hlc(_dateTime.add(const Duration(milliseconds: 1)), 17, 'abc');
|
||||
|
||||
test('then they are not equal.', () {
|
||||
expect(hlc1, isNot(hlc2));
|
||||
});
|
||||
|
||||
test('then compareTo returns -1.', () {
|
||||
expect(hlc1.compareTo(hlc2), -1);
|
||||
});
|
||||
|
||||
test('then < is true.', () {
|
||||
expect(hlc1 < hlc2, isTrue);
|
||||
expect(hlc1, lessThan(hlc2));
|
||||
});
|
||||
|
||||
test('then <= is true.', () {
|
||||
expect(hlc1 <= hlc2, isTrue);
|
||||
expect(hlc1, lessThanOrEqualTo(hlc2));
|
||||
});
|
||||
|
||||
test('then > is false.', () {
|
||||
expect(hlc1 > hlc2, isFalse);
|
||||
});
|
||||
|
||||
test('then >= is false.', () {
|
||||
expect(hlc1 >= hlc2, isFalse);
|
||||
expect(hlc1, isNot(greaterThanOrEqualTo(hlc2)));
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
27
legacy/drift_offline_sync/test/hlc/hlc_converter_test.dart
Normal file
27
legacy/drift_offline_sync/test/hlc/hlc_converter_test.dart
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
const _isoTime = '2001-09-09T01:46:40.000Z';
|
||||
final _dateTime = DateTime.parse(_isoTime);
|
||||
|
||||
void main() {
|
||||
test('Given an HLC and the HlcConverter '
|
||||
'when converting to the database format '
|
||||
'then it returns string with padded unix timestamp.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(
|
||||
hlcConverter.toSql(hlc),
|
||||
'${_dateTime.millisecondsSinceEpoch.toString().padLeft(15, '0')}-0017-abc',
|
||||
);
|
||||
});
|
||||
|
||||
test('Given an HLC and the HlcConverter '
|
||||
'when converting from the database format '
|
||||
'then it returns the equivalent HLC.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
final databaseHlc = hlcConverter.toSql(hlc);
|
||||
|
||||
expect(hlcConverter.fromSql(databaseHlc), hlc);
|
||||
});
|
||||
}
|
||||
29
legacy/drift_offline_sync/test/hlc/hlc_copy_with_test.dart
Normal file
29
legacy/drift_offline_sync/test/hlc/hlc_copy_with_test.dart
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
final _dateTime = DateTime.parse('2001-09-09T01:46:40.000Z');
|
||||
|
||||
void main() {
|
||||
group('Given an HLC', () {
|
||||
final hlc = Hlc(_dateTime, 17, 'abc');
|
||||
|
||||
test('when calling copyWith with new dateTime then result has new dateTime.', () {
|
||||
final newDateTime = _dateTime.add(const Duration(minutes: 1));
|
||||
final newHlc = hlc.copyWith(dateTime: newDateTime);
|
||||
|
||||
expect(newHlc.dateTime, newDateTime);
|
||||
});
|
||||
|
||||
test('when calling copyWith with new counter then result has new counter.', () {
|
||||
final newHlc = hlc.copyWith(counter: 18);
|
||||
|
||||
expect(newHlc.counter, 18);
|
||||
});
|
||||
|
||||
test('when calling copyWith with new nodeId then result has new nodeId.', () {
|
||||
final newHlc = hlc.copyWith(nodeId: 'abcd');
|
||||
|
||||
expect(newHlc.nodeId, 'abcd');
|
||||
});
|
||||
});
|
||||
}
|
||||
82
legacy/drift_offline_sync/test/hlc/hlc_increment_test.dart
Normal file
82
legacy/drift_offline_sync/test/hlc/hlc_increment_test.dart
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
import 'package:clock/clock.dart';
|
||||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
final _dateTime = DateTime.parse('2001-09-09T01:46:40.000Z');
|
||||
|
||||
void main() {
|
||||
group('Given an HLC with lower canonical time than wall time', () {
|
||||
final hlc = Hlc(_dateTime, 17, 'abc');
|
||||
final wallTime = _dateTime.advance();
|
||||
|
||||
test('when incrementing then dateTime becomes wall time and counter resets.', () {
|
||||
withClock(Clock.fixed(wallTime), () {
|
||||
final sendHlc = hlc.increment();
|
||||
|
||||
expect(sendHlc, isNot(hlc));
|
||||
expect(sendHlc.dateTime, wallTime);
|
||||
expect(sendHlc.counter, 0);
|
||||
expect(sendHlc.nodeId, hlc.nodeId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Given an HLC with equal canonical time and wall time', () {
|
||||
final hlc = Hlc(_dateTime, 17, 'abc');
|
||||
final wallTime = _dateTime;
|
||||
|
||||
test('when incrementing then counter increments and dateTime is unchanged.', () {
|
||||
withClock(Clock.fixed(wallTime), () {
|
||||
final sendHlc = hlc.increment();
|
||||
|
||||
expect(sendHlc, isNot(hlc));
|
||||
expect(sendHlc.dateTime, hlc.dateTime);
|
||||
expect(sendHlc.counter, 18);
|
||||
expect(sendHlc.nodeId, hlc.nodeId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Given an HLC with higher canonical time than wall time', () {
|
||||
final hlc = Hlc(_dateTime, 17, 'abc');
|
||||
final wallTime = _dateTime.retreat();
|
||||
|
||||
test('when incrementing then counter increments and dateTime is unchanged.', () {
|
||||
withClock(Clock.fixed(wallTime), () {
|
||||
final sendHlc = hlc.increment();
|
||||
|
||||
expect(sendHlc, isNot(hlc));
|
||||
expect(sendHlc.dateTime, hlc.dateTime);
|
||||
expect(sendHlc.counter, 18);
|
||||
expect(sendHlc.nodeId, hlc.nodeId);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Given an HLC with canonical time more than one minute ahead of wall time', () {
|
||||
final hlc = Hlc(_dateTime.add(const Duration(minutes: 1, seconds: 5)), 0, 'abc');
|
||||
final wallTime = _dateTime;
|
||||
|
||||
test('when incrementing then ClockDriftException is thrown.', () {
|
||||
withClock(Clock.fixed(wallTime), () {
|
||||
expect(hlc.increment, throwsA(isA<ClockDriftException>()));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
group('Given an HLC with counter at maximum and canonical time at wall time', () {
|
||||
final hlc = Hlc(_dateTime, 0xFFFF, 'abc');
|
||||
final wallTime = _dateTime;
|
||||
|
||||
test('when incrementing at same time then OverflowException is thrown.', () {
|
||||
withClock(Clock.fixed(wallTime), () {
|
||||
expect(hlc.increment, throwsA(isA<OverflowException>()));
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
extension on DateTime {
|
||||
DateTime advance() => add(const Duration(milliseconds: 1));
|
||||
DateTime retreat() => subtract(const Duration(milliseconds: 1));
|
||||
}
|
||||
106
legacy/drift_offline_sync/test/hlc/hlc_merge_test.dart
Normal file
106
legacy/drift_offline_sync/test/hlc/hlc_merge_test.dart
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
import 'package:clock/clock.dart';
|
||||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
final _dateTime = DateTime.now().toUtc();
|
||||
|
||||
void main() {
|
||||
group('Given local canonical Hlc and remote with higher time', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime.advance(), 2, 'abd');
|
||||
|
||||
test('when merging then result has remote time and counter with local nodeId.', () {
|
||||
final hlc = canonical.merge(remote);
|
||||
|
||||
expect(hlc, Hlc(remote.dateTime, remote.counter, canonical.nodeId));
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with same time but higher counter', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime, 18, 'abd');
|
||||
|
||||
test('when merging then result has remote counter with local nodeId.', () {
|
||||
final hlc = canonical.merge(remote);
|
||||
|
||||
expect(hlc, Hlc(canonical.dateTime, remote.counter, canonical.nodeId));
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with same time and counter', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime, 17, 'abd');
|
||||
|
||||
test('when merging then local is returned.', () {
|
||||
final hlc = canonical.merge(remote);
|
||||
|
||||
expect(hlc, equals(canonical));
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with lower time', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime.retreat(), 17, 'abcd');
|
||||
|
||||
test('when merging then local is returned.', () {
|
||||
final hlc = canonical.merge(remote);
|
||||
|
||||
expect(hlc, equals(canonical));
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with same nodeId and same time', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime, 17, 'abc');
|
||||
|
||||
test('when merging then node id is not checked and local is returned.', () {
|
||||
expect(canonical.merge(remote), canonical);
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with same nodeId and lower time', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime.retreat(), 17, 'abc');
|
||||
|
||||
test('when merging then node id is not checked and local is returned.', () {
|
||||
expect(canonical.merge(remote), canonical);
|
||||
});
|
||||
});
|
||||
|
||||
group('Given local canonical Hlc and remote with same nodeId and higher time', () {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime.advance(), 0, 'abc');
|
||||
|
||||
test('when merging then DuplicateNodeException is thrown.', () {
|
||||
expect(
|
||||
() => canonical.merge(remote),
|
||||
throwsA(isA<DuplicateNodeException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
group(
|
||||
'Given local canonical Hlc and remote with time more than one minute ahead of wall',
|
||||
() {
|
||||
final canonical = Hlc(_dateTime, 17, 'abc');
|
||||
final remote = Hlc(_dateTime.advance(const Duration(minutes: 2)), 17, 'abd');
|
||||
|
||||
test('when merging then ClockDriftException is thrown.', () {
|
||||
withClock(Clock.fixed(_dateTime.advance()), () {
|
||||
expect(
|
||||
() => canonical.merge(remote),
|
||||
throwsA(isA<ClockDriftException>()),
|
||||
);
|
||||
});
|
||||
});
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
extension on DateTime {
|
||||
DateTime advance([Duration duration = const Duration(milliseconds: 1)]) =>
|
||||
add(duration);
|
||||
|
||||
DateTime retreat([Duration duration = const Duration(milliseconds: 1)]) =>
|
||||
subtract(duration);
|
||||
}
|
||||
|
|
@ -0,0 +1,70 @@
|
|||
import 'package:drift_offline_sync/drift_offline_sync.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
const _isoTime = '2001-09-09T01:46:40.000Z';
|
||||
final _dateTime = DateTime.parse(_isoTime);
|
||||
|
||||
void main() {
|
||||
test('Given an HLC built with dateTime, counter and nodeId '
|
||||
'when reading properties '
|
||||
'then dateTime, counter and nodeId match.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(hlc.dateTime, _dateTime);
|
||||
expect(hlc.counter, 0x17);
|
||||
expect(hlc.nodeId, 'abc');
|
||||
});
|
||||
|
||||
test('Given Hlc.zero '
|
||||
'when building for a nodeId '
|
||||
'then it equals epoch with zero counter.', () {
|
||||
final zero = Hlc.zero('abc');
|
||||
|
||||
expect(zero.unixTimestamp, 0);
|
||||
expect(zero.counter, 0);
|
||||
expect(zero.nodeId, 'abc');
|
||||
});
|
||||
|
||||
test('Given an ISO HLC string '
|
||||
'when parsing '
|
||||
'then it returns the equivalent Hlc.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(Hlc.parse('${_dateTime.toIso8601String()}-0017-abc'), hlc);
|
||||
});
|
||||
|
||||
test('Given a Unix timestamp HLC string '
|
||||
'when parsing '
|
||||
'then it returns the equivalent Hlc.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(Hlc.parse('${_dateTime.millisecondsSinceEpoch}-0017-abc'), hlc);
|
||||
});
|
||||
|
||||
test('Given a parsed Hlc '
|
||||
'when calling toString '
|
||||
'then it returns the ISO HLC string.', () {
|
||||
final hlc = Hlc.parse('$_isoTime-0017-abc');
|
||||
|
||||
expect(hlc.toString(), '$_isoTime-0017-abc');
|
||||
});
|
||||
|
||||
test('Given an HLC '
|
||||
'when converting to JSON '
|
||||
'then it returns the equivalent string.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(hlc.toJson(), '${_dateTime.toIso8601String()}-0017-abc');
|
||||
});
|
||||
|
||||
test('Given an HLC '
|
||||
'when converting to JSON with unixTimestamp '
|
||||
'then it returns the equivalent string with padded unix timestamp.', () {
|
||||
final hlc = Hlc(_dateTime, 0x17, 'abc');
|
||||
|
||||
expect(
|
||||
hlc.toJson(unixTimestamp: true),
|
||||
'${_dateTime.millisecondsSinceEpoch.toString().padLeft(15, '0')}-0017-abc',
|
||||
);
|
||||
});
|
||||
}
|
||||
Loading…
Reference in a new issue