Skip to content

Commit

Permalink
changing factories notation Factory -> FactoryGirl.create; added spec…
Browse files Browse the repository at this point in the history
…_helper method for temporary use before test suite global fix; seeds.rb fixed
  • Loading branch information
MyBartosz authored and Webmaster committed Dec 6, 2012
1 parent 3ce8223 commit 7be9f12
Show file tree
Hide file tree
Showing 31 changed files with 159 additions and 175 deletions.
97 changes: 34 additions & 63 deletions db/seeds.rb
@@ -1,56 +1,43 @@
#encoding: UTF-8
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
#
# Examples:
#
# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
# Mayor.create(name: 'Emanuel', city: cities.first)


# encoding: UTF-8
require 'factory_girl_rails'

puts "seeding data... this process can take 20-25 minutes"
Administrator.find_or_create_by_email({
email: "admin@avoinministerio.fi",
password: "hallinta"
})

[
{ email: "joonas@pekkanen.com",
password: "joonas1", password_confirmation: "joonas1", remember_me: true,
profile_attributes: {first_names: "Joonas", first_name: "Joonas", last_name: "Pekkanen", name: "Joonas Pekkanen"}, },

{ email: "arttu.tervo@gmail.com",
password: "arttu1", password_confirmation: "arttu1", remember_me: true,
profile_attributes: {first_names: "Arttu", first_name: "Arttu", last_name: "Tervo", name: "Arttu Tervo"}, },

{ email: "aleksi.rossi@iki.fi",
password: "aleksi1", password_confirmation: "aleksi1", remember_me: true,
profile_attributes: {first_names: "Aleksi", first_name: "Aleksi", last_name: "Rossi", name: "Aleksi Rossi"}, },

{ email: "hleinone@gmail.com",
password: "hannu1", password_confirmation: "hannu1", remember_me: true,
profile_attributes: {first_names: "Hannu", first_name: "Hannu", last_name: "Leinonen", name: "Hannu Leinonen"}, },

{ email: "juha.yrjola@iki.fi",
password: "juhay1", password_confirmation: "juhay1", remember_me: true,
profile_attributes: {first_names: "Juha", first_name: "Juha", last_name: "Yrjölä", name: "Juha Yrjölä"}, },

{ email: "lauri@kiskolabs.com",
password: "lauri1", password_confirmation: "lauri1", remember_me: true,
profile_attributes: {first_names: "Lauri", first_name: "Lauri", last_name: "Jutila", name: "Lauri Jutila"}, },

{ email: "mikael.kopteff@gmail.com",
password: "mikael1", password_confirmation: "mikael1", remember_me: true,
profile_attributes: {first_names: "Mikael", first_name: "Mikael", last_name: "Kopteff", name: "Mikael Kopteff"}, },
].each { |citizen| Citizen.find_or_create_by_email(citizen) }

@citizens = Citizen.all

def random_citizen
@citizens[rand(@citizens.size)]
end

joonas = Citizen.where(email: "joonas@pekkanen.com").first
koiravero_body = <<EOS
Expand Down Expand Up @@ -103,7 +90,6 @@ def random_citizen
Tämä laki tulee voimaan 1. päivänä tammikuuta 2013.
EOS

opintotuki_body = <<EOS
Yleisperustelut
Expand Down Expand Up @@ -202,7 +188,6 @@ def random_citizen
Ennen lain voimaantuloa voidaan ryhtyä lain toimeenpanon edellyttämiin toimiin.
EOS

[
{
title: "Koiraverolain kumoaminen",
Expand Down Expand Up @@ -237,7 +222,6 @@ def random_citizen
body: "Suuremmat rangaistukset olisivat linjakkaampia!",
state: "idea", author: random_citizen},
].each { |idea| i = Idea.create(idea); i.state = idea[:state]; i.author = idea[:author]; i.save! }

20.times do |i|
idea = Idea.create(
{ title: "Esimerkki-idea #{i}",
Expand All @@ -250,38 +234,32 @@ def random_citizen
idea.author = random_citizen
idea.save!
end

voters = (0..100).map do |i|
Citizen.find_or_create_by_email(
email: "voter#{i}@voter.com",
password: "voter#{i}", password_confirmation: "voter#{i}", remember_me: true,
profile_attributes: {first_names: "Clueless Voter", first_name: "Voter", last_name: "#{i}", name: "Voter #{i}"}
)
end

Idea.all.each do |idea|
rand(5).times { Factory(:comment, commentable: idea, author: Citizen.first(offset: rand(Citizen.count))) }
rand(5).times { FactoryGirl.create :comment, commentable: idea, author: Citizen.first(offset: rand(Citizen.count)) }
end

voter_count = voters.size

ideas = Idea.find(:all).to_a
# first idea has 0 votes
ideas.shift
# next ideas have only one for and against
ideas.shift.vote(voters[rand(voter_count)], 0)
ideas.shift.vote(voters[rand(voter_count)], 1)

class RandomGaussian
def initialize(mean = 0.0, sd = 1.0, rng = lambda { Kernel.rand })
@mean, @sd, @rng = mean, sd, rng
@compute_next_pair = false
end

def rand
if (@compute_next_pair = !@compute_next_pair)
# Compute a pair of random values with normal distribution.
# See http://en.wikipedia.org/wiki/Box-Muller_transform
# Compute a pair of random values with normal distribution.
# See http://en.wikipedia.org/wiki/Box-Muller_transform
theta = 2 * Math::PI * @rng.call
scale = @sd * Math.sqrt(-2 * Math.log(1 - @rng.call))
@g1 = @mean + scale * Math.sin(theta)
Expand All @@ -291,45 +269,39 @@ def rand
end
end
end

# the rest should have all kinds of combinations
secs_per_week = 60*60*24*7
ideas.each do |idea|
rd = RandomGaussian.new(secs_per_week * (rand()*8.0+2.0), secs_per_week * (rand()*4.0+2.0))

# pick random count of random voters
vs = []
(0..rand(voter_count)).each do
v = voters[rand(voter_count)]
while vs.include? v
v = voters[rand(voter_count)]
end
vs.push v
end
vs.each do |v|
rd = RandomGaussian.new(secs_per_week * (rand()*8.0+2.0), secs_per_week * (rand()*4.0+2.0))
# pick random count of random voters
vs = []
(0..rand(voter_count)).each do
v = voters[rand(voter_count)]
while vs.include? v
v = voters[rand(voter_count)]
end
vs.push v
end
vs.each do |v|
idea.vote(v, rand(2))
end
end
end

# let's create some articles

def read_till(f, breaker = /^---+/)
str = ""
while((l = f.gets) !~ breaker)
str.concat l
end
str
str = ""
while((l = f.gets) !~ breaker)
str.concat l
end
str
end

def field(f, name)
str = f.gets
if m = str.match(/^#{name}:/)
return m.post_match
else
raise "line #{str} does not match field name #{name}"
end
str = f.gets
if m = str.match(/^#{name}:/)
return m.post_match
else
raise "line #{str} does not match field name #{name}"
end
end

Dir["articles/*.md"].sort{|a,b| a <=> b}.each do |name|
next unless File.file?(name)
File.open(name) do |f|
Expand All @@ -343,7 +315,6 @@ def field(f, name)
ingress: field(f, "ingress") && read_till(f),
body: field(f, "body") && read_till(f),
}

Article.find_or_create_by_created_at(article)
end
end
2 changes: 1 addition & 1 deletion spec/acceptance/admin/articles_spec.rb
Expand Up @@ -4,7 +4,7 @@

feature "Articles" do
let (:idea) {
Factory :idea
FactoryGirl.create :idea
}
describe "PUT /articles" do
before do
Expand Down
Expand Up @@ -3,7 +3,7 @@

feature "Existing citizen logins in with Facebook" do
background do
_citizen = Factory(:facebookin_erkki)
_citizen = FactoryGirl.create :facebookin_erkki
mock_facebook_omniauth(_citizen.authentication.uid, email: _citizen.email)
end

Expand Down
2 changes: 1 addition & 1 deletion spec/acceptance/citizen/signup_spec.rb
Expand Up @@ -4,7 +4,7 @@
feature "Citizen Signup" do

let(:password) { '123456789' }
let(:citizen) { Factory(:citizen, :password => '123456789', :password_confirmation => '123456789') }
let(:citizen) { FactoryGirl.create :citizen, :password => '123456789', :password_confirmation => '123456789' }

scenario "Signup" do
visit signup_page
Expand Down
22 changes: 11 additions & 11 deletions spec/acceptance/signing/idea_signing_2_spec.rb
Expand Up @@ -10,7 +10,7 @@
let(:citizen_password) { '123456789' }
let(:citizen_email) { 'citizen-kane@example.com'}
let(:idea) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
Expand All @@ -19,47 +19,47 @@
additional_collecting_service_urls: "http://www.example.com/initiative"
}
let(:another_idea) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:state_idea) {
Factory :idea, state: "idea",
FactoryGirl.create :idea, state: "idea",
collecting_in_service: false,
collecting_started: false, collecting_ended: false,
collecting_start_date: today_date + 1, collecting_end_date: today_date + 181,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:state_draft) {
Factory :idea, state: "draft",
FactoryGirl.create :idea, state: "draft",
collecting_in_service: false,
collecting_started: false, collecting_ended: false,
collecting_start_date: today_date + 1, collecting_end_date: today_date + 181,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:not_started_proposal) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: false, collecting_ended: false,
collecting_start_date: today_date + 1, collecting_end_date: today_date + 181,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:ended_proposal) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: true,
collecting_start_date: today_date - 181, collecting_end_date: today_date - 1,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:signature) {
Factory :signature, idea: idea
FactoryGirl.create :signature, idea: idea
}

context "Sign button on Idea page visible" do
Expand Down Expand Up @@ -123,25 +123,25 @@
page.should have_content("Tällä tahdilla ei ehditä kerätä ilmoituksia riittävästi ennen päättymispäivää")
end
scenario "Idea is about to pass" do
idea_about_to_pass = Factory :idea, state: "proposal",
idea_about_to_pass = FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 10
signature_for_idea_about_to_pass = Factory :signature, idea: idea_about_to_pass
signature_for_idea_about_to_pass = FactoryGirl.create :signature, idea: idea_about_to_pass

visit idea_page(idea_about_to_pass.id)
page.should have_content("Tällä tahdilla ehditään kerätä ilmoitukset ennen päättymispäivää")
end
scenario "Idea has passed" do
passed_idea = Factory :idea, state: "proposal",
passed_idea = FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 1
signature_for_passed_idea = Factory :signature, idea: passed_idea
signature_for_passed_idea = FactoryGirl.create :signature, idea: passed_idea

visit idea_page(passed_idea.id)
page.should have_content("Tavoite on täyttynyt")
Expand Down
8 changes: 4 additions & 4 deletions spec/acceptance/signing/idea_signing_spec.rb
Expand Up @@ -11,15 +11,15 @@
let(:citizen_password) { '123456789' }
let(:citizen_email) { 'citizen-kane@example.com'}
let(:idea) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
additional_signatures_count: 0, additional_signatures_count_date: today_date,
target_count: 51500
}
let(:another_idea) {
Factory :idea, state: "proposal",
FactoryGirl.create :idea, state: "proposal",
collecting_in_service: true,
collecting_started: true, collecting_ended: false,
collecting_start_date: today_date, collecting_end_date: today_date + 180,
Expand Down Expand Up @@ -636,14 +636,14 @@ def logged_in_citizen_on_homepage
end

scenario "the idea can't be signed and the citizen enters the URL manually" do
idea_that_cannot_be_signed = Factory :idea
idea_that_cannot_be_signed = FactoryGirl.create :idea
visit signature_idea_introduction(idea_that_cannot_be_signed.id)
page.should have_content "Can't be signed"
page.should_not have_button "Siirry hyväksymään ehdot"
end

scenario "the idea can't be signed and the citizen attempts to enter the approval page directly" do
idea_that_cannot_be_signed = Factory :idea
idea_that_cannot_be_signed = FactoryGirl.create :idea
page.driver.post(signature_idea_approval_path(
idea_that_cannot_be_signed.id))
page.should have_content "Can't be signed"
Expand Down
6 changes: 3 additions & 3 deletions spec/acceptance/support/helpers.rb
Expand Up @@ -11,7 +11,7 @@
module HelperMethods
# Put helper methods you need to be available in all acceptance specs here.
def create_citizen(extra_attributes = {})
Factory(:citizen, extra_attributes)
FactoryGirl.create :citizen, extra_attributes
end

def create_logged_in_citizen(extra_attributes = {})
Expand All @@ -25,7 +25,7 @@ def login_as_citizen(citizen)
end

def create_logged_in_administrator
administrator = Factory(:administrator)
administrator = FactoryGirl.create :administrator
login_as_administrator(administrator)
administrator
end
Expand Down Expand Up @@ -131,7 +131,7 @@ def mock_facebook_omniauth(uid = "1234567", info = {})
end

def create_idea(extra_attributes = {})
Factory(:idea, extra_attributes)
FactoryGirl.create :idea, extra_attributes
end

# This should be refactored somewhere away
Expand Down

0 comments on commit 7be9f12

Please sign in to comment.