From 072da425b723d84df859ecfad92b2e15ca74c858 Mon Sep 17 00:00:00 2001 From: jaimeyfrank Date: Tue, 17 Dec 2024 23:00:47 -0500 Subject: [PATCH] reworked old tests and added new --- bu_passport/lib/classes/event.dart | 1 - bu_passport/lib/classes/user.dart | 39 +++++++++ bu_passport/lib/pages/admin/edit_event.dart | 0 bu_passport/lib/pages/admin/view_events.dart | 87 ------------------- bu_passport/pubspec.yaml | 3 +- bu_passport/test/event_page_test.dart | 4 +- bu_passport/test/event_widget_test.dart | 26 +++--- bu_passport/test/event_widget_test.mocks.dart | 7 +- bu_passport/test/explore_page_test.dart | 4 +- bu_passport/test/firebase_service_test.dart | 1 - bu_passport/test/leaderboard_page_test.dart | 53 ++++++----- bu_passport/test/login_page_test.dart | 18 +--- bu_passport/test/mock_services.dart | 23 +++++ bu_passport/test/passport_page_test.dart | 50 +++++++++++ bu_passport/test/profile_page_test.dart | 24 ++--- bu_passport/test/signup_page_test.dart | 4 +- 16 files changed, 179 insertions(+), 165 deletions(-) delete mode 100644 bu_passport/lib/pages/admin/edit_event.dart delete mode 100644 bu_passport/lib/pages/admin/view_events.dart create mode 100644 bu_passport/test/mock_services.dart create mode 100644 bu_passport/test/passport_page_test.dart diff --git a/bu_passport/lib/classes/event.dart b/bu_passport/lib/classes/event.dart index dfa7edd..4e606c1 100644 --- a/bu_passport/lib/classes/event.dart +++ b/bu_passport/lib/classes/event.dart @@ -1,6 +1,5 @@ import 'package:bu_passport/classes/session.dart'; import 'package:bu_passport/classes/sticker.dart'; -import 'package:cloud_firestore/cloud_firestore.dart'; class Event { final String eventID; diff --git a/bu_passport/lib/classes/user.dart b/bu_passport/lib/classes/user.dart index aa934d8..456483e 100644 --- a/bu_passport/lib/classes/user.dart +++ b/bu_passport/lib/classes/user.dart @@ -30,4 +30,43 @@ class Users { required this.userPhotos, required this.userCreatedAt, }); + +// Method to convert Users object to a map for Firestore + Map toMap() { + return { + 'firstName': firstName, + 'lastName': lastName, + 'userBUID': userBUID, + 'userEmail': userEmail, + 'userSchool': userSchool, + 'userUID': userUID, + 'userYear': userYear, + 'userPoints': userPoints, + 'userProfileURL': userProfileURL, + 'userSavedEvents': userSavedEvents, + 'userCollectedStickers': userCollectedStickers, + 'userPhotos': userPhotos, + 'userCreatedAt': userCreatedAt, + }; + } + + // Method to create Users object from a map + factory Users.fromMap(Map map) { + return Users( + firstName: map['firstName'], + lastName: map['lastName'], + userBUID: map['userBUID'], + userEmail: map['userEmail'], + userSchool: map['userSchool'], + userUID: map['userUID'], + userYear: map['userYear'], + userPoints: map['userPoints'], + userProfileURL: map['userProfileURL'], + userSavedEvents: Map.from(map['userSavedEvents']), + userCollectedStickers: Map.from(map['userCollectedStickers']), + userPhotos: List.from(map['userPhotos']), + userCreatedAt: map['userCreatedAt'], + ); + } + } diff --git a/bu_passport/lib/pages/admin/edit_event.dart b/bu_passport/lib/pages/admin/edit_event.dart deleted file mode 100644 index e69de29..0000000 diff --git a/bu_passport/lib/pages/admin/view_events.dart b/bu_passport/lib/pages/admin/view_events.dart deleted file mode 100644 index 5d29b07..0000000 --- a/bu_passport/lib/pages/admin/view_events.dart +++ /dev/null @@ -1,87 +0,0 @@ -import 'package:flutter/material.dart'; -import 'package:intl/intl.dart'; -import 'package:cloud_firestore/cloud_firestore.dart'; -import 'package:bu_passport/classes/event.dart'; -import 'package:bu_passport/services/firebase_service.dart'; - -class UpdateEventInfoPage extends StatefulWidget { - const UpdateEventInfoPage({Key? key}) : super(key: key); - - @override - _UpdateEventInfoPageState createState() => _UpdateEventInfoPageState(); -} - -class _UpdateEventInfoPageState extends State { - final db = FirebaseFirestore.instance; - FirebaseService firebaseService = FirebaseService(db: FirebaseFirestore.instance); - late Future> fetchEventsFuture; - - @override - void initState() { - super.initState(); - fetchEventsFuture = firebaseService.fetchEventsFromNow(); - } - - // Function to navigate to the edit event page or open a dialog - void navigateToEditEvent(Event event) { - // You can add code here to open a dialog or navigate to a detailed editing page - // For now, we'll use a placeholder action - showDialog( - context: context, - builder: (context) => AlertDialog( - title: Text("Edit Event"), - content: Text("Edit options for ${event.eventTitle}"), - actions: [ - TextButton( - onPressed: () => Navigator.pop(context), - child: Text("Close"), - ), - ], - ), - ); - } - - @override - Widget build(BuildContext context) { - return Scaffold( - appBar: AppBar( - title: Text("Update Event Info"), - ), - body: FutureBuilder>( - future: fetchEventsFuture, - builder: (context, snapshot) { - if (snapshot.connectionState == ConnectionState.waiting) { - return Center(child: CircularProgressIndicator()); - } else if (snapshot.hasError) { - return Center(child: Text('Error: ${snapshot.error}')); - } else if (!snapshot.hasData || snapshot.data!.isEmpty) { - return Center(child: Text("No events found.")); - } else { - List events = snapshot.data!; - return ListView.builder( - itemCount: events.length, - itemBuilder: (context, index) { - Event event = events[index]; - String formattedDate = DateFormat('yyyy/MM/dd HH:mm').format(event.eventStartTime); - return ListTile( - title: Text(event.eventTitle), - subtitle: Column( - crossAxisAlignment: CrossAxisAlignment.start, - children: [ - Text("Date: ${formattedDate}"), // Display event date - Text("Points: ${event.eventPoints}"), - ], - ), - trailing: ElevatedButton( - onPressed: () => navigateToEditEvent(event), - child: Text("Edit"), - ), - ); - }, - ); - } - }, - ), - ); - } -} diff --git a/bu_passport/pubspec.yaml b/bu_passport/pubspec.yaml index d18a549..bc0cf6c 100644 --- a/bu_passport/pubspec.yaml +++ b/bu_passport/pubspec.yaml @@ -84,8 +84,7 @@ flutter: assets: - assets/images/onboarding/ - assets/images/arts/ - - assets/images/leaderboard/ - - assets/images/passport/ + - assets/images/leaderboard/ - assets/images/stickers/ # An image asset can refer to one or more resolution-specific "variants", see diff --git a/bu_passport/test/event_page_test.dart b/bu_passport/test/event_page_test.dart index 5cf2a0b..b17589d 100644 --- a/bu_passport/test/event_page_test.dart +++ b/bu_passport/test/event_page_test.dart @@ -29,12 +29,12 @@ void main() { eventTitle: 'Test Event', eventPhoto: "assets/images/arts/image9.jpeg", eventLocation: 'Test Location', - eventStartTime: DateTime(2024, 4, 18, 10, 0), - eventEndTime: DateTime(2024, 4, 18, 12, 0), eventDescription: 'Test Description', eventPoints: 30, eventURL: 'http://example.com', savedUsers: ['user1', 'user2'], + eventSessions: [], + eventStickers: [], ); // Build our widget and trigger a frame. diff --git a/bu_passport/test/event_widget_test.dart b/bu_passport/test/event_widget_test.dart index c3a4c33..9099ce8 100644 --- a/bu_passport/test/event_widget_test.dart +++ b/bu_passport/test/event_widget_test.dart @@ -11,18 +11,18 @@ import 'package:mockito/mockito.dart'; import 'event_widget_test.mocks.dart'; import 'mock.dart'; -final event = Event( - eventID: '1', - eventTitle: 'Test Event', - eventPhoto: "assets/images/arts/image9.jpeg", - eventLocation: 'Test Location', - eventStartTime: DateTime(2024, 4, 18, 10, 0), // April 18, 2024, 10:00 AM - eventEndTime: DateTime(2024, 4, 18, 12, 0), // April 18, 2024, 12:00 PM - eventDescription: 'Test Description', - eventPoints: 30, - eventURL: 'http://example.com', - savedUsers: ['user1', 'user2'], -); +final testEvent = Event( + eventID: '1', + eventTitle: 'Test Event', + eventPhoto: "assets/images/arts/image9.jpeg", + eventLocation: 'Test Location', + eventDescription: 'Test Description', + eventPoints: 30, + eventURL: 'http://example.com', + savedUsers: ['user1', 'user2'], + eventSessions: [], + eventStickers: [], + ); @GenerateMocks([FirebaseService]) void main() async { @@ -44,7 +44,7 @@ void main() async { // Wrap EventWidget in MaterialApp or Directionality await tester.pumpWidget( MaterialApp( - home: EventWidget(event: event, onUpdateEventPage: () {}), + home: EventWidget(event: testEvent, onUpdateEventPage: () {}), ), ); diff --git a/bu_passport/test/event_widget_test.mocks.dart b/bu_passport/test/event_widget_test.mocks.dart index 0198e9b..fde8b49 100644 --- a/bu_passport/test/event_widget_test.mocks.dart +++ b/bu_passport/test/event_widget_test.mocks.dart @@ -8,6 +8,7 @@ import 'dart:async' as _i5; import 'package:bu_passport/classes/categorized_events.dart' as _i3; import 'package:bu_passport/classes/event.dart' as _i6; import 'package:bu_passport/classes/user.dart' as _i7; +import 'package:bu_passport/classes/sticker.dart'; import 'package:bu_passport/services/firebase_service.dart' as _i4; import 'package:cloud_firestore/cloud_firestore.dart' as _i2; import 'package:mockito/mockito.dart' as _i1; @@ -177,16 +178,14 @@ class MockFirebaseService extends _i1.Mock implements _i4.FirebaseService { ) as _i5.Future<_i6.Event?>); @override - void checkInUserForEvent( - String? eventID, - int? eventPoints, - ) => + void checkInUserForEvent(String eventID, int eventPoints, List stickers) => super.noSuchMethod( Invocation.method( #checkInUserForEvent, [ eventID, eventPoints, + stickers, ], ), returnValueForMissingStub: null, diff --git a/bu_passport/test/explore_page_test.dart b/bu_passport/test/explore_page_test.dart index 72ed267..2ca4257 100644 --- a/bu_passport/test/explore_page_test.dart +++ b/bu_passport/test/explore_page_test.dart @@ -15,12 +15,12 @@ void main() { eventTitle: 'Test Event 2', eventPhoto: "assets/images/arts/image9.jpeg", eventLocation: 'Test Location 2', - eventStartTime: DateTime(2024, 4, 18, 10, 0), - eventEndTime: DateTime(2024, 4, 18, 12, 0), eventDescription: 'Test Description 2', eventPoints: 30, eventURL: 'http://example.com', savedUsers: ['user1', 'user2'], + eventSessions: [], + eventStickers: [], ); MockFirebaseService mockFirebaseService = MockFirebaseService(); diff --git a/bu_passport/test/firebase_service_test.dart b/bu_passport/test/firebase_service_test.dart index 7815998..26a8419 100644 --- a/bu_passport/test/firebase_service_test.dart +++ b/bu_passport/test/firebase_service_test.dart @@ -138,7 +138,6 @@ void main() { expect(filteredEvents.length, 1); // Should only contain one event expect(filteredEvents[0].eventID, "event1"); expect(filteredEvents[0].eventTitle, "Event on Target Date"); - expect(filteredEvents[0].eventStartTime, DateTime(2024, 4, 29, 10, 0)); }); test( diff --git a/bu_passport/test/leaderboard_page_test.dart b/bu_passport/test/leaderboard_page_test.dart index 4ec0155..72cc154 100644 --- a/bu_passport/test/leaderboard_page_test.dart +++ b/bu_passport/test/leaderboard_page_test.dart @@ -1,5 +1,6 @@ import 'package:bu_passport/classes/user.dart'; import 'package:bu_passport/pages/leaderboard_page.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; @@ -11,31 +12,35 @@ import 'mock.dart'; void main() { final List mockUsers = [ Users( - firstName: 'John', - lastName: 'Doe', - userBUID: '123456', - userEmail: 'john.doe@example.com', - userSchool: 'Example School', - userUID: 'user1', - userYear: 3, - userSavedEvents: {}, - userPoints: 100, - userProfileURL: '', - admin: false, - ), + firstName: 'John', + lastName: 'Doe', + userBUID: '123456', + userEmail: 'john.doe@example.com', + userSchool: 'Example School', + userUID: 'user1', + userYear: 3, + userSavedEvents: {}, + userPoints: 100, + userProfileURL: '', + userCollectedStickers: {}, + userPhotos: [], + userCreatedAt: Timestamp.now(), + ), Users( - firstName: 'Jane', - lastName: 'Smith', - userBUID: '654321', - userEmail: 'jane.smith@example.com', - userSchool: 'Another School', - userUID: 'user2', - userYear: 2, - userSavedEvents: {}, - userPoints: 200, - userProfileURL: '', - admin: false, - ), + firstName: 'Jane', + lastName: 'Smith', + userBUID: '654321', + userEmail: 'jane.smith@example.com', + userSchool: 'Another School', + userUID: 'user2', + userYear: 2, + userSavedEvents: {}, + userPoints: 150, + userProfileURL: '', + userCollectedStickers: {}, + userPhotos: [], + userCreatedAt: Timestamp.now(), + ), ]; MockFirebaseService mockFirebaseService = MockFirebaseService(); diff --git a/bu_passport/test/login_page_test.dart b/bu_passport/test/login_page_test.dart index 5797665..6d3ddee 100644 --- a/bu_passport/test/login_page_test.dart +++ b/bu_passport/test/login_page_test.dart @@ -6,13 +6,14 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:bu_passport/pages/explore_page.dart'; import 'package:bu_passport/classes/event.dart'; import 'package:mockito/mockito.dart'; - import 'event_widget_test.mocks.dart'; import 'mock.dart'; +// Not set up to verify the sign in process, just the state of the page + void main() { MockFirebaseService mockFirebaseService = MockFirebaseService(); - + setupFirebaseAuthMocks(); setUpAll(() async { @@ -23,19 +24,8 @@ void main() { await tester.pumpWidget(MaterialApp( home: LoginPage(), )); - expect(find.text('Email'), findsOneWidget); - expect(find.text('Password'), findsOneWidget); - expect(find.text('Sign In'), findsOneWidget); - expect(find.text("Don't have an account?"), findsOneWidget); - expect(find.text('Sign Up'), findsOneWidget); - - // Enter test email and password - await tester.enterText(find.byType(TextField).at(0), 'test@example.com'); - await tester.enterText(find.byType(TextField).at(1), 'password'); - // Tap the sign in button - await tester.tap(find.text('Sign In')); + await tester.tap(find.text('Sign In with BU Gmail')); await tester.pump(); - }); } diff --git a/bu_passport/test/mock_services.dart b/bu_passport/test/mock_services.dart new file mode 100644 index 0000000..b7d5fe9 --- /dev/null +++ b/bu_passport/test/mock_services.dart @@ -0,0 +1,23 @@ +import 'package:mockito/mockito.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; + +class MockFirestore extends Mock implements FirebaseFirestore {} + +class MockFirebaseAuth extends Mock implements FirebaseAuth {} + +class MockUser extends Mock implements User {} + +class MockDocumentSnapshot { + final Map mockData; + + MockDocumentSnapshot(this.mockData); + + @override + Map? data() { + return mockData; + } + + @override + bool get exists => data()?.isNotEmpty ?? false; +} \ No newline at end of file diff --git a/bu_passport/test/passport_page_test.dart b/bu_passport/test/passport_page_test.dart new file mode 100644 index 0000000..e76e66f --- /dev/null +++ b/bu_passport/test/passport_page_test.dart @@ -0,0 +1,50 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:mockito/mockito.dart'; +import 'package:provider/provider.dart'; +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; +import 'package:bu_passport/pages/passport_page.dart'; +import 'package:bu_passport/components/sticker_widget.dart'; +import 'mock_services.dart'; + +void main() { + testWidgets('PassportPage displays user stickers', (WidgetTester tester) async { + // Create mock services + final mockFirestore = MockFirestore(); + final mockFirebaseAuth = MockFirebaseAuth(); + final mockUser = MockUser(); + + // Mock Firestore data + final mockDocumentSnapshot = MockDocumentSnapshot({ + 'userCollectedStickers': [1, 2, 3], + }); + when(mockFirestore.collection('users').doc(any).get()).thenAnswer((_) async => mockDocumentSnapshot as DocumentSnapshot>); + when(mockDocumentSnapshot.exists).thenReturn(true); + when(mockDocumentSnapshot.data()).thenReturn({ + 'userCollectedStickers': [1, 2, 3], + }); + + // Mock FirebaseAuth data + when(mockFirebaseAuth.currentUser).thenReturn(mockUser); + when(mockUser.uid).thenReturn('testUserId'); + + // Build our widget and trigger a frame + await tester.pumpWidget( + MaterialApp( + home: PassportPage( + ), + ), + ); + + // Verify the loading indicator is displayed + expect(find.byType(CircularProgressIndicator), findsOneWidget); + + // Wait for the async operations to complete + await tester.pumpAndSettle(); + + // Verify the stickers are displayed + expect(find.byType(StickerWidget), findsOneWidget); + expect(find.text('Stickers'), findsOneWidget); + }); +} \ No newline at end of file diff --git a/bu_passport/test/profile_page_test.dart b/bu_passport/test/profile_page_test.dart index 46e1c7b..cde87ca 100644 --- a/bu_passport/test/profile_page_test.dart +++ b/bu_passport/test/profile_page_test.dart @@ -14,18 +14,18 @@ void main() { setUpAll(() async { await Firebase.initializeApp(); }); - // testWidgets('ProfilePage test', (WidgetTester tester) async { - // // Build our widget and trigger a frame. - // await tester.pumpWidget(MaterialApp( - // home: ProfilePage(), - // )); + testWidgets('ProfilePage test', (WidgetTester tester) async { + // Build our widget and trigger a frame. + await tester.pumpWidget(MaterialApp( + home: ProfilePage(), + )); - // // // Wait for the widgets to load. - // // await tester.pumpAndSettle(); - // // // Verify that the Saved tab is displayed. - // // expect(find.text('Saved'), findsOneWidget); + // Wait for the widgets to load. + await tester.pumpAndSettle(); + // Verify that the Saved tab is displayed. + expect(find.text('Saved'), findsOneWidget); - // // // Verify that the Attended tab is displayed. - // // expect(find.text('Attended'), findsOneWidget); - // }); + // Verify that the Attended tab is displayed. + expect(find.text('Attended'), findsOneWidget); + }); } diff --git a/bu_passport/test/signup_page_test.dart b/bu_passport/test/signup_page_test.dart index f3dcb0c..677b7cd 100644 --- a/bu_passport/test/signup_page_test.dart +++ b/bu_passport/test/signup_page_test.dart @@ -21,11 +21,9 @@ void main() { await tester.pumpWidget(MaterialApp( home: SignUpPage(), )); - expect(find.byType(TextField), findsNWidgets(7)); + expect(find.byType(TextField), findsNWidgets(5)); expect(find.text('First Name'), findsOneWidget); expect(find.text('Last Name'), findsOneWidget); - expect(find.text('Email'), findsOneWidget); - expect(find.text('Password'), findsOneWidget); expect(find.text('BU ID'), findsOneWidget); expect(find.text('School'), findsOneWidget); expect(find.text('Year'), findsOneWidget);