Skip to content

Commit

Permalink
Added Unit Tests for -Fields
Browse files Browse the repository at this point in the history
  • Loading branch information
lipkau committed Jul 8, 2018
1 parent 33da44c commit 15badd1
Showing 1 changed file with 112 additions and 19 deletions.
131 changes: 112 additions & 19 deletions Tests/Get-JiraIssue.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Describe "Get-JiraIssue" {

Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
BeforeAll {
Remove-Module JiraPS -ErrorAction SilentlyContinue
Import-Module "$PSScriptRoot/../JiraPS" -Force -ErrorAction Stop
}

InModuleScope JiraPS {

Expand Down Expand Up @@ -28,31 +30,54 @@
'@

#region Mocks
Mock Get-JiraConfigServer {
Mock Get-JiraConfigServer -ModuleName JiraPS {
$jiraServer
}

Mock Invoke-JiraMethod -ParameterFilter { $Method -eq 'Get' -and $URI -like "$jiraServer/rest/api/latest/issue/TEST-001*" } {
Mock Get-JiraUser -ModuleName JiraPS {
$object = [PSCustomObject] @{
'Name' = 'username'
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
}

Mock Get-JiraFilter -ModuleName JiraPS {
[PSCustomObject]@{
PSTypeName = "JiraPS.Filter"
Id = 12345
SearchUrl = "https://jira.example.com/rest/api/latest/filter/12345"
}
}

Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/issue/TEST-001*"
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
ConvertFrom-Json $response
}

Mock Invoke-JiraMethod -ParameterFilter { $Method -eq 'Get' -and $URI -like "$jiraServer/rest/api/latest/search" -and $GetParameter["jql"] -eq $jqlEscaped } {
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/search" -and
$GetParameter["jql"] -eq $jqlEscaped
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
ConvertFrom-Json $response
}

Mock Invoke-JiraMethod {
Mock Invoke-JiraMethod -ModuleName JiraPS -ParameterFilter {
$Method -eq 'Get' -and
$URI -like "$jiraServer/rest/api/*/filter/*"
} {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
throw "Unidentified call to Invoke-JiraMethod"
ConvertFrom-Json $response
}

Mock Get-JiraUser {
$object = [PSCustomObject] @{
'Name' = 'username'
}
$object.PSObject.TypeNames.Insert(0, 'JiraPS.User')
return $object
Mock Invoke-JiraMethod -ModuleName JiraPS {
ShowMockInfo 'Invoke-JiraMethod' 'Method', 'Uri'
throw "Unidentified call to Invoke-JiraMethod"
}
#endregion Mocks

Expand All @@ -63,6 +88,7 @@
defParam $command 'InputObject'
defParam $command 'Query'
defParam $command 'Filter'
defParam $command 'Fields'
defParam $command 'StartIndex'
defParam $command 'MaxResults'
defParam $command 'PageSize'
Expand All @@ -72,7 +98,7 @@
Context "Behavior testing" {

It "Obtains information about a provided issue in JIRA" {
{ Get-JiraIssue -Key TEST-001 } | Should Not Throw
{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -89,7 +115,7 @@
}

It "Uses JQL to search for issues if the -Query parameter is used" {
{ Get-JiraIssue -Query $jql } | Should Not Throw
{ Get-JiraIssue -Query $jql } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -107,7 +133,7 @@
}

It "Supports the -StartIndex and -MaxResults parameters to page through search results" {
{ Get-JiraIssue -Query $jql -StartIndex 10 -MaxResults 50 } | Should Not Throw
{ Get-JiraIssue -Query $jql -StartIndex 10 -MaxResults 50 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -127,7 +153,7 @@
}

It "Returns all issues via looping if -MaxResults is not specified" {
{ Get-JiraIssue -Query $jql -PageSize 25 } | Should Not Throw
{ Get-JiraIssue -Query $jql -PageSize 25 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -144,11 +170,78 @@
}
Assert-MockCalled @assertMockCalledSplat
}

It "Returns only the fields required with -Fields" {
$issue = [PSCustomObject]@{
PSTypeName = "JiraPS.Issue"
Key = "TEST-001"
}

{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "key" } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "-summary" } | Should -Not -Throw
{ Get-JiraIssue -Key TEST-001 -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -InputObject $issue -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -Query $jql -Fields "key", "summary", "status" } | Should -Not -Throw
{ Get-JiraIssue -Filter "12345" -Fields "key", "summary", "status" } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "*all"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "key"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "-summary"
}
Scope = 'It'
Exactly = $true
Times = 1
}
Assert-MockCalled @assertMockCalledSplat

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
ModuleName = 'JiraPS'
ParameterFilter = {
$Method -eq 'Get' -and
$GetParameter["fields"] -eq "key,summary,status"
}
Scope = 'It'
Exactly = $true
Times = 4
}
Assert-MockCalled @assertMockCalledSplat
}
}

Context "Input testing" {
It "Accepts an issue key for the -Key parameter" {
{ Get-JiraIssue -Key TEST-001 } | Should Not Throw
{ Get-JiraIssue -Key TEST-001 } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand All @@ -172,7 +265,7 @@
$issue.PSObject.TypeNames.Insert(0, 'JiraPS.Issue')

# Should call Get-JiraIssue using the -Key parameter, so our URL should reflect the key we provided
{ Get-JiraIssue -InputObject $Issue } | Should Not Throw
{ Get-JiraIssue -InputObject $Issue } | Should -Not -Throw

$assertMockCalledSplat = @{
CommandName = 'Invoke-JiraMethod'
Expand Down

0 comments on commit 15badd1

Please sign in to comment.