From 9e85474631a325bfe6d074f1b047c446babfc398 Mon Sep 17 00:00:00 2001 From: Brian Ginsburg Date: Mon, 27 Jan 2025 15:42:17 -0800 Subject: [PATCH] test: Add TestIsCheaperOrOlder unit test --- pkg/solver/matcher/matcher_test.go | 73 +++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/pkg/solver/matcher/matcher_test.go b/pkg/solver/matcher/matcher_test.go index 52d22192..0ab12140 100644 --- a/pkg/solver/matcher/matcher_test.go +++ b/pkg/solver/matcher/matcher_test.go @@ -2,8 +2,11 @@ package matcher -import "testing" +import ( + "testing" + "github.com/lilypad-tech/lilypad/pkg/data" +) func TestMatchOffers(t *testing.T) { testCases := getMatchTestCases() @@ -16,3 +19,71 @@ func TestMatchOffers(t *testing.T) { }) } } + +func TestIsCheaperOrOlder(t *testing.T) { + testCases := []struct { + name string + offerA data.ResourceOffer + offerB data.ResourceOffer + expected bool + }{ + { + name: "cheaper wins", + offerA: data.ResourceOffer{ + CreatedAt: 2, + DefaultPricing: data.DealPricing{ + InstructionPrice: 100, + }, + }, + offerB: data.ResourceOffer{ + CreatedAt: 1, + DefaultPricing: data.DealPricing{ + InstructionPrice: 200, + }, + }, + expected: true, + }, + { + name: "older wins when same price", + offerA: data.ResourceOffer{ + CreatedAt: 1, + DefaultPricing: data.DealPricing{ + InstructionPrice: 100, + }, + }, + offerB: data.ResourceOffer{ + CreatedAt: 2, + DefaultPricing: data.DealPricing{ + InstructionPrice: 100, + }, + }, + expected: true, + }, + { + name: "more expensive loses regardless of age", + offerA: data.ResourceOffer{ + CreatedAt: 1, + DefaultPricing: data.DealPricing{ + InstructionPrice: 200, + }, + }, + offerB: data.ResourceOffer{ + CreatedAt: 2, + DefaultPricing: data.DealPricing{ + InstructionPrice: 100, + }, + }, + expected: false, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result := isCheaperOrOlder(tc.offerA, tc.offerB) + if result != tc.expected { + t.Errorf("isCheaperOrOlder(%+v, %+v) = %v, expected %v", + tc.offerA, tc.offerB, result, tc.expected) + } + }) + } +}