From 160332e25f857464119ccb10f779cebff68ca6b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduardo=20Sim=C3=B3n=20Pic=C3=B3n?= Date: Sun, 15 Oct 2023 13:27:36 +0200 Subject: [PATCH] ci: add lint parallel job (#40) - Add installation notes on how to setup git hooks - Add pre-commit library to support git hooks. - Fix issues detected by standardrb. - Remove old code from accounts controller --- .github/workflows/main.yml | 16 ++++++ .pre-commit-config.yaml | 11 ++++ Gemfile.lock | 7 +++ README.md | 32 ++++++++--- app/controllers/accounts_controller.rb | 9 --- bin/standardrb-wrapper.sh | 4 ++ db/seeds.rb | 6 +- test/controllers/accounts_controller_test.rb | 2 +- test/models/account_test.rb | 4 +- test/models/category_test.rb | 60 ++++++++++---------- 10 files changed, 97 insertions(+), 54 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100755 bin/standardrb-wrapper.sh diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1b963c9..9d5646f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,6 +23,22 @@ env: POSTGRES_PASSWORD: postgres jobs: + lint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + + - name: Set up Ruby + uses: ruby/setup-ruby@v1 + with: + ruby-version: "3.1.0" + bundler-cache: true + + - name: Install deps + run: bundle install --retry 3 + + - name: Check lint issues + run: bundle exec rake standard test: runs-on: ubuntu-latest services: diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..4de6158 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,11 @@ +repos: + - repo: local + hooks: + - id: standardrb + name: Check Ruby style with StandardRB + description: Enforce Ruby style guide with StandardRB + entry: bin/standardrb-wrapper.sh + language: script + types: ["ruby"] + stages: [commit] + verbose: true diff --git a/Gemfile.lock b/Gemfile.lock index 6846208..8c3988c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -156,6 +156,7 @@ GEM matrix (0.4.2) method_source (1.0.0) mini_mime (1.1.5) + mini_portile2 (2.8.4) minitest (5.20.0) monetize (1.12.0) money (~> 6.12) @@ -180,6 +181,9 @@ GEM net-protocol net-ssh (7.2.0) nio4r (2.5.9) + nokogiri (1.15.4) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) nokogiri (1.15.4-aarch64-linux) racc (~> 1.4) nokogiri (1.15.4-x86_64-darwin) @@ -289,6 +293,8 @@ GEM stimulus-rails (1.2.2) railties (>= 6.0.0) stringio (3.0.8) + tailwindcss-rails (2.0.30) + railties (>= 6.0.0) tailwindcss-rails (2.0.30-aarch64-linux) railties (>= 6.0.0) tailwindcss-rails (2.0.30-x86_64-darwin) @@ -323,6 +329,7 @@ GEM PLATFORMS aarch64-linux + ruby x86_64-darwin-22 x86_64-linux diff --git a/README.md b/README.md index f2ce739..326755c 100644 --- a/README.md +++ b/README.md @@ -5,15 +5,17 @@ application up and running. Things you may want to cover: -* Ruby version +# Development + +## Ruby version Use a ruby dependency manager like rbenv or asdf. The supported ruby version is stated in the .ruby-version file -* System dependencies +## Setup -1. Install rails +1. Install dependencies -`gem install rails` +`bundle install` 2. Install postgres @@ -25,12 +27,25 @@ Use a ruby dependency manager like rbenv or asdf. The supported ruby version is 4. Prepare dabatase -`rails db:create db:migrate db:seed` +`bin/rails db:create db:migrate db:seed` 5. Run the server -`rails s` +`bin/rails s` + +## Installing git hooks + +You might install the provided git hooks to prevent committing changes that do not conform to the established style guide. + +For such please install `pre-commit` in your system. Refer to the [docs](https://pre-commit.com/#install) on how to install it. + +Once set up, you will need to install the hooks in your local repository. Please run: +``` +pre-commit install +``` + +After a successful execution, the commit hoo will trigger when committing files. # Testing @@ -43,14 +58,14 @@ Use a ruby dependency manager like rbenv or asdf. The supported ruby version is # Domain ✅ A `budget` is a collection of `categories`. -✅ A `category` has a name and an `allocated_amount` per month. +✅ A `category` has a name and an `allocated_amount` per month. ✅ A `target amount` is the desired allocated amount for a given `category` A `category` can be: + - `overspent` when the sum of movements is greater than the `allocated_amount` - `underfunded` when the `allocated_amount` is smaller than the `target amount` - `funded` when the `allocated_amount` is equal or greater than the `target_amount` and the sum of movements is fewer tham the `allocated_amount` - The `allocated_amount` of a category is depleted by each `movement`. A `movement` belongs to an account. It can be a `credit` or a `debit`. ✅ A `credit` is a money movement flowing out of your account. @@ -58,4 +73,3 @@ A `movement` belongs to an account. It can be a `credit` or a `debit`. ✅ A `movement` has a `payer`. ✅ A `movement` can be assigned to a `category` Every unassigned movement will be considered `Ready to asign` funds, i.e funds to be added to the budget. - diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 9654a22..d78e328 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -23,15 +23,6 @@ def sync PullAccountMovementsJob.perform_later(account_id: params[:account_id]) end - def authentication_result - reference = params[:ref] - - # Add the authenticated check to the account - # Add a common REF for all of the accounts - # Associate the account_id coming as part of the response to the account itself - # Use that account_id to fetch the rest of the details and save them - end - # POST /accounts or /accounts.json def create @account = Account.new(account_params) diff --git a/bin/standardrb-wrapper.sh b/bin/standardrb-wrapper.sh new file mode 100755 index 0000000..9789448 --- /dev/null +++ b/bin/standardrb-wrapper.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +bundle install >/dev/null +git diff --name-only --cached | xargs ls -1 2>/dev/null | grep '\.rb$' | xargs bundle exec standardrb --fix \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index 9c02d20..a847ad4 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -24,9 +24,9 @@ external_account_id: "48883f05-bfe1-46fb-818c-d272ace6a069", external_institution_id: "SANDBOXFINANCE_SFIN0000") -food_category = Category.create!(name: "Food", budget: budget, target_amount_cents: 200_00) -housing_category = Category.create!(name: "Housing", budget: budget, target_amount_cents: 0) -cat_category = Category.create!(name: "Cat", budget: budget, target_amount_cents: 0) +Category.create!(name: "Food", budget: budget, target_amount_cents: 200_00) +Category.create!(name: "Housing", budget: budget, target_amount_cents: 0) +Category.create!(name: "Cat", budget: budget, target_amount_cents: 0) def create_movement(amount:, date:, account:, description:, category: nil) Movement.create!( diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 6a8cd2e..0ed4e2b 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -36,7 +36,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest end test "should update account" do - resopnse = patch account_url(@account), params: {account: {iban: @account.iban, name: @account.name}} + patch account_url(@account), params: {account: {iban: @account.iban, name: @account.name}} assert_redirected_to account_url(@account) end diff --git a/test/models/account_test.rb b/test/models/account_test.rb index 3301a26..be46955 100644 --- a/test/models/account_test.rb +++ b/test/models/account_test.rb @@ -18,8 +18,8 @@ def setup budget: @budget ) - movement = Movement.create(account_id: account.id, payer: "payer", amount_cents: 100_00) - movement_2 = Movement.create(account_id: account.id, payer: "payer", amount_cents: -50_00) + Movement.create(account_id: account.id, payer: "payer", amount_cents: 100_00) + Movement.create(account_id: account.id, payer: "payer", amount_cents: -50_00) assert_equal account.balance.cents, 50_00 end diff --git a/test/models/category_test.rb b/test/models/category_test.rb index f67eaef..f2216d2 100644 --- a/test/models/category_test.rb +++ b/test/models/category_test.rb @@ -24,8 +24,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(60000, "EUR")) @category.update!(target_amount_cents: 50000) @@ -37,8 +37,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(50000, "EUR")) @category.update!(target_amount_cents: 50000) @@ -50,8 +50,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) @category.update!(target_amount_cents: 60000) @@ -62,8 +62,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(10000, "EUR")) assert_equal(@category.overspent?(beginning_of_month), true) @@ -73,8 +73,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(40000, "EUR")) assert_equal(@category.overspent?(beginning_of_month), false) @@ -84,8 +84,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(20000, "EUR")) assert_equal(@category.overspent?(beginning_of_month), false) @@ -107,7 +107,7 @@ class CategoryTest < ActiveSupport::TestCase @category.update!(target_amount_cents: 0) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(0, "EUR")) - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) assert_equal(@category.overspent?(beginning_of_month), true) end @@ -116,8 +116,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(30000, "EUR")) assert_equal(@category.spent_percentage_in_month(beginning_of_month), (20000.to_f / 30000.to_f) * 100) @@ -127,8 +127,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(0, "EUR")) assert_equal(@category.spent_percentage_in_month(beginning_of_month), 0.0) @@ -138,8 +138,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(30000, "EUR")) assert_equal(@category.in_spending?(beginning_of_month), true) @@ -149,8 +149,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: -10000, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(20000, "EUR")) assert_equal(@category.in_spending?(beginning_of_month), false) @@ -160,8 +160,8 @@ class CategoryTest < ActiveSupport::TestCase movement_date = Time.now.utc beginning_of_month = movement_date.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: movement_date) - movement_2 = Movement.create!(payer: "payer_2", amount_cents: 50_00, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_2", amount_cents: 50_00, account: @account, category: @category, created_at: movement_date) MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(20000, "EUR")) assert_equal(@category.spent_amount_in_month(beginning_of_month), Money.new(50_00, "EUR")) @@ -181,7 +181,7 @@ class CategoryTest < ActiveSupport::TestCase beginning_of_month = movement_date.beginning_of_month MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(200_00, "EUR")) - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -200_00, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -200_00, account: @account, category: @category, created_at: movement_date) assert_equal(@category.fully_spent?(beginning_of_month), true) end @@ -191,7 +191,7 @@ class CategoryTest < ActiveSupport::TestCase beginning_of_month = movement_date.beginning_of_month MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(200_00, "EUR")) - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -300_00, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -300_00, account: @account, category: @category, created_at: movement_date) assert_equal(@category.fully_spent?(beginning_of_month), false) end @@ -201,7 +201,7 @@ class CategoryTest < ActiveSupport::TestCase beginning_of_month = movement_date.beginning_of_month MonthlyAssignment.create!(category: @category, start_date: beginning_of_month, end_date: beginning_of_month.next_month, amount: Money.new(200_00, "EUR")) - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: movement_date) + Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: movement_date) assert_equal(@category.available_to_spend_in(beginning_of_month), Money.new(100_00, "EUR")) end @@ -211,8 +211,8 @@ class CategoryTest < ActiveSupport::TestCase beginning_of_current_month = movement_date.beginning_of_month beginning_of_previous_month = movement_date.prev_month.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: beginning_of_current_month) - movement_from_last_month = Movement.create!(payer: "payer_2", amount_cents: -200_00, account: @account, category: @category, created_at: beginning_of_previous_month) + Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: beginning_of_current_month) + Movement.create!(payer: "payer_2", amount_cents: -200_00, account: @account, category: @category, created_at: beginning_of_previous_month) assert_equal(@category.available_to_spend_in(beginning_of_current_month), Money.new(-300_00, "EUR")) end @@ -222,8 +222,8 @@ class CategoryTest < ActiveSupport::TestCase beginning_of_current_month = movement_date.beginning_of_month beginning_of_previous_month = movement_date.prev_month.beginning_of_month - movement_1 = Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: beginning_of_current_month) - movement_from_last_month = Movement.create!(payer: "payer_2", amount_cents: -200_00, account: @account, category: @category, created_at: beginning_of_previous_month) + Movement.create!(payer: "payer_1", amount_cents: -100_00, account: @account, category: @category, created_at: beginning_of_current_month) + Movement.create!(payer: "payer_2", amount_cents: -200_00, account: @account, category: @category, created_at: beginning_of_previous_month) MonthlyAssignment.create!(category: @category, start_date: beginning_of_current_month, end_date: beginning_of_current_month.next_month,