Skip to content

Instantly share code, notes, and snippets.

Install-Module -Name 'PSRule.Rules.Azure' -Force -Scope CurrentUser
Export-AzRuleData `
-ResourceGroupName '<your-resource-group>' `
-OutputPath 'out/templates/'
Assert-PSRule `
-Module 'PSRule.Rules.Azure' `
-InputPath 'out/templates/' `
-o NUnit3 `
@stijnmoreels
stijnmoreels / Blog.Azure.Policy.AutomatedTest.ps1
Created August 26, 2024 11:54
Blog.Azure.Policy.AutomatedTest
Describe 'Azure policies' {
foreach ($policy in $policies) {
It $policy.policyDefinitionReferenceId {
$definition = $definitions | where { $_.id -eq $item.policyDefinitionId }
$item.complianceState | Should -Be 'Compliant' -Because $definition.description
}
}
}
using Arcus.Testing;
string expected = "<root><child>100</child></root>";
string actual = "<root><child>101</child></root>";
AssertXml.Equal(expected, actual);
// Arcus.Testing.EqualAssertionException
// AssertXml.Equal failure: expected and actual XML documents do not match
// actual XML has a different value at /root/child/text(), expected 100 while actual 101
//
using Arcus.Messaging.Abstractions.ServiceBus.MessageHandling;
using Arcus.Messaging.Pumps.Abstractions.Resiliency;
public class OrderMessageHandler : IAzureServiceBusMessageHandler<Order>
{
private readonly IMessagePumpCircuitBreaker _circuitBreaker;
public OrderMessageHandler(IMessagePumpCircuitBreaker circuitBreaker)
{
_circuitBreaker = circuitBreaker;
// Guest -> GuestList -> ValidationResult<GuestList>
let add guest list =
Optic.map guests_ (fun gs -> g :: gs)
// Guest -> GuestList -> ValidationResult<GuestList>
let remove guest list =
Optic.get (guest_ guest.Name) list
>>= fun g -> Optic.map guests_ (List.except [g]) list
let guests = GuestList.create [
Guest.create "Eve"
Guest.create "Lisa" ]
let lens = guest_ >=> name_ "Eve"
// result : ValidationResult<GuestList>
let result = Optic.set lens "AdamEve" guests
// A lens that provides the name of the guest.
let name_ : ResultLens<Guest, string> =
(fun g -> Ok g.Name), (fun n _ -> Guest.create n)
// A lens on the guest list that provides the individual guests on the list.
let guests_ : ResultLens<GuestList, Guest list> =
(fun (GuestList xs) -> Ok xs), fun xs _ -> GuestList.create xs
// A lens that points to a single guest on the guest list.
let guest_ name : ResultLens<GuestList, Guest> =
type ValidationResult<'a> = Result<'a, ErrorsByTag>
type ResultLens<'a, 'b> = ('a -> ValidationResult<'b>) * ('b -> 'a -> ValidationResult<'a>)
module Compose =
type ResultLens =
| ResultLens with
static member (>=>) (ResultLens, (g2, s2) : ResultLens<'b, 'c>) =
fun ((g1, s1) : ResultLens<'a,'b>) ->
(fun a -> g1 a >>= fun b -> g2 b),
(fun c a -> g1 a >>= fun b -> s2 c b >>= fun b -> s1 b a) : ResultLens<'a,'c>
open FPrimitive
type Guest =
private { Name : string } with
static member create name = result {
let! name = specModel id name {
notNullOrWhiteSpace "guest name should not be blank"
alphabetical "guest name should only contain alphabetical characters" }
return { Name = name } }
[Fact]
public void Delete_WithMessageId_ShouldDeleteMessage()
{
// Arrange
Message[] leftOverMessages = CreateMessages();
Message targetMessage = CreateMessage();
var repo = new InMemoryMessageRepository(leftOverMessages.Append(targetMessage));
var service = CreateMessageService(repo);
// Act