diff --git a/lib/api/firebase_aftercare.dart b/lib/api/firebase_aftercare.dart index 81ca613..0c488a0 100644 --- a/lib/api/firebase_aftercare.dart +++ b/lib/api/firebase_aftercare.dart @@ -7,13 +7,17 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'firebase_api.dart'; class FirebaseAftercare extends FirebaseAPI implements IAftercare { - FirebaseAftercare(FirebaseFirestore db) : super(db, 'aftercare'); - + @override void createAftercare(Aftercare aftercare) { collectionReference - .add(aftercare.toJson()) + .withConverter( + fromFirestore: Aftercare.fromFirestore, + toFirestore: (Aftercare aftercare, options) => + aftercare.toFirestore()) + .doc(aftercare.uid) + .set(aftercare) .then((value) => log("Aftercare Added")) .catchError((error) { log("Failed to add aftercare: $error"); @@ -21,31 +25,36 @@ class FirebaseAftercare extends FirebaseAPI implements IAftercare { }); } - @override - void deleteAftercare(String aftercareId) { - collectionReference.doc(aftercareId).delete(); - } - @override Future readAftercare(String aftercareId) async { - final docRef = collectionReference.doc(aftercareId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(aftercareId).withConverter( + fromFirestore: Aftercare.fromFirestore, + toFirestore: (Aftercare city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final aftercare = docSnapshot.data(); + if (aftercare != null) { + return aftercare; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Aftercare.fromJson(data); } @override updateAftercare(Aftercare aftercare) { collectionReference - .doc('FAKE') - .update(aftercare.toJson()) + .doc(aftercare.uid) + .update(aftercare.toFirestore()) .then((value) => log("Aftercare Updated")) .catchError((error) { log("Failed to update aftercare: $error"); throw Exception(error); }); } + + @override + void deleteAftercare(String aftercareId) { + collectionReference.doc(aftercareId).delete(); + } } diff --git a/lib/api/firebase_client.dart b/lib/api/firebase_client.dart index e76f75b..7a1d219 100644 --- a/lib/api/firebase_client.dart +++ b/lib/api/firebase_client.dart @@ -1,19 +1,21 @@ import 'dart:developer'; import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:pdg_app/api/firebase_api.dart'; import 'package:pdg_app/api/iclient.dart'; import 'package:pdg_app/model/client.dart'; -import 'firebase_api.dart'; - class FirebaseClient extends FirebaseAPI implements IClient { - FirebaseClient(FirebaseFirestore db) : super(db, 'client'); @override void createClient(Client client) { collectionReference - .add(client.toJson()) + .withConverter( + fromFirestore: Client.fromFirestore, + toFirestore: (Client client, options) => client.toFirestore()) + .doc(client.uid) + .set(client) .then((value) => log("Client Added")) .catchError((error) { log("Failed to add client: $error"); @@ -23,20 +25,25 @@ class FirebaseClient extends FirebaseAPI implements IClient { @override Future readClient(String clientId) async { - final docRef = collectionReference.doc(clientId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(clientId).withConverter( + fromFirestore: Client.fromFirestore, + toFirestore: (Client city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final client = docSnapshot.data(); + if (client != null) { + return client; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Client.fromJson(data); } @override void updateClient(Client client) { collectionReference - .doc('FAKE') - .update(client.toJson()) + .doc(client.uid) + .update(client.toFirestore()) .then((value) => log("Client Updated")) .catchError((error) { log("Failed to update client: $error"); diff --git a/lib/api/firebase_dietitian.dart b/lib/api/firebase_dietitian.dart index c741d7a..e80df64 100644 --- a/lib/api/firebase_dietitian.dart +++ b/lib/api/firebase_dietitian.dart @@ -13,7 +13,11 @@ class FirebaseDietitian extends FirebaseAPI implements IDietitian { @override void createDietitian(Dietitian dietitian) { collectionReference - .add(dietitian.toJson()) + .withConverter( + fromFirestore: Dietitian.fromFirestore, + toFirestore: (Dietitian dietitian, options) => dietitian.toFirestore()) + .doc(dietitian.uid) + .set(dietitian) .then((value) => log("Dietitian Added")) .catchError((error) { log("Failed to add dietitian: $error"); @@ -23,20 +27,25 @@ class FirebaseDietitian extends FirebaseAPI implements IDietitian { @override Future readDietitian(String dietitianId) async { - final docRef = collectionReference.doc(dietitianId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(dietitianId).withConverter( + fromFirestore: Dietitian.fromFirestore, + toFirestore: (Dietitian city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final dietitian = docSnapshot.data(); + if (dietitian != null) { + return dietitian; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Dietitian.fromJson(data); } @override void updateDietitian(Dietitian dietitian) { collectionReference - .doc('FAKE') - .update(dietitian.toJson()) + .doc(dietitian.uid) + .update(dietitian.toFirestore()) .then((value) => log("Dietitian Updated")) .catchError((error) { log("Failed to update dietitian: $error"); diff --git a/lib/api/firebase_meal.dart b/lib/api/firebase_meal.dart index fe0dc82..c43633b 100644 --- a/lib/api/firebase_meal.dart +++ b/lib/api/firebase_meal.dart @@ -7,13 +7,16 @@ import 'package:cloud_firestore/cloud_firestore.dart'; import 'firebase_api.dart'; class FirebaseMeal extends FirebaseAPI implements IMeal { - FirebaseMeal(FirebaseFirestore db) : super(db, 'meal'); @override void createMeal(Meal meal) { collectionReference - .add(meal.toJson()) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal meal, options) => meal.toFirestore()) + .doc(meal.uid) + .set(meal) .then((value) => log("Meal Added")) .catchError((error) { log("Failed to add meal: $error"); @@ -21,31 +24,36 @@ class FirebaseMeal extends FirebaseAPI implements IMeal { }); } - @override - void deleteMeal(String mealId) { - collectionReference.doc(mealId).delete(); - } - @override Future readMeal(String mealId) async { - final docRef = collectionReference.doc(mealId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(mealId).withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final meal = docSnapshot.data(); + if (meal != null) { + return meal; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Meal.fromJson(data); } @override - updateMeal(Meal meal) { + void updateMeal(Meal meal) { collectionReference - .doc('FAKE') - .update(meal.toJson()) + .doc(meal.uid) + .update(meal.toFirestore()) .then((value) => log("Meal Updated")) .catchError((error) { log("Failed to update meal: $error"); throw Exception(error); }); } + + @override + void deleteMeal(String mealId) { + collectionReference.doc(mealId).delete(); + } } diff --git a/lib/api/firebase_message.dart b/lib/api/firebase_message.dart index 550dec7..6805b65 100644 --- a/lib/api/firebase_message.dart +++ b/lib/api/firebase_message.dart @@ -13,7 +13,11 @@ class FirebaseMessage extends FirebaseAPI implements IMessage { @override void createMessage(Message message) { collectionReference - .add(message.toJson()) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message message, options) => message.toFirestore()) + .doc(message.uid) + .set(message) .then((value) => log("Message Added")) .catchError((error) { log("Failed to add message: $error"); @@ -21,31 +25,36 @@ class FirebaseMessage extends FirebaseAPI implements IMessage { }); } - @override - void deleteMessage(String messageId) { - collectionReference.doc(messageId).delete(); - } - @override Future readMessage(String messageId) async { - final docRef = collectionReference.doc(messageId); - final doc = await docRef.get(); - if (!doc.exists) { + final docRef = collectionReference.doc(messageId).withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ); + final docSnapshot = await docRef.get(); + final message = docSnapshot.data(); + if (message != null) { + return message; + } else { + log("Doc does not exist"); throw Error(); } - final data = doc.data() as Map; - return Message.fromJson(data); } @override - updateMessage(Message message) { + void updateMessage(Message message) { collectionReference - .doc('FAKE') - .update(message.toJson()) + .doc(message.uid) + .update(message.toFirestore()) .then((value) => log("Message Updated")) .catchError((error) { log("Failed to update message: $error"); throw Exception(error); }); } + + @override + void deleteMessage(String messageId) { + collectionReference.doc(messageId).delete(); + } } diff --git a/lib/api/idocument.dart b/lib/api/idocument.dart index 75262a8..3e07964 100644 --- a/lib/api/idocument.dart +++ b/lib/api/idocument.dart @@ -1,7 +1,7 @@ import 'package:pdg_app/model/document.dart'; abstract class IDocument { - void createDocument(Document document); + void createDocument(Document document, String fileName); Future readDocument(String documentId); void updateDocument(Document document); void deleteDocument(String documentId); diff --git a/lib/main.dart b/lib/main.dart index fed9c7b..94a29c2 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -20,7 +20,7 @@ void main() async { ); IMessage msg = FirebaseMessage(FirebaseFirestore.instance); - final m1 = Message(id: '', fromId: 'alice', toId: 'bob', content: "HELLOW"); + final m1 = Message(uid: '', fromId: 'alice', toId: 'bob', content: "HELLOW"); msg.createMessage(m1); log("test"); runApp(MyApp()); diff --git a/lib/model/aftercare.dart b/lib/model/aftercare.dart index 7c437eb..bbe85bd 100644 --- a/lib/model/aftercare.dart +++ b/lib/model/aftercare.dart @@ -1,6 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:uuid/uuid.dart'; + import 'imodel.dart'; class Aftercare implements IModel { + String uid; int? bmi; double? weight; String? diagnostic; @@ -12,7 +16,8 @@ class Aftercare implements IModel { List? documents; Aftercare( - {this.bmi, + {String? uid, + this.bmi, this.weight, this.diagnostic, this.comments, @@ -20,25 +25,32 @@ class Aftercare implements IModel { this.foodObjectives, this.startDate, this.endDate, - this.documents}); + this.documents}) + : uid = uid ?? const Uuid().v1(); - factory Aftercare.fromJson(Map aftercare) { + factory Aftercare.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Aftercare( - bmi: aftercare['bmi'], - weight: aftercare['weight'], - diagnostic: aftercare['diagnostic'], - comments: aftercare['comments'], - motivations: aftercare['motivations'], - foodObjectives: aftercare['foodObjectives'], - startDate: aftercare['startDate'], - endDate: aftercare['endDate'], - documents: aftercare['documents'], + uid: data?['uid'], + bmi: data?['bmi'], + weight: data?['weight'], + diagnostic: data?['diagnostic'], + comments: data?['comments'], + motivations: data?['motivations'], + foodObjectives: data?['foodObjectives'], + startDate: data?['startDate'], + endDate: data?['endDate'], + documents: data?['documents'], ); } @override - Map toJson() { + Map toFirestore() { return { + 'uid': uid, 'bmi': bmi, 'weight': weight, 'diagnostic': diagnostic, @@ -55,4 +67,8 @@ class Aftercare implements IModel { String toString() { return 'Aftercare{$bmi $comments $diagnostic $documents $endDate $foodObjectives $motivations $startDate $weight}'; } + + void setBmi(int newBmi) { + bmi = newBmi; + } } diff --git a/lib/model/client.dart b/lib/model/client.dart index 893abb1..6cfe88c 100644 --- a/lib/model/client.dart +++ b/lib/model/client.dart @@ -1,6 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:uuid/uuid.dart'; + import 'imodel.dart'; -class Client implements IModel{ +class Client implements IModel { + String uid; String? firstName; String? lastName; String? birthDate; @@ -8,30 +12,38 @@ class Client implements IModel{ String? insurance; Client( - {this.firstName, + {String? uid, + this.firstName, this.lastName, this.birthDate, this.insurance, - this.phoneNumber}); + this.phoneNumber}) + : uid = uid ?? const Uuid().v1(); - factory Client.fromJson(Map client) { + factory Client.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Client( - firstName: client['firstName'], - lastName: client['lastName'], - birthDate: client['birthDate'], - insurance: client['insurance'], - phoneNumber: client['phoneNumber'], + uid: data?['uid'], + firstName: data?['firstName'], + lastName: data?['lastName'], + birthDate: data?['birthDate'], + insurance: data?['insurance'], + phoneNumber: data?['phoneNumber'], ); } @override - Map toJson() { + Map toFirestore() { return { - 'firstName': firstName, - 'lastName': lastName, - 'birthDate': birthDate, - 'phoneNumber': phoneNumber, - 'insurance': insurance, + 'uid': firstName, + if (firstName != null) 'firstName': firstName, + if (lastName != null) 'lastName': lastName, + if (birthDate != null) 'birthDate': birthDate, + if (phoneNumber != null) 'phoneNumber': phoneNumber, + if (insurance != null) 'insurance': insurance, }; } @@ -40,4 +52,7 @@ class Client implements IModel{ return 'Client{$firstName $lastName $birthDate $insurance $phoneNumber}'; } + void setFirstName(String name) { + firstName = name; + } } diff --git a/lib/model/dietitian.dart b/lib/model/dietitian.dart index 11f0370..8cc695a 100644 --- a/lib/model/dietitian.dart +++ b/lib/model/dietitian.dart @@ -1,7 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:uuid/uuid.dart'; + import 'imodel.dart'; class Dietitian implements IModel { - String? id; + String uid; String? firstName; String? lastName; List? clientList; @@ -9,28 +12,33 @@ class Dietitian implements IModel { String? avs; Dietitian( - {this.id, + {String? uid, this.firstName, this.lastName, this.clientList, this.avs, - this.birthDate}); + this.birthDate}) + : uid = uid ?? const Uuid().v1(); - factory Dietitian.fromJson(Map dietitian) { + factory Dietitian.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Dietitian( - id: dietitian['id'], - firstName: dietitian['firstName'], - lastName: dietitian['lastName'], - clientList: dietitian['clientList'], - avs: dietitian['avs'], - birthDate: dietitian['birthDate'], + uid: data?['uid'], + firstName: data?['firstName'], + lastName: data?['lastName'], + clientList: data?['clientList'], + avs: data?['avs'], + birthDate: data?['birthDate'], ); } @override - Map toJson() { + Map toFirestore() { return { - 'id': id, + 'uid': uid, 'firstName': firstName, 'lastName': lastName, 'clientList': clientList, @@ -44,4 +52,7 @@ class Dietitian implements IModel { return 'Dietitian{$firstName $lastName $clientList $avs $birthDate}'; } + void setFirstName(String name) { + firstName = name; + } } diff --git a/lib/model/document.dart b/lib/model/document.dart index 708ba21..0f9d130 100644 --- a/lib/model/document.dart +++ b/lib/model/document.dart @@ -13,7 +13,7 @@ class Document implements IModel { } @override - Map toJson() { + Map toFirestore() { return { 'url': url, }; diff --git a/lib/model/imodel.dart b/lib/model/imodel.dart index 510404a..f9d6641 100644 --- a/lib/model/imodel.dart +++ b/lib/model/imodel.dart @@ -1,3 +1,3 @@ abstract class IModel { - Map toJson(); + Map toFirestore(); } \ No newline at end of file diff --git a/lib/model/meal.dart b/lib/model/meal.dart index b0929a3..1421f89 100644 --- a/lib/model/meal.dart +++ b/lib/model/meal.dart @@ -1,41 +1,53 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:uuid/uuid.dart'; + import 'imodel.dart'; class Meal implements IModel { + String uid; DateTime? startTime; DateTime? endTime; String? lastName; List? photo; - String? hunger; - String? satiety; + int? hunger; + int? satiety; String? setting; String? comment; Meal( - {this.startTime, + {String? uid, + this.startTime, this.endTime, this.lastName, this.photo, this.satiety, this.hunger, this.setting, - this.comment}); + this.comment}) + : uid = uid ?? const Uuid().v1(); - factory Meal.fromJson(Map meal) { + factory Meal.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Meal( - startTime: meal['startTime'], - endTime: meal['endTime'], - lastName: meal['lastName'], - photo: meal['photo'], - satiety: meal['satiety'], - hunger: meal['hunger'], - setting: meal['setting'], - comment: meal['comment'], + uid: data?['uid'], + startTime: data?['startTime'], + endTime: data?['endTime'], + lastName: data?['lastName'], + photo: data?['photo'], + satiety: data?['satiety'], + hunger: data?['hunger'], + setting: data?['setting'], + comment: data?['comment'], ); } @override - Map toJson() { + Map toFirestore() { return { + 'uid': uid, 'startTime': startTime, 'endTime': endTime, 'lastName': lastName, @@ -51,4 +63,8 @@ class Meal implements IModel { String toString() { return 'Meal{$endTime $lastName $photo $satiety $hunger}'; } + + void setComment(String newComment) { + comment = newComment; + } } diff --git a/lib/model/message.dart b/lib/model/message.dart index 82f2a7b..e5a304b 100644 --- a/lib/model/message.dart +++ b/lib/model/message.dart @@ -1,9 +1,10 @@ +import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:uuid/uuid.dart'; import 'imodel.dart'; class Message implements IModel { - String id = const Uuid().v1(); + String uid = const Uuid().v1(); DateTime? time; String? fromId; String? toId; @@ -11,29 +12,33 @@ class Message implements IModel { int? type; Message({ - required this.id, + String? uid, this.time, this.fromId, this.toId, this.content, this.type, - }); + }) : uid = uid ?? const Uuid().v1(); - factory Message.fromJson(Map message) { + factory Message.fromFirestore( + DocumentSnapshot> snapshot, + SnapshotOptions? options, + ) { + final data = snapshot.data(); return Message( - id: message['id'], - time: message['time'], - fromId: message['fromId'], - toId: message['toId'], - content: message['content'], - type: message['type'], + uid: data?['uid'], + time: data?['time'], + fromId: data?['fromId'], + toId: data?['toId'], + content: data?['content'], + type: data?['type'], ); } @override - Map toJson() { + Map toFirestore() { return { - 'id': id, + 'uid': uid, 'time': time, 'fromId': fromId, 'toId': toId, @@ -46,4 +51,8 @@ class Message implements IModel { String toString() { return 'Message{$fromId $toId $content $type}'; } + + void setContent(String newContent) { + content = newContent; + } } diff --git a/test/aftercare_test.dart b/test/aftercare_test.dart new file mode 100644 index 0000000..5704d82 --- /dev/null +++ b/test/aftercare_test.dart @@ -0,0 +1,68 @@ +import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pdg_app/api/iaftercare.dart'; +import 'package:pdg_app/api/firebase_aftercare.dart'; +import 'package:pdg_app/model/aftercare.dart'; + +final db = FakeFirebaseFirestore(); +Aftercare a1 = Aftercare(bmi: 12, weight: 14.0); +Aftercare a2 = Aftercare(bmi: 13, weight: 14.0); + +void populateMockAftercare(Aftercare c) async { + await db.collection('aftercare').doc(c.uid).set(c.toFirestore()); +} + +void main() { + late IAftercare aftercareApi; + final aftercares = db.collection('aftercare'); + + setUp(() async { + populateMockAftercare(a2); + aftercareApi = FirebaseAftercare(db); + }); + + test("Create Aftercare", () async { + aftercareApi.createAftercare(a1); + final docSnapshot = await aftercares + .doc(a1.uid) + .withConverter( + fromFirestore: Aftercare.fromFirestore, + toFirestore: (Aftercare aftercare, _) => aftercare.toFirestore(), + ) + .get(); + final aftercare = docSnapshot.data(); + expect(a1.toString(), aftercare.toString()); + }); + + test("Read Aftercare", () async { + final Aftercare a2Bis = await aftercareApi.readAftercare(a2.uid); + expect(a2.toString(), a2Bis.toString()); + }); + + test("Update aftercare", () async { + a1.setBmi(18); + aftercareApi.updateAftercare(a1); + final docSnapshot = await aftercares + .doc(a1.uid) + .withConverter( + fromFirestore: Aftercare.fromFirestore, + toFirestore: (Aftercare city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect(18, c2!.bmi); + }); + + test("Delete aftercare", () async { + aftercareApi.deleteAftercare(a1.uid); + final docSnapshot = await aftercares + .doc(a1.uid) + .withConverter( + fromFirestore: Aftercare.fromFirestore, + toFirestore: (Aftercare city, _) => city.toFirestore(), + ) + .get(); + final aftercare = docSnapshot.data(); + expect(aftercare, null); + }); +} diff --git a/test/client_test.dart b/test/client_test.dart new file mode 100644 index 0000000..0cc64c8 --- /dev/null +++ b/test/client_test.dart @@ -0,0 +1,75 @@ +import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pdg_app/api/iclient.dart'; +import 'package:pdg_app/api/firebase_client.dart'; +import 'package:pdg_app/model/client.dart'; + +final db = FakeFirebaseFirestore(); +Client c1 = Client( + firstName: 'Olivier', lastName: 'D\'Ancona', phoneNumber: '0780001223'); +Client c2 = + Client(firstName: 'ChloƩ', lastName: 'Fontaine', phoneNumber: '0780002334'); +Client c3 = + Client(firstName: 'Luca', lastName: 'Coduri', phoneNumber: '0780003445'); +Client c4 = Client( + firstName: 'Nelson', lastName: 'Jeanrenaud', phoneNumber: '0786834556'); + +Future populateMockClient(Client c) async { + await db.collection('client').doc(c.uid).set(c.toFirestore()); +} + +void main() { + late IClient clientApi; + final clients = db.collection('client'); + + setUp(() async { + populateMockClient(c2); + populateMockClient(c3); + clientApi = FirebaseClient(db); + }); + + test("Create Client", () async { + clientApi.createClient(c1); + final docSnapshot = await clients + .doc(c1.uid) + .withConverter( + fromFirestore: Client.fromFirestore, + toFirestore: (Client city, _) => city.toFirestore(), + ) + .get(); + final client = docSnapshot.data(); + expect(c1.toString(), client.toString()); + }); + + test("Read Client", () async { + final Client c2Bis = await clientApi.readClient(c2.uid); + expect(c2.toString(), c2Bis.toString()); + }); + + test("Update client", () async { + c1.setFirstName('Filippo'); + clientApi.updateClient(c1); + final docSnapshot = await clients + .doc(c1.uid) + .withConverter( + fromFirestore: Client.fromFirestore, + toFirestore: (Client city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.firstName); + }); + + test("Delete client", () async { + clientApi.deleteClient(c3.uid); + final docSnapshot = await clients + .doc(c3.uid) + .withConverter( + fromFirestore: Client.fromFirestore, + toFirestore: (Client client, _) => client.toFirestore(), + ) + .get(); + final client = docSnapshot.data(); + expect(client, null); + }); +} diff --git a/test/default_test.dart b/test/default_test.dart deleted file mode 100644 index 9bbdb6a..0000000 --- a/test/default_test.dart +++ /dev/null @@ -1,14 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility in the flutter_test package. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter_test/flutter_test.dart'; - -void main() { - testWidgets('default', (WidgetTester tester) async { - expect(true, true); - }); -} diff --git a/test/dietitian_test.dart b/test/dietitian_test.dart new file mode 100644 index 0000000..ce0ab14 --- /dev/null +++ b/test/dietitian_test.dart @@ -0,0 +1,70 @@ +import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pdg_app/api/idietitian.dart'; +import 'package:pdg_app/api/firebase_dietitian.dart'; +import 'package:pdg_app/model/dietitian.dart'; + +final db = FakeFirebaseFirestore(); + Dietitian d1 = Dietitian(firstName: 'Claire', lastName: 'Emery'); + Dietitian d2 = Dietitian(firstName: 'Alice', lastName: 'Emery'); + + + +Future populateMockDietitian(Dietitian d) async { + await db.collection('dietitian').doc(d.uid).set(d.toFirestore()); +} + +void main() { + late IDietitian dietitianApi; + final dietitians = db.collection('dietitian'); + + setUp(() async { + populateMockDietitian(d2); + dietitianApi = FirebaseDietitian(db); + }); + + test("Create Dietitian", () async { + dietitianApi.createDietitian(d1); + final docSnapshot = await dietitians + .doc(d1.uid) + .withConverter( + fromFirestore: Dietitian.fromFirestore, + toFirestore: (Dietitian dietitian, _) => dietitian.toFirestore(), + ) + .get(); + final dietitian = docSnapshot.data(); + expect(d1.toString(), dietitian.toString()); + }); + + test("Read Dietitian", () async { + final Dietitian c2Bis = await dietitianApi.readDietitian(d2.uid); + expect(d2.toString(), c2Bis.toString()); + }); + + test("Update dietitian", () async { + d1.setFirstName('Filippo'); + dietitianApi.updateDietitian(d1); + final docSnapshot = await dietitians + .doc(d1.uid) + .withConverter( + fromFirestore: Dietitian.fromFirestore, + toFirestore: (Dietitian city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.firstName); + }); + + test("Delete dietitian", () async { + dietitianApi.deleteDietitian(d2.uid); + final docSnapshot = await dietitians + .doc(d2.uid) + .withConverter( + fromFirestore: Dietitian.fromFirestore, + toFirestore: (Dietitian city, _) => city.toFirestore(), + ) + .get(); + final dietitian = docSnapshot.data(); + expect(dietitian, null); + }); +} diff --git a/test/meal_test.dart b/test/meal_test.dart new file mode 100644 index 0000000..12b022f --- /dev/null +++ b/test/meal_test.dart @@ -0,0 +1,68 @@ +import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pdg_app/api/imeal.dart'; +import 'package:pdg_app/api/firebase_meal.dart'; +import 'package:pdg_app/model/meal.dart'; + +final db = FakeFirebaseFirestore(); +Meal m1 = Meal(hunger: 4, satiety: 5, comment: 'no comment'); +Meal m2 = Meal(hunger: 7, satiety: 2, comment: 'wow'); + +Future populateMockMeal(Meal c) async { + await db.collection('meal').doc(c.uid).set(c.toFirestore()); +} + +void main() { + late IMeal mealApi; + final meals = db.collection('meal'); + + setUp(() async { + populateMockMeal(m2); + mealApi = FirebaseMeal(db); + }); + + test("Create Meal", () async { + mealApi.createMeal(m1); + final docSnapshot = await meals + .doc(m1.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal meal, _) => meal.toFirestore(), + ) + .get(); + final meal = docSnapshot.data(); + expect(m1.toString(), meal.toString()); + }); + + test("Read Meal", () async { + final Meal m2Bis = await mealApi.readMeal(m2.uid); + expect(m2.toString(), m2Bis.toString()); + }); + + test("Update meal", () async { + m1.setComment('Filippo'); + mealApi.updateMeal(m1); + final docSnapshot = await meals + .doc(m1.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.comment); + }); + + test("Delete meal", () async { + mealApi.deleteMeal(m2.uid); + final docSnapshot = await meals + .doc(m2.uid) + .withConverter( + fromFirestore: Meal.fromFirestore, + toFirestore: (Meal city, _) => city.toFirestore(), + ) + .get(); + final meal = docSnapshot.data(); + expect(meal, null); + }); +} diff --git a/test/message_test.dart b/test/message_test.dart new file mode 100644 index 0000000..2b47c26 --- /dev/null +++ b/test/message_test.dart @@ -0,0 +1,68 @@ +import 'package:fake_cloud_firestore/fake_cloud_firestore.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pdg_app/api/imessage.dart'; +import 'package:pdg_app/api/firebase_message.dart'; +import 'package:pdg_app/model/message.dart'; + +final db = FakeFirebaseFirestore(); +Message msg1 = Message(content: 'Bonjour', fromId: 'alice', toId: 'bob'); +Message msg2 = Message(content: 'ciao', fromId: 'alice', toId: 'bob'); + +Future populateMockMessage(Message c) async { + await db.collection('message').doc(c.uid).set(c.toFirestore()); +} + +void main() { + late IMessage messageApi; + final messages = db.collection('message'); + + setUp(() async { + populateMockMessage(msg2); + messageApi = FirebaseMessage(db); + }); + + test("Create Message", () async { + messageApi.createMessage(msg1); + final docSnapshot = await messages + .doc(msg1.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message message, _) => message.toFirestore(), + ) + .get(); + final message = docSnapshot.data(); + expect(msg1.toString(), message.toString()); + }); + + test("Read Message", () async { + final Message m2Bis = await messageApi.readMessage(msg2.uid); + expect(msg2.toString(), m2Bis.toString()); + }); + + test("Update message", () async { + msg1.setContent('Filippo'); + messageApi.updateMessage(msg1); + final docSnapshot = await messages + .doc(msg1.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ) + .get(); + final c2 = docSnapshot.data(); + expect('Filippo', c2!.content); + }); + + test("Delete message", () async { + messageApi.deleteMessage(msg2.uid); + final docSnapshot = await messages + .doc(msg2.uid) + .withConverter( + fromFirestore: Message.fromFirestore, + toFirestore: (Message city, _) => city.toFirestore(), + ) + .get(); + final message = docSnapshot.data(); + expect(message, null); + }); +}