Skip to content

Commit

Permalink
fixup! feat: improve DataSchemaValue handling
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 23, 2023
1 parent a417842 commit 0e3c67b
Showing 1 changed file with 103 additions and 106 deletions.
209 changes: 103 additions & 106 deletions test/core/content_serdes_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,112 +17,109 @@ Content _getTestContent(String input) {

void main() {
group('Content Serdes Tests', () {
setUp(() {
// Additional setup goes here.
test('Content Validation', () async {
final contentSerdes = ContentSerdes();

final testContent1 = _getTestContent('42');
final successfulSchema = DataSchema.fromJson(
<String, dynamic>{'type': 'number'},
PrefixMapping(),
);

expect(
await contentSerdes.contentToValue(testContent1, successfulSchema),
42,
);

final testContent2 = _getTestContent('42');
final failingSchema = DataSchema.fromJson(
<String, dynamic>{'type': 'string'},
PrefixMapping(),
);

expect(
contentSerdes.contentToValue(testContent2, failingSchema),
throwsA(const TypeMatcher<ContentSerdesException>()),
);

expect(
() =>
contentSerdes.valueToContent(42, failingSchema, 'application/json'),
throwsA(const TypeMatcher<ContentSerdesException>()),
);

final testContent3 = _getTestContent('');
expect(
await contentSerdes.contentToValue(testContent3, null),
null,
);
});
test('Codec Registration', () async {
final contentSerdes = ContentSerdes();

expect(
contentSerdes.supportedMediaTypes,
['application/json', 'application/cbor', 'application/link-format'],
);

expect(
contentSerdes.offeredMediaTypes,
['application/json', 'application/cbor'],
);

expect(
() => contentSerdes.addOfferedMediaType('application/xml'),
throwsArgumentError,
);

contentSerdes.addOfferedMediaType('application/td+json; charset=utf-8');

expect(
contentSerdes.offeredMediaTypes,
[
'application/json',
'application/cbor',
'application/td+json; charset=utf-8',
],
);

contentSerdes.removeOfferedMediaType('application/json');

expect(
contentSerdes.offeredMediaTypes,
[
'application/cbor',
'application/td+json; charset=utf-8',
],
);

contentSerdes
..assignCodec('application/xml', JsonCodec())
..addOfferedMediaType('application/xml');

expect(
contentSerdes.supportedMediaTypes,
[
'application/json',
'application/cbor',
'application/link-format',
'application/xml',
],
);

expect(
contentSerdes.offeredMediaTypes,
[
'application/cbor',
'application/td+json; charset=utf-8',
'application/xml',
],
);

expect(
() => contentSerdes.assignCodec('foo', JsonCodec()),
throwsArgumentError,
);
});
});

test('Content Validation', () async {
final contentSerdes = ContentSerdes();

final testContent1 = _getTestContent('42');
final successfulSchema = DataSchema.fromJson(
<String, dynamic>{'type': 'number'},
PrefixMapping(),
);

expect(
await contentSerdes.contentToValue(testContent1, successfulSchema),
42,
);

final testContent2 = _getTestContent('42');
final failingSchema = DataSchema.fromJson(
<String, dynamic>{'type': 'string'},
PrefixMapping(),
);

expect(
contentSerdes.contentToValue(testContent2, failingSchema),
throwsA(const TypeMatcher<ContentSerdesException>()),
);

expect(
() => contentSerdes.valueToContent(42, failingSchema, 'application/json'),
throwsA(const TypeMatcher<ContentSerdesException>()),
);

final testContent3 = _getTestContent('');
expect(
await contentSerdes.contentToValue(testContent3, null),
null,
);
});
test('Codec Registration', () async {
final contentSerdes = ContentSerdes();

expect(
contentSerdes.supportedMediaTypes,
['application/json', 'application/cbor', 'application/link-format'],
);

expect(
contentSerdes.offeredMediaTypes,
['application/json', 'application/cbor'],
);

expect(
() => contentSerdes.addOfferedMediaType('application/xml'),
throwsArgumentError,
);

contentSerdes.addOfferedMediaType('application/td+json; charset=utf-8');

expect(
contentSerdes.offeredMediaTypes,
[
'application/json',
'application/cbor',
'application/td+json; charset=utf-8',
],
);

contentSerdes.removeOfferedMediaType('application/json');

expect(
contentSerdes.offeredMediaTypes,
[
'application/cbor',
'application/td+json; charset=utf-8',
],
);

contentSerdes
..assignCodec('application/xml', JsonCodec())
..addOfferedMediaType('application/xml');

expect(
contentSerdes.supportedMediaTypes,
[
'application/json',
'application/cbor',
'application/link-format',
'application/xml',
],
);

expect(
contentSerdes.offeredMediaTypes,
[
'application/cbor',
'application/td+json; charset=utf-8',
'application/xml',
],
);

expect(
() => contentSerdes.assignCodec('foo', JsonCodec()),
throwsArgumentError,
);
});
}

0 comments on commit 0e3c67b

Please sign in to comment.