Skip to content

Commit

Permalink
test_statistic_admin_controller
Browse files Browse the repository at this point in the history
  • Loading branch information
khungking909 committed Apr 3, 2024
1 parent f1da899 commit 21f19e7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ Metrics/PerceivedComplexity:
# Most of the Naming configurations are enabled by default, we should enable or disable configuration depending on what the team needs
Naming/MemoizedInstanceVariableName:
Enabled: false
Naming/VariableNumber:
Enabled: false
### Example
##
# Naming/VariableNumber:
Expand Down
1 change: 1 addition & 0 deletions app/controllers/admin/statistics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def monthly_statistics
def monthly_statistics_detail
@year = params[:year]
@month = params[:month]
@statistics_detail_sum = OrderHistory.statistical_detail_sum(year: @year, month: @month)
@pagy, @statistics_detail = pagy(OrderHistory.statistical_detail(@year, @month), items: Settings.DIGIT_5)
end
end
6 changes: 6 additions & 0 deletions app/models/order_history.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ class OrderHistory < ApplicationRecord
.where(order: { status: Order.statuses[:approved] })
.where("YEAR(order.created_at) = ? AND MONTH(order.created_at) = ?", year, month)
end)
def self.statistical_detail_sum(year:, month:)
joins(:order)
.where(order: { status: Order.statuses[:approved] })
.where("YEAR(order.created_at) = ? AND MONTH(order.created_at) = ?", year, month)
.sum("order_histories.quantity * order_histories.current_price")
end
end
4 changes: 2 additions & 2 deletions app/views/admin/statistics/monthly_statistics_detail.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
</tr>
</thead>
<tbody>
<%= render(partial: "statistic_detail", collection: @statistics_detail, locals: {total: total_price_monthly_statistic(@statistics_detail)}) || t("admin.statistics.empty") %>
<%= render(partial: "statistic_detail", collection: @statistics_detail, locals: {total: @statistics_detail_sum}) || t("admin.statistics.empty") %>
</tbody>
</table>
<%== pagy_bootstrap_nav(@pagy) if @pagy.pages > 1 %>
<div class="w-75 d-flex justify-content-end "><h3> <%= t("admin.statistics.total_price") %> : <%= number_to_currency(total_price_monthly_statistic(@statistics_detail), precision: 0) %></h3> </div>
<div class="w-75 d-flex justify-content-end "><h3> <%= t("admin.statistics.total_price") %> : <%= number_to_currency(@statistics_detail_sum, precision: 0) %></h3> </div>
</aside>
</div>
2 changes: 0 additions & 2 deletions spec/requests/admin/orders_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ def perform_action(method, action)

context "when order.status = 'pending' is " do
it "sets flash[:admin_success] with 'accept_order' message" do
order_pending_status = create(:order, account_id: account.id)
patch :update, params: { id: order_pending_status.id, status: :accept, comment: :accept }

expect(assigns(:order).reload.status_approved?).to(eq(true))
Expand All @@ -96,7 +95,6 @@ def perform_action(method, action)
end

it "sets flash[:admin_success] with 'refuse_order' message" do
order_pending_status = create(:order, account_id: account.id)
patch :update, params: { id: order_pending_status.id, status: :other_status_difficult_accept, comment: :refuse }

expect(assigns(:order).reload.status_reject?).to(eq(true))
Expand Down
77 changes: 77 additions & 0 deletions spec/requests/admin/statistics_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# frozen_string_literal: true

require "rails_helper"
require "support/shared_context/login_with_admin_context"
require "support/shared_context/login_fails_context"

RSpec.describe(Admin::StatisticsController, type: :controller) do
include SessionsHelper

let(:category) { create(:category) }
let(:product) { create(:product, category_id: category.id) }
let(:account) { FactoryBot.create(:account) }
let!(:order_month_1) { create(:order, account_id: account.id, created_at: "2024-1-12", status: Order.statuses[:approved]) }
let!(:order_month_2) { create(:order, account_id: account.id, created_at: "2024-2-12", status: Order.statuses[:approved]) }
let!(:order_histories_month_1) do
create_list(:order_history,
10,
order_id: order_month_1.id,
product_id: product.id,
quantity: 1,
current_price: 500
)
end
let!(:order_histories_month_2) do
create_list(:order_history,
5,
order_id: order_month_2.id,
product_id: product.id,
quantity: 1,
current_price: 200
)
end

def perform_action(_method, action)
case action
when :monthly_statistics
get(:monthly_statistics)
when :monthly_statistics_detail
get(:monthly_statistics_detail, params: { month: 1, year: 2024 })
end
end

describe "GET #monthly_statistics" do
context "when login success and render success" do
include_context "with a logged-in with admin", :get, :monthly_statistics

it "render index" do
expect(response).to(render_template("monthly_statistics"))
expect(assigns(:statistics)).to(be_a(ActiveRecord::Relation))
expect(assigns(:statistics)).to(all(be_a(Order)))
expect(assigns(:statistics).to_a.count).to(eq(2))
expect(assigns(:statistics).to_a.first.total_price).to(eq(5000))
expect(assigns(:statistics).last.total_price).to(eq(1000))
expect(response).to(have_http_status(:success))
end
end

include_context "login fails", :get, :monthly_statistics
end

describe "GET #monthly_statistics_detail" do
context "when login success and render success" do
include_context "with a logged-in with admin", :get, :monthly_statistics_detail

it "render index" do
expect(response).to(render_template("monthly_statistics_detail"))
expect(assigns(:statistics_detail)).to(be_a(ActiveRecord::Relation))
expect(assigns(:statistics_detail).to_a.count).to(eq(1))
expect(assigns(:statistics_detail).to_a.first.product_total_quantity).to(eq(10))
expect(assigns(:statistics_detail_sum)).to(eq(5000))
expect(assigns(:pagy).count).to(eq(1))
end
end

include_context "login fails", :get, :monthly_statistics_detail
end
end

0 comments on commit 21f19e7

Please sign in to comment.