From 3a932e75c0e109c509c3ef020af7872cc8f40e55 Mon Sep 17 00:00:00 2001 From: Sylvain Date: Wed, 3 Aug 2016 17:25:00 +0200 Subject: [PATCH] coupon data model & tests --- app/models/coupon.rb | 9 ++++++++ app/models/invoice.rb | 1 + db/migrate/20160803085201_create_coupons.rb | 16 ++++++++++++++ .../20160803104701_add_coupon_to_invoice.rb | 5 +++++ db/schema.rb | 18 +++++++++++++++- test/fixtures/coupons.yml | 21 +++++++++++++++++++ test/models/coupon_test.rb | 8 +++++++ test/models/export_test.rb | 6 ++---- test/models/organization_test.rb | 13 +++++++++--- 9 files changed, 89 insertions(+), 8 deletions(-) create mode 100644 app/models/coupon.rb create mode 100644 db/migrate/20160803085201_create_coupons.rb create mode 100644 db/migrate/20160803104701_add_coupon_to_invoice.rb create mode 100644 test/fixtures/coupons.yml create mode 100644 test/models/coupon_test.rb diff --git a/app/models/coupon.rb b/app/models/coupon.rb new file mode 100644 index 000000000..67db94b1d --- /dev/null +++ b/app/models/coupon.rb @@ -0,0 +1,9 @@ +class Coupon < ActiveRecord::Base + has_many :invoice + + validates :code, presence: true + validates :code, format: { with: /[A-Z0-9]+/ ,message: 'only caps letters and numbers'} + validates :percent_off, presence: true + validates :percent_off, :inclusion => 0..100 + +end diff --git a/app/models/invoice.rb b/app/models/invoice.rb index dd56d9629..3cd7c92cb 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -8,6 +8,7 @@ class Invoice < ActiveRecord::Base accepts_nested_attributes_for :invoice_items belongs_to :user belongs_to :wallet_transaction + belongs_to :coupon has_one :avoir, class_name: 'Invoice', foreign_key: :invoice_id, dependent: :destroy diff --git a/db/migrate/20160803085201_create_coupons.rb b/db/migrate/20160803085201_create_coupons.rb new file mode 100644 index 000000000..fbfcf322e --- /dev/null +++ b/db/migrate/20160803085201_create_coupons.rb @@ -0,0 +1,16 @@ +class CreateCoupons < ActiveRecord::Migration + def change + create_table :coupons do |t| + t.string :name + t.string :code + t.integer :percent_off + t.datetime :valid_until + t.integer :max_usages + t.integer :usages + t.boolean :active + t.string :stp_coupon_id + + t.timestamps null: false + end + end +end diff --git a/db/migrate/20160803104701_add_coupon_to_invoice.rb b/db/migrate/20160803104701_add_coupon_to_invoice.rb new file mode 100644 index 000000000..b58874bad --- /dev/null +++ b/db/migrate/20160803104701_add_coupon_to_invoice.rb @@ -0,0 +1,5 @@ +class AddCouponToInvoice < ActiveRecord::Migration + def change + add_reference :invoices, :coupon, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index dba8cbd7f..f94ed49ef 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160801153454) do +ActiveRecord::Schema.define(version: 20160803104701) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -104,6 +104,19 @@ ActiveRecord::Schema.define(version: 20160801153454) do t.string "name", limit: 255, null: false end + create_table "coupons", force: :cascade do |t| + t.string "name" + t.string "code" + t.integer "percent_off" + t.datetime "valid_until" + t.integer "max_usages" + t.integer "usages" + t.boolean "active" + t.string "stp_coupon_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "credits", force: :cascade do |t| t.integer "creditable_id" t.string "creditable_type", limit: 255 @@ -226,8 +239,10 @@ ActiveRecord::Schema.define(version: 20160801153454) do t.text "description" t.integer "wallet_amount" t.integer "wallet_transaction_id" + t.integer "coupon_id" end + add_index "invoices", ["coupon_id"], name: "index_invoices_on_coupon_id", using: :btree add_index "invoices", ["invoice_id"], name: "index_invoices_on_invoice_id", using: :btree add_index "invoices", ["user_id"], name: "index_invoices_on_user_id", using: :btree add_index "invoices", ["wallet_transaction_id"], name: "index_invoices_on_wallet_transaction_id", using: :btree @@ -755,6 +770,7 @@ ActiveRecord::Schema.define(version: 20160801153454) do add_foreign_key "events_event_themes", "event_themes" add_foreign_key "events_event_themes", "events" add_foreign_key "exports", "users" + add_foreign_key "invoices", "coupons" add_foreign_key "invoices", "wallet_transactions" add_foreign_key "o_auth2_mappings", "o_auth2_providers" add_foreign_key "open_api_calls_count_tracings", "open_api_clients" diff --git a/test/fixtures/coupons.yml b/test/fixtures/coupons.yml new file mode 100644 index 000000000..e2ba7a6b1 --- /dev/null +++ b/test/fixtures/coupons.yml @@ -0,0 +1,21 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + name: Christmas deals + code: XMAS10 + percent_off: 10 + valid_until: 2015-12-31 23:59:59 + max_usages: 0 + usages: 1 + active: true + stp_coupon_id: a3OlfO4u + +two: + name: Summer discounts + code: SUNNYFABLAB + percent_off: 15 + valid_until: <%= 1.month.from_now.utc.strftime('%Y-%m-%d %H:%M:%S.%9N Z') %> + max_usages: 10 + usages: 9 + active: true + stp_coupon_id: a42pm11w diff --git a/test/models/coupon_test.rb b/test/models/coupon_test.rb new file mode 100644 index 000000000..69e2e5d69 --- /dev/null +++ b/test/models/coupon_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class CouponTest < ActiveSupport::TestCase + test 'coupon must have a valid percentage' do + c = Coupon.new({code: 'DISCOUNT', percent_off: 800}) + assert c.invalid? + end +end diff --git a/test/models/export_test.rb b/test/models/export_test.rb index 1acf60e3e..aed660cac 100644 --- a/test/models/export_test.rb +++ b/test/models/export_test.rb @@ -2,10 +2,8 @@ require 'test_helper' class ExportTest < ActiveSupport::TestCase test 'export must have a category' do - e = Export.create({export_type: 'global', user: User.first, query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'}) - assert_raises ActiveRecord::RecordInvalid do - e.save! - end + e = Export.new({export_type: 'global', user: User.first, query: '{"query":{"bool":{"must":[{"range":{"date":{"gte":"2016-06-25T02:00:00+02:00","lte":"2016-07-25T23:59:59+02:00"}}}]}}}'}) + assert e.invalid? end test 'export generate an XLSX file' do diff --git a/test/models/organization_test.rb b/test/models/organization_test.rb index d144816ef..0dd15f376 100644 --- a/test/models/organization_test.rb +++ b/test/models/organization_test.rb @@ -1,7 +1,14 @@ require 'test_helper' class OrganizationTest < ActiveSupport::TestCase - # test "the truth" do - # assert true - # end + test 'organization must have a name' do + a = Address.new({address: '14 avenue du Maréchal Tartanpion, 12345 Saint-Robert-sur-Mer'}) + o = Organization.new({address: a}) + assert o.invalid? + end + + test 'organization must have an address' do + o = Organization.new({name: 'Menuiserie G. Dubois'}) + assert o.invalid? + end end