Skip to content
Draft
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
15 changes: 6 additions & 9 deletions Edition-01/Examples/The AAA Approach/Example 1.ps1
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
$swApiUrl = 'https://mc-starwars-data.azurewebsites.net'
function Invoke-StarWarsApi
{
$swApiUrl = 'https://swapi.info'
function Invoke-StarWarsApi {
param (
[Parameter(Mandatory)]
[ValidateSet('Planets', 'Films', 'People')]
[string] $ObjectType,
[string]$objectType,

[int] $id = -1
[int]$id = -1
)
try {
$suffix = $id -ne -1 ? "?id=$id" : ""
$suffix = $id -ne -1 ? "/$id" : ''
$path = "$($objectType.ToLower())$suffix"

$output = Invoke-RestMethod -Uri "$swApiUrl/api/$path" -Method GET
Write-Output $output
}
catch {
} catch {
$msg = "Error calling $swApiUrl/api/$path. $($_.Exception.Message)"
Write-Host $msg -f Red
Write-Output $null
}
}
2 changes: 1 addition & 1 deletion Edition-01/Examples/The AAA Approach/Example 10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Describing Get-SWPerson
[+] Returns person metadata for 'Darth Maul' with gender 'male',
eye colour 'yellow' & film count of 1 3.96s (3.96s|5ms)
[+] Returns person metadata for 'Luke Skywalker' with gender 'male',
eye colour 'blue' & film count of 6 4.37s (4.37s|2ms)
eye colour 'blue' & film count of 4 4.37s (4.37s|2ms)
[+] Returns person metadata for 'Mon Mothma' with gender 'female',
eye colour 'blue' & film count of 1 4.19s (4.19s|3ms)
Tests completed in 16.11s
Expand Down
23 changes: 7 additions & 16 deletions Edition-01/Examples/The AAA Approach/Example 2.ps1
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
function Search-SWPerson {
param (
[Parameter(Mandatory)]
[string] $Name
[string]$Name
)
# load all the people
$response = Invoke-StarWarsApi -objectType People
# filter on the name
$results = $response | Where-Object name -like "*$Name*"
$results = $response | Where-Object name -Like "*$Name*"

if ($null -eq $results) {
Write-Output @{ Error = "No person results found for '$Name'."}
}
else {
Write-Output @{ Error = "No person results found for '$Name'." }
} else {
# return all matches with some properties
$personDetails = $results | ForEach-Object {
Invoke-StarWarsApi -objectType People -id $_.id
}

Write-Output $personDetails | Select-Object @{
Name = "id";
Expression = { $_.id}
Write-Output $results | Select-Object @{
Name = 'id'; Expression = { $_.url | Get-SWIdFromUrl }
}, name, gender, height,
@{
Name = "weight"
Expression = {$_.mass}
}
@{ Name = 'weight'; Expression = { $_.mass } }
}
}
28 changes: 11 additions & 17 deletions Edition-01/Examples/The AAA Approach/Example 3.ps1
Original file line number Diff line number Diff line change
@@ -1,36 +1,30 @@
function Get-SWPerson {
param (
[Parameter(Mandatory)]
[int] $Id
[int]$Id
)
# get the person
$person = Invoke-StarWarsApi -objectType People -id $Id

if ($null -eq $person)
{
Write-Output @{
Error = "Unable to find a person record given Id: $Id"
if ($null -eq $person) {
Write-Output @{
Error = "Unable to find a person record given Id: $id"
}
}
else {
} else {
# get the homeworld planet and the films
$planet = Invoke-StarWarsApi -objectType Planets -id $person.homeworld
$HomeWorldId = $person.homeworld | Get-SWIdFromUrl
$planet = Invoke-StarWarsApi -objectType Planets -id $HomeWorldId
$films = Invoke-StarWarsApi -objectType Films

# get detailed info of all films
$filmDetails = $films | ForEach-Object {
Invoke-StarWarsApi -objectType Films -id $_.id
}

# build the result object as a mix of all the data returned
$result = [PSCustomObject]@{
Name = $person.Name
BodyType = $person |
Name = $person.Name
BodyType = $person |
Select-Object height, mass, gender, skin_color, eye_color
HomeWorld = $planet |
Select-Object name, population, gravity, terrain
Films = $filmDetails |
Where-Object people -contains $person.id |
Films = $films |
Where-Object characters -Contains $person.url |
Select-Object title, director, release_date
}
Write-Output $result
Expand Down
2 changes: 1 addition & 1 deletion Edition-01/Examples/The AAA Approach/Example 5.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Get-SWPerson -Id 9 | Format-List
Get-SWPerson -Id 11 | Format-List
2 changes: 1 addition & 1 deletion Edition-01/Examples/The AAA Approach/Example 6.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Arrange
BeforeAll {
. $PSCommandPath.Replace('.Simple.Tests.ps1','.ps1')
. $PSCommandPath.Replace('.Simple.Tests.ps1', '.ps1')
}

Describe 'Search-SWPerson' -Tag 'Unit' {
Expand Down
4 changes: 2 additions & 2 deletions Edition-01/Examples/The AAA Approach/Example 8.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Arrange
BeforeAll {
. $PSCommandPath.Replace('.Mocked.Tests.ps1','.ps1')
. $PSCommandPath.Replace('.Mocked.Tests.ps1', '.ps1')

Mock Invoke-StarWarsApi {
$output1 = [PSCustomObject]@{
Expand All @@ -25,4 +25,4 @@ BeforeAll {
weight = '84'
}
Write-Output @($output1, $output2, $output3)
} -Verifiable -ParameterFilter { $objectType -eq 'People'}
} -Verifiable -ParameterFilter { $objectType -eq 'People' }
18 changes: 9 additions & 9 deletions Edition-01/Examples/The AAA Approach/Example 9.ps1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Arrange
BeforeAll {
. $PSCommandPath.Replace('.Complex.Tests.ps1','.ps1')
. $PSCommandPath.Replace('.Complex.Tests.ps1', '.ps1')
}

Describe 'Search-SWFilm' -Tag 'Unit' {
Expand All @@ -10,23 +10,23 @@ Describe 'Search-SWFilm' -Tag 'Unit' {
It $itName -TestCases @(
# Arrange
@{
name = 'Phantom'
year = '1999-05-19'
name = 'Phantom'
year = '1999-05-19'
director = 'George Lucas'
}
@{
name = 'Empire'
year = '1980-05-17'
name = 'Empire'
year = '1980-05-17'
director = 'Irvin Kershner'
}
@{
name = 'Return'
year = '1983-05-25'
name = 'Return'
year = '1983-05-25'
director = 'Richard Marquand'
}
) {
) {
# Act
$result = Search-SWFilm -name $name
$result = Search-SWFilm -Name $name

# Assert
$result.Count | Should -Be 1
Expand Down
142 changes: 71 additions & 71 deletions Edition-01/Starwars-Demo/src/StarWarsData.Complex.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
# Arrange
BeforeAll {
. $PSCommandPath.Replace('.Complex.Tests.ps1', '.ps1')
. $PSCommandPath.Replace('.Complex.Tests.ps1', '.ps1')
}

Describe 'Search-SWFilm' -Tag 'Unit' {
$itName = "Returns film with release date '<year>' & director " +
"'<director>' given title fragment '<name>'"
It $itName -TestCases @(
# Arrange
@{
name = 'Phantom'
year = '1999-05-19'
director = 'George Lucas'
}
@{
name = 'Empire'
year = '1980-05-17'
director = 'Irvin Kershner'
}
@{
name = 'Return'
year = '1983-05-25'
director = 'Richard Marquand'
}
) {
# Act
$result = Search-SWFilm -name $name
$itName = "Returns film with release date '<year>' & director " +
"'<director>' given title fragment '<name>'"
It $itName -TestCases @(
# Arrange
@{
name = 'Phantom'
year = '1999-05-19'
director = 'George Lucas'
}
@{
name = 'Empire'
year = '1980-05-17'
director = 'Irvin Kershner'
}
@{
name = 'Return'
year = '1983-05-25'
director = 'Richard Marquand'
}
) {
# Act
$result = Search-SWFilm -Name $name

# Assert
$result.Count | Should -Be 1
$result.title | Should -BeLike "*$name*"
$result.release_date | Should -Be $year
$result.director | Should -Be $director
}
# Assert
$result.Count | Should -Be 1
$result.title | Should -BeLike "*$name*"
$result.release_date | Should -Be $year
$result.director | Should -Be $director
}
}

Describe 'Get-SWPerson' -Tag 'Unit' {
$itName = "Returns person metadata for '<fullname>' with gender " +
"'<gender>', eye colour '<eyeColour>' & film count of <filmCount>"
It $itName -TestCases @(
# Arrange
@{
name = 'maul'
fullName = 'Darth Maul'
gender = 'male'
eyeColour = 'yellow'
homeWorld = 'Dathomir'
filmCount = 1
}
@{
name = 'luke'
fullName = 'Luke Skywalker'
gender = 'male'
eyeColour = 'blue'
homeWorld = 'Tatooine'
filmCount = 6
}
@{
name = 'mothma'
fullName = 'Mon Mothma'
gender = 'female'
eyeColour = 'blue'
homeWorld = 'Chandrila'
filmCount = 1
}
) {
# Act
$result = Search-SWPerson -name $name
$result.Count | Should -Be 1
$details = Get-SWPerson -Id $result.Id
$itName = "Returns person metadata for '<fullname>' with gender " +
"'<gender>', eye colour '<eyeColour>' & film count of <filmCount>"
It $itName -TestCases @(
# Arrange
@{
name = 'maul'
fullName = 'Darth Maul'
gender = 'male'
eyeColour = 'yellow'
homeWorld = 'Dathomir'
filmCount = 1
}
@{
name = 'luke'
fullName = 'Luke Skywalker'
gender = 'male'
eyeColour = 'blue'
homeWorld = 'Tatooine'
filmCount = 4
}
@{
name = 'mothma'
fullName = 'Mon Mothma'
gender = 'female'
eyeColour = 'blue'
homeWorld = 'Chandrila'
filmCount = 1
}
) {
# Act
$result = Search-SWPerson -Name $name
$result.Count | Should -Be 1
$details = Get-SWPerson -Id $result.Id

# Assert
$details | Should -Not -BeNullOrEmpty
$details.Name | Should -Be $fullName
$details.BodyType.gender | Should -Be $gender
$details.BodyType.eye_color | Should -Be $eyeColour
$details.HomeWorld.name | Should -Be $homeWorld
$details.Films | Should -HaveCount $filmCount
}
}
# Assert
$details | Should -Not -BeNullOrEmpty
$details.Name | Should -Be $fullName
$details.BodyType.gender | Should -Be $gender
$details.BodyType.eye_color | Should -Be $eyeColour
$details.HomeWorld.name | Should -Be $homeWorld
$details.Films | Should -HaveCount $filmCount
}
}
Loading