Skip to content

Commit

Permalink
Merge pull request #96 from praveenkrishna0512/adding-tests
Browse files Browse the repository at this point in the history
Adding tests
  • Loading branch information
summerthia02 authored Jul 24, 2022
2 parents be58d20 + 65ad65f commit c4d9e80
Show file tree
Hide file tree
Showing 3 changed files with 275 additions and 36 deletions.
102 changes: 66 additions & 36 deletions lib/models/listings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Listing {
final String createdByEmail;
final int? likeCount;
final dynamic dateListed;
final List<dynamic> usersLiked;
late final List<dynamic> usersLiked;
final List<String> searchIndex;
GeoPoint GeoLocation;
String location;
Expand All @@ -41,13 +41,13 @@ class Listing {
// this.bgColor = const Color(0xFFEFEFF2),
});

// void likePost(User? user) {
// if (usersLiked.contains(user?.uid)) {
// usersLiked.remove(user?.uid);
// } else {
// usersLiked.add(user?.uid);
// }
// }
void likePost(User? user) {
if (usersLiked.contains(user?.uid)) {
usersLiked.remove(user?.uid);
} else {
usersLiked.add(user?.uid);
}
}

// void setId(String id) {
// uid = id;
Expand All @@ -73,35 +73,65 @@ class Listing {
};
}

// Listing createListing(record) {
// Map<String, dynamic> attributes = {
// 'imageUrl': "",
// 'title': '',
// 'price': '',
// 'forRent': '',
// 'description': '',
// 'available': true,
// 'createdByEmail': '',
// 'likeCount': 0,
// 'usersLiked': []
// };
static Listing defaultListing(bool forRent) {
return Listing(
uid: "1",
dateListed: Timestamp.fromDate(DateTime.parse('1969-07-20 20:18:04Z')),
imageUrl: 'https://static.thenounproject.com/png/1913842-200.png',
title: "testTitle",
price: "NA",
description: "test record!",
createdByEmail: "[email protected]",
forRent: forRent,
available: true,
likeCount: 5,
usersLiked: [],
GeoLocation: const GeoPoint(1, 1),
location: 'Test Location',
shareCredits: '500',
searchIndex: [],
);
}

// record.forEach((key, value) => attributes[key] = value);
static Listing createListing(record) {
Map<String, dynamic> attributes = {
'uid': '',
'imageUrl': "",
'dateListed': Timestamp.fromDate(DateTime.parse('1969-07-20 20:18:04Z')),
'title': '',
'price': '',
'shareCredits': '',
'forRent': '',
'description': '',
'available': true,
'createdByEmail': '',
'likeCount': 0,
'usersLiked': [],
'GeoLocation': GeoPoint(1, 1),
'location': '',
'searchIndex': []
};

// Listing listing = Listing(
// available: attributes['available'],
// imageUrl: attributes['imageUrl'],
// title: attributes['title'],
// price: attributes['price'],
// forRent: attributes['forRent'],
// description: attributes['description'],
// createdByEmail: attributes['createdByEmail'],
// uid: attributes['uid'],
// dateListed: attributes['dateListed'],
// likeCount: attributes['likeCount'],
// profImageUrl: attributes['profImageUrl']);
record.forEach((key, value) => attributes[key] = value);

// listing.usersLiked = Set.from(attributes['usersLiked']);
// return listing;
// }
Listing listing = Listing(
available: attributes['available'],
imageUrl: attributes['imageUrl'],
title: attributes['title'],
price: attributes['price'],
forRent: attributes['forRent'],
description: attributes['description'],
createdByEmail: attributes['createdByEmail'],
uid: attributes['uid'],
dateListed: attributes['dateListed'],
likeCount: attributes['likeCount'],
searchIndex: attributes['searchIndex'],
usersLiked: attributes['usersLiked'],
GeoLocation: attributes['GeoLocation'],
location: attributes['location'],
shareCredits: attributes['shareCredits'],
);

return listing;
}
}
85 changes: 85 additions & 0 deletions test/form_validation_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:justsharelah_v1/utils/form_validation.dart';

void main() {
setUp(() {});

group('FormValidation: Email tests', () {
test('valid email doesnt produce error text', () {
String testVal = "[email protected]";
expect(FormValidation.validEmail(testVal), null);
});

test('invalid email produces error text (no @)', () {
String testVal = "testgmail.com";
expect(FormValidation.validEmail(testVal), "Please enter a valid email");
});

test('invalid email produces error text (no .com)', () {
String testVal = "test@gmail";
expect(FormValidation.validEmail(testVal), "Please enter a valid email");
});

test('invalid email produces error text (no @ and .com)', () {
String testVal = "testgmail";
expect(FormValidation.validEmail(testVal), "Please enter a valid email");
});

test('invalid email produces error text (empty string)', () {
String testVal = "";
expect(FormValidation.validEmail(testVal), "This field cannot be empty");
});
});

group('FormValidation: Enforce chars', () {
test(
'input text is empty and numofChars required is 0; no error text produced',
() {
String testVal = "";
int numOfChars = 0;
expect(FormValidation.enforceNumOfChars(testVal, numOfChars), null);
});

test(
'input text is empty and numofChars required is 4; error text produced',
() {
String testVal = "";
int numOfChars = 4;
expect(FormValidation.enforceNumOfChars(testVal, numOfChars),
"This field must have at least $numOfChars characters");
});

test(
'input text is of length 4 and numofChars required is 4; no error text produced',
() {
String testVal = "test";
int numOfChars = 4;
expect(FormValidation.enforceNumOfChars(testVal, numOfChars), null);
});

test('numofChars required is negative; no error text produced', () {
String testVal = "test";
int numOfChars = -1;
expect(FormValidation.enforceNumOfChars(testVal, numOfChars), null);
});

test('numofChars required is negative; no error text produced', () {
String testVal = "";
int numOfChars = -1;
expect(FormValidation.enforceNumOfChars(testVal, numOfChars), null);
});
});

group('FormValidation: Form field empty', () {
test('input text is empty; error text produced', () {
String testVal = "";
expect(
FormValidation.formFieldEmpty(testVal), "This field cannot be empty");
});

test('input text is non-empty; no error text produced', () {
String testVal = "test";
expect(FormValidation.formFieldEmpty(testVal), null);
});
});
}
124 changes: 124 additions & 0 deletions test/listings_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:justsharelah_v1/models/listings.dart';
import 'package:mockito/mockito.dart';

class MockFirebaseUser extends Mock implements User {
@override
String uid;

MockFirebaseUser({required this.uid});
}

void main() {
// adding to mock users
MockFirebaseUser testuser = MockFirebaseUser(uid: "testUser");
MockFirebaseUser testuser2 = MockFirebaseUser(uid: "testUser2");

group('test forRent value', () {
test('testing listing for rent', () {
Listing mockListingForRent = Listing.defaultListing(true);
expect(mockListingForRent.forRent, true);
});
test('testing listing for lending', () {
Listing mockListingForLending = Listing.defaultListing(false);
expect(mockListingForLending.forRent, false);
});
});

group('test usersLiked and likePost', () {
Listing mockListing = Listing.defaultListing(true);
test('usersLiked should be empty list', () {
expect(mockListing.usersLiked, []);
});
test('add one user id and usersLiked should contain that user', () {
mockListing.likePost(testuser);
expect(mockListing.usersLiked, [testuser.uid]);
});
test('add one more user id and usersLiked should contain both users', () {
mockListing.likePost(testuser2);
expect(mockListing.usersLiked, [testuser.uid, testuser2.uid]);
});
test('when user likes post again, usersLiked should remove that user', () {
mockListing.likePost(testuser);
expect(mockListing.usersLiked, [testuser2.uid]);
});
});

// group('test setId', () {
// Listing mockListing = Listing.defaultListing(true);
// test('testing if setID sets ID', () {
// String testId = "test";
// mockListing.setId(testId);
// expect(mockListing.uid, testId);
// });
// });

group('test createListing and toJson', () {
String imageUrl,
title,
price,
description,
createdByEmail,
profImageUrl,
location,
shareCredits,
uid;

DateTime dateListed;
bool forRent, available;
int likeCount;
List<String> usersLiked;
List<String> searchIndex;
GeoPoint GeoLocation;

uid = "1";
dateListed = DateTime.parse('1969-07-20 20:18:04Z');
imageUrl = 'https://static.thenounproject.com/png/1913842-200.png';
title = "testTitle";
price = "NA";
description = "test record!";
createdByEmail = "[email protected]";
forRent = false;
available = true;
likeCount = 5;
usersLiked = ["testuser", "anotheruid"];
searchIndex = ["test", "title"];
GeoLocation = GeoPoint(1, 1);
location = "TestLocation";
shareCredits = "500";

Map<String, dynamic> testRecord = {
'uid': uid,
'imageUrl': imageUrl,
'title': title,
'price': price,
'forRent': forRent,
'description': description,
'available': available,
'createdByEmail': createdByEmail,
'likeCount': likeCount,
'usersLiked': usersLiked,
'dateListed': dateListed,
'searchIndex': searchIndex,
'GeoLocation': GeoLocation,
'location': location,
'shareCredits': shareCredits
};

Listing listing = Listing.createListing(testRecord);

test('created listing values and record values must be same', () {
expect(listing.imageUrl, testRecord["imageUrl"]);
expect(listing.title, testRecord["title"]);
expect(listing.price, testRecord["price"]);
expect(listing.forRent, testRecord["forRent"]);
expect(listing.description, testRecord["description"]);
expect(listing.available, testRecord["available"]);
expect(listing.createdByEmail, testRecord["createdByEmail"]);
expect(listing.likeCount, testRecord["likeCount"]);
expect(listing.usersLiked, testRecord["usersLiked"]);
});
});
}

0 comments on commit c4d9e80

Please sign in to comment.