Sha256: dc59e8f0bc4b82f480773f72e969c8388b514faf6a422ad81fee8f257cf83451

Contents?: true

Size: 1.39 KB

Versions: 1

Compression:

Stored size: 1.39 KB

Contents

require 'spec_helper'
require 'active_record'
require 'sqlite3'

describe 'active record' do

  # Setup DB
  before(:all) do
    @db_file = "test.db"

    # Open a database
    db = SQLite3::Database.new @db_file

    # Create tables
    db.execute_batch <<-SQL
      create table suppliers (
        name varchar(30),
        id int primary key
      );

      create table accounts (
        name varchar(30),
        id int primary key,
        supplier_id int,
        FOREIGN KEY (supplier_id) REFERENCES suppliers(id)
      );
    SQL

    # Insert records
    @account_id = 2
    @supplier_id = 1
    db.execute_batch <<-SQL
      insert into suppliers values ('Supplier1', #{@supplier_id});
      insert into accounts values ('Dollar Account', #{@account_id}, #{@supplier_id});
    SQL
  end

  # Setup Active Record
  before(:all) do
    class Supplier < ActiveRecord::Base
      has_one :account
    end

    class Account < ActiveRecord::Base
      belongs_to :supplier
    end

    ActiveRecord::Base.establish_connection(
      :adapter => 'sqlite3',
      :database  => @db_file
    )
  end

  context 'has one patch' do

    it 'has account_id method for a supplier' do
      expect(Supplier.first.respond_to?(:account_id)).to be true
      expect(Supplier.first.account_id).to eq @account_id
    end

  end

  # Clean up DB
  after(:all) do
    File.delete(@db_file) if File.exists?(@db_file)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fast_jsonapi-1.0.16 spec/lib/extensions/active_record_spec.rb