Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions SfHelper.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@ Write-Information -Message ("Loading {0} ..." -f ($PSCommandPath | Split-Path -L
#Module path is where resides the RootModule file. This file. :)
$MODULE_PATH = $PSScriptRoot

# Import START
# $START = $MODULE_PATH | Join-Path -ChildPath "private" -AdditionalChildPath 'START.ps1'
$START = Get-ChildItem -Path $MODULE_PATH -Filter start.ps1 -Recurse
if($START | Test-Path){ . $($START | Get-Item).FullName }

#Get public and private function definition files.
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $MODULE_PATH\private\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach($import in @($Include + $Public + $Private))
Foreach($import in @($Include + $Private + $Public))
{
Try
{
Expand Down
6 changes: 3 additions & 3 deletions Test/Test.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Write-Information -Message ("Loading {0} ..." -f ($PSCommandPath | Split-Path -L
$MODULE_PATH = $PSScriptRoot

#Get public and private function definition files.
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Include = @( Get-ChildItem -Path $MODULE_PATH\include\*.ps1 -ErrorAction SilentlyContinue )
$Private = @( Get-ChildItem -Path $MODULE_PATH\private\*.ps1 -Recurse -ErrorAction SilentlyContinue )
$Public = @( Get-ChildItem -Path $MODULE_PATH\public\*.ps1 -Recurse -ErrorAction SilentlyContinue )

#Dot source the files
Foreach($import in @($Include + $Public + $Private))
Foreach($import in @($Include + $Private + $Public))
{
Try
{
Expand Down
8 changes: 8 additions & 0 deletions Test/include/database.mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@ function Mock_Database([switch]$ResetDatabase){

MockCallToString $DB_INVOKE_GET_ROOT_PATH_CMD -OutString "test_database_path"

$dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD
Assert-AreEqual -Expected "test_database_path" -Presented $dbstore

if($ResetDatabase){
Reset-DatabaseStore
}

}

function Get-Mock_DatabaseStore{
$dbstore = Invoke-MyCommand -Command $DB_INVOKE_GET_ROOT_PATH_CMD
return $dbstore
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# Managing dependencies
$MODULE_INVOKATION_TAG = "SfHelperModule"
$MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock"
$ROOT = $PSScriptRoot | Split-Path -Parent
$MOCK_PATH = $ROOT | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks'

# INVOKE COMMAND MOCK
#
# This includes help commands to mock invokes in a test module
# You need to set the following variables
# $MODULE_INVOKATION_TAG : name of the module that you are testing. This needs to match with the Tag used in the module you are testing.
# $MODULE_INVOKATION_TAG_MOCK : Tag for the mock functions on the testing moodule you are loading this include in
# MOCK_PATH : path to the mocks folder. This is where the mock files will be saved and loaded from.
#
# Sample:
# $MODULE_INVOKATION_TAG = "SfHelperModule"
# $MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock"
# $MOCK_PATH = $PSScriptRoot | Split-Path -Parent | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks'


function Set-InvokeCommandMock{
Expand Down
19 changes: 19 additions & 0 deletions Test/private/mock_SfDataQueery.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Mock_SfDataQuery {
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)][string]$attrib,
[Parameter(Mandatory = $true)][string]$type,
[Parameter(Mandatory = $true)][string]$id,
[Parameter(Mandatory = $true)][string]$filename

)

$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 $filename
}

#sf data query --query "SELECT Id,Name,OwnerId FROM opportunity WHERE Id='0065c00001SFRbYAAX'" -r=json
#sf data query --query "SELECT Id,Name FROM Opportunity WHERE Id='0065c00001SFRbYAAX'" -r=json
6 changes: 6 additions & 0 deletions Test/private/mock_variables.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# Required for INVOKE COMMAND MOCK

$MODULE_INVOKATION_TAG = "SfHelperModule"

Check warning

Code scanning / PSScriptAnalyzer

The variable 'MODULE_INVOKATION_TAG' is assigned but never used. Warning

The variable 'MODULE_INVOKATION_TAG' is assigned but never used.
$MODULE_INVOKATION_TAG_MOCK = "SfHelperModule-Mock"

Check warning

Code scanning / PSScriptAnalyzer

The variable 'MODULE_INVOKATION_TAG_MOCK' is assigned but never used. Warning

The variable 'MODULE_INVOKATION_TAG_MOCK' is assigned but never used.
$MOCK_PATH = $PSScriptRoot | Split-Path -Parent | Join-Path -ChildPath 'private' -AdditionalChildPath 'mocks'

Check warning

Code scanning / PSScriptAnalyzer

The variable 'MOCK_PATH' is assigned but never used. Warning

The variable 'MOCK_PATH' is assigned but never used.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"status": 0,
"result": {
"records": [
{
"attributes": {
"type": "Opportunity",
"url": "/services/data/v63.0/sobjects/Opportunity/0065c00001SFRbYAAX"
},
"Id": "0065c00001SFRbYAAX",
"Name": "Secret Services IT 10 Seat 2025_Q3 Order",
"OwnerId": "0053o000008Skv3AAC"
}
],
"totalSize": 1,
"done": true
}
}
17 changes: 0 additions & 17 deletions Test/public/Get-SfObjectIdFromUrl.test.ps1

This file was deleted.

42 changes: 42 additions & 0 deletions Test/public/auxiliarfunctions.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function Test_sfObjectIdFromUrl{

. $PSScriptRoot/../../private/auxiliarfunctions.ps1

$urlList = @(
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB/view",
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB/",
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB",
"https://github.my.salesforce.com/0010V00002Q8r78QAB"
)

$urlList | ForEach-Object {
$result = Get-SfObjectIdFromUrl $_
Assert-AreEqual -Expected "0010V00002Q8r78QAB" -Presented $result
}

}

function Test_sfObjectTypeFromUrl{

. $PSScriptRoot/../../private/auxiliarfunctions.ps1

$urlList = @(
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB/view",
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB/",
"https://github.lightning.force.com/lightning/r/Account/0010V00002Q8r78QAB"
)

$urlList | ForEach-Object {
$result = Get-SfObjectTypeFromUrl $_
Assert-AreEqual -Expected "Account" -Presented $result
}

$urlList = @(
"https://github.my.salesforce.com/0010V00002Q8r78QAB"
)
$urlList | ForEach-Object {
$result = Get-SfObjectTypeFromUrl $_
Assert-IsNull -Object $result
}

}
58 changes: 58 additions & 0 deletions Test/public/getsfopportunity.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

function Test_GetSfOpportunity{

Reset-InvokeCommandMock
Mock_Database -ResetDatabase
Mock_Config

# Mock Sf call
$id = "0065c00001SFRbYAAX"
$cacheFileName = "sfDataQuery-opportunity-$id-5106802FEB193611777BC7DA26122EF5.json"
$url = "https://github.lightning.force.com/lightning/r/Opportunity/$id/view"
Mock_SfDataQuery_Opportunity_0065c00001SFRbYAAX

# Act with out cache
$result = Get-SfOpportunity -SfUrl $url

# Assert
Assert-AreEqual -Expected $id -Presented $result.Id
$path = Get-Mock_DatabaseStore | Join-Path -ChildPath $cacheFileName
Assert-ItemExist -Path $path

# Remove sf data
Reset-InvokeCommandMock
Mock_Database
Mock_Config

# Act with cache
$result = Get-SfOpportunity -SfUrl $url

# Assert
Assert-AreEqual -Expected $id -Presented $result.Id

}

function Test_GetSfOpportunity_Live{
Assert-SkipTest "Test live connection to Salesforce"

Reset-InvokeCommandMock
Enable-InvokeCommandAliasModule

$result = Get-SfOpportunity https://github.lightning.force.com/lightning/r/Opportunity/0065c00001SFRbYAAX/view
Assert-notnull -Object $result

Assert-NotImplemented
}


function Mock_SfDataQuery_Opportunity_0065c00001SFRbYAAX{
[CmdletBinding()]
param()

$attrib = "Id,Name,OwnerId"
$type = "Opportunity"
$id = "0065c00001SFRbYAAX"
$filename = "sf-data-query-opportunity_0065c00001SFRbYAAX.json"

Mock_SfDataQuery -attrib $attrib -type $type -id $id -filename $filename
}
70 changes: 70 additions & 0 deletions Test/public/getstaccount.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function Test_GetSfAccount{

Reset-InvokeCommandMock

$mockAttrib = @{account_attributes = @("Potential_Seats_Manual__c","Website","PhotoUrl")}

# Mocks
Mock_Database -ResetDatabase
Mock_Config -Config $mockAttrib

# Mock Sf call
$id = "0010V00002KIWkaQAH"
$url = "https://github.lightning.force.com/lightning/r/Account/$id/view"
$filename = "sfDataQuery-Account-$id-01F5F2DFDE481D1146E8D504BB935E4D.json"
Mock_SfDataQuery_Account_0010V00002KIWkaQAH -AdditionalAttributes $mockAttrib.account_attributes

# Act with out cache
$result = Get-SfAccount -SfUrl $url

# Assert with cache
Assert-AreEqual -Expected $id -Presented $result.Id
$path = Get-Mock_DatabaseStore | Join-Path -ChildPath $filename
Assert-ItemExist -Path $path

# Reset Mock and skip Mock_SfDataQuery_Account_0010V00002KIWkaQAH
Reset-InvokeCommandMock
Mock_Database
Mock_Config -Config $mockAttrib

# Act with cache
$result = Get-SfAccount -SfUrl $url

# Assert
Assert-AreEqual -Expected "0010V00002KIWkaQAH" -Presented $result.Id
}

function Test_GetSfAccount_Transformations{
Reset-InvokeCommandMock

# Mocks
Mock_Database -ResetDatabase
Mock_Config
Mock_SfDataQuery_Account_0010V00002KIWkaQAH

# Act with out cache
$result = Get-SfAccount https://github.lightning.force.com/lightning/r/Account/0010V00002KIWkaQAH/view

# Transformation Account_Owner__c -> OwnerName
Assert-AreEqual -Expected "Oana Dinca" -Presented $result.OwnerName
Assert-IsNull -Object $result.Account_Owner__c

}

function Mock_SfDataQuery_Account_0010V00002KIWkaQAH{
[CmdletBinding()]
param(
[Parameter(Position=0)][string[]]$AdditionalAttributes
)

$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"
$type = "Account"
$id = "0010V00002KIWkaQAH"
$filename = "sf-data-query-account_0010V00002KIWkaQAH.json"

if ($AdditionalAttributes) {
$attrib += "," + ($AdditionalAttributes -join ",")
}

Mock_SfDataQuery -attrib $attrib -type $type -id $id -filename $filename
}
43 changes: 43 additions & 0 deletions Test/public/sfConfig.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function Test_AddSfConfigAttribute{
Reset-InvokeCommandMock
Mock_Config

# Emput config
$result = Get-SfConfig
Assert-IsNull -Object $result

# Add acount
Add-SfConfigAttribute -objectType "Account" -Attribute "acountattribute"
$result = Get-SfConfig
Assert-Count -Expected 1 -Presented $result.account_attributes
Assert-Contains -Expected "acountattribute" -Presented $result.account_attributes
Add-SfConfigAttribute -objectType "Account" -Attribute "acountattribute2"
$result = Get-SfConfig
Assert-Count -Expected 2 -Presented $result.account_attributes
Assert-Contains -Expected "acountattribute" -Presented $result.account_attributes
Assert-Contains -Expected "acountattribute2" -Presented $result.account_attributes

# Add user
Add-SfConfigAttribute -objectType "User" -Attribute "userattribute"
$result = Get-SfConfig
Assert-Count -Expected 1 -Presented $result.user_attributes
Assert-Contains -Expected "userattribute" -Presented $result.user_attributes
Add-SfConfigAttribute -objectType "User" -Attribute "userattribute2"
$result = Get-SfConfig
Assert-Count -Expected 2 -Presented $result.user_attributes
Assert-Contains -Expected "userattribute" -Presented $result.user_attributes
Assert-Contains -Expected "userattribute2" -Presented $result.user_attributes

# Add Opportunity
Add-SfConfigAttribute -objectType "Opportunity" -Attribute "opportunityattribute"
$result = Get-SfConfig
Assert-Count -Expected 1 -Presented $result.opportunity_attributes
Assert-Contains -Expected "opportunityattribute" -Presented $result.opportunity_attributes
Add-SfConfigAttribute -objectType "Opportunity" -Attribute "opportunityattribute2"
$result = Get-SfConfig
Assert-Count -Expected 2 -Presented $result.opportunity_attributes
Assert-Contains -Expected "opportunityattribute" -Presented $result.opportunity_attributes
Assert-Contains -Expected "opportunityattribute2" -Presented $result.opportunity_attributes

}

Loading
Loading