Skip to content

Commit 8d3a89e

Browse files
committed
Merge pull request #1105 from beauby/add-activerecord-fixtures
Add ActiveRecord-backed fixtures.
2 parents 5f03454 + 83f11ac commit 8d3a89e

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,11 @@ else
2525
gem 'actionpack', gem_version
2626
end
2727

28+
group :test do
29+
gem 'activerecord'
30+
gem 'sqlite3', platform: :ruby
31+
gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby
32+
end
33+
2834
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
2935
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

test/fixtures/active_record.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
require 'active_record'
2+
3+
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
4+
ActiveRecord::Schema.define do
5+
create_table :posts, force: true do |t|
6+
t.string :title
7+
t.text :body
8+
t.references :author
9+
t.timestamps null: false
10+
end
11+
create_table :authors, force: true do |t|
12+
t.string :name
13+
t.timestamps null: false
14+
end
15+
create_table :comments, force: true do |t|
16+
t.text :contents
17+
t.references :author
18+
t.references :post
19+
t.timestamp null: false
20+
end
21+
end
22+
23+
module ARModels
24+
class Post < ActiveRecord::Base
25+
has_many :comments
26+
belongs_to :author
27+
end
28+
29+
class Comment < ActiveRecord::Base
30+
belongs_to :post
31+
belongs_to :author
32+
end
33+
34+
class Author < ActiveRecord::Base
35+
has_many :posts
36+
end
37+
38+
class PostSerializer < ActiveModel::Serializer
39+
attributes :id, :title, :body
40+
41+
has_many :comments
42+
belongs_to :author
43+
url :comments
44+
end
45+
46+
class CommentSerializer < ActiveModel::Serializer
47+
attributes :id, :contents
48+
49+
belongs_to :author
50+
end
51+
52+
class AuthorSerializer < ActiveModel::Serializer
53+
attributes :id, :name
54+
55+
has_many :posts
56+
end
57+
end

test/test_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
require 'support/rails_app'
3636

37-
require 'fixtures/poro'
38-
3937
require 'support/test_case'
38+
39+
require 'fixtures/active_record'
40+
41+
require 'fixtures/poro'

0 commit comments

Comments
 (0)