growthbook 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/util_spec.rb DELETED
@@ -1,154 +0,0 @@
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