Skip to content

Commit

Permalink
feat(Generator): project to generate test csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
seangwright committed Jul 25, 2023
1 parent 5520d90 commit 02fcf60
Show file tree
Hide file tree
Showing 5 changed files with 746 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Kentico.Xperience.Contacts.Importer.sln
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
VisualStudioVersion = 17.5.002.0
MinimumVisualStudioVersion =
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{8864B8E5-2F23-4778-BBF7-DDEFD166950B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{ED0957C3-0B03-4EFE-9282-B37ABB6CDB24}"
Expand All @@ -19,6 +22,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "config", "config", "{3BBEF4
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kentico.Xperience.Contacts.Importer", "src\Kentico.Xperience.Contacts.Importer\Kentico.Xperience.Contacts.Importer.csproj", "{C6D47E41-7FC9-4611-AC0A-828BCD4B4162}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{FC395345-C5AA-42FE-906B-4392A29C5D6D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Kentico.Xperience.Contacts.Importer.Generator", "test\Kentico.Xperience.Contacts.Importer.Generator\Kentico.Xperience.Contacts.Importer.Generator.csproj", "{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,8 +36,16 @@ Global
{C6D47E41-7FC9-4611-AC0A-828BCD4B4162}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C6D47E41-7FC9-4611-AC0A-828BCD4B4162}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C6D47E41-7FC9-4611-AC0A-828BCD4B4162}.Release|Any CPU.Build.0 = Release|Any CPU
{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C6D47E41-7FC9-4611-AC0A-828BCD4B4162} = {8864B8E5-2F23-4778-BBF7-DDEFD166950B}
{C3F895F9-7AC4-423D-9355-FC2C52CDAD4C} = {FC395345-C5AA-42FE-906B-4392A29C5D6D}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using CMS.ContactManagement;

namespace Kentico.Xperience.Contacts.Importer.Generator;

public static class ContactsGenerator
{
public static IEnumerable<ContactInfo> Generate(int count)
{
var faker = new Bogus.Faker<ContactInfo>();

faker.RuleFor(c => c.ContactGUID, f => f.Random.Guid())
.RuleFor(c => c.ContactCreated, f => f.Date.Between(new DateTime(), new DateTime()))
.RuleFor(c => c.ContactFirstName, f => f.Person.FirstName)
.RuleFor(c => c.ContactEmail, f => f.Internet.Email())
.RuleFor(c => c.ContactLastName, f => f.Person.LastName)
.RuleFor(c => c.ContactMiddleName, f => f.Person.FirstName)
.RuleFor(c => c.ContactAge, f => f.Random.Number(18, 64))
.RuleFor(c => c.ContactAddress1, f => f.Address.StreetAddress());

foreach (int _ in Enumerable.Range(0, count))
{
yield return faker.Generate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="34.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Kentico.Xperience.Contacts.Importer\Kentico.Xperience.Contacts.Importer.csproj" />
</ItemGroup>

</Project>
48 changes: 48 additions & 0 deletions test/Kentico.Xperience.Contacts.Importer.Generator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Globalization;
using CMS.ContactManagement;
using CsvHelper;
using Kentico.Xperience.Contacts.Importer.Generator;
using static Kentico.Xperience.Contacts.Importer.Services.ImportService;

string? solutionFolder = FindSolutionFolder();

ArgumentNullException.ThrowIfNull(solutionFolder);

using var writer = new StreamWriter(Path.Combine(solutionFolder, "data\\contact_sample.csv"));
using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
csv.Context.RegisterClassMap<ContactInfoMap>();

csv.WriteHeader<ContactInfo>();
csv.NextRecord();

foreach (var contact in ContactsGenerator.Generate(200000))
{
csv.WriteRecord(contact);
csv.NextRecord();
}

csv.Flush();


static string? FindSolutionFolder()
{
// Get the current directory where the application is running
string? currentDirectory = Directory.GetCurrentDirectory();

// Navigate up the directory tree until the solution file is found
while (currentDirectory != null)
{
// Check if the current directory contains a .sln file
string[] solutionFiles = Directory.GetFiles(currentDirectory, "*.sln");
if (solutionFiles.Length > 0)
{
return currentDirectory;
}

// Move up to the parent directory
currentDirectory = Directory.GetParent(currentDirectory)?.FullName;
}

// If no solution file is found, return null or an empty string
return null;
}
Loading

0 comments on commit 02fcf60

Please sign in to comment.