1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-02-19 13:54:25 +01:00

coupon data model & tests

This commit is contained in:
Sylvain 2016-08-03 17:25:00 +02:00
parent c9a7c599c0
commit 3a932e75c0
9 changed files with 89 additions and 8 deletions

9
app/models/coupon.rb Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -0,0 +1,5 @@
class AddCouponToInvoice < ActiveRecord::Migration
def change
add_reference :invoices, :coupon, index: true, foreign_key: true
end
end

View File

@ -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"

21
test/fixtures/coupons.yml vendored Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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