
Spec
Mock Data Design Specification
This document describes how to use definitions from the mock directory in .proto files to configure repeatable test data generation rules for fields or interfaces.
mock in the JSONSchema corresponding to (hope.swagger.field) on the field to generate sample data for that field.mock in the response extension of option (hope.swagger.operation) to provide sample data for simple type responses.Main dependencies:
apihug/protobuf/mock/mock.protomock.Mock in apihug/protobuf/swagger/swagger.protomock.Mock uses oneof rule to represent different types of generation rules. Common branches include:
nature: Generate content based on semantic categories (animal names, country names, email, URL, etc.).string_rule: Generate strings by length range and character pool.bool_rule: Control boolean value distribution.number_rule: Generate numbers by value range and decimal places.date_rule: Generate dates/times by age range or time interval.image_rule: Generate image placeholder information with specified width, height, background color, and text.color_rule: Generate color values in different formats (HEX, RGB, RGBA, HSL, name, etc.).chinese_rule: Generate Chinese paragraphs, sentences, words, or titles.chinese_name_rule: Generate Chinese names (first name, last name, full name).china_address_rule: Generate Chinese region/address information.General string fields (name, remarks, code, etc.)
mock: {
string_rule: {
min: 3;
max: 20;
pool: LOWER;
};
}
If there are limited candidate values, use candidates:
mock: {
string_rule: {
candidates: ["PENDING", "DONE"];
};
}
Numeric fields (price, quantity, ID, etc.)
mock: {
number_rule: {
min: 1;
max: 100000;
min_fraction: 0;
max_fraction: 2;
};
}
Boolean fields
Generally no rule needs to be specified; if you prefer a certain value, you can set current:
mock: {
bool_rule: {
current: TRUE;
};
}
Date/time fields (birthday, creation time, expiration time, etc.)
mock: {
date_rule: {
birth_day: {
min_age: 18;
max_age: 60;
};
};
}
mock: {
date_rule: {
time_gap: 30;
time_unit: DAYS;
dir: PAST;
};
}
Chinese text fields (title, body text, phrases, etc.)
mock: {
chinese_rule: {
type: TITLE; // or PARAGRAPH / SENTENCE / WORD
min: 5;
max: 20;
};
}
Chinese names, addresses, etc.
mock: {
chinese_name_rule: {
type: NAME; // FIRST / LAST / NAME
};
}
mock: {
china_address_rule: {
type: ADDRESS; // REGION/PROVINCE/CITY/COUNTY/ADDRESS
with_prefix: TRUE;
};
}
Strings with clear semantics like email, URL, IP, etc.
Recommended to use nature:
mock: { nature: EMAIL; }
mock: { nature: URL; }
mock: { nature: IP4; }
Field-level mock example:
string phone = 1 [(hope.swagger.field) = {
description: "手机号";
example: "13800138000";
mock: {
china_address_rule: {
type: ADDRESS;
};
};
}];
Response-level mock example (simple types):
option (hope.swagger.operation) = {
post: "/ping";
mock: {
string_rule: {
candidates: ["pong", "ok"];
};
};
};
example on the swagger field. Use mock rules when you need large amounts of random data or automated testing.max_items / min_items and Operation’s pageable / input_repeated / out_repeated, focusing mock rules on the generation logic of elements themselves.