pending_usage_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestNewResourcePendingUsage(t *testing.T) {
  20. tests := []struct {
  21. name string
  22. args map[string]int
  23. want map[string]int
  24. }{
  25. {
  26. name: "map init",
  27. args: map[string]int{"s1": 1, "s2": 2},
  28. want: map[string]int{"s1": 1, "s2": 2},
  29. },
  30. {
  31. name: "map nil init",
  32. args: nil,
  33. want: map[string]int{},
  34. },
  35. }
  36. for _, tt := range tests {
  37. t.Run(tt.name, func(t *testing.T) {
  38. if got := NewResourcePendingUsage(tt.args); !reflect.DeepEqual(got.ToMap(), tt.want) {
  39. t.Errorf("NewResourcePendingUsage() = %v, want %v", got, tt.want)
  40. }
  41. })
  42. }
  43. }
  44. func TestSResourcePendingUsage_Add(t *testing.T) {
  45. tests := []struct {
  46. name string
  47. ou *SResourcePendingUsage
  48. su *SResourcePendingUsage
  49. want map[string]int
  50. }{
  51. {
  52. name: "same add",
  53. ou: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 2}),
  54. su: NewResourcePendingUsage(map[string]int{"k1": 3, "k2": 1}),
  55. want: map[string]int{"k1": 4, "k2": 3},
  56. },
  57. {
  58. name: "diff add",
  59. ou: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 2}),
  60. su: NewResourcePendingUsage(map[string]int{"k1": 3, "k2": 1, "k3": 4}),
  61. want: map[string]int{"k1": 4, "k2": 3, "k3": 4},
  62. },
  63. {
  64. name: "add nil",
  65. ou: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 2}),
  66. su: NewResourcePendingUsage(nil),
  67. want: map[string]int{"k1": 1, "k2": 2},
  68. },
  69. }
  70. for _, tt := range tests {
  71. t.Run(tt.name, func(t *testing.T) {
  72. tt.ou.Add(tt.su)
  73. if got := tt.ou.ToMap(); !reflect.DeepEqual(got, tt.want) {
  74. t.Errorf("NewResourcePendingUsage_add() = %v, want %v", got, tt.want)
  75. }
  76. })
  77. }
  78. }
  79. func TestSResourcePendingUsage_Sub(t *testing.T) {
  80. tests := []struct {
  81. name string
  82. ou *SResourcePendingUsage
  83. su *SResourcePendingUsage
  84. want map[string]int
  85. }{
  86. {
  87. name: "same sub",
  88. ou: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 2}),
  89. su: NewResourcePendingUsage(map[string]int{"k1": 3, "k2": 1}),
  90. want: map[string]int{"k1": 0, "k2": 1},
  91. },
  92. {
  93. name: "sub nil",
  94. ou: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 2}),
  95. su: NewResourcePendingUsage(nil),
  96. want: map[string]int{"k1": 1, "k2": 2},
  97. },
  98. }
  99. for _, tt := range tests {
  100. t.Run(tt.name, func(t *testing.T) {
  101. tt.ou.Sub(tt.su)
  102. if got := tt.ou.ToMap(); !reflect.DeepEqual(got, tt.want) {
  103. t.Errorf("NewResourcePendingUsage_add() = %v, want %v", got, tt.want)
  104. }
  105. })
  106. }
  107. }
  108. func TestSResourcePendingUsage_IsEmpty(t *testing.T) {
  109. tests := []struct {
  110. name string
  111. u *SResourcePendingUsage
  112. want bool
  113. }{
  114. {
  115. name: "nil is empty",
  116. u: NewResourcePendingUsage(nil),
  117. want: true,
  118. },
  119. {
  120. name: "item zeros is empty",
  121. u: NewResourcePendingUsage(map[string]int{"k1": 0, "k2": 0}),
  122. want: true,
  123. },
  124. {
  125. name: "item no zeros not empty",
  126. u: NewResourcePendingUsage(map[string]int{"k1": 1, "k2": 0}),
  127. want: false,
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. if got := tt.u.IsEmpty(); got != tt.want {
  133. t.Errorf("SResourcePendingUsage.IsEmpty() = %v, want %v", got, tt.want)
  134. }
  135. })
  136. }
  137. }