Skip to content

Commit

Permalink
100% test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
f3ath committed Nov 12, 2023
1 parent 0c1a437 commit 022c2e2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/dart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 0 additions & 1 deletion lib/src/document/new_resource.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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}');
}

Expand Down
14 changes: 14 additions & 0 deletions test/unit/document/inbound_document_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
}
24 changes: 24 additions & 0 deletions test/unit/document/new_resource_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'}));
Expand Down
15 changes: 15 additions & 0 deletions test/unit/document/new_to_one_test.dart
Original file line number Diff line number Diff line change
@@ -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 = <NewIdentifier>[];
list.addAll(r);
expect(list.single, equals(id));
});
});
}
24 changes: 24 additions & 0 deletions test/unit/document/payload.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'},
},
}
}
};

0 comments on commit 022c2e2

Please sign in to comment.