You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

174 lines
4.4 KiB

  1. // Copyright 2016 Google LLC
  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 main
  15. import (
  16. "testing"
  17. "time"
  18. "cloud.google.com/go/bigtable"
  19. "cloud.google.com/go/internal/testutil"
  20. "github.com/google/go-cmp/cmp"
  21. )
  22. func TestParseDuration(t *testing.T) {
  23. tests := []struct {
  24. in string
  25. // out or fail are mutually exclusive
  26. out time.Duration
  27. fail bool
  28. }{
  29. {in: "10ms", out: 10 * time.Millisecond},
  30. {in: "3s", out: 3 * time.Second},
  31. {in: "60m", out: 60 * time.Minute},
  32. {in: "12h", out: 12 * time.Hour},
  33. {in: "7d", out: 168 * time.Hour},
  34. {in: "", fail: true},
  35. {in: "0", fail: true},
  36. {in: "7ns", fail: true},
  37. {in: "14mo", fail: true},
  38. {in: "3.5h", fail: true},
  39. {in: "106752d", fail: true}, // overflow
  40. }
  41. for _, tc := range tests {
  42. got, err := parseDuration(tc.in)
  43. if !tc.fail && err != nil {
  44. t.Errorf("parseDuration(%q) unexpectedly failed: %v", tc.in, err)
  45. continue
  46. }
  47. if tc.fail && err == nil {
  48. t.Errorf("parseDuration(%q) did not fail", tc.in)
  49. continue
  50. }
  51. if tc.fail {
  52. continue
  53. }
  54. if got != tc.out {
  55. t.Errorf("parseDuration(%q) = %v, want %v", tc.in, got, tc.out)
  56. }
  57. }
  58. }
  59. func TestParseArgs(t *testing.T) {
  60. got, err := parseArgs([]string{"a=1", "b=2"}, []string{"a", "b"})
  61. if err != nil {
  62. t.Fatal(err)
  63. }
  64. want := map[string]string{"a": "1", "b": "2"}
  65. if !testutil.Equal(got, want) {
  66. t.Fatalf("got %v, want %v", got, want)
  67. }
  68. if _, err := parseArgs([]string{"a1"}, []string{"a1"}); err == nil {
  69. t.Error("malformed: got nil, want error")
  70. }
  71. if _, err := parseArgs([]string{"a=1"}, []string{"b"}); err == nil {
  72. t.Error("invalid: got nil, want error")
  73. }
  74. }
  75. func TestParseColumnsFilter(t *testing.T) {
  76. tests := []struct {
  77. in string
  78. out bigtable.Filter
  79. fail bool
  80. }{
  81. {
  82. in: "columnA",
  83. out: bigtable.ColumnFilter("columnA"),
  84. },
  85. {
  86. in: "familyA:columnA",
  87. out: bigtable.ChainFilters(bigtable.FamilyFilter("familyA"), bigtable.ColumnFilter("columnA")),
  88. },
  89. {
  90. in: "columnA,columnB",
  91. out: bigtable.InterleaveFilters(bigtable.ColumnFilter("columnA"), bigtable.ColumnFilter("columnB")),
  92. },
  93. {
  94. in: "familyA:columnA,columnB",
  95. out: bigtable.InterleaveFilters(
  96. bigtable.ChainFilters(bigtable.FamilyFilter("familyA"), bigtable.ColumnFilter("columnA")),
  97. bigtable.ColumnFilter("columnB"),
  98. ),
  99. },
  100. {
  101. in: "columnA,familyB:columnB",
  102. out: bigtable.InterleaveFilters(
  103. bigtable.ColumnFilter("columnA"),
  104. bigtable.ChainFilters(bigtable.FamilyFilter("familyB"), bigtable.ColumnFilter("columnB")),
  105. ),
  106. },
  107. {
  108. in: "familyA:columnA,familyB:columnB",
  109. out: bigtable.InterleaveFilters(
  110. bigtable.ChainFilters(bigtable.FamilyFilter("familyA"), bigtable.ColumnFilter("columnA")),
  111. bigtable.ChainFilters(bigtable.FamilyFilter("familyB"), bigtable.ColumnFilter("columnB")),
  112. ),
  113. },
  114. {
  115. in: "familyA:",
  116. out: bigtable.FamilyFilter("familyA"),
  117. },
  118. {
  119. in: ":columnA",
  120. out: bigtable.ColumnFilter("columnA"),
  121. },
  122. {
  123. in: ",:columnA,,familyB:columnB,",
  124. out: bigtable.InterleaveFilters(
  125. bigtable.ColumnFilter("columnA"),
  126. bigtable.ChainFilters(bigtable.FamilyFilter("familyB"), bigtable.ColumnFilter("columnB")),
  127. ),
  128. },
  129. {
  130. in: "familyA:columnA:cellA",
  131. fail: true,
  132. },
  133. {
  134. in: "familyA::columnA",
  135. fail: true,
  136. },
  137. }
  138. for _, tc := range tests {
  139. got, err := parseColumnsFilter(tc.in)
  140. if !tc.fail && err != nil {
  141. t.Errorf("parseColumnsFilter(%q) unexpectedly failed: %v", tc.in, err)
  142. continue
  143. }
  144. if tc.fail && err == nil {
  145. t.Errorf("parseColumnsFilter(%q) did not fail", tc.in)
  146. continue
  147. }
  148. if tc.fail {
  149. continue
  150. }
  151. var cmpOpts cmp.Options
  152. cmpOpts =
  153. append(
  154. cmpOpts,
  155. cmp.AllowUnexported(bigtable.ChainFilters([]bigtable.Filter{}...)),
  156. cmp.AllowUnexported(bigtable.InterleaveFilters([]bigtable.Filter{}...)))
  157. if !cmp.Equal(got, tc.out, cmpOpts) {
  158. t.Errorf("parseColumnsFilter(%q) = %v, want %v", tc.in, got, tc.out)
  159. }
  160. }
  161. }