From 022c2e2263bb754a2c21f2b80015cee97efa6f49 Mon Sep 17 00:00:00 2001 From: f3ath Date: Sun, 12 Nov 2023 10:36:39 -0800 Subject: [PATCH] 100% test coverage --- .github/workflows/dart.yml | 2 +- lib/src/document/new_resource.dart | 1 - test/unit/document/inbound_document_test.dart | 14 +++++++++++ test/unit/document/new_resource_test.dart | 24 +++++++++++++++++++ test/unit/document/new_to_one_test.dart | 15 ++++++++++++ test/unit/document/payload.dart | 24 +++++++++++++++++++ 6 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 test/unit/document/new_to_one_test.dart diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index f8c2d35..0297fc1 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -25,4 +25,4 @@ jobs: - name: Tests run: dart test --coverage=.coverage -j1 - name: Coverage - run: dart run coverage:format_coverage -l -c -i .coverage --report-on=lib | dart run check_coverage:check_coverage 98 + run: dart run coverage:format_coverage -l -c -i .coverage --report-on=lib | dart run check_coverage:check_coverage diff --git a/lib/src/document/new_resource.dart b/lib/src/document/new_resource.dart index b111ab7..e79577d 100644 --- a/lib/src/document/new_resource.dart +++ b/lib/src/document/new_resource.dart @@ -61,7 +61,6 @@ class NewResource { if (r is NewToMany) { return ToMany(r.map((identifier) => _toIdentifier(identifier, id))); } - // coverage:ignore-line throw StateError('Unexpected relationship type: ${r.runtimeType}'); } diff --git a/test/unit/document/inbound_document_test.dart b/test/unit/document/inbound_document_test.dart index 0123e38..d5b4c22 100644 --- a/test/unit/document/inbound_document_test.dart +++ b/test/unit/document/inbound_document_test.dart @@ -188,6 +188,20 @@ void main() { expect(() => InboundDocument(payload.many).asToOne(), throwsFormatException); }); + + test('throws on invalid new relationship', () { + expect( + () => InboundDocument(payload.newResourceInvalidRelationship) + .dataAsNewResource(), + throwsFormatException); + }); + + test('throws on incomplete new relationship', () { + expect( + () => InboundDocument(payload.newResourceIncompleteRelationship) + .dataAsNewResource(), + throwsFormatException); + }); }); }); } diff --git a/test/unit/document/new_resource_test.dart b/test/unit/document/new_resource_test.dart index 1e0cd9d..eba069e 100644 --- a/test/unit/document/new_resource_test.dart +++ b/test/unit/document/new_resource_test.dart @@ -5,6 +5,30 @@ import 'package:test/test.dart'; void main() { group('NewResource', () { + test('toResource throws on unmatched local id in "many"', () { + final resource = NewResource('test_type', id: 'test_id', lid: 'test_lid') + ..relationships['many'] = NewToMany([ + LocalIdentifier('test_type', 'test_lid2'), + ]); + + expect(() => resource.toResource(() => 'my-test-id'), throwsStateError); + }); + + test('toResource throws on unmatched local id in "one"', () { + final resource = NewResource('test_type', id: 'test_id', lid: 'test_lid') + ..relationships['one'] = + NewToOne(LocalIdentifier('test_type', 'test_lid2')); + + expect(() => resource.toResource(() => 'my-test-id'), throwsStateError); + }); + + test('toResource throws on invalid relationship', () { + final resource = NewResource('test_type', id: 'test_id') + ..relationships['many'] = NewRelationship(); + + expect(() => resource.toResource(() => 'my-test-id'), throwsStateError); + }); + test('json encoding', () { expect(jsonEncode(NewResource('test_type')), jsonEncode({'type': 'test_type'})); diff --git a/test/unit/document/new_to_one_test.dart b/test/unit/document/new_to_one_test.dart new file mode 100644 index 0000000..6853d3a --- /dev/null +++ b/test/unit/document/new_to_one_test.dart @@ -0,0 +1,15 @@ +import 'package:json_api/document.dart'; +import 'package:test/expect.dart'; +import 'package:test/scaffolding.dart'; + +void main() { + group('NewToOne', () { + test('can be iterated', () { + final id = Identifier('books', '123'); + final r = NewToOne(id); + final list = []; + list.addAll(r); + expect(list.single, equals(id)); + }); + }); +} diff --git a/test/unit/document/payload.dart b/test/unit/document/payload.dart index c7d6cb0..9653ac3 100644 --- a/test/unit/document/payload.dart +++ b/test/unit/document/payload.dart @@ -147,3 +147,27 @@ final resource = { } } }; + +final newResourceInvalidRelationship = { + 'data': { + 'type': 'articles', + 'attributes': {'title': 'JSON:API paints my bikeshed!'}, + 'relationships': { + 'author': { + 'data': true, + }, + } + } +}; + +final newResourceIncompleteRelationship = { + 'data': { + 'type': 'articles', + 'attributes': {'title': 'JSON:API paints my bikeshed!'}, + 'relationships': { + 'author': { + 'data': {'type': 'person'}, + }, + } + } +};