small medium large xlarge

Jruby_pragsmall
22 Mar 2017, 16:34
Serguei Cambour (14 posts)

I get this error when running customer_search_spec.rb after the latest modification as described on page 202. Running refresh_materialized_views rake task didn’t solve the issue. I even tried to refresh the view customer_details in shine_test DB manually from PgAdmin 4. FYI, I’m using Postgres 9.6.10. Here is the stack trace of the error:

Failures:

  1) Customer Search Search by Email
     Failure/Error: customer_detail = CustomerDetail.find(params[:id])
     
     ActiveRecord::StatementInvalid:
       PG::ObjectNotInPrerequisiteState: ERROR:  materialized view "customer_details" has not been populated
       HINT:  Use the REFRESH MATERIALIZED VIEW command.
       : SELECT  "customer_details".* FROM "customer_details" WHERE "customer_details"."customer_id" = $1 LIMIT $2

Any ideas ? Thank you.

Dmfcb_pragsmall
23 Mar 2017, 20:52
David Copeland (476 posts)

Hmmm. This does not happen for me, and it should succeed because you will have inserted data into the tables. Can you post your entire test file as well as the full stack trace?

Jruby_pragsmall
24 Mar 2017, 10:36
Serguei Cambour (14 posts)

Hi, David ! Here the spec file:


require 'rails_helper'

feature "Customer Search" do
  def create_customer(first_name:,
                      last_name:,
                      email: nil)

    username = "#{Faker::Internet.user_name}#{rand(1000)}"
    email  ||= "#{username}#{rand(1000)}@" +
               "#{Faker::Internet.domain_name}"

    customer = Customer.create!(
      first_name: first_name,
       last_name: last_name,
        username: username,
           email: email
    )

    customer.create_customers_billing_address(address: create_address)
    customer.customers_shipping_address.create!(address: create_address, primary: true)

    customer
  end

  def create_address
    state = State.find_or_create_by!(
      code: Faker::Address.state_abbr,
      name: Faker::Address.state)

    Address.create!(
      street: Faker::Address.street_address,
      city: Faker::Address.city,
      state: state,
      zipcode: Faker::Address.zip)
  end

  let(:email)    { "pat@example.com" }
  let(:password) { "password123" }

  before do
    User.create!(email: email,
                 password: password,
                 password_confirmation: password)

    create_customer first_name: "Chris",
                     last_name: "Aaron"

    create_customer first_name: "Pat",
                     last_name: "Johnson"

    create_customer first_name: "I.T.",
                     last_name: "Pat"

    create_customer first_name: "Patricia",
                     last_name: "Dobbs"

    create_customer first_name: "Pat",
                     last_name: "Jones",
                         email: "pat123@somewhere.net"
    visit "/customers"
    fill_in      "Email",    with: email
    fill_in      "Password", with: password
    click_button "Log in"
  end

  scenario "Search by Name" do
    within "section.search-form" do
      fill_in "keywords", with: "pat"
    end
    within "section.search-results" do
      expect(page).to have_content("Results")
      expect(page.all("ol li.list-group-item").count).to eq(4)

      expect(page.all("ol li.list-group-item")[0]).to have_content("Patricia")
      expect(page.all("ol li.list-group-item")[0]).to have_content("Dobbs")

      expect(page.all("ol li.list-group-item")[3]).to have_content("I.T.")
      expect(page.all("ol li.list-group-item")[3]).to have_content("Pat")
    end
  end

  scenario "Search by Email" do
    within "section.search-form" do
      fill_in "keywords", with: "pat123@somewhere.net"
    end

    within "section.search-results" do
      expect(page).to have_content("Results")
      expect(page.all("ol li.list-group-item").count).to eq(4)

      expect(page.all("ol li.list-group-item")[0]).to have_content("Pat")
      expect(page.all("ol li.list-group-item")[0]).to have_content("Jones")

      expect(page.all("ol li.list-group-item")[1]).to have_content("Patricia")
      expect(page.all("ol li.list-group-item")[1]).to have_content("Dobbs")

      expect(page.all("ol li.list-group-item")[3]).to have_content("I.T.")
      expect(page.all("ol li.list-group-item")[3]).to have_content("Pat")
    end

    click_on "View Details...", match: :first
    customer = Customer.find_by!(email: "pat123@somewhere.net")
    within "section.customer-details" do
      expect(page).to have_content(customer.id)
      expect(page).to have_content(customer.first_name)
      expect(page).to have_content(customer.last_name)
      expect(page).to have_content(customer.email)
      expect(page).to have_content(customer.username)
    end
  end
end

And here is the stack trace:

➜  shine git:(materialized_views) ✗ rspec spec/features/customer_search_spec.rb 
/Users/Serguei/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:51: warning: constant ::Fixnum is deprecated
/Users/Serguei/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/xml_mini.rb:52: warning: constant ::Bignum is deprecated
/Users/Serguei/.rvm/gems/ruby-2.4.0/gems/activesupport-5.0.1/lib/active_support/core_ext/numeric/conversions.rb:138: warning: constant ::Fixnum is deprecated
/Users/Serguei/.rvm/gems/ruby-2.4.0/gems/activejob-5.0.1/lib/active_job/arguments.rb:38: warning: constant ::Fixnum is deprecated
/Users/Serguei/.rvm/gems/ruby-2.4.0/gems/activejob-5.0.1/lib/active_job/arguments.rb:38: warning: constant ::Bignum is deprecated

Randomized with seed 48855

Customer Search
 http://localhost:3808/webpack-dev-server/
webpack result is served from //localhost:3808/webpack/
content is served from /Users/Serguei/projects/shine
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
  Search by Name
Angular 2 is running in the development mode. Call enableProdMode() to enable the production mode.
  Search by Email (FAILED - 1)
Killing webpack-dev-server

Failures:

  1) Customer Search Search by Email
     Failure/Error: expect(page).to have_content(customer.id)
       expected to find text "5" in "Customer Info First Name Last Name @ Email Joined12/13/2014 Shipping Address Street Address Street Address Extra City State Zip Billing Info ****-****-****-1234 VISA Expires: 04/19 View Details... Billing Address Same as shipping? Street Address Street Address Extra City State Zip"
     # ./spec/features/customer_search_spec.rb:104:in `block (3 levels) in <top (required)>'
     # /Users/Serguei/.rvm/gems/ruby-2.4.0/gems/capybara-2.12.0/lib/capybara/session.rb:310:in `within'
     # /Users/Serguei/.rvm/gems/ruby-2.4.0/gems/capybara-2.12.0/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
     # ./spec/features/customer_search_spec.rb:103:in `block (2 levels) in <top (required)>'

Top 2 slowest examples (8.03 seconds, 98.7% of total time):
  Customer Search Search by Name
    4.52 seconds ./spec/features/customer_search_spec.rb:66
  Customer Search Search by Email
    3.51 seconds ./spec/features/customer_search_spec.rb:82

Finished in 8.13 seconds (files took 2.46 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/features/customer_search_spec.rb:82 # Customer Search Search by Email

Randomized with seed 48855

Jruby_pragsmall
24 Mar 2017, 10:45
Serguei Cambour (14 posts)

I didn’t run it against your code, just followed the steps in the book, created my own branch, etc. I’m on Ruby 2.4.0, but don’t think it to be an issue.

Dmfcb_pragsmall
24 Mar 2017, 13:09
David Copeland (476 posts)

That is showing a different error than you originally posted. It looks like a “normal” test failure, that it can’t find the customer ID on the view. When you run the app, is the customer ID showing up on the view?

Jruby_pragsmall
24 Mar 2017, 15:58
Serguei Cambour (14 posts)

I can’t see it a browser. If I’m not mistaken, all the data is reset before each test run by database_cleaner. More of that, when starting a server, it runs in dev mode and not in test. We init the data in beforebloc of the feature spec:

create_customer first_name: "Pat",
                     last_name: "Jones",
                         email: "pat123@somewhere.net"

Then it fails on line 104:

expect(page).to have_content(customer.id)

when searching a customer by mail pat123@somewhere.net. It seems like the materialized view is not updated when using triggers.

Jruby_pragsmall
24 Mar 2017, 16:06
Serguei Cambour (14 posts)

I’ve just checked out the code source for that chapter. And the corresponding part of code in customer_search_spec is commented out:

within "section.customer-details" do
      # commenting out since our mockup is just static at this point
      # expect(page).to have_content(customer.id)
      # expect(page).to have_content(customer.first_name)
      # expect(page).to have_content(customer.last_name)
      # expect(page).to have_content(customer.email)
      # expect(page).to have_content(customer.username)
    end
Dmfcb_pragsmall
24 Mar 2017, 16:05
David Copeland (476 posts)

I guess I’m confused about why you think the materialized view is the issue. It looks like your test is simply failing, but it’s hard to tell for sure, because I can’t see your app’s source code. Can you post it somewhere?

The reason I suggested trying the code in a browser in dev mode is to make sure that the app is actually working properly and that the test failure isn’t just detecting a legit bug in your code.

Jruby_pragsmall
25 Mar 2017, 21:00
Serguei Cambour (14 posts)

David, you can check the code source from my github repo.

Thank you and regards.

Dmfcb_pragsmall
26 Mar 2017, 16:55
David Copeland (476 posts)

OK, wow, this is a weird one. I’m not sure how I never encountered this, but I’ll update the book about!

What was confusing is that your test setup inserts into the various tables, which should execute the trigger and thus update the view.

That works as long as the view has ever been materialized. In your case, the very first time, the view won’t have been created ever and the trigger errors out. Since the trigger swallows all errors, it just silently fails.

SO

Try this locally:

> RAILS_ENV=test bundle exec rails dbconsole # note we are using test env
shine_test=# refresh materialized view customer_details;

THEN, your tests should work.

Jruby_pragsmall
27 Mar 2017, 12:18
Serguei Cambour (14 posts)

Cool ! David, thank you very much, it worked as expected.

2012-08-16-102302_pragsmall
20 Jun 2017, 11:39
Michael Pope (10 posts)

I’ve also got this problem and I’ve been working through the book also.

I’ve tried the suggested command to refresh the materialized view but that didn’t help, even though I ran the test straight after;

> RAILS_ENV=test bundle exec rails dbconsole # note we are using test env
shine_test=# refresh materialized view customer_details;
  1) Customer Search Search by Email
     Failure/Error: expect(page).to have_content(customer.id)
       expected to find text "5" in "Customer First Name Last Name Username @ Email Joined 12/13/2014 Shipping Address Street Address City State Zip Billing Info ****-****-****-1234 VISA Expires: 04/19 View Details... Billing Address Same as shipping? Street Address City State Zip"
     # ./spec/features/customer_search_spec.rb:122:in `block (3 levels) in <top (required)>'
     # ./spec/features/customer_search_spec.rb:119:in `block (2 levels) in <top (required)>'

Finished in 11.66 seconds (files took 1.48 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/features/customer_search_spec.rb:91 # Customer Search Search by Email

I’ve copied the test from the book and noticed the section which I have a problem with is skipped

    within "section.customer-details" do
      warn "YOU ARE SKIPPING TESTS!"
      if false
      expect(page).to have_content(customer.id)
      expect(page).to have_content(customer.first_name)
      expect(page).to have_content(customer.last_name)
      expect(page).to have_content(customer.email)
      expect(page).to have_content(customer.username)
      end
    end

If I remove the warning and the if statement and run this test I get the same result as my code.

I’ve put my code up on github: https://github.com/map7/shine.git

Dmfcb_pragsmall
20 Jun 2017, 12:38
David Copeland (476 posts)

It looks like you are using a pretty old version of the book. I believe that at this point in the book, the tests are disabled because the UI is still being built.

The error you posted doesn’t seem to have anything to do with materialized views. It’s complaining that it can’t find the customer’s ID, but your view isn’t rendering it: https://github.com/map7/shine/blob/master/webpack/CustomerDetailsComponent.html

Could be the text is out of date, but as I said, you seem to be using a pretty old version of the book, as many things about the Angular stuff have change d in the recent betas.

2012-08-16-102302_pragsmall
24 Sep 2017, 07:49
Michael Pope (10 posts)

I’ve worked through the latest version of the book (p1) up to this point from scratch and still get the same issue. I think all that’s missing in the book is to skip that part of the test for now until I update the view.

Dmfcb_pragsmall
24 Sep 2017, 13:31
David Copeland (476 posts)

If the error message is the same and the code looks like the link above, the problem is that your test is expecting the customer id, but it is not being rendered in the view.

If that’s not wahts’ happening, can you post the error you are getting and a link to your latest code?

You must be logged in to comment