query_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2016 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package currency
  5. import (
  6. "testing"
  7. "time"
  8. "golang.org/x/text/language"
  9. )
  10. func TestQuery(t *testing.T) {
  11. r := func(region string) language.Region {
  12. return language.MustParseRegion(region)
  13. }
  14. t1800, _ := time.Parse("2006-01-02", "1800-01-01")
  15. type result struct {
  16. region language.Region
  17. unit Unit
  18. isTender bool
  19. from, to string
  20. }
  21. testCases := []struct {
  22. name string
  23. opts []QueryOption
  24. results []result
  25. }{{
  26. name: "XA",
  27. opts: []QueryOption{Region(r("XA"))},
  28. results: []result{},
  29. }, {
  30. name: "AC",
  31. opts: []QueryOption{Region(r("AC"))},
  32. results: []result{
  33. {r("AC"), MustParseISO("SHP"), true, "1976-01-01", ""},
  34. },
  35. }, {
  36. name: "US",
  37. opts: []QueryOption{Region(r("US"))},
  38. results: []result{
  39. {r("US"), MustParseISO("USD"), true, "1792-01-01", ""},
  40. },
  41. }, {
  42. name: "US-hist",
  43. opts: []QueryOption{Region(r("US")), Historical},
  44. results: []result{
  45. {r("US"), MustParseISO("USD"), true, "1792-01-01", ""},
  46. },
  47. }, {
  48. name: "US-non-tender",
  49. opts: []QueryOption{Region(r("US")), NonTender},
  50. results: []result{
  51. {r("US"), MustParseISO("USD"), true, "1792-01-01", ""},
  52. {r("US"), MustParseISO("USN"), false, "", ""},
  53. },
  54. }, {
  55. name: "US-historical+non-tender",
  56. opts: []QueryOption{Region(r("US")), Historical, NonTender},
  57. results: []result{
  58. {r("US"), MustParseISO("USD"), true, "1792-01-01", ""},
  59. {r("US"), MustParseISO("USN"), false, "", ""},
  60. {r("US"), MustParseISO("USS"), false, "", "2014-03-01"},
  61. },
  62. }, {
  63. name: "1800",
  64. opts: []QueryOption{Date(t1800)},
  65. results: []result{
  66. {r("CH"), MustParseISO("CHF"), true, "1799-03-17", ""},
  67. {r("GB"), MustParseISO("GBP"), true, "1694-07-27", ""},
  68. {r("GI"), MustParseISO("GIP"), true, "1713-01-01", ""},
  69. // The date for IE and PR seem wrong, so these may be updated at
  70. // some point causing the tests to fail.
  71. {r("IE"), MustParseISO("GBP"), true, "1800-01-01", "1922-01-01"},
  72. {r("PR"), MustParseISO("ESP"), true, "1800-01-01", "1898-12-10"},
  73. {r("US"), MustParseISO("USD"), true, "1792-01-01", ""},
  74. },
  75. }}
  76. for _, tc := range testCases {
  77. n := 0
  78. for it := Query(tc.opts...); it.Next(); n++ {
  79. if n < len(tc.results) {
  80. got := result{
  81. it.Region(),
  82. it.Unit(),
  83. it.IsTender(),
  84. getTime(it.From()),
  85. getTime(it.To()),
  86. }
  87. if got != tc.results[n] {
  88. t.Errorf("%s:%d: got %v; want %v", tc.name, n, got, tc.results[n])
  89. }
  90. }
  91. }
  92. if n != len(tc.results) {
  93. t.Errorf("%s: unexpected number of results: got %d; want %d", tc.name, n, len(tc.results))
  94. }
  95. }
  96. }
  97. func getTime(t time.Time, ok bool) string {
  98. if !ok {
  99. return ""
  100. }
  101. return t.Format("2006-01-02")
  102. }