Giter VIP home page Giter VIP logo

dataforge's Introduction

Data Forge

This is a Java library that helps generate test data

First, you add this to your pom.xml:

<dependency>
  <groupId>ru.ziplla</groupId>
  <artifactId>dataforge</artifactId>
  <version>1.0.0</version>
</dependency>

Then, you use it like this:

import ru.ziplla.dataforge.constraints.Constraints;
import ru.ziplla.dataforge.templates.Templates;

import java.util.Map;

public class App {
    public static void main(String[] args) {
        DataGenerator dataGenerator = new DataGenerator();

        dataGenerator.addIntegerField("age", Constraints.range(1, 3));
        dataGenerator.addDoubleField("salary", Constraints.rangeDouble(1, 1000, 2));
        dataGenerator.addStringField("string", Constraints.length(2, 6));
        dataGenerator.addTemplate("email", Templates.email());
        dataGenerator.addTemplate("phone number", Templates.phoneNumber());
        dataGenerator.addTemplate("name", Templates.name());

        Map<String, Object> generatedData = dataGenerator.generate();
    }
}

You can then assign this generated data to your entities. Example:

userService.createUser(generatedData);

A more detailed description of each method:

Method addIntegerField:

If you want a number to be generated in the range you need, then you need to do this:

dataGenerator.addIntegerField("Integer", Constraints.range(1, 3));

You can write the range in any order

If you want a number to be generated in any range, then you do not need to specify the range

dataGenerator.addIntegerField("Integer");

Methods addLongField, addByteField, addShortField work the same as addIntegerField:

dataGenerator.addLongField("Long", Constraints.rangeLong(44L, 88L));
dataGenerator.addByteField("Byte", Constraints.rangeByte((byte) 2, (byte) 4));
dataGenerator.addShortField("Short", Constraints.rangeShort((short) 1, (short) 5));

Method addDoubleField: If you want a number to be generated in the range you need, then you need to do this:

dataGenerator.addDoubleField("Double", Constraints.rangeDouble(3, 15));

You can also specify how many digits after the decimal point should be in the number

dataGenerator.addDoubleField("Double", Constraints.rangeDouble(3, 15, 5));

Method addFloatField work as same as addDoubleField:

dataGenerator.addFloatField("Float", Constraints.rangeFloat(4, 75, 2));

Method addStringField generate a random string in a given range:

dataGenerator.addStringField("String", Constraints.lengthString(2, 6));

You can also specify in which language to generate the string (Russian, English) (if you do not do this, the default string will be in English)

dataGenerator.addStringField("string", Constraints.lengthString(6, 2, Language.ru));
dataGenerator.addStringField("string", Constraints.lengthString(6, 2, Language.en));

You can also generate phone numbers, emails, names

For this use method addTemplate:

dataGenerator.addTemplate("email", Templates.email());
dataGenerator.addTemplate("phone number", Templates.phoneNumber());
dataGenerator.addTemplate("name", Templates.name());

How to Contribute

Fork repository, make changes, send us a pull request. We will review your changes and apply them to the master branch. To avoid frustration, before sending us your pull request please run full Maven build

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.