finnow/finnow-api/source/util/sample_data.d

70 lines
2.2 KiB
D

module util.sample_data;
import slf4d;
import auth;
import profile;
import account;
import util.money;
import std.random;
import std.conv;
import std.array;
void generateSampleData() {
UserRepository userRepo = new FileSystemUserRepository;
// Remove all existing user data.
foreach (User user; userRepo.findAll()) {
userRepo.deleteByUsername(user.username);
}
const int userCount = uniform(5, 10);
for (int i = 0; i < userCount; i++) {
generateRandomUser(i, userRepo);
}
info("Random sample data generation complete.");
}
void generateRandomUser(int idx, UserRepository userRepo) {
string username = "testuser" ~ idx.to!string;
string password = "testpass";
infoF!"Generating random user %s, password: %s."(username, password);
User user = createNewUser(userRepo, username, password);
ProfileRepository profileRepo = new FileSystemProfileRepository(username);
const int profileCount = uniform(1, 5);
for (int i = 0; i < profileCount; i++) {
generateRandomProfile(i, profileRepo);
}
}
void generateRandomProfile(int idx, ProfileRepository profileRepo) {
string profileName = "test-profile-" ~ idx.to!string;
infoF!" Generating random profile %s."(profileName);
Profile profile = profileRepo.createProfile(profileName);
ProfileDataSource ds = profileRepo.getDataSource(profile);
ds.getPropertiesRepository().setProperty("sample-data-idx", idx.to!string);
const int accountCount = uniform(1, 10);
for (int i = 0; i < accountCount; i++) {
generateRandomAccount(i, ds);
}
}
void generateRandomAccount(int idx, ProfileDataSource ds) {
AccountRepository accountRepo = ds.getAccountRepository();
string idxStr = idx.to!string;
string numberSuffix = "0".replicate(4 - idxStr.length) ~ idxStr;
string name = "Test Account " ~ idxStr;
AccountType type = choice(ALL_ACCOUNT_TYPES);
Currency currency = choice(ALL_CURRENCIES);
string description = "This is a testing account generated by util.sample_data.generateRandomAccount().";
infoF!" Generating random account: %s, ...%s"(name, numberSuffix);
Account account = accountRepo.insert(
type,
numberSuffix,
name,
currency,
description
);
}