billingexportparser_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package azure
  2. import (
  3. "encoding/csv"
  4. "os"
  5. "testing"
  6. "time"
  7. )
  8. const billingExportPath = "./resources/billingexports/"
  9. const headerSetPath = billingExportPath + "headersets/"
  10. const valueCasesPath = billingExportPath + "values/"
  11. type TestCSVRetriever struct {
  12. CSVName string
  13. }
  14. func (tcr TestCSVRetriever) getCSVReaders(start, end time.Time) ([]*csv.Reader, error) {
  15. csvFile, err := os.Open(tcr.CSVName)
  16. if err != nil {
  17. return nil, err
  18. }
  19. reader := csv.NewReader(csvFile)
  20. return append([]*csv.Reader{}, reader), nil
  21. }
  22. func Test_NewBillingExportParser(t *testing.T) {
  23. loc, _ := time.LoadLocation("UTC")
  24. start := time.Date(2021, 2, 1, 00, 00, 00, 00, loc)
  25. end := time.Date(2021, 2, 3, 00, 00, 00, 00, loc)
  26. tests := map[string]struct {
  27. input string
  28. expected BillingExportParser
  29. }{
  30. "English Headers": {
  31. input: "PayAsYouGo.csv",
  32. expected: BillingExportParser{
  33. Date: 3,
  34. MeterCategory: 4,
  35. InvoiceEntityID: 0,
  36. SubscriptionID: 0,
  37. InstanceID: 14,
  38. Service: 12,
  39. Tags: 15,
  40. AdditionalInfo: 17,
  41. Cost: 11,
  42. NetCost: 11,
  43. DateFormat: azureDateLayout,
  44. },
  45. },
  46. "Enterprise Camel Headers": {
  47. input: "EnterpriseCamel.csv",
  48. expected: BillingExportParser{
  49. Date: 11,
  50. MeterCategory: 18,
  51. InvoiceEntityID: 0,
  52. SubscriptionID: 23,
  53. InstanceID: 29,
  54. Service: 15,
  55. Tags: 45,
  56. AdditionalInfo: 44,
  57. Cost: 38,
  58. NetCost: 38,
  59. DateFormat: AzureEnterpriseDateLayout,
  60. },
  61. },
  62. "Enterprise Headers": {
  63. input: "Enterprise.csv",
  64. expected: BillingExportParser{
  65. Date: 7,
  66. MeterCategory: 9,
  67. InvoiceEntityID: 39,
  68. SubscriptionID: 3,
  69. InstanceID: 20,
  70. Service: 19,
  71. Tags: 21,
  72. AdditionalInfo: 23,
  73. Cost: 17,
  74. NetCost: 17,
  75. DateFormat: AzureEnterpriseDateLayout,
  76. },
  77. },
  78. "German Headers": {
  79. input: "German.csv",
  80. expected: BillingExportParser{
  81. Date: 3,
  82. MeterCategory: 4,
  83. InvoiceEntityID: 0,
  84. SubscriptionID: 0,
  85. InstanceID: 14,
  86. Service: 12,
  87. Tags: 15,
  88. AdditionalInfo: 17,
  89. Cost: 11,
  90. NetCost: 11,
  91. DateFormat: azureDateLayout,
  92. },
  93. },
  94. "YA Headers": {
  95. input: "YA.csv",
  96. expected: BillingExportParser{
  97. Date: 3,
  98. MeterCategory: 4,
  99. InvoiceEntityID: 0,
  100. SubscriptionID: 0,
  101. InstanceID: 14,
  102. Service: 12,
  103. Tags: 15,
  104. AdditionalInfo: 17,
  105. Cost: 11,
  106. NetCost: 11,
  107. DateFormat: AzureEnterpriseDateLayout,
  108. },
  109. },
  110. "BOM Prefixed Headers": {
  111. input: "BOM.csv",
  112. expected: BillingExportParser{
  113. Date: 3,
  114. MeterCategory: 4,
  115. InvoiceEntityID: 0,
  116. SubscriptionID: 0,
  117. InstanceID: 14,
  118. Service: 12,
  119. Tags: 15,
  120. AdditionalInfo: 17,
  121. Cost: 11,
  122. NetCost: 11,
  123. DateFormat: azureDateLayout,
  124. },
  125. },
  126. }
  127. for name, tc := range tests {
  128. t.Run(name, func(t *testing.T) {
  129. csvRetriever := TestCSVRetriever{
  130. CSVName: headerSetPath + tc.input,
  131. }
  132. csvs, err := csvRetriever.getCSVReaders(start, end)
  133. if err != nil {
  134. t.Errorf("Failed to read specified CSV: %s", err.Error())
  135. }
  136. reader := csvs[0]
  137. headers, _ := reader.Read()
  138. abp, err := NewBillingParseSchema(headers)
  139. if err != nil {
  140. t.Errorf("failed to create Azure Billing Parser from headers with error: %s", err.Error())
  141. }
  142. if abp.DateFormat != tc.expected.DateFormat {
  143. t.Errorf("Azure Billing Parser does not have expected DateFormat index. Expected: %s, Actual: %s", tc.expected.DateFormat, abp.DateFormat)
  144. }
  145. if abp.Date != tc.expected.Date {
  146. t.Errorf("Azure Billing Parser does not have expected Date index. Expected: %d, Actual: %d", tc.expected.Date, abp.Date)
  147. }
  148. if abp.MeterCategory != tc.expected.MeterCategory {
  149. t.Errorf("Azure Billing Parser does not have expected MeterCategory index. Expected: %d, Actual: %d", tc.expected.MeterCategory, abp.MeterCategory)
  150. }
  151. if abp.InvoiceEntityID != tc.expected.InvoiceEntityID {
  152. t.Errorf("Azure Billing Parser does not have expected InvoiceEntityID index. Expected: %d, Actual: %d", tc.expected.InvoiceEntityID, abp.InvoiceEntityID)
  153. }
  154. if abp.SubscriptionID != tc.expected.SubscriptionID {
  155. t.Errorf("Azure Billing Parser does not have expected SubscriptionID index. Expected: %d, Actual: %d", tc.expected.SubscriptionID, abp.SubscriptionID)
  156. }
  157. if abp.InstanceID != tc.expected.InstanceID {
  158. t.Errorf("Azure Billing Parser does not have expected InstanceID index. Expected: %d, Actual: %d", tc.expected.InstanceID, abp.InstanceID)
  159. }
  160. if abp.Service != tc.expected.Service {
  161. t.Errorf("Azure Billing Parser does not have expected Service index. Expected: %d, Actual: %d", tc.expected.Service, abp.Service)
  162. }
  163. if abp.Tags != tc.expected.Tags {
  164. t.Errorf("Azure Billing Parser does not have expected Tags index. Expected: %d, Actual: %d", tc.expected.Tags, abp.Tags)
  165. }
  166. if abp.AdditionalInfo != tc.expected.AdditionalInfo {
  167. t.Errorf("Azure Billing Parser does not have expected AdditionalInfo index. Expected: %d, Actual: %d", tc.expected.AdditionalInfo, abp.AdditionalInfo)
  168. }
  169. if abp.Cost != tc.expected.Cost {
  170. t.Errorf("Azure Billing Parser does not have expected Cost index. Expected: %d, Actual: %d", tc.expected.Cost, abp.Cost)
  171. }
  172. if abp.NetCost != tc.expected.NetCost {
  173. t.Errorf("Azure Billing Parser does not have expected NetCost index. Expected: %d, Actual: %d", tc.expected.NetCost, abp.NetCost)
  174. }
  175. })
  176. }
  177. }