Search code examples
ravendbseedtest-data

Generate test data in Raven DB


I am looking for a preferred and maintainable way of test data generation in Raven DB. Currently, our team does have a way to do it through .NET code. Example is provided.

However, i am looking for different options. Please share.

public void Execute()
        {
            using (var documentStore = new DocumentStore { ConnectionStringName = "RavenDb" })
            {
                documentStore.Conventions.DefaultQueryingConsistency = ConsistencyOptions.QueryYourWrites;

                // Override the default key prefix generation strategy of Pascal case to lower case.
                documentStore.Conventions.FindTypeTagName = type => DocumentConvention.DefaultTypeTagName(type).ToLower();

                documentStore.Initialize();

                InitializeData(documentStore);
            }
        }

Edit: Raven-overflow is really helpful. Thanks for pointing out to the right place.


Solution

  • Try checking out RavenOverflow. In there, I've got a FakeData project that has fake data (both hardcoded AND randomly generated). This can then be used in either my Tests project or the Main Website :)

    Here's some sample code...

    if (isDataToBeSeeded)
    {
        HelperUtilities.CreateSeedData(documentStore);
    }
    

    ....

    public static void CreateSeedData(IDocumentStore documentStore)
    {
        Condition.Requires(documentStore).IsNotNull();
    
        using (IDocumentSession documentSession = documentStore.OpenSession())
        {
            // First, check to make sure we don't have any data.
            var user = documentSession.Load<User>(1);
            if (user != null)
            {
                // ooOooo! we have a user, so it's assumed we actually have some seeded data.
                return;
            }
    
            // We have no users, so it's assumed we therefore have no data at all.
            // So let's fake some up :)
    
            // Users.
            ICollection<User> users = FakeUsers.CreateFakeUsers(50);
            StoreFakeEntities(users, documentSession);
    
            // Questions.
            ICollection<Question> questions = FakeQuestions.CreateFakeQuestions(users.Select(x => x.Id).ToList());
            StoreFakeEntities(questions, documentSession);
    
            documentSession.SaveChanges();
    
            // Make sure all our indexes are not stale.
            documentStore.WaitForStaleIndexesToComplete();
        }
    }
    

    ....

    public static ICollection<Question> CreateFakeQuestions(IList<string> userIds, int numberOfFakeQuestions)
    {
    .... you get the idea .....
    }