Skip to content

Commit

Permalink
test: enhance calculate-utils test:
Browse files Browse the repository at this point in the history
- add a missing getItemDescriptionPoints test;
- give names to nameless tests;
- use subtests for improved readability.
  • Loading branch information
kweeuhree committed Jan 3, 2025
1 parent 6be4b13 commit 9bfab36
Showing 1 changed file with 108 additions and 84 deletions.
192 changes: 108 additions & 84 deletions cmd/utils/calculate-utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestMain(m *testing.M) {
}

func Test_getRetailerNamePoints(t *testing.T) {
retailerNameTests := []struct {
tests := []struct {
name string
expected int
}{
Expand All @@ -24,136 +24,158 @@ func Test_getRetailerNamePoints(t *testing.T) {
{"M&M Corner Market", 14},
}

for _, entry := range retailerNameTests {
result := utils.getRetailerNamePoints(entry.name)
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getRetailerNamePoints(entry.name)

if result != entry.expected {
t.Errorf("retailerNameTests. %s: expected %d, received %d", entry.name, entry.expected, result)
}
if result != entry.expected {
t.Errorf("Expected %d, received %d", entry.expected, result)
}
})
}
}

func Test_isAlphanumeric(t *testing.T) {
isAlphanumericTests := []struct {
tests := []struct {
name string
char string
expected bool
}{
{"1", true},
{"a", true},
{"!", false},
{"&", false},
{" ", false},
{"Alphanumeric", "1", true},
{"Alphanumeric", "a", true},
{"Non-alphanumeric", "!", false},
{"Non-alphanumeric", "&", false},
{"Non-alphanumeric", " ", false},
}

for _, entry := range isAlphanumericTests {
result := utils.isAlphanumeric(entry.char)
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.isAlphanumeric(entry.char)

if result != entry.expected {
t.Errorf("isAlphanumericTests. %s: expected %t, received %t", entry.char, entry.expected, result)
}
if result != entry.expected {
t.Errorf("For char %s: expected %t, received %t", entry.char, entry.expected, result)
}
})
}
}

func Test_getRoundTotalPoints(t *testing.T) {
roundTotalTests := []struct {
tests := []struct {
name string
num float64
expected int
}{
{0, 50},
{1.00, 50},
{15.00, 50},
{99.99, 0},
{1.10, 0},
{"Points should be added", 0, 50},
{"Points should be added", 1.00, 50},
{"Points should be added", 15.00, 50},
{"Points should not be added", 99.99, 0},
{"Points should not be added", 1.10, 0},
}

for _, entry := range roundTotalTests {
result := utils.getRoundTotalPoints(entry.num)
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getRoundTotalPoints(entry.num)

if result != entry.expected {
t.Errorf("roundTotalTests. %f: expected %d, received %d", entry.num, entry.expected, result)
}
if result != entry.expected {
t.Errorf("For num %f: expected %d, received %d", entry.num, entry.expected, result)
}
})
}
}

func Test_getQuartersPoints(t *testing.T) {
quartersTests := []struct {
tests := []struct {
name string
num float64
expected int
}{
{0.25, 25},
{1.00, 25},
{15.00, 25},
{99.99, 0},
{1.10, 0},
{"Points should be added", 0.25, 25},
{"Points should be added", 1.00, 25},
{"Points should be added", 15.00, 25},
{"Points should not be added", 99.99, 0},
{"Points should not be added", 1.10, 0},
}

for _, entry := range quartersTests {
result := utils.getQuartersPoints(entry.num)
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getQuartersPoints(entry.num)

if result != entry.expected {
t.Errorf("quartersTests. %f: expected %d, received %d", entry.num, entry.expected, result)
}
if result != entry.expected {
t.Errorf("For num %f: expected %d, received %d", entry.num, entry.expected, result)
}
})
}
}

func Test_getEveryTwoItemsPoints(t *testing.T) {
everyTwoItemsTests := []struct {
tests := []struct {
name string
items []models.Item
expected int
}{
{"Mountain Dew receipt", MountainDewReceiptItems, 10},
{"Gatorade receipt", GatoradeReceiptItems, 10},
}

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getEveryTwoItemsPoints(entry.items)

if result != entry.expected {
t.Errorf("Expected %d, received %d", entry.expected, result)
}
})
}
}

func Test_getItemDescriptionPoints(t *testing.T) {
tests := []struct {
name string
items []models.Item
expected int
}{
{
items: []models.Item{
{ShortDescription: "Mountain Dew 12PK", Price: "6.49"},
{ShortDescription: "Emils Cheese Pizza", Price: "12.25"},
{ShortDescription: "Knorr Creamy Chicken", Price: "1.26"},
{ShortDescription: "Doritos Nacho Cheese", Price: "3.35"},
{ShortDescription: " Klarbrunn 12-PK 12 FL OZ ", Price: "12.00"},
},
expected: 10,
},
{
items: []models.Item{
{ShortDescription: "Gatorade", Price: "2.25"},
{ShortDescription: "Gatorade", Price: "2.25"},
{ShortDescription: "Gatorade", Price: "2.25"},
{ShortDescription: "Gatorade", Price: "2.25"},
},
expected: 10,
},
}

for index, entry := range everyTwoItemsTests {
result := utils.getEveryTwoItemsPoints(entry.items)

if result != entry.expected {
t.Errorf("everyTwoItemsTests[%d]: expected %d, received %d", index, entry.expected, result)
}
{"empty", nil, 0},
{"Gatorade items", GatoradeReceiptItems, 0},
{"Mountain Dew items", MountainDewReceiptItems, 6},
}

for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getItemDescriptionPoints(entry.items)

if result != entry.expected {
t.Errorf("Expected %d, received %d", entry.expected, result)
}
})
}
}

func Test_getOddDayPoints(t *testing.T) {
oddDayTests := []struct {
tests := []struct {
name string
date string
expected int
}{
{"2022-01-01", 6},
{"2022-02-15", 6},
{"2022-03-21", 6},
{"2022-04-10", 0},
{"2022-05-26", 0},
{"Odd day", "2022-01-01", 6},
{"Odd day", "2022-02-15", 6},
{"Odd day", "2022-03-21", 6},
{"Even day", "2022-04-10", 0},
{"Even day", "2022-05-26", 0},
}

for _, entry := range oddDayTests {
result := utils.getOddDayPoints(entry.date)
for _, entry := range tests {
t.Run(entry.name, func(t *testing.T) {
result := utils.getOddDayPoints(entry.date)

if result != entry.expected {
t.Errorf("oddDayTests. %s: expected %d, received %d", entry.date, entry.expected, result)
}
if result != entry.expected {
t.Errorf("For date %s: expected %d, received %d", entry.date, entry.expected, result)
}
})
}
}

func Test_getPurchaseTimePoints(t *testing.T) {
purchaseTimeTests := []struct {
tests := []struct {
date string
expected int
}{
Expand All @@ -164,11 +186,13 @@ func Test_getPurchaseTimePoints(t *testing.T) {
{"18:00", 0},
}

for _, entry := range purchaseTimeTests {
result := utils.getPurchaseTimePoints(entry.date)
for _, entry := range tests {
t.Run(entry.date, func(t *testing.T) {
result := utils.getPurchaseTimePoints(entry.date)

if result != entry.expected {
t.Errorf("purchaseTimeTests. %s: expected %d, received %d", entry.date, entry.expected, result)
}
if result != entry.expected {
t.Errorf("Expected %d, received %d", entry.expected, result)
}
})
}
}

0 comments on commit 9bfab36

Please sign in to comment.