td-logger 0.3.27 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,155 +0,0 @@
1
- require 'spec_helper'
2
- require 'fileutils'
3
- require 'logger'
4
-
5
- TMP_DIR = REPO_ROOT.join("tmp")
6
- FileUtils.rm_rf(TMP_DIR)
7
-
8
- class Rails
9
- def self.configuration
10
- self.new
11
- end
12
-
13
- def self.logger
14
- Logger.new(STDOUT)
15
- end
16
-
17
- def self.root
18
- TMP_DIR
19
- end
20
-
21
- def self.env
22
- "test"
23
- end
24
-
25
- def middleware
26
- self
27
- end
28
-
29
- def use(mod)
30
- end
31
- end
32
-
33
- class ActionController
34
- class Base
35
- end
36
- end
37
-
38
- class ActiveSupport
39
- class TimeWithZone
40
- end
41
- end
42
-
43
- require 'td/logger/agent/rails' # NOTE: should be require after Rails goodies mocked
44
-
45
- describe TreasureData::Logger::Agent::Rails do
46
- describe "#init" do
47
- shared_examples_for "setup Rails hooks" do
48
- it "returns true" do
49
- expect(subject).to eq true
50
- end
51
-
52
- it "middleware setup" do
53
- expect(rails).to receive(:middleware).and_return(rails);
54
- subject
55
- end
56
-
57
- it "Rack hooks register" do
58
- expect(TreasureData::Logger::Agent::Rack::Hook).to receive(:before)
59
- subject
60
- end
61
-
62
- it "TreasureData::Logger.event.attribute.clear registered on before hook" do
63
- expect { subject }.to change{
64
- # TODO: should be refactor later @@before
65
- TreasureData::Logger::Agent::Rack::Hook.class_variable_get(:@@before).length
66
- }.by(1)
67
- end
68
-
69
- it "registerd before hook called TreasureData::Logger.event.attribtue.clear" do
70
- subject # for register hooks
71
- null = double.as_null_object
72
- allow(TreasureData::Logger).to receive(:event).and_return(null)
73
- allow(null).to receive(:attribtue).and_return(null)
74
-
75
- # TODO: should be refactor later @@before
76
- TreasureData::Logger::Agent::Rack::Hook.class_variable_get(:@@before).last.call
77
- expect(null).to have_received(:clear)
78
- end
79
-
80
- it "mixin ControllerExtension" do
81
- expect(TreasureData::Logger::Agent::Rails::ControllerExtension).to receive(:init)
82
- subject
83
- end
84
- end
85
-
86
- let(:config) { TreasureData::Logger::Agent::Rails::Config.new }
87
- let(:rails) { Rails.new }
88
-
89
- before { allow(TreasureData::Logger::Agent::Rails::Config).to receive(:init).and_return(config) }
90
- before do
91
- fixture_of_methods.each do |method, value|
92
- allow(config).to receive(method).and_return(value)
93
- end
94
- end
95
-
96
- subject { TreasureData::Logger::Agent::Rails.init(rails) }
97
-
98
- context "config.disable = true" do
99
- let(:fixture_of_methods) { {disabled: true} }
100
-
101
- it { expect(subject).to eq false }
102
- it { expect(::TreasureData::Logger).to receive(:open_null).with(no_args); subject }
103
- end
104
-
105
- context "config.test_mode? == true" do
106
- let(:fixture_of_methods) { {disabled: false, "test_mode?" => true} }
107
-
108
- it_behaves_like "setup Rails hooks"
109
- it { expect(::TreasureData::Logger).to receive(:open_test).with(no_args); subject }
110
- end
111
-
112
- context "config.agent_mode? == true" do
113
- let(:fixture_of_methods) { {
114
- disabled: false,
115
- "test_mode?" => false,
116
- "agent_mode?" => true,
117
- tag: "tag",
118
- agent_host: "agent-host",
119
- agent_port: "9999",
120
- debug_mode: "debug"
121
- } }
122
-
123
- it_behaves_like "setup Rails hooks"
124
- it {
125
- expect(::TreasureData::Logger).to receive(:open_agent).with(config.tag, {host: config.agent_host, port: config.agent_port, debug: config.debug_mode})
126
- subject
127
- }
128
- end
129
-
130
- context "config.agent_mode? and test_mode? both false" do
131
- let(:fixture_of_methods) { {
132
- disabled: false,
133
- "test_mode?" => false,
134
- "agent_mode?" => false,
135
- apikey: "APIKEY",
136
- database: "DB",
137
- auto_create_table: true,
138
- debug_mode: "debug"
139
- } }
140
-
141
- it_behaves_like "setup Rails hooks"
142
- it {
143
- expect(::TreasureData::Logger).to receive(:open).with(config.database, apikey: config.apikey, auto_create_table: config.auto_create_table, debug: config.debug_mode)
144
- subject
145
- }
146
- end
147
- end
148
- end
149
-
150
-
151
- describe ActiveSupport::TimeWithZone do
152
- it 'has to_msgpack' do
153
- expect(ActiveSupport::TimeWithZone.method_defined?(:to_msgpack)).to eq(true)
154
- end
155
- end
@@ -1,136 +0,0 @@
1
-
2
- require 'spec_helper'
3
-
4
- describe TreasureData::Logger::Event do
5
- describe 'EventPreset' do
6
- let(:test_logger) do
7
- Fluent::Logger::TestLogger.new
8
- end
9
- before(:each) do
10
- t = test_logger
11
- TreasureData::Logger.module_eval do
12
- class_variable_set(:@@logger, t)
13
- end
14
- TD.event.attribute.clear
15
- end
16
-
17
- describe 'action' do
18
- context "`uid` filled" do
19
- it do
20
- expect(test_logger).to receive(:post).with(:doit, {:action=>"doit", :foo=>:bar, :uid=>"uid1"}).twice
21
- TD.event.action(:doit, {:foo=>:bar}, "uid1")
22
- TD.event.attribute[:uid] = "uid1"
23
- TD.event.action(:doit, {:foo=>:bar})
24
- end
25
- end
26
-
27
- context "`uid` unfilled" do
28
- it do
29
- expect { TD.event.action(:doit, {:foo=>:bar}, nil) }.to raise_error(ArgumentError)
30
- end
31
- end
32
- end
33
-
34
- describe 'register' do
35
- context "`uid` filled" do
36
- it do
37
- expect(test_logger).to receive(:post).with(:register, {:action=>"register", :uid=>"uid1"}).twice
38
- TD.event.register("uid1")
39
- TD.event.attribute[:uid] = "uid1"
40
- TD.event.register
41
- end
42
- end
43
-
44
- context "`uid` unfilled" do
45
- it do
46
- expect { TD.event.register(nil) }.to raise_error(ArgumentError)
47
- end
48
- end
49
- end
50
-
51
- describe 'login' do
52
- context "`uid` filled" do
53
- it do
54
- expect(test_logger).to receive(:post).with(:login, {:action=>"login", :uid=>"uid1"}).twice
55
- TD.event.login("uid1")
56
- TD.event.attribute[:uid] = "uid1"
57
- TD.event.login
58
- end
59
- end
60
-
61
- context "`uid` unfilled" do
62
- it do
63
- expect { TD.event.login(nil) }.to raise_error(ArgumentError)
64
- end
65
- end
66
- end
67
-
68
- describe 'pay' do
69
- context "`uid` filled" do
70
- it do
71
- expect(test_logger).to receive(:post).with(:pay, {:action=>"pay", :category=>"cat", :sub_category=>"subcat", :name=>"name", :price=>1980, :count=>1, :uid=>"uid1"}).twice
72
- TD.event.pay("cat", "subcat", "name", 1980, 1, "uid1")
73
- TD.event.attribute[:uid] = "uid1"
74
- TD.event.pay("cat", "subcat", "name", 1980, 1)
75
- end
76
- end
77
-
78
- context "`uid` unfilled" do
79
- it do
80
- expect { TD.event.pay("cat", "subcat", "name", 1980, 1, nil) }.to raise_error(ArgumentError)
81
- end
82
- end
83
- end
84
- end
85
-
86
- describe "Event" do
87
- let(:attrs) { {default: "attr"} }
88
- let(:event) { TreasureData::Logger::Event.new }
89
-
90
- describe "#post" do
91
- subject { event.post(action, record) }
92
- let(:action) { "act" }
93
- let(:record) { {hello: "hello"} }
94
-
95
- context "with default attributes" do
96
- before { event.attribute = attrs }
97
-
98
- it "invoke TreasureData::Logger.post" do
99
- expect(TreasureData::Logger).to receive(:post).with(action, attrs.merge(record))
100
- subject
101
- end
102
- end
103
-
104
- context "without default attributes" do
105
- it "invoke TreasureData::Logger.post" do
106
- expect(TreasureData::Logger).to receive(:post).with(action, record)
107
- subject
108
- end
109
- end
110
- end
111
-
112
- describe "#post_with_time" do
113
- subject { event.post_with_time(action, record, time) }
114
- let(:action) { "act" }
115
- let(:record) { {hello: "hello"} }
116
- let(:time) { Time.now }
117
-
118
- context "with default attributes" do
119
- before { event.attribute = attrs }
120
-
121
- it "invoke TreasureData::Logger.post_with_time" do
122
- expect(TreasureData::Logger).to receive(:post_with_time).with(action, attrs.merge(record), time)
123
- subject
124
- end
125
- end
126
-
127
- context "without default attributes" do
128
- it "invoke TreasureData::Logger.post_with_time" do
129
- expect(TreasureData::Logger).to receive(:post_with_time).with(action, record, time)
130
- subject
131
- end
132
- end
133
- end
134
- end
135
- end
136
-