问题
This question is a follow-up to an earlier question on using FsCheck to generate records. The original question was answered with a well composed example solution. However, prior to the answer being shared, I attempted to create a generator which is included below. Unfortunately, the generated records of type QueryRequest = {Symbol: string; StartDate: DateTime; EndDate: DateTime}
have the following issues:
- Missing
symbol
- Start dates earlier than January 1, 2000
- End dates later than January 1, 2019
Original: { Symbol = "" StartDate = 8/9/2057 4:07:10 AM EndDate = 10/14/2013 6:15:32 PM } Shrunk: { Symbol = "" StartDate = 8/9/2057 12:00:00 AM EndDate = 10/14/2013 12:00:00 AM }
Since I am still in the process of becoming familiar with F#, I would appreciate suggestions/feedback on: how to address the aforementioned issues, and opportunities to improve the code in terms of structure, composition, etc.
namespace Parser
module DataGenerators =
open System
open FsCheck
type QueryRequest = {Symbol: string; StartDate: DateTime; EndDate: DateTime}
type Tweet =
static member GenerateRecords (year, month, day, symbol) =
try
let startDate = DateTime (year, month, day)
let endDate = startDate.AddDays 1.0
Some {Symbol = symbol; StartDate = startDate; EndDate = endDate}
with
| :? ArgumentOutOfRangeException -> None
static member Combine (years: int list) (months: int list) (days: int list) (symbols: string list) =
let rec loop acc years months days symbols =
match years, months, days, symbols with
| [], [], [], [] -> acc
| year :: years, month :: months, day :: days, symbol :: symbols -> loop ((year, month, day, symbol) :: acc) years months days symbols
| _, _, _, _ -> acc
loop [] years months days symbols
static member Generate () =
let years = Gen.choose (2000, 2019) |> Gen.sample 0 10
let months = Gen.choose (1, 12) |> Gen.sample 0 10
let days = Gen.choose(1, 31) |> Gen.sample 0 10
let symbols = Gen.elements ["ORCL"; "IBM"; "AAPL"; "GOOGL"] |> Gen.sample 0 10
Tweet.Combine years months days symbols
|> List.map Tweet.GenerateRecords
|> List.fold (fun acc r -> match r with Some q -> q :: acc | None -> acc) []
回答1:
I cannot reproduce your issue, the following yields true
for 1000s of executions:
Tweet.Generate()
|> List.forall (fun q ->
q.StartDate <= q.EndDate &&
q.StartDate >= DateTime(2000, 1, 1) &&
q.EndDate <= DateTime(2019, 12, 31) &&
["ORCL"; "IBM"; "AAPL"; "GOOGL"] |> List.contains q.Symbol)
However, you can simplify Tweet
like so:
type Tweet =
static member GenerateRecords ((year, month, day), symbol) =
try
let startDate = DateTime (year, month, day)
let endDate = startDate.AddDays 1.0
Some {Symbol = symbol; StartDate = startDate; EndDate = endDate}
with
| :? ArgumentOutOfRangeException -> None
static member Generate () =
let years = Gen.choose (2000, 2019) |> Gen.sample 0 10
let months = Gen.choose (1, 12) |> Gen.sample 0 10
let days = Gen.choose(1, 31) |> Gen.sample 0 10
let symbols = Gen.elements ["ORCL"; "IBM"; "AAPL"; "GOOGL"] |> Gen.sample 0 10
let dates = List.zip3 years months days
List.zip dates symbols
|> List.choose Tweet.GenerateRecords
来源:https://stackoverflow.com/questions/58635256/issues-creating-records-with-fscheck