tdd 진행

This commit is contained in:
2025-08-09 03:09:17 +09:00
parent ed8c7eacba
commit 6033d59590
74 changed files with 17804 additions and 395 deletions
+192
View File
@@ -0,0 +1,192 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/club_model.dart';
void main() {
group('Club 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'id': 'club_id_1',
'name': '볼링 클럽',
'description': '볼링을 사랑하는 사람들의 모임',
'logo': 'https://example.com/logo.png',
'address': '서울시 강남구',
'location': '강남',
'phone': '02-1234-5678',
'email': 'club@example.com',
'website': 'https://example.com',
'memberCount': 50,
'ownerId': 'owner_id_1',
'femaleHandicap': 10,
'averageCalculationPeriod': '3개월',
'createdAt': '2022-01-01T09:00:00.000Z',
'updatedAt': '2022-02-01T15:30:00.000Z',
};
// when
final club = Club.fromJson(json);
// then
expect(club.id, 'club_id_1');
expect(club.name, '볼링 클럽');
expect(club.description, '볼링을 사랑하는 사람들의 모임');
expect(club.logo, 'https://example.com/logo.png');
expect(club.address, '서울시 강남구');
expect(club.location, '강남');
expect(club.phone, '02-1234-5678');
expect(club.email, 'club@example.com');
expect(club.website, 'https://example.com');
expect(club.memberCount, 50);
expect(club.ownerId, 'owner_id_1');
expect(club.femaleHandicap, 10);
expect(club.averageCalculationPeriod, '3개월');
expect(club.createdAt, DateTime.parse('2022-01-01T09:00:00.000Z'));
expect(club.updatedAt, DateTime.parse('2022-02-01T15:30:00.000Z'));
});
test('fromJson 메서드가 phone 필드 대신 contact 필드를 사용할 수 있어야 함', () {
// given
final json = {
'id': 'club_id_1',
'name': '볼링 클럽',
'contact': '02-1234-5678', // phone 대신 contact 사용
};
// when
final club = Club.fromJson(json);
// then
expect(club.phone, '02-1234-5678');
});
test('fromJson 메서드가 일부 필드가 null인 JSON 데이터를 처리해야 함', () {
// given
final json = {
'id': 'club_id_1',
'name': '볼링 클럽',
// 나머지 필드는 null 또는 생략
};
// when
final club = Club.fromJson(json);
// then
expect(club.id, 'club_id_1');
expect(club.name, '볼링 클럽');
expect(club.description, null);
expect(club.logo, null);
expect(club.address, null);
expect(club.location, null);
expect(club.phone, null);
expect(club.email, null);
expect(club.website, null);
expect(club.memberCount, null);
expect(club.ownerId, null);
expect(club.femaleHandicap, null);
expect(club.averageCalculationPeriod, null);
expect(club.createdAt, null);
expect(club.updatedAt, null);
});
test('fromJson 메서드가 필수 필드가 null인 경우 기본값을 설정해야 함', () {
// given
final json = {
// 필수 필드가 null이거나 누락된 경우
'id': null,
'name': null,
};
// when
final club = Club.fromJson(json);
// then
expect(club.id, '');
expect(club.name, '');
});
test('femaleHandicap이 문자열로 제공될 때 int로 변환되어야 함', () {
// given
final json = {
'id': 'club_id_1',
'name': '볼링 클럽',
'femaleHandicap': '15', // 문자열로 제공
};
// when
final club = Club.fromJson(json);
// then
expect(club.femaleHandicap, 15);
});
test('toJson 메서드가 Club 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final club = Club(
id: 'club_id_1',
name: '볼링 클럽',
description: '볼링을 사랑하는 사람들의 모임',
logo: 'https://example.com/logo.png',
address: '서울시 강남구',
location: '강남',
phone: '02-1234-5678',
email: 'club@example.com',
website: 'https://example.com',
memberCount: 50,
ownerId: 'owner_id_1',
femaleHandicap: 10,
averageCalculationPeriod: '3개월',
createdAt: DateTime.parse('2022-01-01T09:00:00.000Z'),
updatedAt: DateTime.parse('2022-02-01T15:30:00.000Z'),
);
// when
final json = club.toJson();
// then
expect(json['id'], 'club_id_1');
expect(json['name'], '볼링 클럽');
expect(json['description'], '볼링을 사랑하는 사람들의 모임');
expect(json['logo'], 'https://example.com/logo.png');
expect(json['address'], '서울시 강남구');
expect(json['location'], '강남');
expect(json['phone'], '02-1234-5678');
expect(json['email'], 'club@example.com');
expect(json['website'], 'https://example.com');
expect(json['memberCount'], 50);
expect(json['ownerId'], 'owner_id_1');
expect(json['femaleHandicap'], 10);
expect(json['averageCalculationPeriod'], '3개월');
expect(json['createdAt'], '2022-01-01T09:00:00.000Z');
expect(json['updatedAt'], '2022-02-01T15:30:00.000Z');
});
test('toJson 메서드가 null 필드를 포함하여 JSON으로 변환해야 함', () {
// given
final club = Club(
id: 'club_id_1',
name: '볼링 클럽',
// 나머지 필드는 null
);
// when
final json = club.toJson();
// then
expect(json['id'], 'club_id_1');
expect(json['name'], '볼링 클럽');
expect(json['description'], null);
expect(json['logo'], null);
expect(json['address'], null);
expect(json['location'], null);
expect(json['phone'], null);
expect(json['email'], null);
expect(json['website'], null);
expect(json['memberCount'], null);
expect(json['ownerId'], null);
expect(json['femaleHandicap'], null);
expect(json['averageCalculationPeriod'], null);
expect(json['createdAt'], null);
expect(json['updatedAt'], null);
});
});
}
+222
View File
@@ -0,0 +1,222 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/event_model.dart';
void main() {
group('Event 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'id': 'event_id_1',
'clubId': 'club_id_1',
'title': '볼링 대회',
'description': '연례 볼링 대회',
'startDate': '2023-01-01T10:00:00.000Z',
'endDate': '2023-01-01T18:00:00.000Z',
'location': '서울 볼링장',
'type': '대회',
'status': '예정됨',
'maxParticipants': 20,
'currentParticipants': 10,
'gameCount': 3,
'participantFee': 15000,
'registrationDeadline': '2022-12-25T23:59:59.000Z',
'publicHash': 'abc123',
'accessPassword': 'pass123',
'isActive': true,
'createdBy': 'admin_id',
'createdAt': '2022-12-01T09:00:00.000Z',
'updatedAt': '2022-12-10T15:30:00.000Z',
};
// when
final event = Event.fromJson(json);
// then
expect(event.id, 'event_id_1');
expect(event.clubId, 'club_id_1');
expect(event.title, '볼링 대회');
expect(event.description, '연례 볼링 대회');
expect(event.startDate, DateTime.parse('2023-01-01T10:00:00.000Z'));
expect(event.endDate, DateTime.parse('2023-01-01T18:00:00.000Z'));
expect(event.location, '서울 볼링장');
expect(event.type, '대회');
expect(event.status, '예정됨');
expect(event.maxParticipants, 20);
expect(event.currentParticipants, 10);
expect(event.gameCount, 3);
expect(event.participantFee, 15000.0);
expect(event.registrationDeadline, DateTime.parse('2022-12-25T23:59:59.000Z'));
expect(event.publicHash, 'abc123');
expect(event.accessPassword, 'pass123');
expect(event.isActive, true);
expect(event.createdBy, 'admin_id');
expect(event.createdAt, DateTime.parse('2022-12-01T09:00:00.000Z'));
expect(event.updatedAt, DateTime.parse('2022-12-10T15:30:00.000Z'));
});
test('fromJson 메서드가 일부 필드가 null인 JSON 데이터를 처리해야 함', () {
// given
final json = {
'id': 'event_id_1',
'clubId': 'club_id_1',
'title': '볼링 대회',
'startDate': '2023-01-01T10:00:00.000Z',
'isActive': true,
// 나머지 필드는 null 또는 생략
};
// when
final event = Event.fromJson(json);
// then
expect(event.id, 'event_id_1');
expect(event.clubId, 'club_id_1');
expect(event.title, '볼링 대회');
expect(event.description, null);
expect(event.startDate, DateTime.parse('2023-01-01T10:00:00.000Z'));
expect(event.endDate, null);
expect(event.location, null);
expect(event.type, null);
expect(event.status, null);
expect(event.maxParticipants, null);
expect(event.currentParticipants, null);
expect(event.gameCount, null);
expect(event.participantFee, null);
expect(event.registrationDeadline, null);
expect(event.publicHash, null);
expect(event.accessPassword, null);
expect(event.isActive, true);
expect(event.createdBy, null);
expect(event.createdAt, null);
expect(event.updatedAt, null);
});
test('fromJson 메서드가 필수 필드가 null인 경우 기본값을 설정해야 함', () {
// given
final json = {
// 필수 필드가 null이거나 누락된 경우
'id': null,
'clubId': null,
'title': null,
'startDate': null,
'isActive': null,
};
// when
final event = Event.fromJson(json);
// then
expect(event.id, '');
expect(event.clubId, '');
expect(event.title, '');
expect(event.startDate.year, DateTime.now().year); // 현재 날짜로 설정되었는지 확인
expect(event.isActive, true); // 기본값이 true로 설정되어야 함
});
test('participantFee가 문자열로 제공될 때 double로 변환되어야 함', () {
// given
final json = {
'id': 'event_id_1',
'clubId': 'club_id_1',
'title': '볼링 대회',
'startDate': '2023-01-01T10:00:00.000Z',
'participantFee': '15000.50', // 문자열로 제공
'isActive': true,
};
// when
final event = Event.fromJson(json);
// then
expect(event.participantFee, 15000.50);
});
test('toJson 메서드가 Event 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final event = Event(
id: 'event_id_1',
clubId: 'club_id_1',
title: '볼링 대회',
description: '연례 볼링 대회',
startDate: DateTime.parse('2023-01-01T10:00:00.000Z'),
endDate: DateTime.parse('2023-01-01T18:00:00.000Z'),
location: '서울 볼링장',
type: '대회',
status: '예정됨',
maxParticipants: 20,
currentParticipants: 10,
gameCount: 3,
participantFee: 15000.0,
registrationDeadline: DateTime.parse('2022-12-25T23:59:59.000Z'),
publicHash: 'abc123',
accessPassword: 'pass123',
isActive: true,
createdBy: 'admin_id',
createdAt: DateTime.parse('2022-12-01T09:00:00.000Z'),
updatedAt: DateTime.parse('2022-12-10T15:30:00.000Z'),
);
// when
final json = event.toJson();
// then
expect(json['id'], 'event_id_1');
expect(json['clubId'], 'club_id_1');
expect(json['title'], '볼링 대회');
expect(json['description'], '연례 볼링 대회');
expect(json['startDate'], '2023-01-01T10:00:00.000Z');
expect(json['endDate'], '2023-01-01T18:00:00.000Z');
expect(json['location'], '서울 볼링장');
expect(json['type'], '대회');
expect(json['status'], '예정됨');
expect(json['maxParticipants'], 20);
expect(json['currentParticipants'], 10);
expect(json['gameCount'], 3);
expect(json['participantFee'], 15000.0);
expect(json['registrationDeadline'], '2022-12-25T23:59:59.000Z');
expect(json['publicHash'], 'abc123');
expect(json['accessPassword'], 'pass123');
expect(json['isActive'], true);
expect(json['createdBy'], 'admin_id');
expect(json['createdAt'], '2022-12-01T09:00:00.000Z');
expect(json['updatedAt'], '2022-12-10T15:30:00.000Z');
});
test('toJson 메서드가 null 필드를 포함하여 JSON으로 변환해야 함', () {
// given
final event = Event(
id: 'event_id_1',
clubId: 'club_id_1',
title: '볼링 대회',
startDate: DateTime.parse('2023-01-01T10:00:00.000Z'),
isActive: true,
// 나머지 필드는 null
);
// when
final json = event.toJson();
// then
expect(json['id'], 'event_id_1');
expect(json['clubId'], 'club_id_1');
expect(json['title'], '볼링 대회');
expect(json['description'], null);
expect(json['startDate'], '2023-01-01T10:00:00.000Z');
expect(json['endDate'], null);
expect(json['location'], null);
expect(json['type'], null);
expect(json['status'], null);
expect(json['maxParticipants'], null);
expect(json['currentParticipants'], null);
expect(json['gameCount'], null);
expect(json['participantFee'], null);
expect(json['registrationDeadline'], null);
expect(json['publicHash'], null);
expect(json['accessPassword'], null);
expect(json['isActive'], true);
expect(json['createdBy'], null);
expect(json['createdAt'], null);
expect(json['updatedAt'], null);
});
});
}
+237
View File
@@ -0,0 +1,237 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/member_model.dart';
void main() {
group('Member 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'id': 'member_id_1',
'userId': 'user_id_1',
'clubId': 'club_id_1',
'name': '홍길동',
'email': 'hong@example.com',
'role': '회원',
'profileImage': 'https://example.com/profile.jpg',
'phone': '010-1234-5678',
'address': '서울시 강남구',
'joinDate': '2022-01-01T09:00:00.000Z',
'isActive': true,
'gender': '남성',
'memberType': '정회원',
'handicap': 10,
'status': '활동중',
'createdAt': '2022-01-01T09:00:00.000Z',
'updatedAt': '2022-02-01T15:30:00.000Z',
};
// when
final member = Member.fromJson(json);
// then
expect(member.id, 'member_id_1');
expect(member.userId, 'user_id_1');
expect(member.clubId, 'club_id_1');
expect(member.name, '홍길동');
expect(member.email, 'hong@example.com');
expect(member.role, '회원');
expect(member.profileImage, 'https://example.com/profile.jpg');
expect(member.phone, '010-1234-5678');
expect(member.address, '서울시 강남구');
expect(member.joinDate, DateTime.parse('2022-01-01T09:00:00.000Z'));
expect(member.isActive, true);
expect(member.gender, '남성');
expect(member.memberType, '정회원');
expect(member.handicap, 10);
expect(member.status, '활동중');
expect(member.createdAt, DateTime.parse('2022-01-01T09:00:00.000Z'));
expect(member.updatedAt, DateTime.parse('2022-02-01T15:30:00.000Z'));
});
test('fromJson 메서드가 일부 필드가 null인 JSON 데이터를 처리해야 함', () {
// given
final json = {
'id': 'member_id_1',
'userId': 'user_id_1',
'clubId': 'club_id_1',
'name': '홍길동',
'email': 'hong@example.com',
'isActive': true,
// 나머지 필드는 null 또는 생략
};
// when
final member = Member.fromJson(json);
// then
expect(member.id, 'member_id_1');
expect(member.userId, 'user_id_1');
expect(member.clubId, 'club_id_1');
expect(member.name, '홍길동');
expect(member.email, 'hong@example.com');
expect(member.role, null);
expect(member.profileImage, null);
expect(member.phone, null);
expect(member.address, null);
expect(member.joinDate, null);
expect(member.isActive, true);
expect(member.gender, null);
expect(member.memberType, null);
expect(member.handicap, null);
expect(member.status, null);
expect(member.createdAt, null);
expect(member.updatedAt, null);
});
test('fromJson 메서드가 필수 필드가 null인 경우 기본값을 설정해야 함', () {
// given
final json = {
// 필수 필드가 null이거나 누락된 경우
'id': null,
'userId': null,
'clubId': null,
'name': null,
'email': null,
'isActive': null,
};
// when
final member = Member.fromJson(json);
// then
expect(member.id, '');
expect(member.userId, '');
expect(member.clubId, '');
expect(member.name, '');
expect(member.email, '');
expect(member.isActive, true); // 기본값이 true로 설정되어야 함
});
test('handicap이 문자열로 제공될 때 int로 변환되어야 함', () {
// given
final json = {
'id': 'member_id_1',
'userId': 'user_id_1',
'clubId': 'club_id_1',
'name': '홍길동',
'email': 'hong@example.com',
'isActive': true,
'handicap': '15', // 문자열로 제공
};
// when
final member = Member.fromJson(json);
// then
expect(member.handicap, 15);
});
test('toJson 메서드가 Member 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final member = Member(
id: 'member_id_1',
userId: 'user_id_1',
clubId: 'club_id_1',
name: '홍길동',
email: 'hong@example.com',
role: '회원',
profileImage: 'https://example.com/profile.jpg',
phone: '010-1234-5678',
address: '서울시 강남구',
joinDate: DateTime.parse('2022-01-01T09:00:00.000Z'),
isActive: true,
gender: '남성',
memberType: '정회원',
handicap: 10,
status: '활동중',
createdAt: DateTime.parse('2022-01-01T09:00:00.000Z'),
updatedAt: DateTime.parse('2022-02-01T15:30:00.000Z'),
);
// when
final json = member.toJson();
// then
expect(json['id'], 'member_id_1');
expect(json['userId'], 'user_id_1');
expect(json['clubId'], 'club_id_1');
expect(json['name'], '홍길동');
expect(json['email'], 'hong@example.com');
expect(json['role'], '회원');
expect(json['profileImage'], 'https://example.com/profile.jpg');
expect(json['phone'], '010-1234-5678');
expect(json['address'], '서울시 강남구');
expect(json['joinDate'], '2022-01-01T09:00:00.000Z');
expect(json['isActive'], true);
expect(json['gender'], '남성');
expect(json['memberType'], '정회원');
expect(json['handicap'], 10);
expect(json['status'], '활동중');
expect(json['createdAt'], '2022-01-01T09:00:00.000Z');
expect(json['updatedAt'], '2022-02-01T15:30:00.000Z');
});
test('toJson 메서드가 null 필드를 포함하여 JSON으로 변환해야 함', () {
// given
final member = Member(
id: 'member_id_1',
userId: 'user_id_1',
clubId: 'club_id_1',
name: '홍길동',
email: 'hong@example.com',
isActive: true,
// 나머지 필드는 null
);
// when
final json = member.toJson();
// then
expect(json['id'], 'member_id_1');
expect(json['userId'], 'user_id_1');
expect(json['clubId'], 'club_id_1');
expect(json['name'], '홍길동');
expect(json['email'], 'hong@example.com');
expect(json['role'], null);
expect(json['profileImage'], null);
expect(json['phone'], null);
expect(json['address'], null);
expect(json['joinDate'], null);
expect(json['isActive'], true);
expect(json['gender'], null);
expect(json['memberType'], null);
expect(json['handicap'], null);
expect(json['status'], null);
expect(json['createdAt'], null);
expect(json['updatedAt'], null);
});
test('copyWith 메서드가 특정 필드만 업데이트된 새 객체를 반환해야 함', () {
// given
final member = Member(
id: 'member_id_1',
userId: 'user_id_1',
clubId: 'club_id_1',
name: '홍길동',
email: 'hong@example.com',
isActive: true,
);
// when
final updatedMember = member.copyWith(
name: '김철수',
email: 'kim@example.com',
role: '관리자',
);
// then
expect(updatedMember.id, 'member_id_1'); // 변경되지 않음
expect(updatedMember.userId, 'user_id_1'); // 변경되지 않음
expect(updatedMember.clubId, 'club_id_1'); // 변경되지 않음
expect(updatedMember.name, '김철수'); // 변경됨
expect(updatedMember.email, 'kim@example.com'); // 변경됨
expect(updatedMember.role, '관리자'); // 변경됨
expect(updatedMember.isActive, true); // 변경되지 않음
});
});
}
+213
View File
@@ -0,0 +1,213 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/score_model.dart';
void main() {
group('Score 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'id': 'test_id',
'memberId': 'test_member_id',
'eventId': 'test_event_id',
'clubId': 'test_club_id',
'frames': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
'totalScore': 55,
'date': '2023-01-01T12:00:00.000Z',
'notes': '테스트 노트',
'participantName': '테스트 참가자',
};
// when
final score = Score.fromJson(json);
// then
expect(score.id, 'test_id');
expect(score.memberId, 'test_member_id');
expect(score.eventId, 'test_event_id');
expect(score.clubId, 'test_club_id');
expect(score.frames, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
expect(score.totalScore, 55);
expect(score.date, DateTime.parse('2023-01-01T12:00:00.000Z'));
expect(score.notes, '테스트 노트');
expect(score.participantName, '테스트 참가자');
});
test('fromJson 메서드가 일부 필드가 null인 JSON 데이터를 처리해야 함', () {
// given
final json = {
'id': 'test_id',
'memberId': 'test_member_id',
'eventId': 'test_event_id',
'clubId': 'test_club_id',
'frames': [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
'totalScore': 55,
'date': '2023-01-01T12:00:00.000Z',
// notes와 participantName은 생략
};
// when
final score = Score.fromJson(json);
// then
expect(score.id, 'test_id');
expect(score.memberId, 'test_member_id');
expect(score.eventId, 'test_event_id');
expect(score.clubId, 'test_club_id');
expect(score.frames, [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
expect(score.totalScore, 55);
expect(score.date, DateTime.parse('2023-01-01T12:00:00.000Z'));
expect(score.notes, null);
expect(score.participantName, null);
});
test('fromJson 메서드가 필수 필드가 null인 경우 기본값을 설정해야 함', () {
// given
final json = {
// 필수 필드가 null이거나 누락된 경우
'id': null,
'memberId': null,
'eventId': null,
'clubId': null,
'frames': null,
'totalScore': null,
'date': null,
};
// when
final score = Score.fromJson(json);
// then
expect(score.id, '');
expect(score.memberId, '');
expect(score.eventId, '');
expect(score.clubId, '');
expect(score.frames, []);
expect(score.totalScore, 0);
expect(score.date.year, DateTime.now().year); // 현재 날짜로 설정되었는지 확인
expect(score.notes, null);
expect(score.participantName, null);
});
test('toJson 메서드가 Score 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final score = Score(
id: 'test_id',
memberId: 'test_member_id',
eventId: 'test_event_id',
clubId: 'test_club_id',
frames: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
totalScore: 55,
date: DateTime.parse('2023-01-01T12:00:00.000Z'),
notes: '테스트 노트',
participantName: '테스트 참가자',
);
// when
final json = score.toJson();
// then
expect(json['id'], 'test_id');
expect(json['memberId'], 'test_member_id');
expect(json['eventId'], 'test_event_id');
expect(json['clubId'], 'test_club_id');
expect(json['frames'], [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
expect(json['totalScore'], 55);
expect(json['date'], '2023-01-01T12:00:00.000Z');
expect(json['notes'], '테스트 노트');
expect(json['participantName'], '테스트 참가자');
});
});
group('ScoreStatistics 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'average': 150.5,
'highScore': 200,
'lowScore': 100,
'gamesPlayed': 10,
'additionalStats': {
'strikes': 5,
'spares': 3,
},
};
// when
final stats = ScoreStatistics.fromJson(json);
// then
expect(stats.average, 150.5);
expect(stats.highScore, 200);
expect(stats.lowScore, 100);
expect(stats.gamesPlayed, 10);
expect(stats.additionalStats?['strikes'], 5);
expect(stats.additionalStats?['spares'], 3);
});
test('fromJson 메서드가 average가 int 타입인 경우도 처리해야 함', () {
// given
final json = {
'average': 150, // int 타입
'highScore': 200,
'lowScore': 100,
'gamesPlayed': 10,
};
// when
final stats = ScoreStatistics.fromJson(json);
// then
expect(stats.average, 150.0); // double 타입으로 변환되어야 함
expect(stats.highScore, 200);
expect(stats.lowScore, 100);
expect(stats.gamesPlayed, 10);
expect(stats.additionalStats, null);
});
test('fromJson 메서드가 필수 필드가 null인 경우 기본값을 설정해야 함', () {
// given
final json = {
// 필수 필드가 null이거나 누락된 경우
'average': null,
'highScore': null,
'lowScore': null,
'gamesPlayed': null,
};
// when
final stats = ScoreStatistics.fromJson(json);
// then
expect(stats.average, 0.0);
expect(stats.highScore, 0);
expect(stats.lowScore, 0);
expect(stats.gamesPlayed, 0);
expect(stats.additionalStats, null);
});
test('toJson 메서드가 ScoreStatistics 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final stats = ScoreStatistics(
average: 150.5,
highScore: 200,
lowScore: 100,
gamesPlayed: 10,
additionalStats: {
'strikes': 5,
'spares': 3,
},
);
// when
final json = stats.toJson();
// then
expect(json['average'], 150.5);
expect(json['highScore'], 200);
expect(json['lowScore'], 100);
expect(json['gamesPlayed'], 10);
expect(json['additionalStats']?['strikes'], 5);
expect(json['additionalStats']?['spares'], 3);
});
});
}
@@ -0,0 +1,449 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:lanebow/models/subscription_model.dart';
void main() {
group('Subscription 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'id': 'sub_123',
'userId': 'user_123',
'clubId': 'club_123',
'planType': 'premium',
'status': 'active',
'startDate': '2023-01-01T00:00:00.000Z',
'endDate': '2024-01-01T00:00:00.000Z',
'price': 19900,
'currency': 'KRW',
'autoRenew': true,
'features': {'feature1': true, 'feature2': false},
'paymentMethod': 'card',
'transactionId': 'tx_123',
'createdAt': '2023-01-01T00:00:00.000Z',
'updatedAt': '2023-01-01T00:00:00.000Z',
};
// when
final subscription = Subscription.fromJson(json);
// then
expect(subscription.id, 'sub_123');
expect(subscription.userId, 'user_123');
expect(subscription.clubId, 'club_123');
expect(subscription.planType, SubscriptionPlanType.premium);
expect(subscription.status, SubscriptionStatus.active);
expect(subscription.startDate, DateTime.parse('2023-01-01T00:00:00.000Z'));
expect(subscription.endDate, DateTime.parse('2024-01-01T00:00:00.000Z'));
expect(subscription.price, 19900);
expect(subscription.currency, 'KRW');
expect(subscription.autoRenew, true);
expect(subscription.features, {'feature1': true, 'feature2': false});
expect(subscription.paymentMethod, 'card');
expect(subscription.transactionId, 'tx_123');
expect(subscription.createdAt, DateTime.parse('2023-01-01T00:00:00.000Z'));
expect(subscription.updatedAt, DateTime.parse('2023-01-01T00:00:00.000Z'));
});
test('fromJson 메서드가 알 수 없는 planType과 status를 기본값으로 처리해야 함', () {
// given
final json = {
'id': 'sub_123',
'userId': 'user_123',
'planType': 'unknown_plan',
'status': 'unknown_status',
'startDate': '2023-01-01T00:00:00.000Z',
'endDate': '2024-01-01T00:00:00.000Z',
'price': 19900,
'currency': 'KRW',
'autoRenew': true,
'createdAt': '2023-01-01T00:00:00.000Z',
'updatedAt': '2023-01-01T00:00:00.000Z',
};
// when
final subscription = Subscription.fromJson(json);
// then
expect(subscription.planType, SubscriptionPlanType.free); // 기본값
expect(subscription.status, SubscriptionStatus.expired); // 기본값
});
test('toJson 메서드가 Subscription 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final subscription = Subscription(
id: 'sub_123',
userId: 'user_123',
clubId: 'club_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.parse('2023-01-01T00:00:00.000Z'),
endDate: DateTime.parse('2024-01-01T00:00:00.000Z'),
price: 19900,
currency: 'KRW',
autoRenew: true,
features: {'feature1': true, 'feature2': false},
paymentMethod: 'card',
transactionId: 'tx_123',
createdAt: DateTime.parse('2023-01-01T00:00:00.000Z'),
updatedAt: DateTime.parse('2023-01-01T00:00:00.000Z'),
);
// when
final json = subscription.toJson();
// then
expect(json['id'], 'sub_123');
expect(json['userId'], 'user_123');
expect(json['clubId'], 'club_123');
expect(json['planType'], 'premium');
expect(json['status'], 'active');
expect(json['startDate'], '2023-01-01T00:00:00.000Z');
expect(json['endDate'], '2024-01-01T00:00:00.000Z');
expect(json['price'], 19900);
expect(json['currency'], 'KRW');
expect(json['autoRenew'], true);
expect(json['features'], {'feature1': true, 'feature2': false});
expect(json['paymentMethod'], 'card');
expect(json['transactionId'], 'tx_123');
expect(json['createdAt'], '2023-01-01T00:00:00.000Z');
expect(json['updatedAt'], '2023-01-01T00:00:00.000Z');
});
test('isActive getter가 올바르게 동작해야 함', () {
// given
final activeSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.now().subtract(const Duration(days: 10)),
endDate: DateTime.now().add(const Duration(days: 10)),
price: 19900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final canceledSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.canceled,
startDate: DateTime.now().subtract(const Duration(days: 10)),
endDate: DateTime.now().add(const Duration(days: 10)),
price: 19900,
currency: 'KRW',
autoRenew: false,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
// then
expect(activeSubscription.isActive, true);
expect(canceledSubscription.isActive, false);
});
test('isExpired getter가 올바르게 동작해야 함', () {
// given
final expiredByStatus = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.expired,
startDate: DateTime.now().subtract(const Duration(days: 20)),
endDate: DateTime.now().add(const Duration(days: 10)), // 아직 만료일이 지나지 않았지만 상태가 expired
price: 19900,
currency: 'KRW',
autoRenew: false,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final expiredByDate = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.now().subtract(const Duration(days: 20)),
endDate: DateTime.now().subtract(const Duration(days: 1)), // 만료일이 지남
price: 19900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final activeSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.now().subtract(const Duration(days: 10)),
endDate: DateTime.now().add(const Duration(days: 10)),
price: 19900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
// then
expect(expiredByStatus.isExpired, true);
expect(expiredByDate.isExpired, true);
expect(activeSubscription.isExpired, false);
});
test('daysLeft getter가 올바르게 동작해야 함', () {
// given
final expiredSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.expired,
startDate: DateTime.now().subtract(const Duration(days: 20)),
endDate: DateTime.now().add(const Duration(days: 10)),
price: 19900,
currency: 'KRW',
autoRenew: false,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final activeSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.now().subtract(const Duration(days: 10)),
endDate: DateTime.now().add(const Duration(days: 10)),
price: 19900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
// then
expect(expiredSubscription.daysLeft, 0); // 만료된 구독은 항상 0일 반환
expect(activeSubscription.daysLeft >= 9 && activeSubscription.daysLeft <= 10, true); // 테스트 실행 시점에 따라 9일 또는 10일이 될 수 있음
});
test('planName getter가 올바르게 동작해야 함', () {
// given
final freeSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.free,
status: SubscriptionStatus.active,
startDate: DateTime.now(),
endDate: DateTime.now().add(const Duration(days: 30)),
price: 0,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final basicSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.basic,
status: SubscriptionStatus.active,
startDate: DateTime.now(),
endDate: DateTime.now().add(const Duration(days: 30)),
price: 9900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final premiumSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.premium,
status: SubscriptionStatus.active,
startDate: DateTime.now(),
endDate: DateTime.now().add(const Duration(days: 30)),
price: 19900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
final enterpriseSubscription = Subscription(
id: 'sub_123',
userId: 'user_123',
planType: SubscriptionPlanType.enterprise,
status: SubscriptionStatus.active,
startDate: DateTime.now(),
endDate: DateTime.now().add(const Duration(days: 30)),
price: 49900,
currency: 'KRW',
autoRenew: true,
createdAt: DateTime.now(),
updatedAt: DateTime.now(),
);
// then
expect(freeSubscription.planName, '무료');
expect(basicSubscription.planName, '기본');
expect(premiumSubscription.planName, '프리미엄');
expect(enterpriseSubscription.planName, '기업용');
});
});
group('SubscriptionPlan 모델 테스트', () {
test('fromJson 메서드가 JSON 데이터를 올바르게 변환해야 함', () {
// given
final json = {
'type': 'premium',
'name': '프리미엄',
'description': '대규모 클럽을 위한 고급 기능',
'monthlyPrice': 19900,
'yearlyPrice': 199000,
'currency': 'KRW',
'features': ['무제한 회원 관리', '고급 통계 및 분석'],
'maxMembers': 100,
'maxEvents': 100,
'hasAdvancedStats': true,
'hasCustomization': true,
'hasPriority': true,
};
// when
final plan = SubscriptionPlan.fromJson(json);
// then
expect(plan.type, SubscriptionPlanType.premium);
expect(plan.name, '프리미엄');
expect(plan.description, '대규모 클럽을 위한 고급 기능');
expect(plan.monthlyPrice, 19900);
expect(plan.yearlyPrice, 199000);
expect(plan.currency, 'KRW');
expect(plan.features, ['무제한 회원 관리', '고급 통계 및 분석']);
expect(plan.maxMembers, 100);
expect(plan.maxEvents, 100);
expect(plan.hasAdvancedStats, true);
expect(plan.hasCustomization, true);
expect(plan.hasPriority, true);
});
test('fromJson 메서드가 알 수 없는 type을 기본값으로 처리해야 함', () {
// given
final json = {
'type': 'unknown_type',
'name': '알 수 없는 플랜',
'description': '설명',
'monthlyPrice': 0,
'yearlyPrice': 0,
'currency': 'KRW',
'features': [],
'maxMembers': 0,
'maxEvents': 0,
'hasAdvancedStats': false,
'hasCustomization': false,
'hasPriority': false,
};
// when
final plan = SubscriptionPlan.fromJson(json);
// then
expect(plan.type, SubscriptionPlanType.free); // 기본값
});
test('fromJson 메서드가 부울 필드에 대한 기본값을 설정해야 함', () {
// given
final json = {
'type': 'basic',
'name': '기본',
'description': '설명',
'monthlyPrice': 9900,
'yearlyPrice': 99000,
'currency': 'KRW',
'features': ['기능1', '기능2'],
'maxMembers': 30,
'maxEvents': 20,
// 부울 필드 생략
};
// when
final plan = SubscriptionPlan.fromJson(json);
// then
expect(plan.hasAdvancedStats, false); // 기본값
expect(plan.hasCustomization, false); // 기본값
expect(plan.hasPriority, false); // 기본값
});
test('toJson 메서드가 SubscriptionPlan 객체를 JSON으로 올바르게 변환해야 함', () {
// given
final plan = SubscriptionPlan(
type: SubscriptionPlanType.premium,
name: '프리미엄',
description: '대규모 클럽을 위한 고급 기능',
monthlyPrice: 19900,
yearlyPrice: 199000,
currency: 'KRW',
features: ['무제한 회원 관리', '고급 통계 및 분석'],
maxMembers: 100,
maxEvents: 100,
hasAdvancedStats: true,
hasCustomization: true,
hasPriority: true,
);
// when
final json = plan.toJson();
// then
expect(json['type'], 'premium');
expect(json['name'], '프리미엄');
expect(json['description'], '대규모 클럽을 위한 고급 기능');
expect(json['monthlyPrice'], 19900);
expect(json['yearlyPrice'], 199000);
expect(json['currency'], 'KRW');
expect(json['features'], ['무제한 회원 관리', '고급 통계 및 분석']);
expect(json['maxMembers'], 100);
expect(json['maxEvents'], 100);
expect(json['hasAdvancedStats'], true);
expect(json['hasCustomization'], true);
expect(json['hasPriority'], true);
});
test('getDefaultPlans 메서드가 기본 플랜 목록을 반환해야 함', () {
// when
final plans = SubscriptionPlan.getDefaultPlans();
// then
expect(plans.length, 4); // 무료, 기본, 프리미엄, 기업용 4가지 플랜
// 무료 플랜 확인
final freePlan = plans.firstWhere((p) => p.type == SubscriptionPlanType.free);
expect(freePlan.name, '무료');
expect(freePlan.monthlyPrice, 0);
expect(freePlan.maxMembers, 10);
// 기본 플랜 확인
final basicPlan = plans.firstWhere((p) => p.type == SubscriptionPlanType.basic);
expect(basicPlan.name, '기본');
expect(basicPlan.monthlyPrice, 9900);
expect(basicPlan.maxMembers, 30);
// 프리미엄 플랜 확인
final premiumPlan = plans.firstWhere((p) => p.type == SubscriptionPlanType.premium);
expect(premiumPlan.name, '프리미엄');
expect(premiumPlan.monthlyPrice, 19900);
expect(premiumPlan.maxMembers, 100);
// 기업용 플랜 확인
final enterprisePlan = plans.firstWhere((p) => p.type == SubscriptionPlanType.enterprise);
expect(enterprisePlan.name, '기업용');
expect(enterprisePlan.monthlyPrice, 49900);
expect(enterprisePlan.maxMembers, 1000);
});
});
}