From 723699d363e280df35cda6327bb198619b5774ac Mon Sep 17 00:00:00 2001 From: Felix Kirchner Date: Sun, 12 Apr 2026 01:32:21 +0200 Subject: [PATCH] Added test for json schema --- lib/services/data_transfer_service.dart | 5 +- test/services/data_transfer_service_test.dart | 68 ++++++++++++++++++- 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/lib/services/data_transfer_service.dart b/lib/services/data_transfer_service.dart index d868722..6c7455d 100644 --- a/lib/services/data_transfer_service.dart +++ b/lib/services/data_transfer_service.dart @@ -138,7 +138,7 @@ class DataTransferService { final jsonString = await _readFileContent(path.files.single); if (jsonString == null) return ImportResult.fileReadError; - final isValid = await _validateJsonSchema(jsonString); + final isValid = await validateJsonSchema(jsonString); if (!isValid) return ImportResult.invalidSchema; final decoded = json.decode(jsonString) as Map; @@ -321,7 +321,8 @@ class DataTransferService { } /// Validates the given JSON string against the predefined schema. - static Future _validateJsonSchema(String jsonString) async { + @visibleForTesting + static Future validateJsonSchema(String jsonString) async { final String schemaString; schemaString = await rootBundle.loadString('assets/schema.json'); diff --git a/test/services/data_transfer_service_test.dart b/test/services/data_transfer_service_test.dart index e5f2860..f156b52 100644 --- a/test/services/data_transfer_service_test.dart +++ b/test/services/data_transfer_service_test.dart @@ -192,7 +192,7 @@ void main() { }); }); - group('Specific data', () { + group('Checking specific data', () { testWidgets('Player data is correct', (tester) async { await database.playerDao.addPlayer(player: testPlayer1); @@ -798,5 +798,71 @@ void main() { expect(matches[0].endedAt, endedDate); }); }); + + testWidgets('validateJsonSchema()', (tester) async { + final validJson = json.encode({ + 'players': [ + { + 'id': testPlayer1.id, + 'name': testPlayer1.name, + 'description': testPlayer1.description, + 'createdAt': testPlayer1.createdAt.toIso8601String(), + }, + ], + 'games': [ + { + 'id': testGame.id, + 'name': testGame.name, + 'ruleset': testGame.ruleset.name, + 'description': testGame.description, + 'color': testGame.color.name, + 'icon': testGame.icon, + 'createdAt': testGame.createdAt.toIso8601String(), + }, + ], + 'groups': [ + { + 'id': testGroup.id, + 'name': testGroup.name, + 'description': testGroup.description, + 'memberIds': [testPlayer1.id, testPlayer2.id], + 'createdAt': testGroup.createdAt.toIso8601String(), + }, + ], + 'teams': [ + { + 'id': testTeam.id, + 'name': testTeam.name, + 'memberIds': [testPlayer1.id, testPlayer2.id], + 'createdAt': testTeam.createdAt.toIso8601String(), + }, + ], + 'matches': [ + { + 'id': testMatch.id, + 'name': testMatch.name, + 'gameId': testGame.id, + 'groupId': testGroup.id, + 'playerIds': [testPlayer1.id, testPlayer2.id], + 'notes': testMatch.notes, + 'scores': { + testPlayer1.id: [ + {'roundNumber': 1, 'score': 10, 'change': 10}, + {'roundNumber': 2, 'score': 20, 'change': 10}, + ], + testPlayer2.id: [ + {'roundNumber': 1, 'score': 15, 'change': 15}, + {'roundNumber': 2, 'score': 25, 'change': 10}, + ], + }, + 'createdAt': testMatch.createdAt.toIso8601String(), + 'endedAt': null, + }, + ], + }); + + final isValid = await DataTransferService.validateJsonSchema(validJson); + expect(isValid, true); + }); }); }