From a43e0faf202218400344aed2129dc2c9efa70e84 Mon Sep 17 00:00:00 2001 From: bchavez Date: Sat, 10 Oct 2020 17:49:07 -0700 Subject: [PATCH] Fixes #330: Add GettingStarted example. --- Examples/Examples.sln | 25 +++- Examples/GettingStarted/GettingStarted.csproj | 13 ++ Examples/GettingStarted/Program.cs | 118 ++++++++++++++++++ Examples/GettingStarted/README.md | 18 +++ 4 files changed, 170 insertions(+), 4 deletions(-) create mode 100644 Examples/GettingStarted/GettingStarted.csproj create mode 100644 Examples/GettingStarted/Program.cs create mode 100644 Examples/GettingStarted/README.md diff --git a/Examples/Examples.sln b/Examples/Examples.sln index 2930c03a..5b48410b 100644 --- a/Examples/Examples.sln +++ b/Examples/Examples.sln @@ -3,7 +3,9 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.29613.14 MinimumVisualStudioVersion = 16.0.29613.14 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreSeedDb", "EFCoreSeedDb\EFCoreSeedDb.csproj", "{7B012C90-4B93-4DB8-A0BA-AA91460DE392}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreSeedDb", "EFCoreSeedDb\EFCoreSeedDb.csproj", "{7B012C90-4B93-4DB8-A0BA-AA91460DE392}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GettingStarted", "GettingStarted\GettingStarted.csproj", "{4B10A48D-F572-4984-8C61-8598E792436A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -14,9 +16,6 @@ Global Release|x64 = Release|x64 Release|x86 = Release|x86 EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {7B012C90-4B93-4DB8-A0BA-AA91460DE392}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {7B012C90-4B93-4DB8-A0BA-AA91460DE392}.Debug|Any CPU.Build.0 = Debug|Any CPU @@ -30,5 +29,23 @@ Global {7B012C90-4B93-4DB8-A0BA-AA91460DE392}.Release|x64.Build.0 = Release|Any CPU {7B012C90-4B93-4DB8-A0BA-AA91460DE392}.Release|x86.ActiveCfg = Release|Any CPU {7B012C90-4B93-4DB8-A0BA-AA91460DE392}.Release|x86.Build.0 = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|x64.ActiveCfg = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|x64.Build.0 = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|x86.ActiveCfg = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Debug|x86.Build.0 = Debug|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|Any CPU.Build.0 = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|x64.ActiveCfg = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|x64.Build.0 = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|x86.ActiveCfg = Release|Any CPU + {4B10A48D-F572-4984-8C61-8598E792436A}.Release|x86.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {6C5437BE-1C81-49D9-931A-FCA2BC2DA589} EndGlobalSection EndGlobal diff --git a/Examples/GettingStarted/GettingStarted.csproj b/Examples/GettingStarted/GettingStarted.csproj new file mode 100644 index 00000000..6d8c68da --- /dev/null +++ b/Examples/GettingStarted/GettingStarted.csproj @@ -0,0 +1,13 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + diff --git a/Examples/GettingStarted/Program.cs b/Examples/GettingStarted/Program.cs new file mode 100644 index 00000000..8c2b4aee --- /dev/null +++ b/Examples/GettingStarted/Program.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using Bogus; +using Bogus.DataSets; +using Bogus.Extensions; +using Newtonsoft.Json; + +namespace GettingStarted +{ + class Program + { + static void Main(string[] args) + { + //Set the randomzier seed if you wish to generate repeatable data sets. + Randomizer.Seed = new Random(3897234); + + var fruit = new[] {"apple", "banana", "orange", "strawberry", "kiwi"}; + + var orderIds = 0; + var testOrders = new Faker() + //Ensure all properties have rules. By default, StrictMode is false + //Set a global policy by using Faker.DefaultStrictMode if you prefer. + .StrictMode(true) + //OrderId is deterministic + .RuleFor(o => o.OrderId, f => orderIds++) + //Pick some fruit from a basket + .RuleFor(o => o.Item, f => f.PickRandom(fruit)) + //A random quantity from 1 to 10 + .RuleFor(o => o.Quantity, f => f.Random.Number(1, 10)) + //A nullable int? with 80% probability of being null. + //The .OrNull extension is in the Bogus.Extensions namespace. + .RuleFor(o => o.LotNumber, f => f.Random.Int(0, 100).OrNull(f, .8f)); + + var userIds = 0; + var testUsers = new Faker() + //Optional: Call for objects that have complex initialization + .CustomInstantiator(f => new User(userIds++, f.Random.Replace("###-##-####"))) + + //Basic rules using built-in generators + .RuleFor(u => u.FirstName, f => f.Name.FirstName()) + .RuleFor(u => u.LastName, f => f.Name.LastName()) + .RuleFor(u => u.Avatar, f => f.Internet.Avatar()) + .RuleFor(u => u.UserName, (f, u) => f.Internet.UserName(u.FirstName, u.LastName)) + .RuleFor(u => u.Email, (f, u) => f.Internet.Email(u.FirstName, u.LastName)) + .RuleFor(u => u.SomethingUnique, f => $"Value {f.UniqueIndex}") + .RuleFor(u => u.SomeGuid, Guid.NewGuid) + + //Use an enum outside scope. + .RuleFor(u => u.Gender, f => f.PickRandom()) + //Use a method outside scope. + .RuleFor(u => u.CartId, f => Guid.NewGuid()) + //Compound property with context, use the first/last name properties + .RuleFor(u => u.FullName, (f, u) => u.FirstName + " " + u.LastName) + //And composability of a complex collection. + .RuleFor(u => u.Orders, f => testOrders.Generate(3)) + //After all rules are applied finish with the following action + .FinishWith((f, u) => + { + Console.WriteLine("User Created! Name={0}", u.FullName); + }); + + var user = testUsers.Generate(3); + user.Dump(); + } + } + + public class Order + { + public int OrderId { get; set; } + public string Item { get; set; } + public int Quantity { get; set; } + public int? LotNumber { get; set; } + } + + public enum Gender + { + Male, + Female + } + + public class User + { + public User(int userId, string ssn) + { + this.Id = userId; + this.SSN = ssn; + } + + public int Id { get; set; } + public string FirstName { get; set; } + public string LastName { get; set; } + public string FullName { get; set; } + public string UserName { get; set; } + public string Email { get; set; } + public string SomethingUnique { get; set; } + public Guid SomeGuid { get; set; } + + public string Avatar { get; set; } + public Guid CartId { get; set; } + public string SSN { get; set; } + public Gender Gender { get; set; } + + public List Orders { get; set; } + } + + public static class ExtensionsForTesting + { + public static void Dump(this object obj) + { + Console.WriteLine(obj.DumpString()); + } + + public static string DumpString(this object obj) + { + return JsonConvert.SerializeObject(obj, Formatting.Indented); + } + } +} diff --git a/Examples/GettingStarted/README.md b/Examples/GettingStarted/README.md new file mode 100644 index 00000000..12990932 --- /dev/null +++ b/Examples/GettingStarted/README.md @@ -0,0 +1,18 @@ +[1]:https://github.com/bchavez/Bogus#the-great-c-example + +## Getting Started with Bogus + +#### Requirements +* **.NET Core 3.1** or later + +#### Description + +The `GettingStarted` example is the full working example of [**"The Great C# Example"**][1] on the homepage of this repository. + +To run the example, perform the following commands *inside* this `GettingStarted` folder: + + * `dotnet restore` + * `dotnet build` + * `dotnet run` + +After the `dotnet` commands are successfully executed above, you should see some fake JSON data printed to the console! \ No newline at end of file