forked from nkumeiko/asari
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_helper.rb
135 lines (105 loc) · 2.29 KB
/
spec_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
require 'asari'
require 'asari/active_record'
require 'ostruct'
require 'simplecov'
SimpleCov.start
# Fake production mode to test.
Asari.mode = :production
RSpec.configuration.expect_with(:rspec) { |c| c.syntax = :expect }
def fake_response
OpenStruct.new(:parsed_response => { "hits" => {
"found" => 2,
"start" => 0,
"hit" => [{"id" => "123"}, {"id" => "456"}]}},
:response => OpenStruct.new(:code => "200"))
end
def fake_empty_response
OpenStruct.new(:parsed_response => { "hits" => { "found" => 0, "start" => 0, "hit" => []}},
:response => OpenStruct.new(:code => "200"))
end
def fake_error_response
OpenStruct.new(:response => OpenStruct.new(:code => "404"))
end
def fake_post_success
OpenStruct.new(:response => OpenStruct.new(:code => "200"))
end
module ActiveRecord
class RecordNotFound < StandardError
end
end
class ActiveRecordFake
class << self
def before_destroy(sym)
@before_destroy = sym
end
def after_create(sym)
@after_create = sym
end
def after_update(sym)
@after_update = sym
end
def where(query, ids)
if ids.size > 0
[ActiveRecordFake.new]
else
[]
end
end
end
include Asari::ActiveRecord
asari_index("test-domain", [:name, :email])
def id
1
end
def name
"Fritters"
end
def email
"fritters@aredelicious.com"
end
end
class ActiveRecordFakeWithErrorOverride < ActiveRecordFake
def self.asari_on_error(exception)
false
end
end
class ARConditionalsSpy
attr_accessor :be_indexable
attr_accessor :was_asked
class << self
def before_destroy(sym)
@before_destroy = sym
end
def after_create(sym)
@after_create = sym
end
def after_update(sym)
@after_update = sym
end
def find(*args)
if args.size > 0
return [ARConditionalsSpy.new]
else
raise ActiveRecord::RecordNotFound
end
end
end
include Asari::ActiveRecord
asari_index("test-domain", [:name, :email], :when => :indexable)
def initialize
@was_asked = false
end
def id
1
end
def name
"Tommy"
end
def email
"some@email.com"
end
def indexable
@was_asked = true
@be_indexable
end
end