Files
bowlingManager/mobile/test/models/club_model_test.dart
T
2025-08-09 03:09:17 +09:00

193 lines
6.1 KiB
Dart

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);
});
});
}