Skip to content

Commit

Permalink
test: data/models coverage %100 OK - +128 ~12: All tests passed!
Browse files Browse the repository at this point in the history
  • Loading branch information
cevheri committed Nov 18, 2024
1 parent 10b60ae commit 449dfbc
Show file tree
Hide file tree
Showing 15 changed files with 5,444 additions and 88 deletions.
5,013 changes: 5,013 additions & 0 deletions docs/unittest.excalidraw

Large diffs are not rendered by default.

21 changes: 10 additions & 11 deletions lib/data/models/authorities.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@ class Authorities extends Equatable {
}

static Authorities? fromJson(Map<String, dynamic> json) {
var result = JsonMapper.fromMap<Authorities>(json);
if (result == null) {
return null;
if (json['name'] != null) {
return Authorities(name: json['name']);
}
return result;
return null;
}

static Authorities? fromJsonString(String json){
var result = JsonMapper.deserialize<Authorities>(jsonDecode(json));
if (result == null) {
return null;
}
return result;
static Authorities? fromJsonString(String json) {
return fromJson(jsonDecode(json));
}

Map<String, dynamic>? toJson() => JsonMapper.toMap(this);
Map<String, dynamic>? toJson() {
return {
'name': name,
};
}

@override
List<Object?> get props => [
Expand Down
47 changes: 42 additions & 5 deletions test/data/model/authority_test.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
import 'dart:convert';

import 'package:flutter_bloc_advance/data/models/authorities.dart';
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../fake/user_data.dart';

/// Test the Authorities model
void main() {
var model = mockAuthorityPayload;

setUp(() {
initializeJsonMapper();
});

group("Authorities Model", () {
test('should create a Authorities instance (Constructor)', () {
expect(model.name, 'ROLE_USER');
expect(mockAuthorityPayload.name, 'ROLE_USER');
});

test('should copy a Authorities instance with new values (copyWith)', () {
final updatedAuthorities = mockAuthorityPayload.copyWith();

expect(updatedAuthorities == mockAuthorityPayload, true);
});

test('should copy a Authorities instance with new values (copyWith)', () {
final updatedAuthorities = model.copyWith(
final updatedAuthorities = mockAuthorityPayload.copyWith(
name: 'ROLE_ADMIN',
);

expect(updatedAuthorities.name, 'ROLE_ADMIN');
});

test('should compare two Authorities instances', () {
final finalAuthorities = model;
final finalAuthorities = mockAuthorityPayload;

final updatedAuthorities = finalAuthorities.copyWith(
name: 'ROLE_ADMIN',
Expand All @@ -34,4 +41,34 @@ void main() {
expect(finalAuthorities == updatedAuthorities, false);
});
});

group("Authority Model Json Test", () {
test('should convert Authorities from Json', () {
final json = mockAuthorityPayload.toJson();

final authority = Authorities.fromJson(json!);

expect(authority?.name, 'ROLE_USER');
});

test('should convert Authorities from JsonString', () {
final jsonString = jsonEncode(mockAuthorityPayload.toJson());

final authority = Authorities.fromJsonString(jsonString);

expect(authority?.name, 'ROLE_USER');
});

test('should convert Authorities to Json', () {
final json = mockAuthorityPayload.toJson()!;

expect(json['name'], 'ROLE_USER');
});

test("to string method", () {
final authority = mockAuthorityPayload;

expect(authority.toString(), 'Authorities(ROLE_USER)');
});
});
}
66 changes: 59 additions & 7 deletions test/data/model/change_password_test.dart
Original file line number Diff line number Diff line change
@@ -1,39 +1,91 @@
import 'dart:convert';

import 'package:flutter_bloc_advance/data/models/change_password.dart';
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../fake/user_data.dart';

/// Test the Change Password model
void main() {
var model = mockPasswordChangePayload;

setUp(() {
initializeJsonMapper();
});

group("Change Password Model", () {
test('should create a ChangePassword instance (Constructor)', () {
expect(model.currentPassword, 'password');
expect(model.newPassword, 'new_password');
expect(mockPasswordChangePayload.currentPassword, 'password');
expect(mockPasswordChangePayload.newPassword, 'new_password');
});

test('should copy a ChangePassword instance with new values (copyWith)', () {
final updatedChangePassword = model.copyWith(
final updatedChangePassword = mockPasswordChangePayload.copyWith(
currentPassword: 'new_password',
newPassword: 'password',
);

expect(updatedChangePassword.currentPassword, 'new_password');
expect(updatedChangePassword.newPassword, 'password');
});

test('should copy a ChangePassword instance with new values (copyWith)', () {
final updatedChangePassword = mockPasswordChangePayload.copyWith();
expect(updatedChangePassword == mockPasswordChangePayload, true);
});

test('should compare two ChangePassword instances', () {
final updatedChangePassword = model.copyWith(
final updatedChangePassword = mockPasswordChangePayload.copyWith(
currentPassword: 'new_password',
newPassword: 'password',
);

expect(model == updatedChangePassword, false);
expect(mockPasswordChangePayload == updatedChangePassword, false);
});
});

//fromJson, fromJsonString, toJson, props, toString
group("Change Password Model Json Test", () {
test('should convert ChangePassword from Json', () {
final json = mockPasswordChangePayload.toJson();

final changePassword = PasswordChangeDTO.fromJson(json!);

expect(changePassword?.currentPassword, 'password');
expect(changePassword?.newPassword, 'new_password');
});

test('should convert ChangePassword from JsonString', () {
final jsonString = jsonEncode(mockPasswordChangePayload.toJson());

final changePassword = PasswordChangeDTO.fromJsonString(jsonString);

expect(changePassword?.currentPassword, 'password');
expect(changePassword?.newPassword, 'new_password');
});

test('should convert ChangePassword to Json', () {
final json = mockPasswordChangePayload.toJson()!;
expect(json['currentPassword'], 'password');
expect(json['newPassword'], 'new_password');
});

test('should compare two ChangePassword instances props', () {
final updatedChangePassword = mockPasswordChangePayload.copyWith(
currentPassword: 'new_password',
newPassword: 'password',
);

expect(mockPasswordChangePayload.props == updatedChangePassword.props, false);
});

test('should compare two ChangePassword instances toString', () {
final updatedChangePassword = mockPasswordChangePayload.copyWith(
currentPassword: 'new_password',
newPassword: 'password',
);

expect(mockPasswordChangePayload.toString() == updatedChangePassword.toString(), false);
});
});

}
41 changes: 40 additions & 1 deletion test/data/model/city_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:convert';

import 'package:flutter_bloc_advance/data/models/city.dart';
import 'package:flutter_bloc_advance/main/main_local.mapper.g.dart';
import 'package:flutter_test/flutter_test.dart';

Expand All @@ -18,7 +21,7 @@ void main() {
expect(finalCity.plateCode, '34');
});

test('should copy a City instance with new values (copyWith)', () {
test('should copy a City instance with new values (copyWith) new data', () {
final updatedCity = cityMockPayload.copyWith(
name: 'ankara',
plateCode: '06',
Expand All @@ -29,6 +32,12 @@ void main() {
expect(updatedCity.plateCode, '06');
});

test('should copy a City instance with new values (copyWith)', () {
final updatedCity = cityMockPayload.copyWith();

expect(updatedCity == cityMockPayload, true);
});

test('should compare two City instances', () {
final updatedCity = cityMockPayload.copyWith(
id: 1,
Expand All @@ -39,4 +48,34 @@ void main() {
expect(cityMockPayload == updatedCity, false);
});
});

//fromJson, fromJsonString, toJson, props
group("City Model Json Test", () {
test('should convert City from Json', () {
final json = cityMockPayload.toJson();

final city = City.fromJson(json!);

expect(city?.id, 1);
expect(city?.name, 'istanbul');
expect(city?.plateCode, '34');
});

test('should convert City from JsonString', () {
final jsonString = jsonEncode(cityMockPayload.toJson());

final city = City.fromJsonString(jsonString);

expect(city?.id, 1);
expect(city?.name, 'istanbul');
expect(city?.plateCode, '34');
});

test('should convert City to Json', () {
final json = cityMockPayload.toJson()!;
expect(json['id'], 1);
expect(json['name'], 'istanbul');
expect(json['plateCode'], '34');
});
});
}
23 changes: 23 additions & 0 deletions test/data/model/customer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ void main() {
test('should copy a Customer instance with new values (copyWith)', () {
final finalCustomer = customerMockFullPayload;

final updatedCustomer = finalCustomer.copyWith();

expect(updatedCustomer == finalCustomer, true);
});

test('should copy a Customer instance with new values (copyWith) new values', () {
final finalCustomer = customerMockFullPayload;

final updatedCustomer = finalCustomer.copyWith(
name: 'new Acme',
cityName: 'izmir',
Expand Down Expand Up @@ -74,5 +82,20 @@ void main() {
expect(finalCustomer?.address, 'yazır mh.');
expect(finalCustomer?.active, true);
});

//props
test('props test', () {
final finalCustomer = customerMockFullPayload;
final updatedCustomer = finalCustomer.copyWith();

expect(finalCustomer.props, updatedCustomer.props);
});

//toString
test('should return string', () {
final finalCustomer = customerMockFullPayload;
expect(finalCustomer.toString(),
'Customer(1, Acme, 5055055050, [email protected], Konya, selçuklu, yazır mh., true)');
});
});
}
Loading

0 comments on commit 449dfbc

Please sign in to comment.