Added complex test cases to validate

This commit is contained in:
thomashii@dukefirehawk.com 2023-10-29 14:03:23 +08:00
parent 3afc91edfa
commit 0ca3eb7e01
5 changed files with 70 additions and 8 deletions

View file

@ -1 +0,0 @@
void main() {}

View file

@ -0,0 +1,66 @@
import 'package:angel3_validate/angel3_validate.dart';
import 'package:test/test.dart';
void main() {
final Validator orderItemSchema = Validator({
'id': [isInt, isPositive],
'item_no': isString,
'item_name': isString,
'quantity': isInt,
'description?': isString
});
final Validator orderSchema = Validator({
'id': [isInt, isPositive],
'order_no': isString,
'order_items*': [isList, everyElement(orderItemSchema)]
}, defaultValues: {
'order_items': []
});
group('json data', () {
test('validate with child element', () {
var orderItem = {
'id': 1,
'item_no': 'a1',
'item_name': 'Apple',
'quantity': 1
};
var formData = {
'id': 1,
'order_no': '2',
'order_items': [orderItem]
};
var result = orderSchema.check(formData);
expect(result.errors.isEmpty, true);
});
test('validate empty child', () {
var formData = {'id': 1, 'order_no': '2'};
var result = orderSchema.check(formData);
expect(result.errors.isEmpty, true);
});
test('validate invalid child field', () {
var orderItem = {
'id': 1,
'item_no': 'a1',
'item_name': 'Apple',
'quantity': 1,
'description': 1
};
var formData = {
'id': 1,
'order_no': '2',
'order_items': [orderItem]
};
var result = orderSchema.check(formData);
expect(result.errors.isEmpty, false);
});
});
}

View file

@ -1 +0,0 @@
void main() {}

View file

@ -18,29 +18,27 @@ void printRecord(LogRecord rec) {
} }
void main() { void main() {
Angel? app; late Angel app;
late AngelHttp http; late AngelHttp http;
//TestClient client; //TestClient client;
setUp(() async { setUp(() async {
app = Angel(); app = Angel();
http = AngelHttp(app!, useZone: false); http = AngelHttp(app, useZone: false);
app!.chain([validate(echoSchema)]).post('/echo', app.chain([validate(echoSchema)]).post('/echo',
(RequestContext req, res) async { (RequestContext req, res) async {
await req.parseBody(); await req.parseBody();
res.write('Hello, ${req.bodyAsMap['message']}!'); res.write('Hello, ${req.bodyAsMap['message']}!');
}); });
app!.logger = Logger('angel')..onRecord.listen(printRecord); app.logger = Logger('angel3')..onRecord.listen(printRecord);
//client = await connectTo(app); //client = await connectTo(app);
}); });
tearDown(() async { tearDown(() async {
//await client.close(); //await client.close();
await http.close(); await http.close();
app = null;
//client = null;
}); });
group('echo', () { group('echo', () {