require 'rails_helper'

RSpec.describe GestaoDeEstoque::ItemDaDevolucaoDeMaterial, type: :model do
  it { is_expected.to belong_to(:devolucao_de_material).class_name("GestaoDeEstoque::DevolucaoDeMaterial") }
  it { is_expected.to belong_to(:item).class_name("Base::Item") }
  it { is_expected.to belong_to(:unidade_de_medida).class_name("UnidadeDeMedida") }

  it { is_expected.to validate_presence_of(:quantidade_devolvida) }

  context "Valor únitario médio" do
    it "Quando tem um estoque" do
      devolucao = FactoryBot.create(:gestao_de_estoque_devolucao_de_material)
      item_da_devolucao = FactoryBot.build(:gestao_de_estoque_item_da_devolucao_de_material,
      devolucao_de_material_id: devolucao.id)
      estoque = FactoryBot.build(:gestao_de_estoque_estoque,
        item_id: item_da_devolucao.item_id,
        unidade_de_medida_id: item_da_devolucao.unidade_de_medida_id ,
        almoxarifado_id: devolucao.requisicao_de_material.almoxarifado_id,
        unidade_orcamentaria_id: devolucao.requisicao_de_material.unidade_orcamentaria_id,
        valor_unitario_medio: 10)
      estoque.save(validate: false)
      expect(item_da_devolucao.valor_unitario_medio).to eq 10
    end
    it "Quando não tem um estoque" do
      item_da_devolucao = FactoryBot.build(:gestao_de_estoque_item_da_devolucao_de_material)
      expect(item_da_devolucao.valor_unitario_medio).to eq 0
    end
  end

  context "Valor total" do
    it "Quando tem um estoque" do
      devolucao = FactoryBot.create(:gestao_de_estoque_devolucao_de_material)
      item_da_devolucao = FactoryBot.build(:gestao_de_estoque_item_da_devolucao_de_material,
        devolucao_de_material_id: devolucao.id,
        quantidade_devolvida: 10)
      estoque = FactoryBot.build(:gestao_de_estoque_estoque,
        item_id: item_da_devolucao.item_id,
        unidade_de_medida_id: item_da_devolucao.unidade_de_medida_id ,
        almoxarifado_id: devolucao.requisicao_de_material.almoxarifado_id,
        unidade_orcamentaria_id: devolucao.requisicao_de_material.unidade_orcamentaria_id,
        valor_unitario_medio: 10)
      estoque.save(validate: false)
      expect(item_da_devolucao.valor_total).to eq 100
    end
    it "Quando não tem um estoque" do
      item_da_devolucao = FactoryBot.build(:gestao_de_estoque_item_da_devolucao_de_material,
      quantidade_devolvida: 10)
      expect(item_da_devolucao.valor_unitario_medio).to eq 0
    end
  end
end
