-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from PabloBona/feature/add-food-tests
Add Unit and Integration Tests for Food
- Loading branch information
Showing
20 changed files
with
456 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.