Comments (5)
Hi @mdmoura ,
Seems straight forward:
[Test]
public void issue_65_create_n_time_series()
{
var measureFaker = new Faker<Issue65Measure>()
.RuleFor(x => x.Id, f => f.IndexGlobal)
.RuleFor(x => x.MeasureTypeId, f => f.Random.Number(1, 4))
.RuleFor(x => x.Created, f => DateTime.Parse("1/1/2017").AddDays(f.IndexVariable++))
.RuleFor(x => x.Value, f => f.Random.Decimal(10, 20));
var userFaker = new Faker<Isssue65User>()
.RuleFor(x => x.Id, f => f.IndexVariable++)
.RuleFor(x => x.Name, y => y.Person.FirstName)
.RuleFor( x => x.Measures, (f, u) => measureFaker.Generate(4).ForEach( m => m.UserId = u.Id) );
userFaker.Generate(20).ToList().Dump();
}
public class Issue65Measure
{
public Int32 Id { get; set; }
public Int32 UserId { get; set; }
public Int32 MeasureTypeId { get; set; }
public DateTime Created { get; set; }
public Decimal Value { get; set; }
}
public class Isssue65User
{
public Int32 Id { get; set; }
public String Name { get; set; }
public ICollection<Issue65Measure> Measures { get; set; }
}
Produces:
{
"Id": 0,
"Name": "Lee",
"Measures": [
{
"Id": 1,
"UserId": 0,
"MeasureTypeId": 3,
"Created": "2017-01-01T00:00:00",
"Value": 18.580745825814430
},
{
"Id": 2,
"UserId": 0,
"MeasureTypeId": 3,
"Created": "2017-01-02T00:00:00",
"Value": 19.016423713889170
},
{
"Id": 3,
"UserId": 0,
"MeasureTypeId": 2,
"Created": "2017-01-03T00:00:00",
"Value": 18.706475281485580
},
{
"Id": 4,
"UserId": 0,
"MeasureTypeId": 4,
"Created": "2017-01-04T00:00:00",
"Value": 19.49160031484980
}
]
},
{
"Id": 1,
"Name": "Peyton",
"Measures": [
{
"Id": 6,
"UserId": 1,
"MeasureTypeId": 3,
"Created": "2017-01-05T00:00:00",
"Value": 19.750085333245840
},
{
"Id": 7,
"UserId": 1,
"MeasureTypeId": 3,
"Created": "2017-01-06T00:00:00",
"Value": 12.914435650647820
},
{
"Id": 8,
"UserId": 1,
"MeasureTypeId": 3,
"Created": "2017-01-07T00:00:00",
"Value": 19.127092565934680
},
{
"Id": 9,
"UserId": 1,
"MeasureTypeId": 2,
"Created": "2017-01-08T00:00:00",
"Value": 14.997773391659270
}
]
},
...
🍂 🍃 "I get a sense of weightlessness…"
from bogus.
@bchavez That isn't exactly what I am trying to create ... I would need, for each day, one record of each MeasureType selected. Consider I have two measure types I would get for the first 3 days;
"Measures": [
{
"Id": 1,
"UserId": 0,
"MeasureTypeId": 1,
"Created": "2017-01-01T00:00:00",
"Value": 18.580745825814430
},
{
"Id": 2,
"UserId": 0,
"MeasureTypeId": 2,
"Created": "2017-01-01T00:00:00",
"Value": 28.580745825814430
},
{
"Id": 3,
"UserId": 0,
"MeasureTypeId": 1,
"Created": "2017-01-02T00:00:00",
"Value": 12.580745825814430
},
{
"Id": 4,
"UserId": 0,
"MeasureTypeId": 2,
"Created": "2017-01-02T00:00:00",
"Value": 16.580745825814430
},
{
"Id": 5,
"UserId": 0,
"MeasureTypeId": 1,
"Created": "2017-01-03T00:00:00",
"Value": 10.580745825814430
},
{
"Id": 6,
"UserId": 0,
"MeasureTypeId": 2,
"Created": "2017-01-03T00:00:00",
"Value": 10.580745825814430
}
]
Thank You,
Miguel
from bogus.
@bchavez I am doing it using FinishWith:
List<User> users = new Faker<User>()
.RuleFor(x => x.Id, f => f.IndexVariable++)
.RuleFor(x => x.Name, f => f.Person.FirstName)
.FinishWith((f, x) => {
x.Measures = Enumerable.Range(1, 8).SelectMany(y => Enumerable.Range(1, 200), (t, d) =>
new Measure {
MeasureTypeId = t,
Created = DateTime.Now.AddDays(d),
Value = f.Random.Decimal(5, 20)
}).ToList();
})
.Generate(20).ToList();
So for each user I am adding 200 points, one per day, for each MeasureType. I can't find a better way.
from bogus.
@mdmoura LGTM, you could do this also:
using static System.Linq.Enumerable;
List<User> users = new Faker<User>()
.RuleFor(x => x.Id, f => f.IndexVariable++)
.RuleFor(x => x.Name, f => f.Person.FirstName)
.RuleFor(x => x.Measures, (f, u) =>
Range(1, 8).SelectMany(y => Range(1, 200),
(t, d) => new Measure
{
MeasureTypeId = t,
Created = DateTime.Now.AddDays(d),
Value = f.Random.Decimal(5, 20)
}).ToList()
)
.Generate(20).ToList();
A bit more terse I'd say.
Thanks,
Brian
⛅ 👥 "Ain't no sunshine when she's gone... And she's always gone too long..."
from bogus.
@bchavez Yes, it looks better that way. Thanks
from bogus.
Related Issues (20)
- First and Full names are incorrectly generated with locale "sv" HOT 2
- Request to add language English (Hong Kong) / locale en_HK
- Faker.Tv() is missing from Bogus.Hollywood HOT 3
- Feature request: New localized data: Word(s).
- Add ability to except random numbers HOT 2
- Update Denmark (da) locale from faker-js
- Nuget for .NET 8.0.x HOT 4
- Random history facts generation
- Are Randomizer's number ranges inclusive or exclusive? HOT 3
- bogus is missing NuGet package README file HOT 1
- .NET 8 support HOT 9
- Gender-digit is incorrect for Swedish Personnummer generation HOT 3
- Switch to licence expression rather than URL HOT 1
- Invalid Iban generation for CR (Costa Rica) HOT 3
- Add hungarian (hu) locale from faker-js HOT 1
- How to make convert to generic method HOT 6
- Fakers used inside Rules of other Fakers share the same object reference HOT 2
- Determinism with Custom Datasets HOT 1
- Include the increment count for the Rules Expressions HOT 1
- Stack memory issue in locale tests (overflows on Mac) HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bogus.