Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setting up fake data in mongodb for testing

We have a project in which we need to create a fake database and fake data for functional testing. Initially we started with a script that creates the entities using mongoose, initializes them and save them.

var StudentA = new Student();
StudentA.name = "Bob";
StudentA.surname = "Marley";
StudentA.save();

As the application grew and the relations between the documents increased this script has become an absolute mess. Now it poses a bottleneck in terms of the time we invest in application development.

The question is, is there a better way to do it? e.g. a library which fills the database with data in a more structured way? Or should we parse a file like a csv file and then push it to the mongo?

The question is for mongodb but it can be generalized into any kind of NO-SQL database that needs to be filled with fake data. (It is simpler with a SQL like grammar for RDBMS)

like image 788
ralzaul Avatar asked Jun 02 '15 10:06

ralzaul


3 Answers

You could try to write json files instead of code and use mongoimport to recreate your database. That's easier to maintain than kilometers of very verbose and repetitive code.

like image 147
Maxime Beugnet Avatar answered Sep 19 '22 11:09

Maxime Beugnet


I agree with the above solutions and think the best way is to:

  1. Generate fake information using a library tool.
  2. Convert the fake info into a json file.
  3. Upload it onto mongo using mongoimport.

I ound there are libraries out there which allows you to do generate fake data for free such as Faker.js (if you are familiar with node.js and js in general) or you can use the free java version of Faker here: https://github.com/blocoio/faker

I also found a paid solution here: https://www.mockaroo.com/ but don't know why anyone would want to pay for this as it is fairly easy to generate the fake data - here is a step by step guide.

Import the faker java library and the json writer into your project (I'm using gradle so here is the gradle code):

    repositories {
        maven { url 'https://jitpack.io' }
    }
dependencies {
    compile 'com.github.blocoio:faker:1.0.1'
    compile 'com.googlecode.json-simple:json-simple:1.1.1'
}

Use the following java code to generate as many fake objects as you want, here I'm using a loop to generate 3 objects, and save it into a json.file.

public class FakerTest {
    static FileWriter file;
    public static void main(String[] args) {
        try {
            file = new FileWriter("c:\\<Your Location>\\test.json"); //try opening the file
            for (int i = 0; i < 3; i ++) {
                Faker faker = new Faker();
                JSONObject obj = new JSONObject();
                obj.put("Name", faker.name.firstName());
                obj.put("address",faker.address.streetAddress());
                obj.put("email",faker.internet.email());
                file.write(obj.toJSONString());

            }
            file.flush();
            file.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Results of the json file:

{"address":"790 Murphy Vista","email":"[email protected]","Name":"Christop"}{"address":"7706 Larkin River","email":"[email protected]","Name":"Braeden"}{"address":"1893 Jamarcus Rest","email":"[email protected]","Name":"Marlee"}

Now, upload it with mongoimport.

The faker library will let you generate lots of fields, please refer to:

https://github.com/stympy/faker/blob/master/README.md

like image 29
Simon Avatar answered Sep 19 '22 11:09

Simon


Download this json file provided by MongoDB.

You can mongoimport it using:

mongoimport --db testDB --collection testCollection --file test.json

More details on Mongoimport can be found here.

like image 37
Dev Avatar answered Sep 17 '22 11:09

Dev