Skip to content

Commit

Permalink
Improve datemasking (#5741)
Browse files Browse the repository at this point in the history
* Added option to set dates for past and future
Fixed bugs for wrong method in dates

* Added support for random date

* Removed types that are not possibl to create

* Removed types that are redundant

* Fixed test to comply with removed types
  • Loading branch information
sanderstad authored and potatoqualitee committed Jun 17, 2019
1 parent 63fe7bf commit 4bebe05
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 63 deletions.
58 changes: 0 additions & 58 deletions bin/randomizer/en.randomizertypes.csv
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ Hacker,IngVerb
Hacker,Noun
Hacker,Phrase
Hacker,Verb
Hacker,Random
Hashids,Decode
Hashids,DecodeHex
Hashids,DecodeLong
Expand All @@ -93,58 +92,6 @@ Image,People
Image,Sports
Image,Technics
Image,Transport
Image,Random
IndexFaker,CompareTo
IndexFaker,GetTypeCode
IndexFaker,ToBoolean
IndexFaker,ToByte
IndexFaker,ToChar
IndexFaker,ToDateTime
IndexFaker,ToDecimal
IndexFaker,ToDouble
IndexFaker,ToInt16
IndexFaker,ToInt32
IndexFaker,ToInt64
IndexFaker,ToSByte
IndexFaker,ToSingle
IndexFaker,ToType
IndexFaker,ToUInt16
IndexFaker,ToUInt32
IndexFaker,ToUInt64
IndexGlobal,CompareTo
IndexGlobal,GetTypeCode
IndexGlobal,ToBoolean
IndexGlobal,ToByte
IndexGlobal,ToChar
IndexGlobal,ToDateTime
IndexGlobal,ToDecimal
IndexGlobal,ToDouble
IndexGlobal,ToInt16
IndexGlobal,ToInt32
IndexGlobal,ToInt64
IndexGlobal,ToSByte
IndexGlobal,ToSingle
IndexGlobal,ToType
IndexGlobal,ToUInt16
IndexGlobal,ToUInt32
IndexGlobal,ToUInt64
IndexVariable,CompareTo
IndexVariable,GetTypeCode
IndexVariable,ToBoolean
IndexVariable,ToByte
IndexVariable,ToChar
IndexVariable,ToDateTime
IndexVariable,ToDecimal
IndexVariable,ToDouble
IndexVariable,ToInt16
IndexVariable,ToInt32
IndexVariable,ToInt64
IndexVariable,ToSByte
IndexVariable,ToSingle
IndexVariable,ToType
IndexVariable,ToUInt16
IndexVariable,ToUInt32
IndexVariable,ToUInt64
Internet,Avatar
Internet,Color
Internet,DomainName
Expand All @@ -171,7 +118,6 @@ Lorem,Slug
Lorem,Text
Lorem,Word
Lorem,Words
Lorem,Random
Name,FindName
Name,FirstName
Name,FullName
Expand All @@ -183,7 +129,6 @@ Name,LastName
Name,Prefix
Name,Suffix
Name,HasFirstNameList
Name,Random
Name,SupportsGenderFirstNames
Name,SupportsGenderLastNames
Name,SupportsGenderPrefixes
Expand All @@ -198,12 +143,10 @@ Person,FullName
Person,Gender
Person,LastName
Person,Phone
Person,Random
Person,UserName
Person,Website
Phone,PhoneNumber
Phone,PhoneNumberFormat
Phone,Random
Random,AlphaNumeric
Random,Bool
Random,Byte
Expand Down Expand Up @@ -246,7 +189,6 @@ Random,Words
Random,WordsArray
Rant,Review
Rant,Reviews
Rant,Random
System,AndroidId
System,ApplePushToken
System,BlackBerryPin
Expand Down
41 changes: 38 additions & 3 deletions functions/Get-DbaRandomizedValue.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,46 @@ function Get-DbaRandomizedValue {
($faker.Date.Between($Min, $Max)).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
}
} elseif ($randSubType -eq 'past') {
$faker.Date.Past().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
if ($Max) {
if ($Min) {
$yearsToGoBack = [math]::round((([datetime]$Max - [datetime]$Min).Days / 365), 0)
} else {
$yearsToGoBack = 1
}

$faker.Date.Past($yearsToGoBack, $Max).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
} else {
$faker.Date.Past().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
}
} elseif ($randSubType -eq 'future') {
$faker.Future.Past().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
if ($Min) {
if ($Max) {
$yearsToGoForward = [math]::round((([datetime]$Max - [datetime]$Min).Days / 365), 0)
} else {
$yearsToGoForward = 1
}

$faker.Date.Future($yearsToGoForward, $Min).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
} else {
$faker.Date.Future().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
}

} elseif ($randSubType -eq 'recent') {
$faker.Recent.Past().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
$faker.Date.Recent().ToString("yyyy-MM-dd HH:mm:ss.fffffff")
} elseif ($randSubType -eq 'random') {
if ($Min -or $Max) {
if (-not $Min) {
$Min = Get-Date
}

if (-not $Max) {
$Max = (Get-Date).AddYears(1)
}

($faker.Date.Between($Min, $Max)).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
} else {
($faker.Date.Past()).ToString("yyyy-MM-dd HH:mm:ss.fffffff")
}
} else {
$faker.Date.$RandomizerSubType()
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Get-DbaRandomizedType.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
It "Should have at least 263 rows" {
$types = Get-DbaRandomizedType

$types.count | Should BeGreaterOrEqual 263
$types.count | Should BeGreaterOrEqual 205
}

It "Should return correct type based on subtype" {
Expand All @@ -31,7 +31,7 @@ Describe "$commandname Integration Tests" -Tags "IntegrationTests" {
It "Should return values based on pattern" {
$types = Get-DbaRandomizedType -Pattern Name

$types.Count | Should BeGreaterOrEqual 27
$types.Count | Should BeGreaterOrEqual 26
}
}
}

0 comments on commit 4bebe05

Please sign in to comment.