Skip to content

Commit

Permalink
test: contract json roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
eseidel committed Mar 10, 2024
1 parent 76f8726 commit d4598cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/types/lib/src/contract.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:collection/collection.dart';
import 'package:meta/meta.dart';
import 'package:openapi/api.dart' as openapi;
import 'package:types/api.dart';
import 'package:types/types.dart';

/// A contract to deliver goods to a faction.
class Contract {
Expand Down Expand Up @@ -79,11 +80,15 @@ class Contract {

/// Converts the Contract to JSON.
Map<String, dynamic> toJson() {
// Work around OpenAPI not supporting toJson recursion.
final termsJson = terms.toJson();
termsJson['payment'] = (termsJson['payment'] as ContractPayment).toJson();

return <String, dynamic>{
'id': id,
'factionSymbol': factionSymbol,
'type': type.value,
'terms': terms.toJson(),
'terms': termsJson,
'deadlineToAccept': deadlineToAccept.toIso8601String(),
'accepted': accepted,
'fulfilled': fulfilled,
Expand Down
14 changes: 14 additions & 0 deletions packages/types/test/src/contract_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,18 @@ void main() {
expect(fulfill.contractAction, ContractAction.fulfillment);
expect(fulfill.creditsChange, 20);
});

test('Contract json round trip', () {
final now = DateTime(2021);
final contractTerms = ContractTerms(
deadline: now,
payment: ContractPayment(onAccepted: 10, onFulfilled: 20),
);
final contract = Contract.test(id: 'id', terms: contractTerms);

final json = contract.toJson();
final fromJson = Contract.fromJson(json);
// Contract does not currently support equality so compare the json.
expect(fromJson.toJson(), contract.toJson());
});
}

0 comments on commit d4598cb

Please sign in to comment.