Skip to content

Commit 89c3df9

Browse files
authored
Slow tests after (#7)
* Removes product_spec * Adds shop model with associations between use and products * Adds user account association with full name specs * Adds shop builder specs * Adds destroy product specs * Adds view specs as part of the toolkit * Fixes system views on the page object * Refactors the view spec of product deletion * Adds webmocl to stub requests
1 parent 8f0bf37 commit 89c3df9

33 files changed

+284
-7
lines changed

.bash_history

+4
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,7 @@ export
3737
export
3838
#!/bin/bash -i
3939
export
40+
#!/bin/bash -i
41+
export
42+
#!/bin/bash -i
43+
export

Gemfile

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ group :development do
3232
gem "annotate"
3333
gem "rack-timeout", require:"rack/timeout/base"
3434
gem "rails-erd"
35+
gem 'httparty'
3536

3637
# Support for Ruby IDE tools - including "Ruby for Visual Studio Code"
3738
gem 'debase', '~> 0.2.4.1'
@@ -55,6 +56,7 @@ group :test do
5556
gem "simplecov-console", "~> 0.4.2", require: false
5657
gem "simplecov-json", require: false
5758
gem "simplecov-reporter", require: false
59+
gem 'webmock'
5860
end
5961

6062
group :development, :test do
@@ -78,4 +80,4 @@ end
7880

7981
gem "tzinfo-data", platforms: [:mingw, :mswin, :x64_mingw, :jruby]
8082

81-
gem "devise"
83+
gem "devise"

Gemfile.lock

+10
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ GEM
9999
coercible (1.0.0)
100100
descendants_tracker (~> 0.0.1)
101101
concurrent-ruby (1.1.6)
102+
crack (0.4.3)
103+
safe_yaml (~> 1.0.0)
102104
crass (1.0.6)
103105
debase (0.2.4.1)
104106
debase-ruby_core_source (>= 0.10.2)
@@ -125,6 +127,7 @@ GEM
125127
ffi (1.12.2)
126128
globalid (0.4.2)
127129
activesupport (>= 4.2.0)
130+
hashdiff (1.0.1)
128131
hirb (0.7.3)
129132
httparty (0.18.0)
130133
mime-types (~> 3.0)
@@ -276,6 +279,7 @@ GEM
276279
ruby-progressbar (1.10.1)
277280
ruby_dep (1.5.0)
278281
rubyzip (2.2.0)
282+
safe_yaml (1.0.5)
279283
sass (3.7.4)
280284
sass-listen (~> 4.0.0)
281285
sass-listen (4.0.0)
@@ -354,6 +358,10 @@ GEM
354358
nokogiri (~> 1.6)
355359
rubyzip (>= 1.3.0)
356360
selenium-webdriver (>= 3.0, < 4.0)
361+
webmock (3.8.2)
362+
addressable (>= 2.3.6)
363+
crack (>= 0.3.2)
364+
hashdiff (>= 0.4.0, < 2.0.0)
357365
webpacker (4.2.2)
358366
activesupport (>= 4.2)
359367
rack-proxy (>= 0.6.1)
@@ -381,6 +389,7 @@ DEPENDENCIES
381389
devise
382390
factory_bot_rails
383391
ffaker
392+
httparty
384393
listen (>= 3.0.5, < 3.2)
385394
normalize-rails
386395
pg (>= 0.18, < 2.0)
@@ -413,6 +422,7 @@ DEPENDENCIES
413422
tzinfo-data
414423
web-console (>= 3.3.0)
415424
webdrivers (~> 4.0)
425+
webmock
416426
webpacker (~> 4.0)
417427

418428
RUBY VERSION

app/controllers/products_controller.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
class ProductsController < ApplicationController
2+
before_action :authenticate_user!, only: [:destroy, :update, :new, :create]
23
before_action :set_product, only: [:show, :edit, :update, :destroy]
34

45
# GET /products
@@ -53,6 +54,6 @@ def set_product
5354

5455
# Only allow a trusted parameter "white list" through.
5556
def product_params
56-
params.require(:product).permit(:name, :description, :price, :status)
57+
params.require(:product).permit(:name, :description, :price, :status, :shop_id)
5758
end
5859
end

app/models/account.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Account < ApplicationRecord
2+
belongs_to :user
3+
end

app/models/product.rb

+2
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ class Product < ApplicationRecord
88
validates :name,
99
:description,
1010
:price, presence: true
11+
12+
belongs_to :shop
1113
end

app/models/shop.rb

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Shop < ApplicationRecord
2+
belongs_to :user
3+
has_many :products
4+
end

app/models/user.rb

+13
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,17 @@ class User < ApplicationRecord
33
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
44
devise :database_authenticatable, :registerable,
55
:recoverable, :rememberable, :validatable
6+
7+
has_many :shops
8+
has_one :account
9+
10+
def full_name
11+
"#{first_name} #{last_name}".strip
12+
end
13+
14+
def shop_ready?
15+
# completed_profile?
16+
# && billing_information?
17+
# && active_plan?
18+
end
619
end

app/services/shop_builder.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
class ShopBuilder
4+
def initialize(user)
5+
@user = user
6+
end
7+
8+
def build
9+
if @user.shop_ready?
10+
# charge_user!
11+
end
12+
end
13+
14+
private
15+
16+
def charge_user!
17+
# ...
18+
end
19+
end

app/views/products/_form.html.erb

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
<%= form.text_field :price %>
2727
</div>
2828

29+
<div>
30+
<%= form.label :shop_id %>
31+
<%= form.collection_select :shop_id, current_user.shops, :id, :name, prompt: true %>
32+
</div>
33+
2934
<div class="actions">
3035
<%= form.submit 'Create' %>
3136
</div>

app/views/products/index.html.erb

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
<td><%= product.price %></td>
2121
<td><%= link_to 'Show', product %></td>
2222
<td><%= link_to 'Edit', edit_product_path(product) %></td>
23-
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
23+
<% if user_signed_in? && current_user.admin? %>
24+
<td><%= link_to 'Destroy', product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
25+
<% end %>
2426
</tr>
2527
<% end %>
2628
</tbody>

app/views/products/show.html.erb

+4
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,9 @@
2424
<%= @product.price %>
2525
</p>
2626

27+
<% if current_user.admin? %>
28+
<%= link_to 'Destroy', @product, method: :delete, data: { confirm: 'Are you sure?' } %>
29+
<% end %>
30+
2731
<%= link_to 'Edit', edit_product_path(@product) %> |
2832
<%= link_to 'Back', products_path %>
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class CreateShops < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :shops do |t|
4+
t.string :name
5+
t.references :user, null: false, foreign_key: true
6+
7+
t.timestamps
8+
end
9+
end
10+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddShopIdToProducts < ActiveRecord::Migration[6.0]
2+
def change
3+
add_reference :products, :shop, null: false, foreign_key: true
4+
end
5+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class AddLastNameToUsers < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :users, :last_name, :string
4+
add_column :users, :first_name, :string
5+
end
6+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class CreateAccounts < ActiveRecord::Migration[6.0]
2+
def change
3+
create_table :accounts do |t|
4+
t.references :user, null: false, foreign_key: true
5+
6+
t.timestamps
7+
end
8+
end
9+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddAdminToUsers < ActiveRecord::Migration[6.0]
2+
def change
3+
add_column :users, :admin, :boolean, default: false
4+
end
5+
end

db/schema.rb

+24-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema.define(version: 2020_02_27_193902) do
13+
ActiveRecord::Schema.define(version: 2020_03_02_194034) do
1414

1515
# These are extensions that must be enabled in order to support this database
1616
enable_extension "plpgsql"
1717

18+
create_table "accounts", force: :cascade do |t|
19+
t.bigint "user_id", null: false
20+
t.datetime "created_at", precision: 6, null: false
21+
t.datetime "updated_at", precision: 6, null: false
22+
t.index ["user_id"], name: "index_accounts_on_user_id"
23+
end
24+
1825
create_table "net_promoter_scores", force: :cascade do |t|
1926
t.integer "score"
2027
t.datetime "created_at", precision: 6, null: false
@@ -28,6 +35,16 @@
2835
t.datetime "created_at", precision: 6, null: false
2936
t.datetime "updated_at", precision: 6, null: false
3037
t.integer "status", default: 0
38+
t.bigint "shop_id", null: false
39+
t.index ["shop_id"], name: "index_products_on_shop_id"
40+
end
41+
42+
create_table "shops", force: :cascade do |t|
43+
t.string "name"
44+
t.bigint "user_id", null: false
45+
t.datetime "created_at", precision: 6, null: false
46+
t.datetime "updated_at", precision: 6, null: false
47+
t.index ["user_id"], name: "index_shops_on_user_id"
3148
end
3249

3350
create_table "users", force: :cascade do |t|
@@ -38,8 +55,14 @@
3855
t.datetime "remember_created_at"
3956
t.datetime "created_at", precision: 6, null: false
4057
t.datetime "updated_at", precision: 6, null: false
58+
t.string "last_name"
59+
t.string "first_name"
60+
t.boolean "admin", default: false
4161
t.index ["email"], name: "index_users_on_email", unique: true
4262
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
4363
end
4464

65+
add_foreign_key "accounts", "users"
66+
add_foreign_key "products", "shops"
67+
add_foreign_key "shops", "users"
4568
end

erd.pdf

768 Bytes
Binary file not shown.

spec/factories/accounts.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FactoryBot.define do
2+
factory :account do
3+
user
4+
end
5+
end

spec/factories/products.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
name { FFaker::Product.product_name }
44
description { FFaker::Product.letters(200) }
55
price { 100.0 }
6+
shop
67
end
78
end

spec/factories/shops.rb

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FactoryBot.define do
2+
factory :shop do
3+
name { FFaker::Company.name }
4+
user
5+
6+
after(:create) do |shop|
7+
create_list(:product, 3, shop: shop)
8+
end
9+
end
10+
end

spec/factories/users.rb

+10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
11
FactoryBot.define do
22
factory :user do
3+
first_name { FFaker::Name.first_name }
4+
last_name { FFaker::Name.last_name }
35
sequence(:email) { |n| "test_user#{n}@mail.com" }
46
password { '12345678' }
57
password_confirmation { '12345678' }
8+
9+
trait :as_admin do
10+
admin { true }
11+
end
12+
13+
after(:create) do |user|
14+
create_list(:shop, 1, user: user)
15+
end
616
end
717
end
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
# frozen_string_literal: true
2-
31
require 'rails_helper'
42

5-
RSpec.describe Product, type: :model do
3+
RSpec.describe Account, type: :model do
64
pending "add some examples to (or delete) #{__FILE__}"
75
end

spec/models/shop_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe Shop, type: :model do
4+
pending "add some examples to (or delete) #{__FILE__}"
5+
end

spec/models/user_spec.rb

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe Shop, type: :model do
6+
describe '#full_name' do
7+
8+
it 'returns the first_name and last_name concatenaded' do
9+
user = create(:user, first_name: 'Naruto', last_name: 'Uzumaki')
10+
11+
expect(user.full_name).to eq 'Naruto Uzumaki'
12+
end
13+
14+
it 'returns only the first_name if no last_name is provided' do
15+
user = create(:user, first_name: 'Naruto', last_name: nil)
16+
17+
expect(user.full_name).to eq 'Naruto'
18+
end
19+
end
20+
end

spec/rails_helper.rb

+5
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
abort('The Rails environment is running in production mode!')
1212
end
1313
require 'rspec/rails'
14+
require 'webmock/rspec'
15+
require 'httparty'
1416
# require 'simplecov_helper'
1517
# Add additional requires below this line. Rails is not loaded until this point!
1618

@@ -65,6 +67,9 @@
6567
config.filter_rails_from_backtrace!
6668
# arbitrary gems may also be filtered via:
6769
# config.filter_gems_from_backtrace("gem name")
70+
71+
config.include Devise::TestHelpers, type: :view
72+
config.include Warden::Test::Helpers
6873
end
6974

7075
Shoulda::Matchers.configure do |config|

spec/services/shop_builder_spec.rb

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe ShopBuilder do
6+
describe '#build' do
7+
# https://relishapp.com/rspec/rspec-mocks/docs/basics/test-doubles
8+
it 'builds the shop for valid users' do
9+
user = instance_double(User, shop_ready?: true)
10+
11+
ShopBuilder.new(user).build
12+
13+
# here will come the expectation
14+
end
15+
end
16+
end

spec/support/page/product.rb

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ def create
1414
fill_in :product_name, with: @name
1515
fill_in :product_description, with: 'description'
1616
fill_in :product_price, with: 500
17+
select Shop.first.name, from: :product_shop_id
1718
click_button 'Create'
1819
end
1920

0 commit comments

Comments
 (0)