Reputation: 1587
I have an RSpec test suite that tests many things including the creation of a model called CompanyReportByReturnOnCapital
. The associated table used to be called company_report_by_return_on_capitals
, due to Rails' automatic pluralization, and I've since run a migration to rename the table to company_reports_by_return_on_capital
.
However when I run rake
to run the test suite, I receive the following error:
1) StockReader#create_company_reports_by_roc creates reports
Failure/Error: CompanyReportByReturnOnCapital.delete_all
ActiveRecord::StatementInvalid:
PG::UndefinedTable: ERROR: relation "company_report_by_return_on_capitals" does not exist
LINE 1: DELETE FROM "company_report_by_return_on_capitals"
^
: DELETE FROM "company_report_by_return_on_capitals"
# ./spec/lib/stock_reader_spec.rb:102:in `block (3 levels) in <top (required)>'
My schema.rb:
ActiveRecord::Schema.define(version: 20141029193309) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "company_reports_by_earnings_yield", force: true do |t|
t.string "name"
t.string "symbol"
t.string "market_cap"
t.datetime "created_at"
t.datetime "updated_at"
t.string "ebit"
t.string "ebit_date"
t.string "enterprise_value"
t.string "earnings_yield"
t.string "fixed_assets"
t.string "working_capital"
t.string "return_on_capital"
t.string "market_cap_date"
t.string "total_assets"
t.string "total_assets_date"
t.string "current_assets"
t.string "current_assets_date"
t.string "total_debt"
t.string "total_debt_date"
t.string "cash_and_equivalents"
t.string "cash_and_equivalents_date"
t.string "working_capital_date"
end
create_table "company_reports_by_return_on_capital", force: true do |t|
t.string "name"
t.string "symbol"
t.string "ebit"
t.string "ebit_date"
t.string "market_cap"
t.string "market_cap_date"
t.string "working_capital"
t.string "working_capital_date"
t.string "total_assets"
t.string "total_assets_date"
t.string "current_assets"
t.string "current_assets_date"
t.string "total_debt"
t.string "total_debt_date"
t.string "cash_and_equivalents"
t.string "cash_and_equivalents_date"
t.string "fixed_assets"
t.string "enterprise_value"
t.string "earnings_yield"
t.string "return_on_capital"
t.datetime "created_at"
t.datetime "updated_at"
end
end
My migration files renaming the table:
class CreateCompanyReportByReturnOnCapitals < ActiveRecord::Migration
def change
create_table :company_report_by_return_on_capitals do |t|
t.string :name
t.string :symbol
t.string :ebit
t.string :ebit_date
t.string :market_cap
t.string :market_cap_date
t.string :working_capital
t.string :working_capital_date
t.string :total_assets
t.string :total_assets_date
t.string :current_assets
t.string :current_assets_date
t.string :total_debt
t.string :total_debt_date
t.string :cash_and_equivalents
t.string :cash_and_equivalents_date
t.string :fixed_assets
t.string :enterprise_value
t.string :earnings_yield
t.string :return_on_capital
t.timestamps
end
end
end
class RenameCompanyReportByReturnOnCapitals < ActiveRecord::Migration
def change
rename_table :company_report_by_return_on_capitals, :company_report_by_return_on_capital
end
end
class RenameReturnOnCapitalReports < ActiveRecord::Migration
def change
rename_table :company_report_by_return_on_capital, :company_reports_by_return_on_capital
end
end
Here is the test in spec/lib/stock_reader_spec.rb:
require 'spec_helper'
require 'stock_reader'
RSpec.describe StockReader do
describe '#create_company_reports_by_roc' do
before(:each) do
CompanyReportByReturnOnCapital.delete_all
end
it 'creates reports' do
StockReader.create_company_reports_by_roc($return_on_capital_array)
expect(CompanyReportByReturnOnCapital.count).to eq(2)
end
end
end
And finally, lib/stock_reader.rb where the actual code resides:
require 'csv'
require 'httparty'
module StockReader
def self.create_company_reports_by_roc(company_data_array)
company_data_array.each do |company|
CompanyReportByReturnOnCapital.create(
symbol: company[:symbol],
enterprise_value: company[:enterprise_value].to_s,
enterprise_value_date: company[:enterprise_value_date],
ebit: company[:ebit].to_s,
ebit_date: company[:ebit_date],
earnings_yield: company[:earnings_yield].to_s)
end
end
end
So far I've tried resetting the test database using the following commands:
RAILS_ENV=test rake db:drop db:create db:migrate
, rake db:test:load
, rake db:test:prepare
AND the development database using:
bin/rake db:drop
and bin/rake db:create
and bin/rake db:migrate
with no success.
I'm out of ideas!
Just for good measure I'll include my model, app/models/company_report_by_return_on_capital.rb:
class CompanyReportByReturnOnCapital < ActiveRecord::Base
end
Thanks in advance
Upvotes: 0
Views: 846
Reputation: 1437
Something similar happened for me where PG was looking for a non-existent table which gave me errors when I tried to test. I deleted the fixture file corresponding to the non-existent table and that solved the issue for me.
Upvotes: 3
Reputation: 1587
The problem was that I had to add inflections to my config/initializers/inflections.rb file so that Rails would not expect a table called company_report_by_return_on_capitals
. Despite many db:drop
's and migrations, Rails was still expecting this although company_report_by_return_on_capitals
was nowhere to be found in my application. All's well now.
Upvotes: 1