-
Notifications
You must be signed in to change notification settings - Fork 153
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Datafaker annotations #675
Comments
Something like:
And then:
? |
I like this idea, but I'll be honest I don't know much about custom annotations. |
kind of @Fake(country="nl", language="nl", seed = 31415)
class Person {
@Provider(expression="#{...faker expression here ...}")
private String firstname;
@MethodProvider("methodToUse")
private Address address;
}
|
would these annotations stay in the domain class? I believe it would be polluted if that same class has other annotations, but I liked the idea |
in the class which will be faked. |
I don't really see the need for the top level annotation but yes, I think this could be helpful for classes which are used for generating test data only. |
The idea is that you create a new annotation class with some fields, just like a normal class, and using reflection you can check if the class has the annotation, get access to it and its fields, and handle them how you see fit. It's less magic than it seems. |
They would, but I wouldn't recommend mixing production classes, like hibernate or Jackson annotated classes with classes typically used for testing, like Datafaker classes. But that depends mostly on the use case I guess. |
yes, probably top level annotation could be replaced by fields level. |
What should the expression look like?:
|
could be extra attributes for annotations with some predefined defaults
same as mentioned here https://www.datafaker.net/documentation/expressions/ |
I would like to implement its first version, I already have a small prototype. I will create pr as soon as possible to continue the discussion. |
go ahead @RVRhub , looking forward to see it in datafaker |
After having several chats and POCs with @RVRhub we faced 2 issues:
To address these issues we decided to have only one annotation The idea is to build the solution based on java transformers approach #513 which already can generate objects. So the objects could be generated in 2 ways: MyClass object = Faker.populate(MyClass.class); this will generate an object with usage of default schema mentioned by MyClass object = Faker.populate(MyClass.class, myschema); this will allow to generate objects based on others schemas. In this way it's possible to have a number of different schemas and generate objects based on them. |
merged with #754 |
// cc @eliasnogueira since it could be an alternative to several builders |
Thank you @snuyanzin for pinging me. To highlight the differences we could have, as the builder alternative. Alternative 1 (without using this feature)Usage of the Model class that implements the Builder pattern. The faker user is used directly to the attribute/field. Preconditions
Advantages
Disadvantages
public class SimulationDataFactory {
public static Simulation newSimulation() {
return new SimulationBuilder().
name(faker.name().nameWithMiddle()).
cpf(faker.cpf().valid()).
email(faker.internet().emailAddress()).
amount(new BigDecimal(faker.number().numberBetween(MIN_AMOUNT, MAX_AMOUNT))).
installments(faker.number().numberBetween(MIN_INSTALLMENTS, MAX_INSTALLMENTS)).
insurance(faker.bool().bool()).build();
}
} Alternative 2 (using this feature)Preconditions
Advantages
Disadvantages
public class SimulationDataFactory {
public static Simulation newSimulation() {
Schema<Object, ?> schema = Schema.of(
field("name", () -> faker.name().nameWithMiddle()),
field("cpf", () -> faker.cpf().valid()),
field("email", () -> faker.internet().emailAddress()),
field("amount", () -> new BigDecimal(faker.number().numberBetween(MIN_AMOUNT, MAX_AMOUNT))),
field("installments", () -> faker.number().numberBetween(MIN_INSTALLMENTS, MAX_INSTALLMENTS)),
field("insurance", () -> faker.bool().bool())
);
return (Simulation) new JavaObjectTransformer().apply(Simulation.class, schema);
}
} |
thanks for the summary @eliasnogueira a minor comment about in fact it can be used public class SimulationDataFactory {
public static Simulation newSimulation() {
Schema<Object, ?> schema = Schema.of(
field("name", () -> faker.name().nameWithMiddle()),
field("cpf", () -> faker.cpf().valid()),
field("email", () -> faker.internet().emailAddress()),
field("amount", () -> new BigDecimal(faker.number().numberBetween(MIN_AMOUNT, MAX_AMOUNT))),
field("installments", () -> faker.number().numberBetween(MIN_INSTALLMENTS, MAX_INSTALLMENTS)),
field("insurance", () -> faker.bool().bool())
);
return Faker.populate(Simulation.class, schema);
}
} |
@snuyanzin Not yet as it's not available in the 1.8.1 :-( |
Currently this is just an idea out loud. So feel free to criticize/propose improvements or something completely new.
The idea is to have
The text was updated successfully, but these errors were encountered: