/
test_helper.rb
184 lines (149 loc) · 4.18 KB
/
test_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
require 'active_record'
require 'minitest/autorun'
require 'bourne'
require 'database_cleaner'
if ENV['TEST_UNPATCHED_AMS']
require 'active_model_serializers'
else
require 'postgres_ext/serializers'
end
unless ENV['CI']
begin
require 'pry'
require 'pry-highlight'
require 'pry-byebug' unless RUBY_PLATFORM =~ /java/
rescue LoadError => e
STDERR.puts "test/test_helper.rb:#{__LINE__}: #{e.message}"
end
end
require 'dotenv'
Dotenv.load
ActiveRecord::Base.establish_connection(ENV['DATABASE_URL'])
class TestController < ActionController::Base
def url_options
{}
end
end
class Person < ActiveRecord::Base
def self.full_name__sql
"first_name || ' ' || last_name"
end
end
class PeopleController < TestController; end
class PersonSerializer < ActiveModel::Serializer
attributes :id, :full_name, :attendance_name
def attendance_name__sql
if current_user && current_user[:admin]
"'ADMIN ' || last_name || ', ' || first_name"
else
"last_name || ', ' || first_name"
end
end
end
class Note < ActiveRecord::Base
has_many :tags
has_many :sorted_tags
has_many :custom_sorted_tags, lambda { order(:name) }, class_name: 'Tag'
has_many :popular_tags, lambda { where(popular: true) }, class_name: 'Tag'
end
class NotesController < TestController; end
class NoteSerializer < ActiveModel::Serializer
attributes :id, :content, :name
has_many :tags
embed :ids, include: true
end
class ShortTagSerializer < ActiveModel::Serializer
attributes :id, :name
end
class SortedTagSerializer < ActiveModel::Serializer
attributes :id, :name
end
class CustomKeyTagSerializer < ActiveModel::Serializer
attributes :id, :name
embed :ids
has_one :note, key: :tagged_note_id
end
class OtherNoteSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :tags, serializer: ShortTagSerializer, embed: :ids, include: true
end
class CustomKeysNoteSerializer < ActiveModel::Serializer
attributes :id, :name
has_many :tags, serializer: CustomKeyTagSerializer, embed: :ids, include: true, key: :tag_names, embed_key: :name
end
class SortedTagsNoteSerializer < ActiveModel::Serializer
attributes :id
has_many :sorted_tags
embed :ids, include: true
end
class CustomSortedTagsNoteSerializer < ActiveModel::Serializer
attributes :id
has_many :custom_sorted_tags, serializer: ShortTagSerializer
embed :ids, include: true
end
class Tag < ActiveRecord::Base
belongs_to :note
end
class SortedTag < Tag
belongs_to :note
default_scope { order(:name) }
end
class TagWithNote < Tag
belongs_to :note
default_scope { joins(:note) }
end
class TagsController < TestController; end
class TagSerializer < ActiveModel::Serializer
attributes :id, :name
embed :ids
has_one :note
end
class TagWithNoteSerializer < ActiveModel::Serializer
attributes :id, :name
embed :ids, include: true
has_one :note
end
class User < ActiveRecord::Base
has_many :offers, foreign_key: :created_by_id, inverse_of: :created_by
has_many :reviewed_offers, foreign_key: :reviewed_by_id, inverse_of: :reviewed_by, class_name: 'Offer'
has_one :address
end
class Address < ActiveRecord::Base
belongs_to :user
end
class Offer < ActiveRecord::Base
belongs_to :created_by, class_name: 'User', inverse_of: :offers
belongs_to :reviewed_by, class_name: 'User', inverse_of: :reviewed_offers
end
class UsersController < TestController; end
class AddressController < TestController; end
class OfferSerializer < ActiveModel::Serializer
attributes :id
end
class AddressSerializer < ActiveModel::Serializer
attributes :id, :district_name
embed :ids, include: true
end
class UserSerializer < ActiveModel::Serializer
attributes :id, :name, :mobile
embed :ids, include: true
has_many :offers, serializer: OfferSerializer
has_many :reviewed_offers, serializer: OfferSerializer
has_one :address, serializer: AddressSerializer
def include_mobile?
current_user && current_user[:permission_id]
end
alias_method :include_address?, :include_mobile?
end
DatabaseCleaner.strategy = :deletion
class Minitest::Spec
class << self
alias :context :describe
end
before do
DatabaseCleaner.start
end
after do
DatabaseCleaner.clean
end
end