growthbook 0.0.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/util_spec.rb ADDED
@@ -0,0 +1,154 @@
1
+ require 'growthbook'
2
+
3
+ describe 'util' do
4
+ describe "checkRule function" do
5
+ it "works for all operators and normal inputs" do
6
+ # =
7
+ expect(Growthbook::Util.checkRule("test", "=", "test")).to eq(true)
8
+ expect(Growthbook::Util.checkRule("test", "=", "other")).to eq(false)
9
+ # !=
10
+ expect(Growthbook::Util.checkRule("test", "!=", "other")).to eq(true)
11
+ expect(Growthbook::Util.checkRule("test", "!=", "test")).to eq(false)
12
+ # >
13
+ expect(Growthbook::Util.checkRule("b", ">", "a")).to eq(true)
14
+ expect(Growthbook::Util.checkRule("a", ">", "b")).to eq(false)
15
+ # <
16
+ expect(Growthbook::Util.checkRule("a", "<", "b")).to eq(true)
17
+ expect(Growthbook::Util.checkRule("b", "<", "a")).to eq(false)
18
+ # ~
19
+ expect(Growthbook::Util.checkRule("123-456-abc", "~", "^[0-9]{3}-[0-9]{3}-[a-z]{3}$")).to eq(true)
20
+ expect(Growthbook::Util.checkRule("123-abc-456", "~", "^[0-9]{3}-[0-9]{3}-[a-z]{3}$")).to eq(false)
21
+ # !~
22
+ expect(Growthbook::Util.checkRule("123-abc-456", "!~", "^[0-9]{3}-[0-9]{3}-[a-z]{3}$")).to eq(true)
23
+ expect(Growthbook::Util.checkRule("123-456-abc", "!~", "^[0-9]{3}-[0-9]{3}-[a-z]{3}$")).to eq(false)
24
+ end
25
+
26
+ it "returns true when there's an unknown operator" do
27
+ expect(Growthbook::Util.checkRule("abc", "*", "123")).to eq(true)
28
+ end
29
+
30
+ it "returns false when the regex is invalid" do
31
+ expect(Growthbook::Util.checkRule("abc", "~", "abc)")).to eq(false)
32
+ end
33
+
34
+ it "compares numeric strings with natural ordering" do
35
+ expect(Growthbook::Util.checkRule("10", ">", "9")).to eq(true)
36
+ expect(Growthbook::Util.checkRule("9", "<", "1000")).to eq(true)
37
+ expect(Growthbook::Util.checkRule("90", ">", "800")).to eq(false)
38
+ expect(Growthbook::Util.checkRule("-10", "<", "10")).to eq(true)
39
+ expect(Growthbook::Util.checkRule("10", ">", "abc")).to eq(false)
40
+ end
41
+
42
+ it "checks for numeric equality properly" do
43
+ expect(Growthbook::Util.checkRule("9.0", "=", "9")).to eq(true)
44
+ expect(Growthbook::Util.checkRule("1.3", "!=", "1.30000")).to eq(false)
45
+ end
46
+
47
+ it "handles empty strings" do
48
+ expect(Growthbook::Util.checkRule("", "=", "")).to eq(true)
49
+ expect(Growthbook::Util.checkRule("", "!=", "")).to eq(false)
50
+ expect(Growthbook::Util.checkRule("", ">", "")).to eq(false)
51
+ expect(Growthbook::Util.checkRule("", "<", "")).to eq(false)
52
+ expect(Growthbook::Util.checkRule("", "~", "")).to eq(true)
53
+ expect(Growthbook::Util.checkRule("", "!~", "")).to eq(false)
54
+ end
55
+ end
56
+
57
+ describe "chooseVariation function" do
58
+ it "does not have a sample ratio mismatch bug" do
59
+ # Full coverage
60
+ experiment = Growthbook::Experiment.new("my-test", 2)
61
+ variations = [0, 0]
62
+ for i in 0..999
63
+ variations[Growthbook::Util.chooseVariation(i.to_s, experiment)] += 1
64
+ end
65
+ expect(variations[0]).to eq(503)
66
+ end
67
+
68
+ it "does not have a sample ratio mismatch bug for reduced coverage" do
69
+ # Reduced coverage
70
+ experiment = Growthbook::Experiment.new("my-test", 2, coverage: 0.4)
71
+ var0 = 0
72
+ var1 = 0
73
+ varn = 0
74
+ for i in 0..999
75
+ result = Growthbook::Util.chooseVariation(i.to_s, experiment)
76
+ case result
77
+ when -1
78
+ varn += 1
79
+ when 0
80
+ var0 += 1
81
+ else
82
+ var1 += 1
83
+ end
84
+ end
85
+ expect(var0).to eq(200)
86
+ expect(var1).to eq(204)
87
+ expect(varn).to eq(596)
88
+ end
89
+
90
+ it "assigns variations with default weights" do
91
+ experiment = Growthbook::Experiment.new("my-test", 2)
92
+
93
+ expect(Growthbook::Util.chooseVariation('1', experiment)).to eq(1)
94
+ expect(Growthbook::Util.chooseVariation('2', experiment)).to eq(0)
95
+ expect(Growthbook::Util.chooseVariation('3', experiment)).to eq(0)
96
+ expect(Growthbook::Util.chooseVariation('4', experiment)).to eq(1)
97
+ expect(Growthbook::Util.chooseVariation('5', experiment)).to eq(1)
98
+ expect(Growthbook::Util.chooseVariation('6', experiment)).to eq(1)
99
+ expect(Growthbook::Util.chooseVariation('7', experiment)).to eq(0)
100
+ expect(Growthbook::Util.chooseVariation('8', experiment)).to eq(1)
101
+ expect(Growthbook::Util.chooseVariation('9', experiment)).to eq(0)
102
+ end
103
+
104
+ it "assigns variations with uneven weights" do
105
+ experiment = Growthbook::Experiment.new("my-test", 2, weights: [0.1, 0.9])
106
+
107
+ expect(Growthbook::Util.chooseVariation('1', experiment)).to eq(1)
108
+ expect(Growthbook::Util.chooseVariation('2', experiment)).to eq(1)
109
+ expect(Growthbook::Util.chooseVariation('3', experiment)).to eq(0)
110
+ expect(Growthbook::Util.chooseVariation('4', experiment)).to eq(1)
111
+ expect(Growthbook::Util.chooseVariation('5', experiment)).to eq(1)
112
+ expect(Growthbook::Util.chooseVariation('6', experiment)).to eq(1)
113
+ expect(Growthbook::Util.chooseVariation('7', experiment)).to eq(0)
114
+ expect(Growthbook::Util.chooseVariation('8', experiment)).to eq(1)
115
+ expect(Growthbook::Util.chooseVariation('9', experiment)).to eq(1)
116
+ end
117
+
118
+ it "assigns variations with reduced coverage" do
119
+ experiment = Growthbook::Experiment.new("my-test", 2, coverage: 0.4)
120
+
121
+ expect(Growthbook::Util.chooseVariation('1', experiment)).to eq(-1)
122
+ expect(Growthbook::Util.chooseVariation('2', experiment)).to eq(0)
123
+ expect(Growthbook::Util.chooseVariation('3', experiment)).to eq(0)
124
+ expect(Growthbook::Util.chooseVariation('4', experiment)).to eq(-1)
125
+ expect(Growthbook::Util.chooseVariation('5', experiment)).to eq(-1)
126
+ expect(Growthbook::Util.chooseVariation('6', experiment)).to eq(-1)
127
+ expect(Growthbook::Util.chooseVariation('7', experiment)).to eq(0)
128
+ expect(Growthbook::Util.chooseVariation('8', experiment)).to eq(-1)
129
+ expect(Growthbook::Util.chooseVariation('9', experiment)).to eq(1)
130
+ end
131
+
132
+ it "assigns variations with default 3 variations" do
133
+ experiment = Growthbook::Experiment.new("my-test", 3)
134
+
135
+ expect(Growthbook::Util.chooseVariation('1', experiment)).to eq(2)
136
+ expect(Growthbook::Util.chooseVariation('2', experiment)).to eq(0)
137
+ expect(Growthbook::Util.chooseVariation('3', experiment)).to eq(0)
138
+ expect(Growthbook::Util.chooseVariation('4', experiment)).to eq(2)
139
+ expect(Growthbook::Util.chooseVariation('5', experiment)).to eq(1)
140
+ expect(Growthbook::Util.chooseVariation('6', experiment)).to eq(2)
141
+ expect(Growthbook::Util.chooseVariation('7', experiment)).to eq(0)
142
+ expect(Growthbook::Util.chooseVariation('8', experiment)).to eq(1)
143
+ expect(Growthbook::Util.chooseVariation('9', experiment)).to eq(0)
144
+ end
145
+
146
+ it "uses experiment name to choose a variation" do
147
+ experiment1 = Growthbook::Experiment.new("my-test", 2)
148
+ experiment2 = Growthbook::Experiment.new("my-test-3", 2)
149
+
150
+ expect(Growthbook::Util.chooseVariation('1', experiment1)).to eq(1)
151
+ expect(Growthbook::Util.chooseVariation('1', experiment2)).to eq(0)
152
+ end
153
+ end
154
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: growthbook
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GrowthBook
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-12-22 00:00:00.000000000 Z
11
+ date: 2022-09-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -46,11 +46,24 @@ extra_rdoc_files: []
46
46
  files:
47
47
  - lib/growthbook.rb
48
48
  - lib/growthbook/client.rb
49
+ - lib/growthbook/conditions.rb
50
+ - lib/growthbook/context.rb
49
51
  - lib/growthbook/experiment.rb
50
52
  - lib/growthbook/experiment_result.rb
53
+ - lib/growthbook/feature.rb
54
+ - lib/growthbook/feature_result.rb
55
+ - lib/growthbook/feature_rule.rb
56
+ - lib/growthbook/inline_experiment.rb
57
+ - lib/growthbook/inline_experiment_result.rb
51
58
  - lib/growthbook/lookup_result.rb
52
59
  - lib/growthbook/user.rb
53
60
  - lib/growthbook/util.rb
61
+ - spec/cases.json
62
+ - spec/client_spec.rb
63
+ - spec/context_spec.rb
64
+ - spec/json_spec.rb
65
+ - spec/user_spec.rb
66
+ - spec/util_spec.rb
54
67
  homepage: https://github.com/growthbook/growthbook-ruby
55
68
  licenses:
56
69
  - MIT
@@ -74,4 +87,10 @@ rubygems_version: 3.1.2
74
87
  signing_key:
75
88
  specification_version: 4
76
89
  summary: GrowthBook SDK for Ruby
77
- test_files: []
90
+ test_files:
91
+ - spec/json_spec.rb
92
+ - spec/user_spec.rb
93
+ - spec/util_spec.rb
94
+ - spec/context_spec.rb
95
+ - spec/client_spec.rb
96
+ - spec/cases.json