unit_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package unit
  2. import (
  3. "testing"
  4. )
  5. func TestParseUnit_Strings(t *testing.T) {
  6. tests := []struct {
  7. name string
  8. input string
  9. expect Unit
  10. expectErr bool
  11. }{
  12. // Duration units
  13. {name: "millisecond", input: "ms", expect: Millisecond, expectErr: false},
  14. {name: "second", input: "s", expect: Second, expectErr: false},
  15. {name: "minute", input: "min", expect: Minute, expectErr: false},
  16. {name: "hour", input: "hr", expect: Hour, expectErr: false},
  17. // Data storage units
  18. {name: "byte", input: "B", expect: Byte, expectErr: false},
  19. {name: "kibibyte", input: "KiB", expect: KiB, expectErr: false},
  20. {name: "mebibyte", input: "MiB", expect: MiB, expectErr: false},
  21. {name: "gibibyte", input: "GiB", expect: GiB, expectErr: false},
  22. // Compute resources
  23. {name: "mCPU", input: "mCPU", expect: MCPU, expectErr: false},
  24. {name: "vCPU", input: "vCPU", expect: VCPU, expectErr: false},
  25. {name: "GPU", input: "GPU", expect: GPU, expectErr: false},
  26. // Compute resources over time
  27. {name: "GiB-hr", input: "GiB-hr", expect: GiBHour, expectErr: false},
  28. {name: "vCPU-hr", input: "vCPU-hr", expect: VCPUHour, expectErr: false},
  29. {name: "GPU-hr", input: "GPU-hr", expect: GPUHour, expectErr: false},
  30. // Case insensitive tests
  31. {name: "uppercase ms", input: "MS", expect: Millisecond, expectErr: false},
  32. {name: "mixed case GiB", input: "gib", expect: GiB, expectErr: false},
  33. {name: "mixed case mCPU", input: "Mcpu", expect: MCPU, expectErr: false},
  34. {name: "mixed case vCPU-hr", input: "VCPU-HR", expect: VCPUHour, expectErr: false},
  35. // Invalid units
  36. {name: "invalid empty", input: "", expect: "", expectErr: true},
  37. {name: "invalid unknown", input: "xyz", expect: "", expectErr: true},
  38. {name: "invalid number", input: "123", expect: "", expectErr: true},
  39. {name: "invalid partial", input: "G", expect: "", expectErr: true},
  40. {name: "invalid with space", input: "G B", expect: "", expectErr: true},
  41. }
  42. for _, tt := range tests {
  43. t.Run(tt.name, func(t *testing.T) {
  44. got, err := ParseUnit(tt.input)
  45. if (err != nil) != tt.expectErr {
  46. t.Errorf("ParseUnit(%q) error = %v, expectErr %v", tt.input, err, tt.expectErr)
  47. return
  48. }
  49. if got != tt.expect {
  50. t.Errorf("ParseUnit(%q) = %v, expected %v", tt.input, got, tt.expect)
  51. }
  52. })
  53. }
  54. }
  55. func TestParseUnit_Constants(t *testing.T) {
  56. for _, unit := range validUnits {
  57. t.Run(string(unit), func(t *testing.T) {
  58. parsed, err := ParseUnit(string(unit))
  59. if err != nil {
  60. t.Errorf("ParseUnit(%q) unexpected error: %v", unit, err)
  61. }
  62. if parsed != unit {
  63. t.Errorf("ParseUnit(%q) = %v, expected %v", unit, parsed, unit)
  64. }
  65. })
  66. }
  67. }