Skip to content

Commit

Permalink
added factory girl, facker, added factories, added specs for scope an…
Browse files Browse the repository at this point in the history
…d relationship
  • Loading branch information
Praveen V authored and PraveenVignesh committed Mar 13, 2015
1 parent 296803d commit 50b386a
Show file tree
Hide file tree
Showing 14 changed files with 256 additions and 36 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ group :development, :test do
gem 'dotenv-rails'
gem 'vcr'
gem 'fakeweb'
gem 'faker'
end

group :test do
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ GEM
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
fakeweb (1.3.0)
faker (1.4.3)
i18n (~> 0.5)
faraday (0.8.7)
multipart-post (~> 1.1)
fastercsv (1.5.5)
Expand Down Expand Up @@ -272,6 +274,7 @@ DEPENDENCIES
dotenv-rails
factory_girl_rails
fakeweb
faker
friendly_id (~> 4.0.10)
geocoder
jquery-rails
Expand Down
37 changes: 37 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
FactoryGirl.define do
sequence :email do |n|
"#{Faker::Internet.user_name}#{n}@#{Faker::Internet.domain_name}"
end

sequence :user_name do |n|
"#{Faker::Internet.user_name}#{n}"
end

sequence :random_word do |n|
"#{Faker::Lorem.word}#{n}"
end

factory :event do
short_code { generate(:random_word) }
name Faker::Lorem.word
end

factory :organization do
name Faker::Company.name
end

factory :job do
title Faker::Lorem.sentence(3)
end

factory :project do
name { generate(:random_word) }
github_repo { generate(:random_word) }
end

factory :user do
email { generate(:email) }
password Faker::Internet.password
end

end
26 changes: 26 additions & 0 deletions spec/models/event_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,30 @@
describe Event do
it { should have_many(:featured_projects) }
it { should have_many(:projects).through(:featured_projects) }
it { should have_many(:event_registrations) }
it { should have_many(:sponsors).through(:sponsorships) }
it { should have_many(:sponsorships) }
it { should have_many(:users).through(:event_registrations) }

before(:each) do
@event1 = create(:event, start_date: Time.now, end_date: Time.now+1.day)
@event2 = create(:event, start_date: Time.now, end_date: Time.now)
@event3 = create(:event, start_date: Time.now+1.day, end_date: Time.now+2.day)
end

describe "upcoming event scope" do

it "should return only upcoming events" do
expect(Event.upcoming_events).to eq([@event1, @event3])
end

end

describe "featured events" do

it "should return only one upcoming event sorted by starting date" do
expect(Event.featured).to eq(@event1)
end

end
end
6 changes: 6 additions & 0 deletions spec/models/favorite_project_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

describe FavoriteProject do
it { should belong_to(:project) }
it { should belong_to(:user) }
end
21 changes: 21 additions & 0 deletions spec/models/job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require 'spec_helper'

describe Job do
it { should belong_to(:organization) }

before(:each) do
@organization = create(:organization)
@job1 = create(:job, :organization_id => @organization.id, :expires_at => DateTime.now+1.day)
@job2 = create(:job, :organization_id => @organization.id, :expires_at => DateTime.now-1.day)
@job3 = create(:job, :organization_id => @organization.id)
end

describe "active scope" do

it "should return jobs which not expires or does not have expires_at field" do
expect(Job.active).to eq([@job1, @job3])
end

end

end
90 changes: 68 additions & 22 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
@@ -1,84 +1,130 @@
require 'spec_helper'

describe Organization do

it { should have_many(:organization_metrics) }
it { should have_many(:projects) }
it { should have_many(:jobs) }
it { should have_many(:sponsorships)}
it { should validate_presence_of(:name) }

context 'validations' do
context "validations" do

context 'if public submission' do
before { subject.stub(:is_public_submission) { true } }
before { subject.stub(:is_public_submission){true} }
it { should validate_presence_of(:github_org) }
end

context 'if not public submission' do
before { subject.stub(:is_public_submission) { false } }
before { subject.stub(:is_public_submission){false} }
it { should_not validate_presence_of(:github_org) }
end

context 'if not public submission, implicit' do
it { should_not validate_presence_of(:github_org) }
end

end

context "scopes" do

before(:each) do
@organization1 = create(:organization, :name => "Spritle")
@organization2 = create(:organization, :name => "Foo")
@project1 = create(:project, :organization_id => @organization1.id)
@project2 = create(:project, :organization_id => @organization1.id, :is_active => false)
@project3 = create(:project, :organization_id => @organization1.id, :is_approved => true)
@job = create(:job, :organization_id => @organization1.id)
end

describe "approved scope" do
it "should return all organization which has approved projects" do
expect(Organization.approved).to eq([@organization1])
end
end

describe "featured scope" do
it "should return all organization which has approved & active projects" do
expect(Organization.featured).to eq([@organization1])
end
end

describe "hiring scope" do
it "should return all organization which has jobs" do
expect(Organization.hiring).to eq([@organization1])
end
end

end


context "url wrangling" do

let(:organization) { Organization.new(url: 'http://www.amazing.org', github_org: 'amazing')}

context 'url wrangling' do
let(:organization) { Organization.new(url: 'http://www.amazing.org', github_org: 'amazing') }
describe "#display_url" do

describe '#display_url' do
it 'returns the hostname of its url' do
it "returns the hostname of its url" do
expect(organization.display_url).to eq('www.amazing.org')
end

it 'returns an empty string if its url is missing' do
it "returns an empty string if its url is missing" do
organization.stub(url: nil)
expect(organization.display_url).to eq('')
end

end

describe '#github_url' do
it 'concatentates its github url' do
describe "#github_url" do

it "concatentates its github url" do
expect(organization.github_url).to eq('https://github.com/amazing')
end

it 'returns an empty string if it has no github org' do
it "returns an empty string if it has no github org" do
organization.stub(github_org: nil)
expect(organization.github_url).to be_nil
end

end

end

context 'logo deletion' do
context "logo deletion" do

let(:organization) { Organization.new }
let(:logo) { double }

before do
organization.stub(:logo) { logo }
end

describe '#logo_delete' do
it 'defaults its logo_delete flag' do
describe "#logo_delete" do
it "defaults its logo_delete flag" do
expect(organization.logo_delete).to eq('0')
end
end
end

describe '#logo_delete=' do
it 'sets its logo_delete flag' do
describe "#logo_delete=" do
it "sets its logo_delete flag" do
organization.logo_delete = 'foo'
expect(organization.logo_delete).to eq('foo')
end
end
end

describe '#delete_logo' do
it 'clears its logo if its logo_delete flag is set to 1' do
describe "#delete_logo" do
it "clears its logo if its logo_delete flag is set to 1" do
organization.stub(:logo_delete) { '1' }
logo.should_receive(:clear)
organization.send(:delete_logo)
end
it 'does not clear its logo if its logo_delete flag is not set to 1' do
it "does not clear its logo if its logo_delete flag is not set to 1" do
organization.stub(:logo_delete) { nil }
logo.should_not_receive(:clear)
organization.send(:delete_logo)
end
end

end

end
54 changes: 44 additions & 10 deletions spec/models/project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,67 @@
it { should belong_to(:organization) }
it { should have_many(:featured_projects) }
it { should have_many(:events).through(:featured_projects) }

it { should have_many(:favorite_projects) }
it { should have_many(:users).through(:favorite_projects) }
it { should validate_presence_of(:name) }
it { should validate_presence_of(:github_repo) }

context 'github-related methods' do
context "scopes" do
before(:each) do
@organization = create(:organization)
@project1 = create(:project, :organization_id => @organization.id)
@project2 = create(:project, :organization_id => @organization.id, :is_active => false)
@project3 = create(:project, :organization_id => @organization.id, :is_approved => true)
end

describe "approved scope" do
it "should return only approved project" do
expect(Project.approved).to eq([@project3])
end
end

describe "active scope" do
it "should return only approved & active project" do
expect(Project.active).to eq([@project3])
end
end

describe "featured scope" do
it "should return only active project which has organization_id" do
expect(Project.featured).to eq([@project3])
end
end

end

context "github-related methods" do

let(:project) { Project.new }
let(:organization) { double(github_org: 'codemontage', github_url: 'https://github.com/codemontage') }

before do
project.stub(:organization) { organization }
project.stub(:github_repo) { 'foo' }
project.stub(:github_repo) { 'foo'}
end

describe '#github_display' do
it 'creates an organization/repo string' do
describe "#github_display" do
it "creates an organization/repo string" do
expect(project.github_display).to eq('codemontage/foo')
end
end

describe '#github_url' do
it 'creates a repo url' do
describe "#github_url" do
it "creates a repo url" do
expect(project.github_url).to eq('https://github.com/codemontage/foo')
end
end

describe '#tasks_url' do
it 'creates an issues url' do
describe "#tasks_url" do
it "creates an issues url" do
expect(project.tasks_url).to eq('https://github.com/codemontage/foo/issues')
end
end

end

describe "GitHub API interaction" do
Expand Down Expand Up @@ -82,7 +113,8 @@
end
end

describe '#related_projects' do
describe "#related_projects" do

let(:organization) { Organization.create!(name: 'CodeMontage') }

before do
Expand All @@ -93,5 +125,7 @@
it "returns its organization's other projects" do
expect(@project_1.related_projects).to eq([@project_2])
end

end

end
5 changes: 5 additions & 0 deletions spec/models/service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Service do
it { should belong_to(:user) }
end
6 changes: 6 additions & 0 deletions spec/models/sponsorship_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

describe Sponsorship do
it { should belong_to(:organization) }
it { should belong_to(:event) }
end
5 changes: 5 additions & 0 deletions spec/models/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Tag do

end
6 changes: 6 additions & 0 deletions spec/models/user_profile_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require 'spec_helper'

describe UserProfile do
it { should belong_to(:user) }
it { should ensure_length_of(:headline).is_at_most(128) }
end
Loading

0 comments on commit 50b386a

Please sign in to comment.