Hive adapter + testing

This commit is contained in:
andrei 2021-12-02 10:27:02 -05:00
parent 98942b231b
commit 5e532a132a
14 changed files with 484 additions and 14 deletions

0
checks/checks.hive Normal file
View File

0
checks/checks.lock Normal file
View File

BIN
checks/test.hive Normal file

Binary file not shown.

0
checks/test.lock Normal file
View File

View File

@ -1,13 +1,32 @@
import 'dart:core';
import 'dart:core' as core;
import 'package:hive/hive.dart';
part 'data_util.g.dart';
enum Page { lists, templates }
@HiveType(typeId: 1)
class List {
List(this.name, {this.id, this.description, this.isTemplate});
List(
this.name, {
this.id,
this.description,
this.isTemplate,
this.checks = const [],
});
@HiveField(0)
int? id;
@HiveField(1)
String name;
@HiveField(2)
String? description;
@HiveField(3)
bool? isTemplate;
@HiveField(4)
core.List<Check> checks;
Map<String, dynamic> toMap() {
var map = <String, dynamic>{};
if (id != null) map["id"] = id;
@ -27,12 +46,17 @@ class List {
}
}
@HiveType(typeId: 2)
class Check {
Check(this.text, this.value, {this.id, this.listID});
@HiveField(5)
int? id;
@HiveField(6)
String text;
@HiveField(7)
bool value;
@HiveField(8)
int? listID;
Map<String, dynamic> toMap() {

96
lib/data_util.g.dart Normal file
View File

@ -0,0 +1,96 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'data_util.dart';
// **************************************************************************
// TypeAdapterGenerator
// **************************************************************************
class ListAdapter extends TypeAdapter<List> {
@override
final int typeId = 1;
@override
List read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return List(
fields[1] as String,
id: fields[0] as int?,
description: fields[2] as String?,
isTemplate: fields[3] as bool?,
checks: (fields[4] as core.List).cast<Check>(),
);
}
@override
void write(BinaryWriter writer, List obj) {
writer
..writeByte(5)
..writeByte(0)
..write(obj.id)
..writeByte(1)
..write(obj.name)
..writeByte(2)
..write(obj.description)
..writeByte(3)
..write(obj.isTemplate)
..writeByte(4)
..write(obj.checks);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is ListAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}
class CheckAdapter extends TypeAdapter<Check> {
@override
final int typeId = 2;
@override
Check read(BinaryReader reader) {
final numOfFields = reader.readByte();
final fields = <int, dynamic>{
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
};
return Check(
fields[6] as String,
fields[7] as bool,
id: fields[5] as int?,
listID: fields[8] as int?,
);
}
@override
void write(BinaryWriter writer, Check obj) {
writer
..writeByte(4)
..writeByte(5)
..write(obj.id)
..writeByte(6)
..write(obj.text)
..writeByte(7)
..write(obj.value)
..writeByte(8)
..write(obj.listID);
}
@override
int get hashCode => typeId.hashCode;
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is CheckAdapter &&
runtimeType == other.runtimeType &&
typeId == other.typeId;
}

BIN
lists/lists.hive Normal file

Binary file not shown.

0
lists/lists.lock Normal file
View File

View File

@ -1,6 +1,27 @@
# Generated by pub
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
_fe_analyzer_shared:
dependency: transitive
description:
name: _fe_analyzer_shared
url: "https://pub.dartlang.org"
source: hosted
version: "31.0.0"
analyzer:
dependency: transitive
description:
name: analyzer
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.0"
args:
dependency: transitive
description:
name: args
url: "https://pub.dartlang.org"
source: hosted
version: "2.3.0"
async:
dependency: transitive
description:
@ -15,6 +36,62 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
build:
dependency: transitive
description:
name: build
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
build_config:
dependency: transitive
description:
name: build_config
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
build_daemon:
dependency: transitive
description:
name: build_daemon
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
build_resolvers:
dependency: transitive
description:
name: build_resolvers
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.5"
build_runner:
dependency: "direct dev"
description:
name: build_runner
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.5"
build_runner_core:
dependency: transitive
description:
name: build_runner_core
url: "https://pub.dartlang.org"
source: hosted
version: "7.2.2"
built_collection:
dependency: transitive
description:
name: built_collection
url: "https://pub.dartlang.org"
source: hosted
version: "5.1.1"
built_value:
dependency: transitive
description:
name: built_value
url: "https://pub.dartlang.org"
source: hosted
version: "8.1.3"
characters:
dependency: transitive
description:
@ -29,6 +106,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
checked_yaml:
dependency: transitive
description:
name: checked_yaml
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.1"
cli_util:
dependency: transitive
description:
name: cli_util
url: "https://pub.dartlang.org"
source: hosted
version: "0.3.5"
clock:
dependency: transitive
description:
@ -36,6 +127,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
code_builder:
dependency: transitive
description:
name: code_builder
url: "https://pub.dartlang.org"
source: hosted
version: "4.1.0"
collection:
dependency: transitive
description:
@ -43,6 +141,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
convert:
dependency: transitive
description:
name: convert
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
crypto:
dependency: transitive
description:
name: crypto
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
cupertino_icons:
dependency: "direct main"
description:
@ -50,6 +162,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
dart_style:
dependency: transitive
description:
name: dart_style
url: "https://pub.dartlang.org"
source: hosted
version: "2.2.0"
fake_async:
dependency: transitive
description:
@ -57,6 +176,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
file:
dependency: transitive
description:
name: file
url: "https://pub.dartlang.org"
source: hosted
version: "6.1.2"
fixnum:
dependency: transitive
description:
name: fixnum
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
flutter:
dependency: "direct main"
description: flutter
@ -74,6 +207,76 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
frontend_server_client:
dependency: transitive
description:
name: frontend_server_client
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.2"
glob:
dependency: transitive
description:
name: glob
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
graphs:
dependency: transitive
description:
name: graphs
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
hive:
dependency: "direct main"
description:
name: hive
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.4"
hive_generator:
dependency: "direct dev"
description:
name: hive_generator
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
http_multi_server:
dependency: transitive
description:
name: http_multi_server
url: "https://pub.dartlang.org"
source: hosted
version: "3.0.1"
http_parser:
dependency: transitive
description:
name: http_parser
url: "https://pub.dartlang.org"
source: hosted
version: "4.0.0"
io:
dependency: transitive
description:
name: io
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.3"
js:
dependency: transitive
description:
name: js
url: "https://pub.dartlang.org"
source: hosted
version: "0.6.3"
json_annotation:
dependency: transitive
description:
name: json_annotation
url: "https://pub.dartlang.org"
source: hosted
version: "4.4.0"
lints:
dependency: transitive
description:
@ -81,6 +284,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
logging:
dependency: transitive
description:
name: logging
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
matcher:
dependency: transitive
description:
@ -95,6 +305,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
mime:
dependency: transitive
description:
name: mime
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
package_config:
dependency: transitive
description:
name: package_config
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
path:
dependency: transitive
description:
@ -102,11 +326,60 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
pool:
dependency: transitive
description:
name: pool
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
pub_semver:
dependency: transitive
description:
name: pub_semver
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
pubspec_parse:
dependency: transitive
description:
name: pubspec_parse
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
shelf:
dependency: transitive
description:
name: shelf
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
shelf_web_socket:
dependency: transitive
description:
name: shelf_web_socket
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
sky_engine:
dependency: transitive
description: flutter
source: sdk
version: "0.0.99"
source_gen:
dependency: transitive
description:
name: source_gen
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
source_helper:
dependency: transitive
description:
name: source_helper
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
source_span:
dependency: transitive
description:
@ -142,6 +415,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
stream_transform:
dependency: transitive
description:
name: stream_transform
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.0"
string_scanner:
dependency: transitive
description:
@ -170,6 +450,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
timing:
dependency: transitive
description:
name: timing
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
typed_data:
dependency: transitive
description:
@ -184,6 +471,27 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
watcher:
dependency: transitive
description:
name: watcher
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.1"
web_socket_channel:
dependency: transitive
description:
name: web_socket_channel
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
yaml:
dependency: transitive
description:
name: yaml
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.0"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.14.0 <3.0.0"
flutter: ">=1.10.0"

View File

@ -30,6 +30,7 @@ dependencies:
flutter:
sdk: flutter
sqflite: ^2.0.0+4
hive: ^2.0.4
# The following adds the Cupertino Icons font to your application.
@ -39,7 +40,8 @@ dependencies:
dev_dependencies:
flutter_test:
sdk: flutter
hive_generator: ^1.1.1
build_runner:
# The "flutter_lints" package below contains a set of recommended lints to
# encourage good coding practices. The lint set provided by the package is
# activated in the `analysis_options.yaml` file located at the root of your
@ -47,6 +49,7 @@ dev_dependencies:
# rules and activating additional ones.
flutter_lints: ^1.0.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

45
test/box_test.dart Normal file
View File

@ -0,0 +1,45 @@
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:boxchecker/data_util.dart' as data;
import 'package:hive/hive.dart';
void main() {
Hive.init("lists");
Hive.registerAdapter(data.ListAdapter());
Hive.registerAdapter(data.CheckAdapter());
test('opening box', () async {
var box = await Hive.openBox<data.List>("lists");
expect(box, isNotNull);
box.close();
});
test('adding lists to box', () async {
var box = await Hive.openBox<data.List>("lists");
await box.clear();
expect(box.length, 0);
final data.List list = data.List("test_list");
box.add(list);
expect(box.length, 1);
var l = box.get(0);
expect(l, isNotNull);
box.close();
});
test('adding chekcs to lists', () async {
var box = await Hive.openBox<data.List>("lists");
var l = box.get(0);
expect(l, isNotNull);
expect(l!.checks, isEmpty);
//print(l.checks);
l.checks.add(data.Check("test Check", false));
expect(l.checks, isNotEmpty);
var l2 = box.get(0);
expect(l2, isNotNull);
expect(l2!.checks, isNotEmpty);
});
}

BIN
test/test.hive Normal file

Binary file not shown.

0
test/test.lock Normal file
View File

View File

@ -11,20 +11,14 @@ import 'package:flutter_test/flutter_test.dart';
import 'package:boxchecker/box_checker.dart';
void main() {
testWidgets('Counter increments smoke test', (WidgetTester tester) async {
testWidgets('swaping tabs', (WidgetTester tester) async {
// Build our app and trigger a frame.
await tester.pumpWidget(const MyApp());
await tester.pumpWidget(const BoxChecker());
// Verify that our counter starts at 0.
expect(find.text('0'), findsOneWidget);
expect(find.text('1'), findsNothing);
expect(find.text('test list'), findsOneWidget);
expect(find.text('test template'), findsNothing);
// Tap the '+' icon and trigger a frame.
await tester.tap(find.byIcon(Icons.add));
await tester.pump();
// Verify that our counter has incremented.
expect(find.text('0'), findsNothing);
expect(find.text('1'), findsOneWidget);
tester.tap(find.byIcon(Icons.list_alt));
});
}