Skip to content

Commit

Permalink
[feature] userCacheManager test completed
Browse files Browse the repository at this point in the history
  • Loading branch information
cihangirtuncer committed Jun 28, 2022
1 parent 0738e0c commit 1acf77c
Show file tree
Hide file tree
Showing 16 changed files with 68 additions and 20 deletions.
Binary file added database/database.hive
Binary file not shown.
Empty file added database/database.lock
Empty file.
Empty file added database/tesdatabase.hive
Empty file.
Empty file added database/tesdatabase.lock
Empty file.
Empty file added databaseTest/tesdatabase.hive
Empty file.
Empty file added databaseTest/tesdatabase.lock
Empty file.
Empty file.
Empty file.
6 changes: 3 additions & 3 deletions lib/core/manager/cache/user_cache_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class ICacheManager<T> {
await _box?.clear();
}

Future<void> addItems(List<T> items);
Future<void> addItems(List<T>? items);
Future<void> putItems(List<T> items);

T? getItem(String key);
Expand All @@ -33,8 +33,8 @@ class UserCacheManager extends ICacheManager<User> {
UserCacheManager(String key) : super(key);

@override
Future<void> addItems(List<User> items) async {
await _box?.addAll(items);
Future<void> addItems(List<User>? items) async {
await _box?.addAll(items ?? []);
}

@override
Expand Down
11 changes: 8 additions & 3 deletions lib/feature/search/search_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,28 @@ setState(() {
return Scaffold(
appBar: AppBar(
title: TextField(
autofocus: true,
style: const TextStyle(
color: Colors.white
),
cursorColor: Colors.white,
onChanged: (value){
findAndSet(value);
},
),
leading: InkWell(
child: Icon(CupertinoIcons.back),
child: const Icon(CupertinoIcons.back),
onTap: (){
Navigator.pop(context);
},
),
),
body: ListView.builder(
itemCount:_items.length ,
itemCount:1,
itemBuilder: (((context, index) {
return Card(
child: ListTile(
title: Text(_items.map((e) => '${e.name}').join('')),
title: Text(_items.map((e) => '${e.name}').join(",")),
),
);
}) ) ),
Expand Down
18 changes: 10 additions & 8 deletions lib/feature/user/view/user_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import 'package:provider/provider.dart';
import '../../search/search_view.dart';
import '../model/user_model.dart';

class UserViewWidget extends StatefulWidget {
const UserViewWidget({Key? key}) : super(key: key);
class UserView extends StatefulWidget {
const UserView({Key? key}) : super(key: key);

@override
State<UserViewWidget> createState() => _UserViewWidgetState();
State<UserView> createState() => _UserViewState();
}

class _UserViewWidgetState extends State<UserViewWidget> {
class _UserViewState extends State<UserView> {
late UserViewModel userViewModel;
late final ICacheManager<User> cacheManager;
List<User>? _items;
Expand All @@ -32,7 +32,7 @@ class _UserViewWidgetState extends State<UserViewWidget> {
Future<void> fetchDatasInit() async {
await cacheManager.init();
if (cacheManager.getValues()?.isNotEmpty ?? false) {
_items = cacheManager.getValues();
_items = cacheManager.getValues();
} else {
_items = userViewModel.userViewModelList;
}
Expand All @@ -41,8 +41,8 @@ class _UserViewWidgetState extends State<UserViewWidget> {

@override
Widget build(BuildContext context) {
return cacheManager.getValues()!.isNotEmpty
? bodyView(context)
return cacheManager.getValues()!.isNotEmpty
? bodyView(context)
: ChangeNotifierProvider.value(
value: userViewModel,
child: Consumer<UserViewModel>(
Expand All @@ -52,6 +52,7 @@ class _UserViewWidgetState extends State<UserViewWidget> {
}

Scaffold bodyView(BuildContext context) {

return Scaffold(
appBar: AppBar(
actions: [
Expand All @@ -78,8 +79,9 @@ class _UserViewWidgetState extends State<UserViewWidget> {
}
}),
body: ListView.builder(
itemCount: _items!.length,
itemCount: _items?.length,
itemBuilder: ((context, index) {

return Card(
child: ListTile(
leading: const CircleAvatar(
Expand Down
6 changes: 4 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void main() async {
NetworkManager.instance.init('https://jsonplaceholder.typicode.com', {});
WidgetsFlutterBinding.ensureInitialized();
await Hive.initFlutter();

runApp(MyApp());
}

Expand All @@ -21,9 +22,10 @@ class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return Provider(
create:(context) => UserContext(),
child: MaterialApp(
child:const MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Material App',
home: UserViewWidget()
home: UserView()
),
);
}
Expand Down
46 changes: 43 additions & 3 deletions test/cache/cache_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,52 @@
import 'package:flutter_clean_app_examples/core/manager/cache/user_cache_manager.dart';
import 'package:flutter_clean_app_examples/feature/user/model/user_model.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:hive_flutter/hive_flutter.dart';

void main() {
setUp(() {
Hive.init("database");
late UserCacheManager userCacheManager;
List<User>? _items;
List<User>? _itemsAllValues;

setUp(() async {
Hive.init('database');
userCacheManager = UserCacheManager("database");
await userCacheManager.init();
});
group("UserCacheManager testing", () {
test(("putItem and getItem method "), () async {
await userCacheManager.putItem("name", User(name: "cihangir"));
expect(userCacheManager.getItem("name")?.name.toString(), "cihangir");
});

test("getValues method", () {
_items = userCacheManager.getValues();
expect(_items?.first.name, "cihangir");
});

test("addItems method", () async {
await userCacheManager.putItem("name2", User(name: "emirhan"));
await userCacheManager.addItems(_items);
_itemsAllValues = userCacheManager.getValues();

expect(_itemsAllValues?.last.name, "emirhan");
});
test("removeItem method", () async {
await userCacheManager.removeItem("name2");
_itemsAllValues = userCacheManager.getValues();
print(_itemsAllValues?.last.name);
expect(_itemsAllValues?.last.name, "cihangir");

});


});
}
/*
group(("Hive main examples group"), () {
test("Add examples", () async {
final box = await Hive.openBox('examples');
box.add("cihangir");
Expand All @@ -29,4 +69,4 @@ void main() {
});
}
}*/
1 change: 0 additions & 1 deletion test/core/user_view_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ void main() {
final response = userViewModel.controlResponse(Response(
data:userList,
requestOptions: RequestOptions(path: "/hello")));
print(response?[0].name.toString());
expect(response?[0].name, "cihangir");

});
Expand Down
Empty file added testDatabase/tesdatabase.hive
Empty file.
Empty file added testDatabase/tesdatabase.lock
Empty file.

0 comments on commit 1acf77c

Please sign in to comment.