Skip to content

Commit

Permalink
Started unit testing the ProductController
Browse files Browse the repository at this point in the history
  • Loading branch information
haritha037 committed Oct 17, 2024
1 parent a75bcb9 commit 7951411
Show file tree
Hide file tree
Showing 11 changed files with 349 additions and 27 deletions.
37 changes: 30 additions & 7 deletions front-end/lib/controllers/product_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import 'package:image_picker/image_picker.dart';
class ProductController extends GetxController {
final ProductRepo productRepo;
final FileController fileController;
final LocationController locationController;

ProductController({required this.productRepo, required this.fileController});
ProductController(
{required this.locationController,
required this.productRepo,
required this.fileController});

List<Product> _allProductsList = [];
List<Product> get allProductsList => _allProductsList;
Expand All @@ -34,11 +38,18 @@ class ProductController extends GetxController {
bool _isLoading = true;
bool get isLoading => _isLoading;

String _error = '';
String get error => _error;

int _newProductId = 0;
int get newProductId => _newProductId;

// ALL PRODUCTS
Future<void> getAllProducts() async {
_isLoading = true;
_error = ''; // reset error
update();

Response response = await productRepo.getAllProducts();

if (response.statusCode == 200) {
Expand All @@ -47,18 +58,23 @@ class ProductController extends GetxController {

_allProductsList.addAll(Products.fromJson(response.body).products);
print(_allProductsList);
update();
} else {
print(_allProductsList);
print(response.statusCode);
print('Fetching all products failed');
_error =
'Failed to fetch all products. Status code: ${response.statusCode}';
}

_isLoading = false;
update();
}

// NEARBY PRODUCTS
Future<void> getNearbyProducts(double radius) async {
_isLoading = true;
Position myLocation = Get.find<LocationController>().myLocation;
_error = '';

Position myLocation = locationController.myLocation;

print('my location - $myLocation');
print('search radius in controller - $radius');
Expand All @@ -79,29 +95,36 @@ class ProductController extends GetxController {
} else {
print(_nearbyProductsList);
print('***************status code-${response.statusCode}');
print('Fetching all products failed');
print('Fetching nearby products failed');
_error =
'Failed to fetch nearby products. Status code: ${response.statusCode}';
}

_isLoading = false;
update();
}

// PRODUCT BY ID
Future<void> getProductById(int productId) async {
_error = '';
_isLoading = true;
update();

Response response = await productRepo.getProductById(productId);

if (response.statusCode == 200) {
print('got product with id $productId');
_productOnProductDetails = Product.fromJson(response.body);
print(_productOnProductDetails);
update();
} else {
print(_productOnProductDetails);
print(response.statusCode);
print('Fetching product with product id $productId failed');

_error = 'Fetching product with product id $productId failed';
}

_isLoading = false;
update();
}

// PRODUCTS BY CATEGORY
Expand Down
1 change: 1 addition & 0 deletions front-end/lib/helper/dependencies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Future<void> init() async {
() => ProductController(
productRepo: Get.find(),
fileController: Get.find(),
locationController: Get.find(),
),
);
Get.lazyPut(
Expand Down
2 changes: 1 addition & 1 deletion front-end/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:food_buddy_frontend/controllers/file_controller.dart';
import 'package:food_buddy_frontend/controllers/location_controller.dart';
import 'package:food_buddy_frontend/controllers/product_controller.dart';
import 'package:food_buddy_frontend/controllers/shop_controller.dart';
import 'package:food_buddy_frontend/pages/home_page.dart';
import 'package:food_buddy_frontend/pages/home%20page/home_page.dart';
import 'package:food_buddy_frontend/pages/product_details_page.dart';
import 'package:food_buddy_frontend/routes/route_helper.dart';
import 'package:get/get.dart';
Expand Down
2 changes: 1 addition & 1 deletion front-end/lib/pages/auth/sign_up_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:food_buddy_frontend/controllers/auth_controller.dart';
import 'package:food_buddy_frontend/models/api_response.dart';
import 'package:food_buddy_frontend/models/sign_up_body.dart';
import 'package:food_buddy_frontend/pages/auth/sign_in_page.dart';
import 'package:food_buddy_frontend/pages/home_page.dart';
import 'package:food_buddy_frontend/pages/home%20page/home_page.dart';
import 'package:food_buddy_frontend/routes/route_helper.dart';
import 'package:food_buddy_frontend/utils/colors.dart';
import 'package:food_buddy_frontend/utils/dimentions.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ class _HomePageState extends State<HomePage> {
child: LoadingIndicator(),
);
}
if (productController.error != '') {
return Container(
margin: EdgeInsets.only(
top: 200 * Dimentions.hUnit),
child: Text(productController.error),
);
}

_nearbyProducts =
productController.nearbyProductsList;
Expand Down
2 changes: 1 addition & 1 deletion front-end/lib/pages/nav_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';
import 'package:food_buddy_frontend/pages/account_page.dart';
import 'package:food_buddy_frontend/pages/auth/sign_in_page.dart';
import 'package:food_buddy_frontend/pages/auth/sign_up_page.dart';
import 'package:food_buddy_frontend/pages/home_page.dart';
import 'package:food_buddy_frontend/pages/home%20page/home_page.dart';
import 'package:food_buddy_frontend/pages/map_page.dart';
import 'package:food_buddy_frontend/pages/shop%20page/shop_on_nav.dart';
import 'package:food_buddy_frontend/pages/shop%20page/shop_sign_up_page.dart';
Expand Down
6 changes: 6 additions & 0 deletions front-end/lib/pages/product_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ class ProductDetailsPage extends StatelessWidget {
if (productController.isLoading) {
return const LoadingIndicator();
}

if (productController.error != '') {
return Center(
child: Text(productController.error),
);
}
var product = productController.productOnProductDetails;
print('**********************************product - $product');
return Stack(
Expand Down
2 changes: 1 addition & 1 deletion front-end/lib/routes/route_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:food_buddy_frontend/pages/auth/sign_in_page.dart';
import 'package:food_buddy_frontend/pages/auth/sign_up_page.dart';
import 'package:food_buddy_frontend/pages/splash_screen.dart';
import 'package:food_buddy_frontend/pages/category_products_page.dart';
import 'package:food_buddy_frontend/pages/home_page.dart';
import 'package:food_buddy_frontend/pages/home%20page/home_page.dart';
import 'package:food_buddy_frontend/pages/nav_page.dart';
import 'package:food_buddy_frontend/pages/product_details_page.dart';
import 'package:food_buddy_frontend/pages/shop%20page/shop_details_page.dart';
Expand Down
32 changes: 20 additions & 12 deletions front-end/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -492,18 +492,18 @@ packages:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
sha256: "3f87a60e8c63aecc975dda1ceedbc8f24de75f09e4856ea27daf8958f2f0ce05"
url: "https://pub.dev"
source: hosted
version: "10.0.4"
version: "10.0.5"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
sha256: "932549fb305594d82d7183ecd9fa93463e9914e1b67cacc34bc40906594a1806"
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.0.5"
leak_tracker_testing:
dependency: transitive
description:
Expand Down Expand Up @@ -548,18 +548,18 @@ packages:
dependency: transitive
description:
name: material_color_utilities
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
sha256: f7142bb1154231d7ea5f96bc7bde4bda2a0945d2806bb11670e30b850d56bdec
url: "https://pub.dev"
source: hosted
version: "0.8.0"
version: "0.11.1"
meta:
dependency: transitive
description:
name: meta
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
sha256: bdb68674043280c3428e9ec998512fb681678676b3c54e773629ffe74419f8c7
url: "https://pub.dev"
source: hosted
version: "1.12.0"
version: "1.15.0"
mgrs_dart:
dependency: transitive
description:
Expand All @@ -576,6 +576,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.5"
mocktail:
dependency: "direct dev"
description:
name: mocktail
sha256: "890df3f9688106f25755f26b1c60589a92b3ab91a22b8b224947ad041bf172d8"
url: "https://pub.dev"
source: hosted
version: "1.0.4"
path:
dependency: transitive
description:
Expand Down Expand Up @@ -833,10 +841,10 @@ packages:
dependency: transitive
description:
name: test_api
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
sha256: "5b8a98dafc4d5c4c9c72d8b31ab2b23fc13422348d2997120294d3bac86b4ddb"
url: "https://pub.dev"
source: hosted
version: "0.7.0"
version: "0.7.2"
typed_data:
dependency: transitive
description:
Expand Down Expand Up @@ -961,10 +969,10 @@ packages:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
sha256: "5c5f338a667b4c644744b661f309fb8080bb94b18a7e91ef1dbd343bed00ed6d"
url: "https://pub.dev"
source: hosted
version: "14.2.1"
version: "14.2.5"
web:
dependency: transitive
description:
Expand Down
7 changes: 3 additions & 4 deletions front-end/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: food_buddy_frontend
description: "A new Flutter project."
# The following line prevents the package from being accidentally published to
# pub.dev using `flutter pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
publish_to: "none" # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
Expand All @@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=3.4.1 <4.0.0'
sdk: ">=3.4.1 <4.0.0"

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand All @@ -31,7 +31,6 @@ dependencies:
flutter:
sdk: flutter


# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.6
Expand All @@ -54,6 +53,7 @@ dev_dependencies:
flutter_test:
sdk: flutter

mocktail: ^1.0.4
# 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
Expand All @@ -66,7 +66,6 @@ dev_dependencies:

# The following section is specific to Flutter packages.
flutter:

# The following line ensures that the Material Icons font is
# included with your application, so that you can use the icons in
# the material Icons class.
Expand Down
Loading

0 comments on commit 7951411

Please sign in to comment.