|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "fixturebot/rails" |
| 4 | +require "tmpdir" |
| 5 | + |
| 6 | +RSpec.describe FixtureBot::Rails, ".generate" do |
| 7 | + before do |
| 8 | + ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:") |
| 9 | + |
| 10 | + ActiveRecord::Schema.define(version: 2024_01_01_000000) do |
| 11 | + create_table "users", force: :cascade do |t| |
| 12 | + t.string "name" |
| 13 | + t.string "email" |
| 14 | + t.timestamps |
| 15 | + end |
| 16 | + |
| 17 | + create_table "posts", force: :cascade do |t| |
| 18 | + t.string "title" |
| 19 | + t.text "body" |
| 20 | + t.integer "author_id" |
| 21 | + t.timestamps |
| 22 | + end |
| 23 | + |
| 24 | + add_foreign_key "posts", "users", column: "author_id" |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + after do |
| 29 | + ActiveRecord::Base.connection_pool.disconnect! |
| 30 | + end |
| 31 | + |
| 32 | + it "generates YAML fixture files from a DSL file" do |
| 33 | + Dir.mktmpdir do |tmpdir| |
| 34 | + fixtures_file = File.join(tmpdir, "fixtures.rb") |
| 35 | + output_dir = File.join(tmpdir, "fixtures") |
| 36 | + |
| 37 | + File.write(fixtures_file, <<~RUBY) |
| 38 | + FixtureBot.define do |
| 39 | + user :alice do |
| 40 | + name "Alice" |
| 41 | + email "alice@example.com" |
| 42 | + end |
| 43 | +
|
| 44 | + user :bob do |
| 45 | + name "Bob" |
| 46 | + email "bob@example.com" |
| 47 | + end |
| 48 | +
|
| 49 | + post :hello do |
| 50 | + title "Hello World" |
| 51 | + body "First post" |
| 52 | + author :alice |
| 53 | + end |
| 54 | + end |
| 55 | + RUBY |
| 56 | + |
| 57 | + described_class.generate(fixtures_file: fixtures_file, output_dir: output_dir) |
| 58 | + |
| 59 | + users_yaml = YAML.load_file(File.join(output_dir, "users.yml")) |
| 60 | + expect(users_yaml.keys).to contain_exactly("alice", "bob") |
| 61 | + expect(users_yaml["alice"]["name"]).to eq("Alice") |
| 62 | + expect(users_yaml["alice"]["email"]).to eq("alice@example.com") |
| 63 | + expect(users_yaml["bob"]["name"]).to eq("Bob") |
| 64 | + |
| 65 | + posts_yaml = YAML.load_file(File.join(output_dir, "posts.yml")) |
| 66 | + expect(posts_yaml.keys).to contain_exactly("hello") |
| 67 | + expect(posts_yaml["hello"]["title"]).to eq("Hello World") |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + it "skips empty tables" do |
| 72 | + Dir.mktmpdir do |tmpdir| |
| 73 | + fixtures_file = File.join(tmpdir, "fixtures.rb") |
| 74 | + output_dir = File.join(tmpdir, "fixtures") |
| 75 | + |
| 76 | + File.write(fixtures_file, <<~RUBY) |
| 77 | + FixtureBot.define do |
| 78 | + user :alice do |
| 79 | + name "Alice" |
| 80 | + email "alice@example.com" |
| 81 | + end |
| 82 | + end |
| 83 | + RUBY |
| 84 | + |
| 85 | + described_class.generate(fixtures_file: fixtures_file, output_dir: output_dir) |
| 86 | + |
| 87 | + expect(File.exist?(File.join(output_dir, "users.yml"))).to be true |
| 88 | + expect(File.exist?(File.join(output_dir, "posts.yml"))).to be false |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + it "returns early when fixtures file does not exist" do |
| 93 | + Dir.mktmpdir do |tmpdir| |
| 94 | + output_dir = File.join(tmpdir, "fixtures") |
| 95 | + |
| 96 | + described_class.generate( |
| 97 | + fixtures_file: File.join(tmpdir, "nonexistent.rb"), |
| 98 | + output_dir: output_dir |
| 99 | + ) |
| 100 | + |
| 101 | + expect(Dir.exist?(output_dir)).to be false |
| 102 | + end |
| 103 | + end |
| 104 | +end |
0 commit comments