Skip to content

Commit

Permalink
model specs
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Aug 18, 2023
1 parent 4cf1413 commit 7226ef7
Show file tree
Hide file tree
Showing 16 changed files with 279 additions and 52 deletions.
20 changes: 9 additions & 11 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ class Event < ApplicationRecord
has_many :event_tickets
has_many :purchases, as: :purchasable
has_many :purchased_items, through: :purchases

has_many :paid_purchased_items, through: :purchases, class_name: "PurchasedItem", source: :purchased_items

has_many :purchased_event_tickets, -> {
where(purchased_items: { purchased_item_type: 'EventTicket' })
}, through: :purchased_items, source: :purchased_item, source_type: 'EventTicket'
Expand Down Expand Up @@ -59,14 +57,6 @@ class Event < ApplicationRecord
.order(event_start: :desc)
}

def available_tickets(argument)
self.event_tickets
.where('selling_start <= ?', argument)
.where('selling_end >= ?', argument)
.where('qty > ?', 0)
end


include AASM
aasm column: :state do
state :draft, initial: true
Expand All @@ -77,6 +67,13 @@ def available_tickets(argument)
end
end

def available_tickets(argument)
self.event_tickets
.where('selling_start <= ?', argument)
.where('selling_end >= ?', argument)
.where('qty > ?', 0)
end

def self.format_date_range(start_date, end_date)
return if end_date.nil?
# If end_date is nil or same as start_date, just show start_date
Expand Down Expand Up @@ -120,7 +117,8 @@ def cover_url(size = nil)
end

def sales_count
purchased_event_tickets.where("purchased_items.state =?", "paid").sum("price")
purchased_event_tickets
.where("purchased_items.state =?", "paid").sum("price")
end

def tickets_sold
Expand Down
8 changes: 2 additions & 6 deletions app/models/playlist.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ class Playlist < ApplicationRecord
has_many :track_playlists
has_many :tracks, through: :track_playlists
has_many :listening_events
has_many :comments, as: :commentable
has_many :likes, as: :likeable
has_one_attached :cover
has_one_attached :zip

acts_as_likeable
has_many :comments, as: :commentable
has_many :likes, as: :likeable

accepts_nested_attributes_for :track_playlists, allow_destroy: true

Expand Down Expand Up @@ -55,17 +55,13 @@ def album?
playlist_type != "playlist"
end


def self.list_playlists_by_user_with_track(track_id, user_id)
Playlist.select('playlists.*, COUNT(track_playlists.track_id) as track_count')
.left_outer_joins(:track_playlists)
.where(user_id: user_id, track_playlists: { track_id: [nil, track_id] })
.group('playlists.id')
end

def iframe_code_string(url)
end

def iframe_code_string(url)
<<-HTML
<iframe width="100%" height="100%" scrolling="no" frameborder="no" allow="autoplay" src="#{url}">
Expand Down
5 changes: 5 additions & 0 deletions app/models/schedule_scheduling.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class ScheduleScheduling < ApplicationRecord
belongs_to :event_schedule

validates :title, :start_date, :end_date, :short_description, presence: true

validates :start_date, comparison: { greater_than: :end_date }

end
3 changes: 2 additions & 1 deletion app/models/track.rb
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def self.series_by_month(profile_id)
end

def tags=(list)
self[:tags] = list.reject { |item| item.empty? }
self[:tags] = list.map(&:downcase).reject { |item| item.empty? }
end

def iframe_code_string(url)
Expand All @@ -291,6 +291,7 @@ def iframe_code_string(url)
end

def self.get_tracks_by_tag(tag)
tag = tag.downcase
includes(:user).where('? = ANY (tags)', tag)
end

Expand Down
5 changes: 5 additions & 0 deletions config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@
# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []

config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

Rails.application.routes.default_url_options = { host: 'localhost', port: 3000 }


# Raises error for missing translations.
# config.i18n.raise_on_missing_translations = true

Expand Down
4 changes: 2 additions & 2 deletions spec/factories/event_tickets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
selling_start { "2023-07-26 18:20:42" }
selling_end { "2023-07-26 18:20:42" }
short_description { "MyString" }
settings { "" }
# settings { "" }
event { nil }
inserted_at { "2023-07-26 18:20:42" }
created_at { "2023-07-26 18:20:42" }
updated_at { "2023-07-26 18:20:42" }
end
end
20 changes: 10 additions & 10 deletions spec/factories/events.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
factory :event do
title { "MyString" }
description { "MyText" }
slug { "MyString" }
state { "MyString" }
# slug { "MyString" }
# state { "MyString" }
timezone { "MyString" }
event_start { "2023-07-26 16:05:55" }
event_ends { "2023-07-26 16:05:55" }
Expand All @@ -24,15 +24,15 @@
event_capacity_limit { 1 }
eticket { false }
will_call { false }
order_form { "" }
widget_button { "" }
# order_form { "" }
# widget_button { "" }
event_short_link { "MyString" }
tax_rates_settings { "" }
attendee_list_settings { "" }
scheduling_settings { "" }
event_settings { "" }
tickets { "" }
# tax_rates_settings { "" }
# attendee_list_settings { "" }
# scheduling_settings { "" }
# event_settings { "" }
# tickets { "" }
user { nil }
streaming_service { "" }
# streaming_service { "" }
end
end
8 changes: 2 additions & 6 deletions spec/factories/tracks.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
FactoryBot.define do
factory :track do
title { "MyString" }
sequence(:title) {|n| "title-#{n}" }
private { false }
slug { "MyString" }
caption { "MyString" }
user { nil }
notification_settings { "" }
metadata { "" }
likes_count { 1 }
reposts_count { 1 }
state { "MyString" }
tags { "" }
tags { [] }
end
end
17 changes: 6 additions & 11 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
FactoryBot.define do
factory :user do
email { "MyString" }
username { "MyString" }
label { false }
support_link { "MyString" }
first_name { "MyString" }
last_name { "MyString" }
country { "MyString" }
city { "MyString" }
bio { "MyText" }
settings { "" }
role { "MyString" }
first_name { "User Name" }
last_name { "User Name" }
password { "123456" }
password_confirmation {"123456"}
sequence(:username) {|n| "user-#{n}" }
sequence(:email) { |n| "person#{n}@example.com" }
end
end
Binary file added spec/fixtures/audio.mp3
Binary file not shown.
58 changes: 57 additions & 1 deletion spec/models/event_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
require 'rails_helper'

RSpec.describe Event, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
let!(:user) {
FactoryBot.create(:user, username: "user", role: "user")
}

describe 'associations' do
it { should belong_to(:user) }
it { should have_many(:event_hosts) }
it { should have_many(:event_schedules) }
it { should have_many(:event_recordings) }
# it { should have_many(:schedule_schedulings).through(:event_schedules) }
it { should have_many(:event_tickets) }
it { should have_many(:purchases) }
it { should have_many(:purchased_items) }
it { should have_many(:paid_purchased_items) }
it { should have_many(:purchased_event_tickets) }
it { should have_one_attached(:cover) }
end

describe 'scopes' do
before do
@event1 = FactoryBot.create(:event, state: "published", user: user, event_start: 1.day.from_now)
@event2 = FactoryBot.create(:event, state: "draft", user: user, event_start: 2.days.from_now)
@event3 = FactoryBot.create(:event, state: "published", user: user, event_start: 3.days.from_now)
@event4 = FactoryBot.create(:event, state: "published", user: user, event_start: 3.days.ago)
end

describe '.upcoming' do
it 'returns events in ascending order of start date' do
expect(Event.upcoming_events).to eq([@event1, @event3])
end
end

describe '.published' do
it 'returns published events' do
expect(Event.upcoming_events).to eq([@event1, @event3])
end
end

describe '.past_events' do
it 'returns past_events events' do
expect(Event.past_events).to eq([@event4])
end
end

describe 'creating tickets' do
let(:event) { FactoryBot.create(:event, user: user) }

it 'can create associated tickets' do
ticket = FactoryBot.create(:event_ticket, event: event, selling_start: 1.day.from_now, selling_end: 3.day.from_now)
expect(event.event_tickets).to include(ticket)

expect(event.available_tickets(2.day.from_now)).to be_any
expect(event.available_tickets(Time.now)).to be_empty

end
end
end
end
31 changes: 30 additions & 1 deletion spec/models/playlist_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
require 'rails_helper'

RSpec.describe Playlist, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
it { belong_to( :user) }
it { have_many( :track_playlists) }
it { have_many( :tracks).through(:track_playlists) }
it { have_many( :listening_events) }
it { have_many( :comments) }
it { have_many( :likes) }
it { have_one_attached( :cover) }
it { have_one_attached( :zip) }

let!(:user) { FactoryBot.create(:user) }

describe 'scopes' do
before do
@playlist1 = FactoryBot.create(:playlist, private: false, user: user, created_at: 1.day.ago)
@playlist2 = FactoryBot.create(:playlist, private: true, user: user, created_at: 2.days.ago)
@playlist3 = FactoryBot.create(:playlist, private: false, user: user, created_at: 3.days.ago)
end

describe '.published' do
it 'returns only published tracks' do
expect(Playlist.published).to contain_exactly(@playlist1, @playlist3)
end
end

describe '.latests' do
it 'returns tracks in descending order of creation' do
expect(Playlist.latests.ids).to eq([@playlist3.id, @playlist2.id, @playlist1.id])
end
end
end
end
67 changes: 66 additions & 1 deletion spec/models/track_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,70 @@
require 'rails_helper'

RSpec.describe Track, type: :model do
pending "add some examples to (or delete) #{__FILE__}"

it { should belong_to(:user) }
it { should have_many(:track_comments) }
it { should have_many(:track_playlists) }
it { should have_many(:playlists).through(:track_playlists) }
it { should have_many(:listening_events) }
it { should have_many(:reposts) }
it { should have_many(:purchased_items) }
it { should have_many(:likes) }
it { should have_many(:comments) }

it { should have_one_attached(:cover) }
it { should have_one_attached(:audio) }
it { should have_one_attached(:mp3_audio) }
it { should have_one_attached(:zip) }

let!(:user) {
FactoryBot.create(:user, username: "user", role: "user")
}

describe 'scopes' do
before do
@track1 = FactoryBot.create(:track, private: false, user: user, created_at: 1.day.ago)
@track2 = FactoryBot.create(:track, private: true, user: user, created_at: 2.days.ago)
@track3 = FactoryBot.create(:track, private: false, user: user, created_at: 3.days.ago)
end

describe '.published' do
it 'returns only published tracks' do
expect(Track.published).to contain_exactly(@track1, @track3)
end
end

describe '.latests' do
it 'returns tracks in descending order of creation' do
expect(Track.latests.ids).to eq([@track3.id, @track2.id, @track1.id])
end
end
end

describe 'state' do
it "pending" do
@track = FactoryBot.create(:track, private: false, user: user, created_at: 1.day.ago)
expect(@track).to be_pending
end
end

describe 'tags' do
it "create with tags" do
@track = FactoryBot.create(:track, user: user, tags: ["Ambient", "Techno"])
expect(@track.tags).to be == ["ambient", "techno"]
expect(Track.get_tracks_by_tag("Ambient")).to be_any
expect(Track.get_tracks_by_tag("ambient")).to be_any
end
end

describe 'audio' do
xit 'can be created with an audio file' do
audio = Rack::Test::UploadedFile.new(Rails.root.join('spec', 'fixtures', 'audio.mp3'), 'audio/mp3')
track = FactoryBot.create(:track, audio: audio, user: user)
expect_any_instance_of(Track).to receive(:reprocess_async)
expect(track).to be_persisted
expect(track.audio).to be_persisted
end
end

end
Loading

0 comments on commit 7226ef7

Please sign in to comment.