NeuCharFramework (NCF)
  • NCF - NeuCharFramework
  • Projects

    • Preparation
    • Basic Library Source Code Analysis
    • DynamicWebApiEngine
    • Deployment
  • Help

    • Online Resources
    • Q&A Community
    • QQ Group (147054579)
    • Senparc WeChat SDK
  • Gitee
  • GitHub
  • English
  • 简体中文
GitHub
  • NCF - NeuCharFramework
  • Projects

    • Preparation
    • Basic Library Source Code Analysis
    • DynamicWebApiEngine
    • Deployment
  • Help

    • Online Resources
    • Q&A Community
    • QQ Group (147054579)
    • Senparc WeChat SDK
  • Gitee
  • GitHub
  • English
  • 简体中文
GitHub
  • NCF Overview

    • NCF - NeuCharFramework
    • About NCF
    • Environment Requirements
    • Frontend and Backend Separation Mode
    • Introduction to Xncf Modules
  • Prepare Development

    • Get NCF Template Source Code
    • Using Visual Studio to Run NCF
    • Running NCF Using CLI
    • Installation
    • Admin Login
    • Admin Backend
    • appsettings.json Configuration
    • Module Management
    • Accessing Documentation
  • Configuration

    • Entry File
    • Database Settings
    • appsettings.json Configuration
    • Docker
    • Dapr
    • Configure Multi-Tenant
    • Redis Cache
  • Modular Development

    • NCF Modular Development Concept
    • Composition of Xncf
    • Create the First Xncf Module
    • Xncf Module Sample Detailed Explanation
    • Implement Your Own Business Logic
    • Update Xncf Module
    • How to Call Between Modules
    • Publish Xncf Module to nuget.org
    • Update Base Library
    • Xncf Module Development
    • Embedding Static Resource Files into NCF
    • Publish Local Nuget Package
    • Advanced
  • Database

    • Database Settings
    • Multi-Database Support
    • Specify Database
    • Update database migration files for the Senparc.Service project (Migrations)
    • Multi-Database Principle
    • DatabasePlant
    • Tarmac Operation Database Migration and Update
  • Unit Testing

    • NCF Unit Test Introduction
    • Start Development
    • Advanced
    • Appendix
  • Q&A

    • NCF Terminology
    • NCF FAQ
  • Release

    • New Features
    • Upgrade Guide
    • Logs

Advanced

Advanced: Generating Seed Data

During regular system testing and integration testing, testers usually need to insert a large amount of data into the database. This process is time-consuming and labor-intensive. In actual operations, a large number of unit tests (or developer tests) also repeat similar processes. NCF provides developers with a friendly Seed Data loading function, which allows all loading and testing to be completed directly in memory without using a database.

The source of seed data can be directly creating new objects in memory, or it can come from persistent storage containers such as files, databases, caches, etc. Here is one way to implement it.

In the unit test class, create a static method:

private static Action<Dictionary<Type, List<object>>> InitSeedData = seedData =>
{
    var list = new List<object>();
    Random rand = new Random();
    for (int i = 0; i < 1000000; i++)
    {
        var username = $"Admin-{i}";
        var password = $"pWd-{i}";
        var realName = $"Admin{rand.Next(10000)}";
        var adminUserInfo = new AdminUserInfo(ref username, ref password, realName, "", "");
        list.Add(adminUserInfo);
    }
    seedData.Add(typeof(AdminUserInfo), list);
};

In the above code, seedData is a container for all the mock data that NCF has already processed for you. You can quickly get or set mock data for a particular entity through seedData[<Type>]. We created 1 million pieces of data through a for loop.

How to make this seed data configuration process effective?

Very simple! We can directly add a base class constructor call base(null, InitSeedData) to the constructor of the already created unit test:

public AdminUserInfoServiceTests() : base(null, InitSeedData)
{
    // No modification needed
}

Then we create a unit test to test it:

[TestMethod]
public async Task MyTestMethod()
{
    var obj = await adminUserInfoService.GetUserInfoAsync("Admin-600");
    Assert.IsNotNull(obj);

    var dataset = base.dataLists[typeof(AdminUserInfo)];
    Assert.AreEqual(dataset.Skip(600).Take(1).First() as AdminUserInfo, obj);
}

Run the test results:

Through the test, we can see that the initialization time of 1 million pieces of data plus the entire unit test class only took about 0.2 seconds. The simulated query process uses Linq's lambda query by default, which took 4.3 seconds. This depends on the performance of the CPU. If it is just to test business logic, you can also reduce the number of seed data. Usually, 1000 pieces are enough.

Edit this page on GitHub
Last Updated:
Contributors: Jeffrey Su
Prev
Start Development
Next
Appendix