Skip to content

Commit aaef325

Browse files
committed
FIRST: fast tests
1 parent 5d74d11 commit aaef325

13 files changed

Lines changed: 14063 additions & 7 deletions

File tree

SockstoreNet/Migration.Console/LegacyData.cs

Lines changed: 1846 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
using Bogus;
2+
using Migration.Legacy;
3+
using System.Drawing;
4+
using System.Text;
5+
using ClosedXML.Excel;
6+
7+
namespace Migration.Console;
8+
9+
public class LegacyExporter
10+
{
11+
/// <summary>
12+
/// Export our data from our legacy system to CSV
13+
/// </summary>
14+
public void ExportSocks()
15+
{
16+
var faker = new Faker();
17+
var brands = new[] { "DevWear", "CodeComfort", "TechThreads", "GridGear", "ResponsiveFit", "StateWear", "UtilityThreads", "DataFeet", "LightThreads", "FullStackFeet", "TypeSafe", "ContainerWear", "VersionFeet", "ProgressiveWear", "QualityThreads", "FormatFeet" };
18+
var materials = new[] { "Cotton", "Cotton Blend", "Merino Wool", "Bamboo Fiber", "Polyester", "Polyester Blend", "Compression Material", "Wool Blend" };
19+
var allSizes = Enum.GetValues<SockSize>();
20+
var colors = new[] { "Blue", "Green", "Red", "Yellow", "Purple", "Black", "Navy Blue", "Orange", "Gray", "Teal", "Pink", "Light Blue", "White", "Steel Blue", "Blue Gray", "Forest Green", "Purple Blue", "Red Orange", "Lavender", "Magenta", "Dark Green" };
21+
22+
const string filePath = "socks_catalog.csv";
23+
using var writer = new StreamWriter(filePath);
24+
25+
foreach (var name in LegacyData.GetNames())
26+
{
27+
var availableSizes = faker.Random.Bool(0.7f)
28+
? allSizes
29+
: GetRandomSizeSubset(faker, allSizes);
30+
31+
foreach (var size in availableSizes)
32+
{
33+
string line = CreateLine(
34+
name,
35+
faker.Random.Guid(),
36+
faker.Random.Decimal(15.00m, 35.00m).ToString("F2"),
37+
faker.PickRandom(colors),
38+
size,
39+
faker.Random.Bool(0.85f), // 85% chance of being active
40+
faker.Date.Between(DateTime.Now.AddYears(-2), DateTime.Now.AddMonths(-3)),
41+
faker.Date.Between(DateTime.Now.AddMonths(-3), DateTime.Now),
42+
faker.PickRandom(materials),
43+
faker.Lorem.Sentence(faker.Random.Int(8, 20)),
44+
faker.PickRandom(brands)
45+
);
46+
47+
writer.WriteLine(line);
48+
}
49+
}
50+
}
51+
52+
private static SockSize[] GetRandomSizeSubset(Faker faker, SockSize[] allSizes)
53+
{
54+
int minSizes = faker.Random.Int(2, 4);
55+
int maxSizes = faker.Random.Int(minSizes, allSizes.Length);
56+
return faker.PickRandom(allSizes, maxSizes).ToArray();
57+
}
58+
59+
private static string CreateLine(string name, Guid id, string price, string color, SockSize size, bool active, DateTime createdOn, DateTime updatedOn, string material, string description, string brand)
60+
{
61+
return $"{id},{name},{price},{color},{size},{active},{createdOn:yyyy-MM-ddTHH:mm:ss},{updatedOn:yyyy-MM-ddTHH:mm:ss},{material},\"{description}\",{brand}";
62+
}
63+
64+
/// <summary>
65+
/// Export stock details from legacy system 2 (Excel)
66+
/// </summary>
67+
public void ExportStock()
68+
{
69+
const string stockFilePath = "stock_inventory.xlsx";
70+
const string catalogFilePath = "socks_catalog.csv";
71+
72+
var faker = new Faker();
73+
var guids = new List<Guid>();
74+
75+
using var reader = new StreamReader(catalogFilePath);
76+
77+
while (reader.ReadLine() is { } line)
78+
{
79+
string guidString = line.Split(',').First();
80+
if (Guid.TryParse(guidString, out var guid))
81+
{
82+
guids.Add(guid);
83+
}
84+
}
85+
86+
using (var workbook = new XLWorkbook())
87+
{
88+
var worksheet = workbook.Worksheets.Add("Stock Inventory");
89+
90+
worksheet.Cell(1, 1).Value = "ProductId";
91+
worksheet.Cell(1, 2).Value = "StockQuantity";
92+
worksheet.Cell(1, 3).Value = "LastStockUpdate";
93+
worksheet.Cell(1, 4).Value = "MinimumThreshold";
94+
worksheet.Cell(1, 5).Value = "MaximumCapacity";
95+
96+
var headerRange = worksheet.Range(1, 1, 1, 5);
97+
headerRange.Style.Font.Bold = true;
98+
headerRange.Style.Fill.BackgroundColor = XLColor.LightGray;
99+
headerRange.Style.Border.OutsideBorder = XLBorderStyleValues.Thick;
100+
101+
// Add stock data for each product
102+
for (int i = 0; i < guids.Count; i++)
103+
{
104+
int row = i + 2;
105+
106+
worksheet.Cell(row, 1).Value = guids[i].ToString();
107+
worksheet.Cell(row, 2).Value = faker.Random.Int(0, 500);
108+
worksheet.Cell(row, 3).Value = faker.Date.Between(DateTime.Now.AddDays(-100), DateTime.Now);
109+
worksheet.Cell(row, 4).Value = faker.Random.Int(5, 25);
110+
worksheet.Cell(row, 5).Value = faker.Random.Int(200, 1000);
111+
}
112+
113+
worksheet.Columns().AdjustToContents();
114+
worksheet.Column(3).Style.DateFormat.Format = "yyyy-mm-dd hh:mm:ss";
115+
116+
// Add some conditional formatting for low stock
117+
var stockRange = worksheet.Range(2, 2, guids.Count + 1, 2);
118+
stockRange.AddConditionalFormat()
119+
.WhenLessThan(10)
120+
.Fill.SetBackgroundColor(XLColor.Red)
121+
.Font.SetFontColor(XLColor.White);
122+
123+
stockRange.AddConditionalFormat()
124+
.WhenBetween(10, 25)
125+
.Fill.SetBackgroundColor(XLColor.Yellow);
126+
127+
workbook.SaveAs(stockFilePath);
128+
}
129+
}
130+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net9.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Bogus" Version="35.6.3" />
12+
<PackageReference Include="ClosedXML" Version="0.105.0" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Migration\Migration.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using Migration.Console;
2+
3+
var exporter = new LegacyExporter();
4+
exporter.ExportSocks();
5+
exporter.ExportStock();
6+
7+
// This creates socks_catalog.csv and stock_inventory.xlsx in the bin folder
8+
// Which can then be used by our migration code to inject in our new system.

0 commit comments

Comments
 (0)