Skip to content

Instantly share code, notes, and snippets.

@jathayde
Created October 12, 2017 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jathayde/8625a62b2c2f0ff4c69e566c3c54379d to your computer and use it in GitHub Desktop.
Save jathayde/8625a62b2c2f0ff4c69e566c3c54379d to your computer and use it in GitHub Desktop.
Rspec error on issue
FactoryGirl.define do
factory :issue do
association :issueable, factory: :lodge
association :issue_type
issue_number { Faker::Number.number(2) }
quantity_issued { Faker::Number.number(2) }
description 'Test description for test issue'
oa_bb_category_id '1'
category_id '1'
border_color_id '1'
border_type_id '1'
background_color_id '1'
lettering_color_id '1'
fdl_bsa_color_id '1'
variety { Faker::Lorem.character }
before(:each) do
@lodge_issue = FactoryGirl.create(:lodge_issue)
@issue = @lodge_issue.issue
end
# Assign an issue if we can
after(:build) do |issue|
# Assign an issue based on a provided :combined value
if issue.combined.present?
string = issue.combined
type_abbrev,
second_abbrev,
issue_number,
variety = string.match(/([A-Z])([A-Z]?)([\d]*)([a-z])?/).captures
if second_abbrev
issue.issue_type = IssueType.find_by_abbreviation(second_abbrev)
else
issue.issue_type = IssueType.find_by_abbreviation(type_abbrev)
end
issue.issue_number = issue_number
issue.variety = variety.to_s
elsif issue.issue_type.nil?
# If an IssueType was not specified, assign a random one
issue_type = IssueType.order_by_rand.first
issue.issue_type = issue_type
issue.variety = Faker::Lorem.character
end
end
end
factory :lodge_issue, class: "Issue" do
association :issueable, factory: :lodge
end
end
require 'rails_helper'
describe Issue do
it "has a valid factory" do
expect(FactoryGirl.create(:issue)).to be_valid
end
it "has many annotations" do
a = Issue.reflect_on_association(:annotations)
expect(a.macro).to eq(:has_many)
end
it "has many variations" do
v = Issue.reflect_on_association(:variations)
expect(v.macro).to eq(:has_many)
end
it "belongs to a lodge" do
l = Issue.reflect_on_association(:lodge)
expect(l.macro).to eq(:belongs_to)
end
it "belongs to a council" do
c = Issue.reflect_on_association(:council)
expect(c.macro).to eq(:belongs_to)
end
it "belongs to a chapter" do
c = Issue.reflect_on_association(:chapter)
expect(c.macro).to eq(:belongs_to)
end
it "belongs to a manufacturer" do
m = Issue.reflect_on_association(:manufacturer)
expect(m.macro).to eq(:belongs_to)
end
it "has many wishlist items" do
wi = Issue.reflect_on_association(:wishlist_items)
expect(wi.macro).to eq(:has_many)
end
it "has many issue sets" do
is = Issue.reflect_on_association(:issue_sets)
expect(is.macro).to eq(:has_many)
end
it "has many error reports" do
er = Issue.reflect_on_association(:error_reports)
expect(er.macro).to eq(:has_many)
end
describe '#set_combined' do
context 'when all elements are present and category is 1' do
let(:category_id) { '1' }
let(:issue_type) { IssueType.order_by_rand.first }
let(:issue_number) { '1' }
let(:variety) { Faker::Lorem.character }
let(:issue) { FactoryGirl.build(:issue) }
it 'sets combined in the form of the issue_type + issue_number + variety' do
issue.issue_type = issue_type
issue.issue_number = issue_number
issue.variety = variety
expect(issue.combined).to be_nil
issue.save
expect(issue.combined).to eq("#{issue_type.abbreviation}#{issue_number}#{variety}")
end
end
context 'when all elements are present and category is not 1' do
it 'sets combined in the form of the category + issue_type + issue_number + variety'
end
context 'when an element is missing' do
it 'is an invalid record and not saved'
end
end
describe '#set_oa_bb_combined' do
context 'when oa_bb_issue_type and oa_bb_issue_number are present' do
let(:oa_bb_category_id) { '1' }
let(:oa_bb_issue_type) { IssueType.order_by_rand.first }
let(:oa_bb_issue_number) { '1' }
let(:oa_bb_variety) { Faker::Lorem.character }
let(:issue) { FactoryGirl.build(:issue) }
it 'sets oa_bb_combined in the form of the issue type abbreviation + the oa bb issue number' do
issue.oa_bb_issue_type = oa_bb_issue_type
issue.oa_bb_issue_number = oa_bb_issue_number
issue.oa_bb_variety = oa_bb_variety
expect(issue.oa_bb_combined).to be_nil
issue.save
expect(issue.oa_bb_combined).to eq("#{oa_bb_issue_type.abbreviation}#{oa_bb_issue_number}#{oa_bb_variety}")
end
end
context 'when oa_bb_issue_type or oa_bb_issue_number is missing' do
let(:issue) { FactoryGirl.build(:issue) }
it 'sets the oa_bb_combined to "unlisted"' do
issue.oa_bb_issue_type_id = nil
issue.oa_bb_issue_number = ''
expect(issue.oa_bb_combined).to be_nil
issue.save
expect(issue.oa_bb_combined).to eq('unlisted')
end
end
end
describe '#ordered_issues' do
let!(:lodge) { FactoryGirl.create(:lodge) }
context 'issue_type abbreviation' do
let!(:issue_one) { FactoryGirl.create(:issue, issueable: lodge, combined: 'QS1') }
let!(:issue_two) { FactoryGirl.create(:issue, issueable: lodge, combined: 'S1') }
let!(:issue_three) { FactoryGirl.create(:issue, issueable: lodge, combined: 'QS2') }
let!(:issue_four) { FactoryGirl.create(:issue, issueable: lodge, combined: 'S2') }
let!(:issue_five) { FactoryGirl.create(:issue, issueable: lodge, combined: 'A1') }
let!(:issue_six) { FactoryGirl.create(:issue, issueable: lodge, combined: 'X1') }
it 'sorts in A, B, C order, then numerics ascending' do
# values: A1 QS1 QS2 S1 S2 X1
order = [ issue_five, issue_one, issue_three, issue_two, issue_four, issue_six ]
expect(lodge.issues.ordered_issues).to eq(order)
end
end
context 'issue_number' do
let!(:issue_one) { FactoryGirl.create(:issue, issueable: lodge, combined: 'J100') }
let!(:issue_two) { FactoryGirl.create(:issue, issueable: lodge, combined: 'J57') }
let!(:issue_three) { FactoryGirl.create(:issue, issueable: lodge, combined: 'J222') }
let!(:issue_four) { FactoryGirl.create(:issue, issueable: lodge, combined: 'J5') }
it 'sorts in A, B, C order, then numerics ascending' do
# values: J5 J57 J100 J222
order = [ issue_four, issue_two, issue_one, issue_three ]
expect(lodge.issues.ordered_issues).to eq(order)
end
end
context 'variety' do
let!(:issue_one) { FactoryGirl.create(:issue, issueable: lodge, combined: 'A1') }
let!(:issue_two) { FactoryGirl.create(:issue, issueable: lodge, combined: 'B1b') }
let!(:issue_three) { FactoryGirl.create(:issue, issueable: lodge, combined: 'B1c') }
let!(:issue_four) { FactoryGirl.create(:issue, issueable: lodge, combined: 'B2a') }
it 'sorts by abbrev and numerics, then in a, b, c variety order' do
# values: A1 B1b B1c B2a
order = [ issue_one, issue_two, issue_three, issue_four ]
expect(lodge.issues.ordered_issues).to eq(order)
end
end
end
end
class Issue < ApplicationRecord
# The key object. This is a patch issued by a Lodge (typically) and may be a flap, an event patch, or something else.
has_paper_trail :on => [:update, :destroy]
extend FriendlyId
friendly_id :slug, use: :slugged
# Lodge, Council, Chapter, etc can all be issuers
belongs_to :issueable, polymorphic: true
belongs_to :lodge
belongs_to :council
belongs_to :chapter
belongs_to :background_color,
optional: true,
class_name: 'Color',
foreign_key: 'background_color_id'
belongs_to :border_color,
class_name: 'Color',
foreign_key: 'border_color_id',
optional: true
belongs_to :border_type,
class_name: 'BorderType',
foreign_key: 'border_type_id',
optional: true
belongs_to :category
belongs_to :fdl_bsa_color,
class_name: 'Color',
foreign_key: 'fdl_bsa_color_id',
optional: true
belongs_to :issue_type
belongs_to :lettering_color,
class_name: 'Color',
foreign_key: 'lettering_color_id',
optional: true
belongs_to :oa_bb_issue_type,
class_name: 'IssueType',
foreign_key: "oa_bb_issue_type_id",
optional: true
belongs_to :oa_bb_category,
class_name: 'Category',
foreign_key: "oa_bb_category_id",
optional: true
belongs_to :manufacturer,
optional: true
belongs_to :material
belongs_to :backing_type
belongs_to :issue_shape
has_many :wishlist_items
has_many :issue_sets_issues
has_many :issue_sets, through: :issue_sets_issues
has_many :items # Is this the right setup?
has_many :issue_annotations
has_many :annotations,
through: :issue_annotations
# Self-referential Issue variations
has_many :issue_variations
has_many :variations,
through: :issue_variations
has_many :inverse_variations, # has_one parent might be a more accurate phrase
class_name: 'IssueVariation',
foreign_key: "variation_id"
has_many :inverse_issues,
through: :inverse_variations,
source: :issue
has_many :error_reports, as: :reportable_error
accepts_nested_attributes_for :error_reports, reject_if: :all_blank, allow_destroy: true
validates :issueable_id, presence: true
validates :issueable_type, presence: true
validates :category_id, presence: true
validates :issue_type, presence: true
validates :issue_number, presence: true
validates :slug, uniqueness: { scope: :issueable }
include Redcarpeted
redcarpet :description
redcarpet :history
# Image attachment via Carrierwave
mount_uploader :image, ImageUploader
mount_uploader :imageback, ImagebackUploader
before_validation :set_combined
before_validation :set_oa_bb_combined
before_validation :set_slug
scope :load_with_borders_colors_and_type, -> {
includes(
:background_color, :border_type, :border_color, :fdl_bsa_color,
:lettering_color, :issue_type, :category, :issue_sets
)
}
scope :event_issues, -> {
where(category_id: 2).includes(:lodge)
}
scope :non_event_issues, -> {
where(category_id: [1,3,4,5,6,7]).includes(:lodge)
}
def self.ordered_issues
self.load_with_borders_colors_and_type.sort_by do |issue|
type_abbrev,
second_abbrev,
issue_number,
variety = issue.combined.match(/([A-Z])([A-Z]?)([\d]*)([a-z])?/).captures
[ type_abbrev, second_abbrev, issue_number.to_i, variety.to_s ]
end
end
private
def set_combined
if category_id == 1
self.combined = "#{issue_type.abbreviation}#{issue_number}#{variety}"
else
self.combined = "#{category.abbreviation}#{issue_type.abbreviation}#{issue_number}#{variety}"
end
end
def set_oa_bb_combined
if oa_bb_issue_type.present? && oa_bb_issue_number.present?
if oa_bb_category_id == 1
self.oa_bb_combined = "#{oa_bb_issue_type.abbreviation}#{oa_bb_issue_number}#{oa_bb_variety}"
else
self.oa_bb_combined = "#{oa_bb_category.abbreviation}#{oa_bb_issue_type.abbreviation}#{oa_bb_issue_number}#{oa_bb_variety}"
end
else
self.oa_bb_combined = 'unlisted'
end
end
def set_slug
self.slug = combined.parameterize
end
def should_generate_new_friendly_id?
slug.nil?
end
end
Failures:
1) Issue#ordered_issues issue_type abbreviation sorts in A, B, C order, then numerics ascending
Failure/Error: self.combined = "#{issue_type.abbreviation}#{issue_number}#{variety}"
NoMethodError:
undefined method `abbreviation' for nil:NilClass
# ./app/models/issue.rb:126:in `set_combined'
# ./spec/models/issue_spec.rb:109:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
2) Issue#ordered_issues issue_number sorts in A, B, C order, then numerics ascending
Failure/Error: self.combined = "#{issue_type.abbreviation}#{issue_number}#{variety}"
NoMethodError:
undefined method `abbreviation' for nil:NilClass
# ./app/models/issue.rb:126:in `set_combined'
# ./spec/models/issue_spec.rb:122:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
3) Issue#ordered_issues variety sorts by abbrev and numerics, then in a, b, c variety order
Failure/Error: self.combined = "#{issue_type.abbreviation}#{issue_number}#{variety}"
NoMethodError:
undefined method `abbreviation' for nil:NilClass
# ./app/models/issue.rb:126:in `set_combined'
# ./spec/models/issue_spec.rb:133:in `block (4 levels) in <top (required)>'
# -e:1:in `<main>'
Finished in 1.42 seconds (files took 0.42328 seconds to load)
18 examples, 3 failures, 2 pending
Failed examples:
rspec ./spec/models/issue_spec.rb:115 # Issue#ordered_issues issue_type abbreviation sorts in A, B, C order, then numerics ascending
rspec ./spec/models/issue_spec.rb:126 # Issue#ordered_issues issue_number sorts in A, B, C order, then numerics ascending
rspec ./spec/models/issue_spec.rb:137 # Issue#ordered_issues variety sorts by abbrev and numerics, then in a, b, c variety order
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment