Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a belongs_to test to Ahoy::Event causes failure #105

Open
rokumatsumoto opened this issue Apr 22, 2020 · 0 comments
Open

Adding a belongs_to test to Ahoy::Event causes failure #105

rokumatsumoto opened this issue Apr 22, 2020 · 0 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@rokumatsumoto
Copy link
Owner

rokumatsumoto commented Apr 22, 2020

Describe the bug

Adding a belongs_to test to Ahoy::Event Active Record model causes failure

# == Schema Information
#
# Table name: ahoy_events
#
#  id         :bigint(8)        not null, primary key
#  visit_id   :bigint(8)
#  user_id    :bigint(8)
#  name       :string
#  properties :jsonb
#  time       :datetime
#

require 'spec_helper'

RSpec.describe Ahoy::Event, type: :model do
  describe 'associations' do
    it { is_expected.to belong_to(:visit) }
  end
end
Ahoy::Event associations should belong to visit required: true
     Failure/Error: it { is_expected.to belong_to(:visit) }

     NoMethodError:
       undefined method `keys' for nil:NilClass
     # ./spec/models/ahoy/event_spec.rb:38:in `block (3 levels) in <top (required)>'

Removing store: :properties from Ahoy::Event model fixes the issue.

module Ahoy
  class Event < ApplicationRecord
    belongs_to :design, class_name: 'Design', store: :properties, optional: true
  end
end

:store option on :belongs_to association comes from activerecord-jsonb-associations gem and enables saving of foreign ids in properties (event details) jsonb column.

=> #<Ahoy::Event:0x00007f9ee9e78490
 id: 634042,
 visit_id: 633041,
 user_id: 1593,
 name: "Downloaded design",
 properties: {"design_id"=>645},
 time: Mon, 16 Mar 2020 19:08:43 +03 +03:00>

=> #<Ahoy::Event:0x00007f9ee86f68d0
 id: 646237,
 visit_id: 645079,
 user_id: 1588,
 name: "Liked design",
 properties: {"design_id"=>709},
 time: Tue, 16 Mar 2020 23:09:01 +03 +03:00>

I use counter_culture gem for storing counter of specific events (Downloaded design, Liked design, etc.) on Design model with the help of :store option.

I tracked the issue in activerecord-jsonb-associations and activerecord gems. See for screenshots section below.

To Reproduce

The script below uses

  • Minitest instead of RSpec
  • shoulda and lower version of shoulda-matchers for Minitest

You need the replace blog_test string with an unimportant Postgres database name.

I could not reproduce the test failure, the test that should have failed was successful with Minitest. I will try again with RSpec and same version of shoulda-matchers

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  # Activate the gem you are reporting the issue against.
  gem "activerecord", "5.2.3"
  gem 'minitest'
  gem 'shoulda', '~> 3.6'
  gem 'shoulda-matchers', '~> 3.0'
  # gem 'activerecord-jsonb-associations', path: "/Users/sgnydn/.rbenv/versions/2.5.5/lib/ruby/gems/2.5.0/bundler/gems/activerecord-jsonb-associations-9abe0dd9724a"
  gem 'activerecord-jsonb-associations', github: 'rokumatsumoto/activerecord-jsonb-associations', branch: 'master'
  gem 'pg', '>= 0.18', '< 2.0'
end

require 'pg'
require "active_record"
require 'activerecord/jsonb/associations'
require "minitest"
require "minitest/autorun"
require "shoulda"
require 'shoulda/matchers'
require "logger"

ActiveRecord::Base.establish_connection(
  adapter: 'postgresql',
  database: 'blog_test' # use an unimportant database
)

ActiveRecord::Base.logger = Logger.new(STDOUT)

# this is the default in Rails
ActiveRecord::Base.belongs_to_required_by_default = true

ActiveRecord::Schema.define do
  create_table :ahoy_visits, force: true do |t|
  end

  create_table :ahoy_events, force: true do |t|
    t.references :visit

    t.jsonb :properties
  end

  create_table :designs, force: true do |t|
  end
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :minitest

    with.library :active_record
    with.library :active_model
  end
end

module Ahoy
  class Visit < ActiveRecord::Base
    self.table_name = 'ahoy_visits'

    has_many :events, class_name: 'Ahoy::Event'
  end
end

module Ahoy
  class Event < ActiveRecord::Base
    self.table_name = 'ahoy_events'

    belongs_to :visit
    belongs_to :design, class_name: 'Design', store: :properties, optional: true
  end
end

class Design < ActiveRecord::Base
end

module Ahoy
  class EventTest < ActiveSupport::TestCase
    should belong_to(:visit)
  end
end

Expected behavior

Rails belongs_to test should pass

Screenshots

Screen Shot 2020-04-22 at 23 47 24

Desktop

  • OS: macOS Sierra
  • Browser Google Chrome
  • Version 80.0.3987.163
@rokumatsumoto rokumatsumoto added the bug Something isn't working label Apr 22, 2020
@rokumatsumoto rokumatsumoto added this to the 0.13.0 milestone Apr 22, 2020
@rokumatsumoto rokumatsumoto self-assigned this Apr 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant