Added test for json schema
This commit is contained in:
@@ -138,7 +138,7 @@ class DataTransferService {
|
|||||||
final jsonString = await _readFileContent(path.files.single);
|
final jsonString = await _readFileContent(path.files.single);
|
||||||
if (jsonString == null) return ImportResult.fileReadError;
|
if (jsonString == null) return ImportResult.fileReadError;
|
||||||
|
|
||||||
final isValid = await _validateJsonSchema(jsonString);
|
final isValid = await validateJsonSchema(jsonString);
|
||||||
if (!isValid) return ImportResult.invalidSchema;
|
if (!isValid) return ImportResult.invalidSchema;
|
||||||
|
|
||||||
final decoded = json.decode(jsonString) as Map<String, dynamic>;
|
final decoded = json.decode(jsonString) as Map<String, dynamic>;
|
||||||
@@ -321,7 +321,8 @@ class DataTransferService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Validates the given JSON string against the predefined schema.
|
/// Validates the given JSON string against the predefined schema.
|
||||||
static Future<bool> _validateJsonSchema(String jsonString) async {
|
@visibleForTesting
|
||||||
|
static Future<bool> validateJsonSchema(String jsonString) async {
|
||||||
final String schemaString;
|
final String schemaString;
|
||||||
|
|
||||||
schemaString = await rootBundle.loadString('assets/schema.json');
|
schemaString = await rootBundle.loadString('assets/schema.json');
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ void main() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
group('Specific data', () {
|
group('Checking specific data', () {
|
||||||
testWidgets('Player data is correct', (tester) async {
|
testWidgets('Player data is correct', (tester) async {
|
||||||
await database.playerDao.addPlayer(player: testPlayer1);
|
await database.playerDao.addPlayer(player: testPlayer1);
|
||||||
|
|
||||||
@@ -798,5 +798,71 @@ void main() {
|
|||||||
expect(matches[0].endedAt, endedDate);
|
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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user