-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Firebase (Booking/Operator) with Fixtures
- Loading branch information
Showing
11 changed files
with
527 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'package:flutter_test/flutter_test.dart'; | ||
|
||
import 'package:integration_test/integration_test.dart'; | ||
|
||
import 'package:mera_aadhar/main.dart' as app; | ||
import 'package:firebase_core/firebase_core.dart'; | ||
|
||
import 'package:mera_aadhar/fixtures/booking_fixture.dart'; | ||
import 'package:mera_aadhar/models/booking_model.dart'; | ||
import 'package:mera_aadhar/firebase/booking_db.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
|
||
Future<void> addDelay(int ms) async { | ||
await Future<void>.delayed(Duration(milliseconds: ms)); | ||
} | ||
|
||
|
||
void main() { | ||
|
||
IntegrationTestWidgetsFlutterBinding.ensureInitialized(); | ||
|
||
testWidgets('Authentication Testing', (WidgetTester tester) async { | ||
await Firebase.initializeApp(); | ||
|
||
debugPrint("init nwo dummy"); | ||
Booking dummy = BookingFixture.dummyBooking(); | ||
|
||
BookingDB bdb = new BookingDB(); | ||
await bdb.addNewBooking(dummy); | ||
debugPrint("Successfully added booking!"); | ||
}); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:mera_aadhar/models/booking_model.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
|
||
class BookingDB { | ||
|
||
CollectionReference _collectionRef = | ||
FirebaseFirestore.instance.collection('bookings'); | ||
|
||
Future<void> addNewBooking(Booking booking) { | ||
return _collectionRef | ||
.add(booking.toJson()) | ||
.then((value) => print("Booking entry added")) | ||
.catchError((error) => print("Failed to add entry: $error")); | ||
} | ||
|
||
Future<Booking?> getMyCurrentBooking() async { | ||
FirebaseAuth auth = FirebaseAuth.instance; | ||
|
||
print("my ph "+(auth.currentUser?.phoneNumber)!); | ||
|
||
QuerySnapshot snap = await _collectionRef | ||
.where('phoneNo', isEqualTo: auth.currentUser?.phoneNumber) | ||
.orderBy('timestamp', descending: true) | ||
.limit(1) | ||
.get(); | ||
|
||
if(snap.size != 0){ | ||
Booking b = Booking.fromJson(snap.docs[0].data() as Map<String, dynamic>); | ||
if(b.bookingStatus! != "Completed") return b; | ||
else return null; | ||
} | ||
else return null; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'package:cloud_firestore/cloud_firestore.dart'; | ||
import 'package:mera_aadhar/models/operator_model.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
|
||
class OperatorDB { | ||
|
||
CollectionReference _collectionRef = | ||
FirebaseFirestore.instance.collection('operators'); | ||
|
||
Future<void> addNewOperator(Operator booking) { | ||
// TODO: UNSAFE/TESTING ONLY | ||
return _collectionRef | ||
.add(booking.toJson()) | ||
.then((value) => print("Operator entry added")) | ||
.catchError((error) => print("Failed to add entry: $error")); | ||
} | ||
|
||
Future<Operator?> getOperatorById(int operatorId) async { | ||
QuerySnapshot snap = await _collectionRef | ||
.where('operatorId', isEqualTo: operatorId) | ||
.limit(1) | ||
.get(); | ||
|
||
if(snap.size != 0){ | ||
Operator b = Operator.fromJson(snap.docs[0].data() as Map<String, dynamic>); | ||
if(b.isOperatorActive! != false) return b; | ||
else return null; | ||
} | ||
else return null; | ||
} | ||
|
||
Future<List<Operator>> getOperatorsByLatLong(String latlong) async { | ||
QuerySnapshot snap = await _collectionRef | ||
.where('centerLocation', isEqualTo: latlong) | ||
.get(); | ||
|
||
List<Operator> operators = []; | ||
|
||
snap.docs.forEach((doc){ | ||
Operator op = Operator.fromJson(doc.data() as Map<String, dynamic>); | ||
if(op.isOperatorActive!){ | ||
operators.add(op); | ||
} | ||
}); | ||
|
||
return operators; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import 'package:mera_aadhar/models/booking_model.dart'; | ||
import 'dart:convert'; | ||
|
||
class BookingFixture { | ||
static Booking dummyBooking(){ | ||
Map<String, dynamic> data = jsonDecode('{"bookingId":42,"operatorId":22451,"bookingType":0,"phoneNo":"+918872276957","bookingLocation":{"lat":23.25564,"lon":21.22134},"userdata":{"phoneNo":"8872276957","type":0,"locationText":"Hostel O, Tiet"},"paydata":{"type":"CASH","receipt":null,"status":"Success"},"bookingStatus":"Completed","timestamp":1660465314}'); | ||
final Booking bookin = Booking.fromJson(data); | ||
return bookin; | ||
} | ||
|
||
Future<Booking?> fixture_booking_getMyCurrentBooking() async { | ||
return dummyBooking(); | ||
} | ||
|
||
Future<Booking?> fixture_nobooking_getMyCurrentBooking() async { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import 'package:mera_aadhar/models/operator_model.dart'; | ||
import 'dart:convert'; | ||
|
||
class OperatorFixture { | ||
static Operator dummySurabhi(){ | ||
Map<String, dynamic> data = jsonDecode('{"operatorId":42254,"centerLocation":"31.66525645-23.2554126","name":"Surabhi Misra","picture":"","age":18,"gender":"Female","phoneNo":"+91123456789","email":"[email protected]","ratings":"4.5","reviews":["Good work","She is very hardworking operator","quickly resolved my services"],"isOperatorActive":true,"timestamp":1660465314}'); | ||
final Operator surabhi = Operator.fromJson(data); | ||
return surabhi; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
class Booking { | ||
int? bookingId; | ||
int? operatorId; | ||
int? bookingType; | ||
String? phoneNo; | ||
BookingLocation? bookingLocation; | ||
Userdata? userdata; | ||
Paydata? paydata; | ||
String? bookingStatus; | ||
int? timestamp; | ||
|
||
Booking( | ||
{this.bookingId, | ||
this.operatorId, | ||
this.bookingType, | ||
this.phoneNo, | ||
this.bookingLocation, | ||
this.userdata, | ||
this.paydata, | ||
this.bookingStatus, | ||
this.timestamp}); | ||
|
||
Booking.fromJson(Map<String, dynamic> json) { | ||
bookingId = json['bookingId']; | ||
operatorId = json['operatorId']; | ||
bookingType = json['bookingType']; | ||
phoneNo = json['phoneNo']; | ||
bookingLocation = json['bookingLocation'] != null | ||
? new BookingLocation.fromJson(json['bookingLocation']) | ||
: null; | ||
userdata = json['userdata'] != null | ||
? new Userdata.fromJson(json['userdata']) | ||
: null; | ||
paydata = | ||
json['paydata'] != null ? new Paydata.fromJson(json['paydata']) : null; | ||
bookingStatus = json['bookingStatus']; | ||
timestamp = json['timestamp']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['bookingId'] = this.bookingId; | ||
data['operatorId'] = this.operatorId; | ||
data['bookingType'] = this.bookingType; | ||
data['phoneNo'] = this.phoneNo; | ||
if (this.bookingLocation != null) { | ||
data['bookingLocation'] = this.bookingLocation!.toJson(); | ||
} | ||
if (this.userdata != null) { | ||
data['userdata'] = this.userdata!.toJson(); | ||
} | ||
if (this.paydata != null) { | ||
data['paydata'] = this.paydata!.toJson(); | ||
} | ||
data['bookingStatus'] = this.bookingStatus; | ||
data['timestamp'] = this.timestamp; | ||
return data; | ||
} | ||
} | ||
|
||
class BookingLocation { | ||
double? lat; | ||
double? lon; | ||
|
||
BookingLocation({this.lat, this.lon}); | ||
|
||
BookingLocation.fromJson(Map<String, dynamic> json) { | ||
lat = json['lat']; | ||
lon = json['lon']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['lat'] = this.lat; | ||
data['lon'] = this.lon; | ||
return data; | ||
} | ||
} | ||
|
||
class Userdata { | ||
String? phoneNo; | ||
int? type; | ||
String? locationText; | ||
|
||
Userdata({this.phoneNo, this.type, this.locationText}); | ||
|
||
Userdata.fromJson(Map<String, dynamic> json) { | ||
phoneNo = json['phoneNo']; | ||
type = json['type']; | ||
locationText = json['locationText']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['phoneNo'] = this.phoneNo; | ||
data['type'] = this.type; | ||
data['locationText'] = this.locationText; | ||
return data; | ||
} | ||
} | ||
|
||
class Paydata { | ||
String? type; | ||
Null? receipt; | ||
String? status; | ||
|
||
Paydata({this.type, this.receipt, this.status}); | ||
|
||
Paydata.fromJson(Map<String, dynamic> json) { | ||
type = json['type']; | ||
receipt = json['receipt']; | ||
status = json['status']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['type'] = this.type; | ||
data['receipt'] = this.receipt; | ||
data['status'] = this.status; | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class Operator { | ||
int? operatorId; | ||
String? centerLocation; | ||
String? name; | ||
String? picture; | ||
int? age; | ||
String? gender; | ||
String? phoneNo; | ||
String? email; | ||
String? ratings; | ||
List<String>? reviews; | ||
bool? isOperatorActive; | ||
int? timestamp; | ||
|
||
Operator( | ||
{this.operatorId, | ||
this.centerLocation, | ||
this.name, | ||
this.picture, | ||
this.age, | ||
this.gender, | ||
this.phoneNo, | ||
this.email, | ||
this.ratings, | ||
this.reviews, | ||
this.isOperatorActive, | ||
this.timestamp}); | ||
|
||
Operator.fromJson(Map<String, dynamic> json) { | ||
operatorId = json['operatorId']; | ||
centerLocation = json['centerLocation']; | ||
name = json['name']; | ||
picture = json['picture']; | ||
age = json['age']; | ||
gender = json['gender']; | ||
phoneNo = json['phoneNo']; | ||
email = json['email']; | ||
ratings = json['ratings']; | ||
reviews = json['reviews'].cast<String>(); | ||
isOperatorActive = json['isOperatorActive']; | ||
timestamp = json['timestamp']; | ||
} | ||
|
||
Map<String, dynamic> toJson() { | ||
final Map<String, dynamic> data = new Map<String, dynamic>(); | ||
data['operatorId'] = this.operatorId; | ||
data['centerLocation'] = this.centerLocation; | ||
data['name'] = this.name; | ||
data['picture'] = this.picture; | ||
data['age'] = this.age; | ||
data['gender'] = this.gender; | ||
data['phoneNo'] = this.phoneNo; | ||
data['email'] = this.email; | ||
data['ratings'] = this.ratings; | ||
data['reviews'] = this.reviews; | ||
data['isOperatorActive'] = this.isOperatorActive; | ||
data['timestamp'] = this.timestamp; | ||
return data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.