-
Notifications
You must be signed in to change notification settings - Fork 5
Defining Test Data
How test data is loaded and defined depends on the data provider. So, if you are using the CSV data provider then you have to follow this data provider's rules and define the test data in CSV. However, there are some rules which are valid for all data providers:
No matter what test data format is used you have to set the entity name, the entity field names and the entity field values. For example the CSV data provider sets the entity name via CSV file name, the entity field names via first row of CSV and the entity field values starting from second row of CSV.
The entity name in the test data should match the entity class name. There are three ways to have a match:
-
The entity name in the test data matches the full name of the entity class. The full name of a class is the fully qualified name of the class, including its namespace but not its assembly (e.g.
MyNamespace.MyEntityClass). -
The entity name in the test data matches the name of the entity class (e.g.
MyEntityClass). -
The entity name in the test data matches the entity name set via entity specific settings
HasEntityName(string entityName).
| Entity Name in Test Data | C# Code | Entity Settings | Match (yes/no) |
|---|---|---|---|
| Domain.Entities.Person |
class Person { } in namespace Domain.Entities
|
yes | |
| Person |
class Person { } in namespace Domain.Entities
|
yes | |
| MyPerson |
class Person { } in namespace Domain.Entities
|
no | |
| MyPerson |
class Person { } in namespace Domain.Entities
|
HasEntityName("MyPerson") |
yes |
The field name of an entity in the test data have to exactly match the field name of the entity class. There is no intelligent logic behind that.
Often an entity consists of nullable fields, e.g. Nullable<int>. To set a nullable field to null you have to keep it empty. CherrySeed recognice that the field is empty and replace it with a real null value during the seeding process.
To set a string field to empty you have to initialize the field with the constant $EMPTY$. By using this constant CherrySeed knows that the string is supposed to be empty and replaces the constant with a real empty string. If you want to change this constant globally to another one you can achieve this by calling the method WithEmptyStringMarker(string marker).
var config = new CherrySeedConfiguration(cfg =>
{
cfg.WithEmptyStringMarker("%MyCustomEmptyString%");
...
});
You also have to define primary key IDs in the test data. You don't have to use "1", "2", "3" as primary key IDs. Instead you can use human-readable IDs like "Person1", "Person2", "Person3" or "Vincent Masuka", "Debra Morgan", "Jesse Katsopolis". These human-readable IDs are replaced by valid primary key IDs during the seeding process. It is a big advantage to have these human-readable IDs because they are used to setup references between entities.
Defining references between entities is also possible. A foreign key is a field of an entity which holds the human-readable primary key ID of another entity. During the seeding process these entity references are resolved automatically.