Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Unit and Integration Tests for Food #21

Merged
merged 6 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--require spec_helper
6 changes: 5 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ gem 'devise'
gem 'bullet'
gem 'rails', '~> 7.0.8'
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem 'faker'
gem 'sprockets-rails'

# Use postgresql as the database for Active Record
gem 'pg', '~> 1.1'
gem 'shoulda-matchers'

# Use the Puma web server [https://github.com/puma/puma]
gem 'puma', '~> 5.0'
Expand All @@ -26,6 +27,7 @@ gem 'importmap-rails'
gem 'turbo-rails'

# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem 'factory_bot_rails'
gem 'stimulus-rails'

# Build JSON APIs with ease [https://github.com/rails/jbuilder]
Expand Down Expand Up @@ -55,6 +57,7 @@ gem 'rspec'
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem 'debug', platforms: %i[mri mingw x64_mingw]
gem 'rspec-rails', '~> 6.0.0'
end

group :development do
Expand All @@ -73,3 +76,4 @@ group :test do
gem 'capybara'
gem 'selenium-webdriver'
end
# Run against this stable release
25 changes: 23 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ GEM
warden (~> 1.2.3)
diff-lcs (1.5.0)
erubi (1.12.0)
factory_bot (6.2.1)
activesupport (>= 5.0.0)
factory_bot_rails (6.2.0)
factory_bot (~> 6.2.0)
railties (>= 5.0.0)
faker (3.1.1)
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.1)
Expand Down Expand Up @@ -136,7 +143,7 @@ GEM
mini_mime (1.1.5)
minitest (5.20.0)
msgpack (1.7.2)
net-imap (0.4.0)
net-imap (0.4.1)
date
net-protocol
net-pop (0.1.2)
Expand Down Expand Up @@ -214,6 +221,14 @@ GEM
rspec-mocks (3.12.6)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.12.0)
rspec-rails (6.0.3)
actionpack (>= 6.1)
activesupport (>= 6.1)
railties (>= 6.1)
rspec-core (~> 3.12)
rspec-expectations (~> 3.12)
rspec-mocks (~> 3.12)
rspec-support (~> 3.12)
rspec-support (3.12.1)
rubocop (1.56.4)
base64 (~> 0.1.1)
Expand All @@ -235,6 +250,8 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
shoulda-matchers (5.3.0)
activesupport (>= 5.2.0)
sprockets (4.2.1)
concurrent-ruby (~> 1.0)
rack (>= 2.2.4, < 4)
Expand Down Expand Up @@ -280,15 +297,19 @@ DEPENDENCIES
capybara
debug
devise
factory_bot_rails
faker
importmap-rails
jbuilder
letter_opener
pg (~> 1.1)
puma (~> 5.0)
rails (~> 7.0.8)
rspec
rspec-rails (~> 6.0.0)
rubocop (>= 1.0, < 2.0)
selenium-webdriver
shoulda-matchers
sprockets-rails
stimulus-rails
turbo-rails
Expand All @@ -299,4 +320,4 @@ RUBY VERSION
ruby 3.2.2p53

BUNDLED WITH
2.4.19
2.4.20
10 changes: 10 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@
.navbar-nav .nav-link.active {
color: #1875cc !important;
}

.triangle-up {
width: 0;
height: 0;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
border-bottom: 8px #000;
display: inline-block;
margin-left: 5px;
}
4 changes: 3 additions & 1 deletion app/controllers/foods_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ class FoodsController < ApplicationController
before_action :authenticate_user!
def index
@user = current_user
@foods = @user.foods
order_direction = params[:sort] == 'name' && session[:sort_direction] == 'asc' ? 'desc' : 'asc'
session[:sort_direction] = order_direction
@foods = @user.foods.order("LOWER(name) #{order_direction}")
end

def show; end
Expand Down
7 changes: 7 additions & 0 deletions app/models/food.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
class Food < ApplicationRecord
belongs_to :user
has_many :recipe_foods, dependent: :destroy
has_many :recipes, through: :recipe_foods

validates :name, presence: true
validates :price, presence: true
validates :quantity, presence: true
validates :measurement_unit, presence: true
end
13 changes: 8 additions & 5 deletions app/views/foods/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
<%= render 'devise/shared/navbar' %>

<div class="container mt-5">
<h1 class="text-center">Food List</h1>
<h2 class="text-center"><%= @user.name %>'s Food</h2>
<h2 class="text-center">Food List</h2>
<% if notice %>
<p class="notice"><%= notice %></p>
<% end %>
<% if alert %>
<p class="alert"><%= alert %></p>
<% end %>
<div class="my-3 text-end">
<%= link_to "Add New Food", new_food_path, class: "btn btn-primary mt-3" %>
<%= link_to "Add New Food", new_food_path, class: "btn btn-primary mt-3" %>
</div>
<table class="border table">
<thead>
<tr>
<th>Food</th>
<th class="food-name">
Name
<%= link_to '', foods_path(sort: 'name'), class: 'triangle-up' %>
</th>
<th>Measurement Unit</th>
<th>Unit Price</th>
<th>Quantity</th>
Expand All @@ -29,11 +33,10 @@
<td>$ <%= food.price %></td>
<td><%= food.quantity %></td>
<td>
<%= button_to "Delete", food, method: :delete, class: "btn btn-danger btn-sm" %>
<%= button_to "Delete", food, method: :delete, class: "btn btn-danger btn-sm" %>
</td>
</tr>
<% end %>
</tbody>
</table>

</div>
57 changes: 49 additions & 8 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,49 @@
# frozen_string_literal: true
# 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 bin/rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: "Star Wars" }, { name: "Lord of the Rings" }])
# Character.create(name: "Luke", movie: movies.first)
# db/seeds.rb

# Crear usuarios
user1 = User.create!(
name: "Ejemplo Usuario 1",
email: "[email protected]",
password: "password123",
confirmed_at: Time.now
)

user2 = User.create!(
name: "Ejemplo Usuario 2",
email: "[email protected]",
password: "password123",
confirmed_at: Time.now
)

# Crear alimentos asociados a usuarios específicos
food1 = user1.foods.create!(name: 'Apple', measurement_unit: 'piece', price: 1, quantity: 10)
food2 = user1.foods.create!(name: 'Banana', measurement_unit: 'piece', price: 2, quantity: 8)

food3 = user2.foods.create!(name: 'Orange', measurement_unit: 'piece', price: 1, quantity: 12)
food4 = user2.foods.create!(name: 'Carrot', measurement_unit: 'piece', price: 1, quantity: 15)

# Crear recetas asociadas a usuarios específicos y alimentos específicos
recipe1 = user1.recipes.create!(
name: 'Fruit Salad',
preparation_time: 10,
cooking_time: 0,
description: 'Healthy fruit salad recipe.',
public: true
)

recipe2 = user2.recipes.create!(
name: 'Carrot Soup',
preparation_time: 15,
cooking_time: 20,
description: 'Delicious carrot soup recipe.',
public: true
)

# Asociar alimentos a recetas
RecipeFood.create!(recipe: recipe1, food: food1, quantity: 2)
RecipeFood.create!(recipe: recipe1, food: food2, quantity: 3)

RecipeFood.create!(recipe: recipe2, food: food3, quantity: 4)
RecipeFood.create!(recipe: recipe2, food: food4, quantity: 5)

puts "Seed data created successfully!"
50 changes: 50 additions & 0 deletions spec/controllers/foods_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'rails_helper'

RSpec.describe FoodsController, type: :controller do
let(:user) { TestConfiguration.create_example_user }

before do
sign_in user
end

describe 'GET #index' do
it 'returns a successful response' do
get :index
expect(response).to be_successful
end
end

describe 'GET #show' do
it 'returns a successful response' do
food = create(:food, user:)
get :show, params: { id: food.id }
expect(response).to be_successful
end
end

describe 'GET #new' do
it 'returns a successful response' do
get :new
expect(response).to be_successful
end
end

describe 'POST #create' do
it 'creates a new food with valid params' do
expect do
post :create, params: { food: attributes_for(:food) }
end.to change(Food, :count).by(1)
expect(response).to redirect_to(foods_path)
end
end

describe 'DELETE #destroy' do
it 'destroys the food' do
food = create(:food, user:)
expect do
delete :destroy, params: { id: food.id }
end.to change(Food, :count).by(-1)
expect(response).to redirect_to(foods_path)
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/devise.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :user do
email { Faker::Internet.email }
password { 'password' }
password_confirmation { 'password' }
name { Faker::Name.name }
end
end
9 changes: 9 additions & 0 deletions spec/factories/food.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FactoryBot.define do
factory :food do
name { Faker::Food.dish }
measurement_unit { Faker::Food.metric_measurement }
price { Faker::Number.decimal(l_digits: 2) }
quantity { Faker::Number.number(digits: 2) }
user
end
end
18 changes: 18 additions & 0 deletions spec/models/food_model_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'rails_helper'

RSpec.describe Food, type: :model do
let(:user) { TestConfiguration.create_example_user }
let(:food) { create(:food) }
let(:food_with_custom_values) { create(:food_with_price_and_quantity, price: 5, quantity: 10) }
describe 'Associations' do
it { should have_many(:recipe_foods).dependent(:destroy) }
it { should have_many(:recipes).through(:recipe_foods) }
end

describe 'Validations' do
it { should validate_presence_of(:name) }
it { should validate_presence_of(:measurement_unit) }
it { should validate_presence_of(:price) }
it { should validate_presence_of(:quantity) }
end
end
15 changes: 15 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe User, type: :model do
let(:user) { TestConfiguration.create_example_user }

it 'has name' do
expect(user.name).to be_present
end
it 'has email' do
expect(user.email).to be_present
end
it 'has password' do
expect(user.password).to be_present
end
end
Loading
Loading