Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functionality for converting Trial models to json #73

Merged
merged 4 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/models/trial.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:json_annotation/json_annotation.dart';

import 'trial_type.dart';

part 'trial.g.dart';

/// Represents the data of a single trial.
@JsonSerializable()
class Trial {
/// Unique identifier for the participant
final String participantID;
Expand Down Expand Up @@ -34,4 +39,9 @@ class Trial {
return "Trial(participantID: $participantID, sessionID: $sessionID, "
"trialType: $trialType, stim: $stim, response: $response)";
}

/// Convert the [Trial] object to its json representation.
/// This method is particularly useful when uploading data to Firebase and
/// similar no-sql dbs.
Map<String, dynamic> toJson() => _$TrialToJson(this);
}
28 changes: 28 additions & 0 deletions lib/models/trial.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions test/models/trial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,21 @@ void main() {
"response: ${trial.response})";
expect(trial.toString(), strRep);
});

test(
"Trial.toJson returns a valid json representation",
() {
final Trial trial = Trial(
participantID: '101',
sessionID: '001',
trialType: TrialType.practice,
stim: 'stimuli',
response: 'participant response',
);

final Map<String, dynamic> trialJson = trial.toJson();

expect(trialJson, isMap);
},
);
}
Loading