Skip to content

Commit 6f34d69

Browse files
authored
chore(ENT-923): add priority comparison methods (#171)
* chore(ENT-923): add priority comparison methods * chore(ENT-923): add unit test
1 parent 7e0a400 commit 6f34d69

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

pkg/models/priority.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,30 @@ const (
55
PriorityImportant string = "IMPORTANT"
66
PriorityInformational string = "INFORMATIONAL"
77
)
8+
9+
var priorityToInt = map[string]int{
10+
PriorityInformational: 0,
11+
PriorityImportant: 1,
12+
PriorityUrgent: 2,
13+
}
14+
15+
func ComparePriority(priority1, priority2 string) int {
16+
val1, ok1 := priorityToInt[priority1]
17+
val2, ok2 := priorityToInt[priority2]
18+
19+
if !ok1 && !ok2 {
20+
return 0
21+
} else if !ok1 {
22+
return -1
23+
} else if !ok2 {
24+
return 1
25+
}
26+
27+
if val1 < val2 {
28+
return -1
29+
} else if val1 > val2 {
30+
return 1
31+
} else {
32+
return 0
33+
}
34+
}

pkg/models/priority_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package models
2+
3+
import "testing"
4+
5+
func TestComparePriority(t *testing.T) {
6+
tests := []struct {
7+
priority1 string
8+
priority2 string
9+
expected int
10+
}{
11+
{"INVALID", "URGENT", -1},
12+
{"UNKNOWN", "IMPORTANT", -1},
13+
{"INFORMATIONAL", "URGENT", -1},
14+
{"IMPORTANT", "URGENT", -1},
15+
{"URGENT", "IMPORTANT", 1},
16+
{"IMPORTANT", "INFORMATIONAL", 1},
17+
{"URGENT", "UNKNOWN", 1},
18+
{"IMPORTANT", "INVALID", 1},
19+
{"UNKNOWN", "UNKNOWN", 0},
20+
{"INFORMATIONAL", "INFORMATIONAL", 0},
21+
{"IMPORTANT", "IMPORTANT", 0},
22+
{"URGENT", "URGENT", 0},
23+
}
24+
25+
for _, test := range tests {
26+
result := ComparePriority(test.priority1, test.priority2)
27+
if result != test.expected {
28+
t.Errorf("ComparePriority(%s, %s) = %d; want %d",
29+
test.priority1, test.priority2, result, test.expected)
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)