-
-
Notifications
You must be signed in to change notification settings - Fork 0
Implement database caching for Get-SfAccount function and improve test clarity #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7380117
c659c14
28a7d86
3be5cc5
98b06db
877de7f
e40fb59
29734cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
|
|
||
|
|
||
| function Mock_Config{ | ||
| param( | ||
| [Parameter(Position=0)][string] $key = "config", | ||
| [Parameter(Position=1)][object] $Config | ||
| ) | ||
|
|
||
| $MOCK_CONFIG_PATH = "test_config_path" | ||
|
|
||
| # Remove mock config path if exists | ||
| if(Test-Path $MOCK_CONFIG_PATH){ | ||
| Remove-Item -Path $MOCK_CONFIG_PATH -ErrorAction SilentlyContinue -Recurse -Force | ||
| } | ||
|
|
||
| # create mock config path | ||
| New-Item -Path $MOCK_CONFIG_PATH -ItemType Directory -Force | ||
|
|
||
| # if $config is not null save it to a file | ||
| if($null -ne $Config){ | ||
| $configfile = Join-Path -Path $MOCK_CONFIG_PATH -ChildPath "$key.json" | ||
| $Config | ConvertTo-Json -Depth 10 | Set-Content $configfile | ||
| } | ||
|
|
||
| # Mock invoke call | ||
| MockCallToString "Invoke-GetConfigRootPath" -OutString $MOCK_CONFIG_PATH | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| function Mock_Database([switch]$ResetDatabase){ | ||
|
|
||
| MockCallToString "Invoke-GetDatabaseStorePath" -OutString "test_database_path" | ||
|
|
||
| if($ResetDatabase){ | ||
| Reset-DatabaseStore | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| { | ||
| "status": 0, | ||
| "result": { | ||
| "records": [ | ||
| { | ||
| "attributes": { | ||
| "type": "Account", | ||
| "url": "/services/data/v63.0/sobjects/Account/0010V00002KIWkaQAH" | ||
| }, | ||
| "Id": "0010V00002KIWkaQAH", | ||
| "Name": "Contoso", | ||
| "OwnerId": "0053o000008Skv3AAC", | ||
| "Industry": "Retail", | ||
| "Account_Owner__c": "<a href=\"/0053o000008Skv3\" target=\"_self\">Oana Dinca</a> [<a href=\"/0010V00002KIWka/a?sendMail=1&retURL=%2F0010V00002KIWka\" target=\"_self\">Change</a>]", | ||
| "Account_Segment__c": "Enterprise", | ||
| "Account_Owner_Role__c": "EMEA - Enterprise Sales Manager - South EMEA", | ||
| "Account_Tier__c": "Tier 1", | ||
| "Potential_Seats__c": 998, | ||
| "Country_Name__c": "Spain", | ||
| "Current_Seats__c": 600, | ||
| "Current_ARR_10__c": 116928, | ||
| "Salesforce_Record_URL__c": "https://github.my.salesforce.com/0010V00002KIWkaQAH", | ||
| "Potential_Seats_Manual__c": 1500, | ||
| "Website": "www.contos.es", | ||
| "PhotoUrl": "/services/images/photo/0010V00002KIWkaQAH" | ||
| } | ||
| ], | ||
| "totalSize": 1, | ||
| "done": true | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| function Test_GetSfAccount{ | ||
|
|
||
| Reset-InvokeCommandMock | ||
| Mock_Database -ResetDatabase | ||
| $mockAttrib = @{attributes = @("Potential_Seats_Manual__c","Website","PhotoUrl")} | ||
| Mock_Config -Config $mockAttrib | ||
|
|
||
| $dbstore = Invoke-MyCommand -Command GetDatabaseStorePath | ||
| Assert-AreEqual -Expected "test_database_path" -Presented $dbstore | ||
|
|
||
| $attrib = "Id,Name,OwnerId,Industry,Account_Owner__c,Account_Segment__c,Account_Owner_Role__c,Account_Tier__c,Potential_Seats__c,Country_Name__c,Current_Seats__c,Current_ARR_10__c,Salesforce_Record_URL__c,Potential_Seats_Manual__c,Website,PhotoUrl" | ||
| $type = "Account" | ||
| $id = "0010V00002KIWkaQAH" | ||
|
|
||
| $command = 'sf data query --query "SELECT {attributes} FROM {type} WHERE Id=''{id}''" -r=json' | ||
| $command = $command -replace "{attributes}", $attrib | ||
| $command = $command -replace "{type}", $type | ||
| $command = $command -replace "{id}", $id | ||
| MockCall -Command $command -filename "sf-data-query-account.json" | ||
|
|
||
| # Act with out cache | ||
| $result = get-sfAccount https://github.lightning.force.com/lightning/r/Account/0010V00002KIWkaQAH/view | ||
| Assert-AreEqual -Expected $Id -Presented $result.Id | ||
| $dbfiles = Get-ChildItem $dbstore | ||
| Assert-Count -Expected 1 -Presented $dbfiles | ||
| Assert-IsTrue -Condition ($dbfiles[0].Name -like "sfDataQuery*-$type-$id-*.json") | ||
|
|
||
| # Remove sf data | ||
|
||
| Reset-InvokeCommandMock | ||
| Mock_Database | ||
| Mock_Config -Config $mockAttrib | ||
|
|
||
| # Act with cache | ||
| $result = Get-SfAccount https://github.lightning.force.com/lightning/r/Account/0010V00002KIWkaQAH/view | ||
| Assert-AreEqual -Expected $Id -Presented $result.Id | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| # Configuration management module | ||
|
|
||
| Set-MyInvokeCommandAlias -Alias GetConfigRootPath -Command "Invoke-GetConfigRootPath" | ||
|
|
||
| $moduleName = Get-ModuleName | ||
| $CONFIG_ROOT = [System.Environment]::GetFolderPath('UserProfile') | Join-Path -ChildPath ".helpers" -AdditionalChildPath $moduleName, "config" | ||
|
|
||
| # Create the config root if it does not exist | ||
| if(-Not (Test-Path $CONFIG_ROOT)){ | ||
| New-Item -Path $CONFIG_ROOT -ItemType Directory | ||
| } | ||
|
|
||
| function Invoke-GetConfigRootPath { | ||
Check noticeCode scanning / PSScriptAnalyzer The cmdlet 'Invoke-GetConfigRootPath' does not have a help comment. Note
The cmdlet 'Invoke-GetConfigRootPath' does not have a help comment.
|
||
| [CmdletBinding()] | ||
| param() | ||
|
|
||
| $configRoot = $CONFIG_ROOT | ||
| return $configRoot | ||
| } Export-ModuleMember -Function Invoke-GetConfigRootPath | ||
|
|
||
| function GetConfigFile { | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Mandatory = $true, Position = 0)][string]$Key | ||
| ) | ||
|
|
||
| $configRoot = Invoke-MyCommand -Command GetConfigRootPath | ||
| $path = Join-Path -Path $configRoot -ChildPath "$Key.json" | ||
| return $path | ||
| } | ||
|
|
||
| function Test-Configuration { | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position = 0)][string]$Key = "config" | ||
| ) | ||
|
|
||
| $path = GetConfigFile -Key $Key | ||
|
|
||
| return Test-Path $path | ||
| } | ||
|
|
||
| function Get-Configuration { | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position = 0)][string]$Key = "config" | ||
| ) | ||
|
|
||
| $path = GetConfigFile -Key $Key | ||
|
|
||
| if(-Not (Test-Configuration -Key $Key)){ | ||
| return $null | ||
| } | ||
|
|
||
| try{ | ||
| $ret = Get-Content $path | ConvertFrom-Json -ErrorAction Stop | ||
| return $ret | ||
| } | ||
| catch{ | ||
| Write-Warning "Error reading configuration ($Key) file: $($path). $($_.Exception.Message)" | ||
| return $null | ||
| } | ||
| } | ||
|
|
||
| function Save-Configuration { | ||
| [CmdletBinding()] | ||
| param( | ||
| [Parameter(Position = 0)][string]$Key = "config", | ||
| [Parameter(Mandatory = $true, Position = 1)][Object]$Config | ||
| ) | ||
|
|
||
| $path = GetConfigFile -Key $Key | ||
|
|
||
| try { | ||
| $Config | ConvertTo-Json -Depth 10 | Set-Content $path -ErrorAction Stop | ||
| } | ||
| catch { | ||
| Write-Warning "Error saving configuration ($Key) to file: $($path). $($_.Exception.Message)" | ||
| return $false | ||
Check noticeCode scanning / PSScriptAnalyzer The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note
The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
|
||
| } | ||
|
|
||
| return $true | ||
Check noticeCode scanning / PSScriptAnalyzer The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute. Note
The cmdlet 'Save-Configuration' returns an object of type 'System.Boolean' but this type is not declared in the OutputType attribute.
|
||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
Check notice
Code scanning / PSScriptAnalyzer
Line has trailing whitespace Note