Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
simc committed Nov 13, 2019
1 parent ee92500 commit b59a6af
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 101 deletions.
16 changes: 10 additions & 6 deletions lib/src/box/keystore.dart
Expand Up @@ -101,7 +101,8 @@ class Keystore<E> {
return _notifier.watch(key: key);
}

Frame insert(Frame frame, [bool notify = true]) {
Frame insert(Frame frame, {bool notify = true}) {
var value = frame.value;
Frame deletedFrame;

if (!frame.deleted) {
Expand All @@ -110,8 +111,9 @@ class Keystore<E> {
_autoIncrement = key;
}

if (frame.value is HiveObject) {
initHiveObject(key, frame.value as HiveObject, _box);
if (value is HiveObject) {
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
value.init(key, _box);
}

deletedFrame = _store.insert(key, frame);
Expand All @@ -122,8 +124,9 @@ class Keystore<E> {
if (deletedFrame != null) {
_deletedEntries++;
if (deletedFrame.value is HiveObject &&
!identical(deletedFrame.value, frame.value)) {
unloadHiveObject(deletedFrame.value as HiveObject);
!identical(deletedFrame.value, value)) {
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
(deletedFrame.value as HiveObject).unload();
}
}

Expand Down Expand Up @@ -203,7 +206,8 @@ class Keystore<E> {

for (var frame in frameList) {
if (frame.value is HiveObject) {
unloadHiveObject(frame.value as HiveObject);
// ignore: invalid_use_of_protected_member, invalid_use_of_visible_for_testing_member
(frame.value as HiveObject).unload();
}
_notifier.notify(Frame.deleted(frame.key));
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/box/lazy_box_impl.dart
Expand Up @@ -3,7 +3,7 @@ import 'package:hive/src/backend/storage_backend.dart';
import 'package:hive/src/binary/frame.dart';
import 'package:hive/src/box/box_base.dart';
import 'package:hive/src/hive_impl.dart';
import 'package:hive/src/object/hive_object.dart';
import 'package:hive/src/hive_object.dart';

class LazyBoxImpl extends BoxBase implements LazyBox {
LazyBoxImpl(
Expand Down
50 changes: 28 additions & 22 deletions lib/src/hive_object.dart
@@ -1,4 +1,5 @@
import 'package:hive/hive.dart';
import 'package:meta/meta.dart';

/// Extend `HiveObject` to add useful methods to the objects you want to store
/// in Hive
Expand All @@ -15,21 +16,22 @@ abstract class HiveObject {
/// not been added to a box yet.
dynamic get key => _key;

/// Persists this object.
Future<void> save() {
void _requireInitialized() {
if (_box == null) {
throw HiveError('This object is currently not in a box.');
}
}

/// Persists this object.
Future<void> save() {
_requireInitialized();
return _box.put(_key, this);
}

/// Deletes this object from the box it is stored in.
Future<void> delete() {
if (_box != null) {
return _box.delete(_key);
} else {
throw HiveError('This object is currently not in a box.');
}
_requireInitialized();
return _box.delete(_key);
}

/// Returns whether this object is currently stored in a box.
Expand All @@ -46,23 +48,27 @@ abstract class HiveObject {
}
return false;
}
}

void initHiveObject(dynamic key, HiveObject object, Box box) {
if (object._box != null) {
if (object._box != box) {
throw HiveError('The same instance of an HiveObject cannot '
'be stored in two different boxes.');
} else if (object._key != key) {
throw HiveError('The same instance of an HiveObject cannot '
'be stored with two different keys.');
@protected
@visibleForTesting
void init(dynamic key, Box box) {
if (_box != null) {
if (_box != box) {
throw HiveError('The same instance of an HiveObject cannot '
'be stored in two different boxes.');
} else if (_key != key) {
throw HiveError('The same instance of an HiveObject cannot '
'be stored with two different keys.');
}
}
_box = box;
_key = key;
}
object._box = box;
object._key = key;
}

void unloadHiveObject(HiveObject object) {
object._box = null;
object._key = null;
@protected
@visibleForTesting
void unload() {
_box = null;
_key = null;
}
}
145 changes: 74 additions & 71 deletions test/hive_object_test.dart
Expand Up @@ -8,12 +8,12 @@ class _TestObject extends HiveObject {}

void main() {
group('HiveObject', () {
group('initHiveObject()', () {
group('.init()', () {
test('adds key and box to HiveObject', () {
var obj = _TestObject();
var box = BoxMock();

initHiveObject('someKey', obj, box);
obj.init('someKey', box);

expect(obj.key, 'someKey');
expect(obj.box, box);
Expand All @@ -23,8 +23,8 @@ void main() {
var obj = _TestObject();
var box = BoxMock();

initHiveObject('someKey', obj, box);
initHiveObject('someKey', obj, box);
obj.init('someKey', box);
obj.init('someKey', box);

expect(obj.key, 'someKey');
expect(obj.box, box);
Expand All @@ -35,93 +35,96 @@ void main() {
var box1 = BoxMock();
var box2 = BoxMock();

initHiveObject('someKey', obj, box1);
expect(() => initHiveObject('someKey', obj, box2),
obj.init('someKey', box1);
expect(() => obj.init('someKey', box2),
throwsHiveError('two different boxes'));
});

test('throws exception if object has already different key', () {
var obj = _TestObject();
var box = BoxMock();

initHiveObject('key1', obj, box);
expect(() => initHiveObject('key2', obj, box),
throwsHiveError('two different keys'));
obj.init('key1', box);
expect(
() => obj.init('key2', box), throwsHiveError('two different keys'));
});
});

test('unloadHiveObject removes key and box', () {
var obj = _TestObject();
var box = BoxMock();

initHiveObject('key', obj, box);
unloadHiveObject(obj);

expect(obj.key, null);
expect(obj.box, null);
});

group('.save()', () {
test('updates object in box', () {
var obj = _TestObject();
var box = BoxMock();
initHiveObject('key', obj, box);
verifyZeroInteractions(box);

obj.save();
verify(box.put('key', obj));
});

test('throws HiveError if object is not in a box', () async {
var obj = _TestObject();
await expectLater(() => obj.save(), throwsHiveError('not in a box'));
});
});

group('.delete()', () {
test('removes object from box', () {
group('.unload()', () {
test('removes key and box', () {
var obj = _TestObject();
var box = BoxMock();
initHiveObject('key', obj, box);
verifyZeroInteractions(box);

obj.delete();
verify(box.delete('key'));
});
obj.init('key', box);
obj.unload();

test('throws HiveError if object is not in a box', () async {
var obj = _TestObject();
await expectLater(() => obj.delete(), throwsHiveError('not in a box'));
expect(obj.key, null);
expect(obj.box, null);
});
});

group('.isInBox', () {
test('returns false if box is not set', () {
var obj = _TestObject();
expect(obj.isInBox, false);
group('.save()', () {
test('updates object in box', () {
var obj = _TestObject();
var box = BoxMock();
obj.init('key', box);
verifyZeroInteractions(box);

obj.save();
verify(box.put('key', obj));
});

test('throws HiveError if object is not in a box', () async {
var obj = _TestObject();
await expectLater(() => obj.save(), throwsHiveError('not in a box'));
});
});

test('returns true if object is in normal box', () {
var obj = _TestObject();
var box = BoxMock();
when(box.lazy).thenReturn(false);
initHiveObject('key', obj, box);

expect(obj.isInBox, true);
group('.delete()', () {
test('removes object from box', () {
var obj = _TestObject();
var box = BoxMock();
obj.init('key', box);
verifyZeroInteractions(box);

obj.delete();
verify(box.delete('key'));
});

test('throws HiveError if object is not in a box', () async {
var obj = _TestObject();
await expectLater(
() => obj.delete(), throwsHiveError('not in a box'));
});
});

test('returns the result ob box.containsKey() if object is in lazy box',
() {
var obj = _TestObject();
var box = BoxMock();
when(box.lazy).thenReturn(true);
initHiveObject('key', obj, box);

when(box.containsKey('key')).thenReturn(true);
expect(obj.isInBox, true);

when(box.containsKey('key')).thenReturn(false);
expect(obj.isInBox, false);
group('.isInBox', () {
test('returns false if box is not set', () {
var obj = _TestObject();
expect(obj.isInBox, false);
});

test('returns true if object is in normal box', () {
var obj = _TestObject();
var box = BoxMock();
when(box.lazy).thenReturn(false);
obj.init('key', box);

expect(obj.isInBox, true);
});

test('returns the result ob box.containsKey() if object is in lazy box',
() {
var obj = _TestObject();
var box = BoxMock();
when(box.lazy).thenReturn(true);
obj.init('key', box);

when(box.containsKey('key')).thenReturn(true);
expect(obj.isInBox, true);

when(box.containsKey('key')).thenReturn(false);
expect(obj.isInBox, false);
});
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/integration/hive_object_test.dart
@@ -1,6 +1,6 @@
import 'package:hive/hive.dart';
import 'package:hive/src/hive_impl.dart';
import 'package:hive/src/object/hive_object.dart';
import 'package:hive/src/hive_object.dart';
import 'package:test/test.dart';

import 'integration.dart';
Expand Down

0 comments on commit b59a6af

Please sign in to comment.