Skip to content

Instantly share code, notes, and snippets.

@David-99
Created November 8, 2013 11:45
Show Gist options
  • Save David-99/7369891 to your computer and use it in GitHub Desktop.
Save David-99/7369891 to your computer and use it in GitHub Desktop.
tests for published on date
describe "published products" do
before do
days_hash =
{
"monday" => true,
"tuesday" => true,
"wednesday" => true,
"thursday" => true,
"friday" => true,
"saturday" => true,
"sunday" => true
}
@venue = Venue.create(name: "Venue 1")
@hotel = venue.hotels.create!(name: 'The Grand Hotel')
@course = venue.courses.create!(name: 'The 21 Hole Course')
# products 1-2 are products on golf items
# product 3 is a product on golf item and hotel
# products 4-5 are products on hotels
@product_1 = Product.create!(ygt_name: 'product_1')
@product_2 = Product.create!(ygt_name: 'product_2')
@product_3 = Product.create!(ygt_name: 'product_3')
@product_4 = Product.create!(ygt_name: 'product_4')
@product_5 = Product.create!(ygt_name: 'product_5')
golf_item_1 = @product_1.golf_items.create!(rounds: 3, rounds_type: "maximum")
golf_item_2 = @product_2.golf_items.create!(rounds: 3, rounds_type: "maximum")
golf_item_3 = @product_3.golf_items.create!(rounds: 3, rounds_type: "maximum")
[golf_item_1, golf_item_2, golf_item_3].each do |golf_item|
golf_item.courses << @course
golf_item.save!
end
accomm_item_attrs = {nights: 10, board_basis: "Full Board"}
# In the following 3 blocks the room types are being created implicitly
# by after_initialize in Hotel model.
@product_3.accommodation_items.create!(accomm_item_attrs) { |accomm_item|
accomm_item.hotel = @hotel
accomm_item.room_types << @hotel.room_types.where(name: "single").first
}
@product_4.accommodation_items.create!(accomm_item_attrs) { |accomm_item|
accomm_item.hotel = @hotel
accomm_item.room_types << @hotel.room_types.where(name: "double").first
}
@product_5.accommodation_items.create!(accomm_item_attrs) { |accomm_item|
accomm_item.hotel = @hotel
accomm_item.room_types << @hotel.room_types.where(name: "double").first
}
price_rules_attributes = {
position: 1,
currency: "GBP",
cost_price: 1.00,
gross_margin_percent: 1.00,
gross_margin_fixed: 1.00,
gross_margin_gbp: 1.00,
selling_price: 1.00,
selling_price_gbp: 1.00,
travel_date_from: Date.today,
travel_date_to: Date.today + 20,
days: days_hash,
contact_name: "test_contact_name",
offer: true
}
@product_1.price_rules
.create!({ travel_date_from: Date.today + 11,
travel_date_to: Date.today + 20,
days: days_hash}
.merge!(price_rules_attributes)
)
@product_2.price_rules
.create!({ travel_date_from: Date.today - 2,
travel_date_to: Date.today + 4,
days: days_hash}
.merge!(price_rules_attributes)
)
@product_3.price_rules
.create!({ travel_date_from: Date.today - 3,
travel_date_to: Date.today - 1,
days: days_hash}
.merge!(price_rules_attributes)
)
@product_3.price_rules
.create!({travel_date_from: Date.today,
travel_date_to: Date.today + 12,
days: days_hash}
.merge!(price_rules_attributes)
)
@product_4.price_rules
.create!({travel_date_from: Date.today - 3,
travel_date_to: Date.today - 4,
days: days_hash}
.merge!(price_rules_attributes)
)
@product_5.price_rules
.create!({travel_date_from: Date.today + -2,
travel_date_to: Date.today + 4,
days: days_hash}
.merge!(price_rules_attributes)
)
[@product_1, @product_2, @product_3, @product_5, @product_5].each do |pro|
pro.published = true
pro.save!
end
end
context 'when hotels have published products' do
it "returns only published products for where given date is within date range" do
expect(@course.products.published_on_date(Date.today))
.to eq([@product_1, @product_2])
end
it "returns a list of only published products that are within the desired date range" do
@product_2.published = false
expect(@course.products.published_on_date(Date.today))
.to eq([@product_1])
end
end
context 'when hotels have published products' do
it "returns only published products for where given date is within date range" do
expect(@hotel.products.published_on_date(Date.today))
.to eq([@product_3, @product_5])
@product_3.published = false
@product_3.save!
expect(@hotel.products.published_on_date(Date.today - 1))
.to eq([@product_4, @product_5 ])
end
end
end #end describe
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment