Skip to content

Commit

Permalink
Fixes bchavez#330: Add GettingStarted example.
Browse files Browse the repository at this point in the history
  • Loading branch information
bchavez committed Oct 11, 2020
1 parent 195a447 commit a43e0fa
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
25 changes: 21 additions & 4 deletions Examples/Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
13 changes: 13 additions & 0 deletions Examples/GettingStarted/GettingStarted.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="31.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
118 changes: 118 additions & 0 deletions Examples/GettingStarted/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Order>()
//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<User>()
//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<Gender>())
//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<Order> 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);
}
}
}
18 changes: 18 additions & 0 deletions Examples/GettingStarted/README.md
Original file line number Diff line number Diff line change
@@ -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!

0 comments on commit a43e0fa

Please sign in to comment.