Merge pull request #1388 from internetee/add-db-constraints

Add database constraints
This commit is contained in:
Timo Võhmar 2019-10-28 15:16:50 +02:00 committed by GitHub
commit 3d73f41a55
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 58 additions and 18 deletions

View file

@ -0,0 +1,14 @@
class AddInvoiceItemsQuantityConstraint < ActiveRecord::Migration
def up
execute <<~SQL
ALTER TABLE invoice_items ADD CONSTRAINT invoice_items_quantity_is_positive
CHECK (quantity > 0);
SQL
end
def down
execute <<~SQL
ALTER TABLE invoice_items DROP CONSTRAINT invoice_items_quantity_is_positive;
SQL
end
end

View file

@ -0,0 +1,14 @@
class AddInvoicesDueDateConstraint < ActiveRecord::Migration
def up
execute <<~SQL
ALTER TABLE invoices ADD CONSTRAINT invoices_due_date_is_not_before_issue_date
CHECK (due_date >= issue_date);
SQL
end
def down
execute <<~SQL
ALTER TABLE invoices DROP CONSTRAINT invoices_due_date_is_not_before_issue_date;
SQL
end
end

View file

@ -899,7 +899,8 @@ CREATE TABLE public.invoice_items (
created_at timestamp without time zone, created_at timestamp without time zone,
updated_at timestamp without time zone, updated_at timestamp without time zone,
creator_str character varying, creator_str character varying,
updator_str character varying updator_str character varying,
CONSTRAINT invoice_items_quantity_is_positive CHECK ((quantity > 0))
); );
@ -968,7 +969,8 @@ CREATE TABLE public.invoices (
total numeric(10,2) NOT NULL, total numeric(10,2) NOT NULL,
in_directo boolean DEFAULT false, in_directo boolean DEFAULT false,
buyer_vat_no character varying, buyer_vat_no character varying,
issue_date date NOT NULL issue_date date NOT NULL,
CONSTRAINT invoices_due_date_is_not_before_issue_date CHECK ((due_date >= issue_date))
); );
@ -4878,3 +4880,7 @@ INSERT INTO schema_migrations (version) VALUES ('20191007123000');
INSERT INTO schema_migrations (version) VALUES ('20191008024334'); INSERT INTO schema_migrations (version) VALUES ('20191008024334');
INSERT INTO schema_migrations (version) VALUES ('20191024153351');
INSERT INTO schema_migrations (version) VALUES ('20191024160038');

View file

@ -11,7 +11,8 @@ class InvoiceTest < ActiveSupport::TestCase
def test_overdue_scope_returns_unpaid_uncancelled_invoices_with_past_due_date def test_overdue_scope_returns_unpaid_uncancelled_invoices_with_past_due_date
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
@invoice.update!(account_activity: nil, cancelled_at: nil, due_date: '2010-07-04') @invoice.update!(account_activity: nil, cancelled_at: nil, issue_date: '2010-07-04',
due_date: '2010-07-04')
assert Invoice.overdue.include?(@invoice), 'Should return overdue invoice' assert Invoice.overdue.include?(@invoice), 'Should return overdue invoice'
end end
@ -108,4 +109,4 @@ class InvoiceTest < ActiveSupport::TestCase
seller_zip: nil) seller_zip: nil)
assert_equal 'street, city, state', invoice.seller_address assert_equal 'street, city, state', invoice.seller_address
end end
end end

View file

@ -2,7 +2,6 @@ require 'test_helper'
class OverdueInvoiceCancellerTest < ActiveSupport::TestCase class OverdueInvoiceCancellerTest < ActiveSupport::TestCase
setup do setup do
@invoice = invoices(:one)
@original_days_to_keep_overdue_invoices_active = Setting.days_to_keep_overdue_invoices_active @original_days_to_keep_overdue_invoices_active = Setting.days_to_keep_overdue_invoices_active
end end
@ -28,26 +27,24 @@ class OverdueInvoiceCancellerTest < ActiveSupport::TestCase
def test_cancels_overdue_invoices def test_cancels_overdue_invoices
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
@invoice.update!(account_activity: nil, cancelled_at: nil, due_date: '2010-07-03') invoice = cancellable_invoice(due_date: '2010-07-03')
assert @invoice.cancellable?
canceller = OverdueInvoiceCanceller.new(delay: 1.day) canceller = OverdueInvoiceCanceller.new(delay: 1.day)
canceller.cancel canceller.cancel
@invoice.reload invoice.reload
assert @invoice.cancelled? assert invoice.cancelled?
end end
def test_yields_cancelled_invoices def test_yields_cancelled_invoices
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
@invoice.update!(account_activity: nil, cancelled_at: nil, due_date: '2010-07-03') invoice = cancellable_invoice(due_date: '2010-07-03')
assert @invoice.cancellable?
canceller = OverdueInvoiceCanceller.new(delay: 1.day) canceller = OverdueInvoiceCanceller.new(delay: 1.day)
iteration_count = 0 iteration_count = 0
canceller.cancel do |invoice| canceller.cancel do |cancelled_invoice|
assert_equal @invoice, invoice assert_equal invoice, cancelled_invoice
iteration_count += 1 iteration_count += 1
end end
assert_equal 1, iteration_count assert_equal 1, iteration_count
@ -55,13 +52,21 @@ class OverdueInvoiceCancellerTest < ActiveSupport::TestCase
def test_keeps_not_overdue_invoices_intact def test_keeps_not_overdue_invoices_intact
travel_to Time.zone.parse('2010-07-05') travel_to Time.zone.parse('2010-07-05')
@invoice.update!(account_activity: nil, cancelled_at: nil, due_date: '2010-07-04') invoice = cancellable_invoice(due_date: '2010-07-04')
assert @invoice.cancellable?
canceller = OverdueInvoiceCanceller.new(delay: 1.day) canceller = OverdueInvoiceCanceller.new(delay: 1.day)
canceller.cancel canceller.cancel
@invoice.reload invoice.reload
assert @invoice.not_cancelled? assert invoice.not_cancelled?
end end
end
private
def cancellable_invoice(due_date:)
invoice = invoices(:one)
invoice.update!(account_activity: nil, cancelled_at: nil, issue_date: due_date,
due_date: due_date)
invoice
end
end