omega-tariffs-custom 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,15 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ lib/common_tariff.rb
5
+ lib/morning_packet_tariff.rb
6
+ lib/night_packet_holiday_tariff.rb
7
+ lib/night_packet_weekday_tariff.rb
8
+ lib/omega-tariffs-custom.rb
9
+ spec/common_tariff_spec.rb
10
+ spec/lib/active_record_classes_stub.rb
11
+ spec/lib/init.rb
12
+ spec/lib/spec_helper.rb
13
+ spec/morning_packet_tariff_spec.rb
14
+ spec/night_packet_holiday_tariff_spec.rb
15
+ spec/night_packet_weekday_tariff_spec.rb
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Uniq Systems Omega Sector custom tariffs gem
2
+
3
+ This gem implements custom Omega Sector tariffs. Bases on the generic tariffs gem.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+
5
+ Echoe.new('omega-tariffs-custom', '0.1.0') do |p|
6
+ p.description = "Just a test sky gem"
7
+ p.url = "http://uniqsystems.ru"
8
+ p.author = "Uniq Systems"
9
+ p.email = "ivan@uniqsystems.ru"
10
+ p.ignore_pattern = ["tmp/*", "script/*"]
11
+ p.runtime_dependencies = p.development_dependencies = ["omega-tariffs-base >=0.1.4"]
12
+ end
13
+
14
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,47 @@
1
+ module UniqSysOmega
2
+ module CustomTariffs
3
+ module Specs
4
+
5
+ def self.common_tariff(tariff_id)
6
+ settings = YAML.load <<-CONFIG
7
+ ---
8
+ :klass: RepeatingFilter
9
+ :settings:
10
+ ?
11
+ :length: 64800
12
+ :starts_at: 2010-08-01 14:00:00 +04:00
13
+ :period: 86400
14
+ :
15
+ :klass: ComputerClassFilter
16
+ :settings:
17
+ 1:
18
+ :klass: RateFilter
19
+ :settings:
20
+ :rate: 395
21
+ 2:
22
+ :klass: RateFilter
23
+ :settings:
24
+ :rate: 495
25
+
26
+ ?
27
+ :length: 21600
28
+ :starts_at: 2010-08-01 08:00:00 +04:00
29
+ :period: 86400
30
+ :
31
+ :klass: ComputerClassFilter
32
+ :settings:
33
+ 1:
34
+ :klass: RateFilter
35
+ :settings:
36
+ :rate: 195
37
+ 2:
38
+ :klass: RateFilter
39
+ :settings:
40
+ :rate: 295
41
+ CONFIG
42
+ TarifBuilder.build Settings.new(settings), tariff_id
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,37 @@
1
+ module UniqSysOmega
2
+ module CustomTariffs
3
+ module Specs
4
+
5
+ def self.morning_packet_tariff(tariff_id)
6
+ settings = YAML.load <<-CONFIG
7
+ ---
8
+ :klass: RepeatingFilter
9
+ :settings:
10
+ ?
11
+ :length: 21600
12
+ :starts_at: 2010-08-02 04:00:00 +04:00
13
+ :period: 86400
14
+ :
15
+ :klass: ComputerClassFilter
16
+ :settings:
17
+ 1:
18
+ :klass: SingleTimeFilter
19
+ :settings:
20
+ :price: 500
21
+ 2:
22
+ :klass: SingleTimeFilter
23
+ :settings:
24
+ :price: 700
25
+ ?
26
+ :length: 64800
27
+ :starts_at: 2010-08-02 10:00:00 +04:00
28
+ :period: 86400
29
+ :
30
+ :klass: DenyFilter
31
+ CONFIG
32
+ TarifBuilder.build Settings.new(settings), tariff_id
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,50 @@
1
+ module UniqSysOmega
2
+ module CustomTariffs
3
+ module Specs
4
+
5
+ def self.night_packet_holiday_tariff(tariff_id)
6
+ settings = YAML.load <<-CONFIG
7
+ ---
8
+ :klass: RepeatingFilter
9
+ :settings:
10
+ ?
11
+ :length: 122400
12
+ :starts_at: 2010-08-07 22:00:00 +04:00
13
+ :period: 604800
14
+ :
15
+ :klass: RepeatingFilter
16
+ :settings:
17
+ ?
18
+ :length: 36000
19
+ :starts_at: 2010-08-07 22:00:00 +04:00
20
+ :period: 86400
21
+ :
22
+ :klass: ComputerClassFilter
23
+ :settings:
24
+ 1:
25
+ :klass: SingleTimeFilter
26
+ :settings:
27
+ :price: 1495
28
+ 2:
29
+ :klass: SingleTimeFilter
30
+ :settings:
31
+ :price: 1995
32
+ ?
33
+ :length: 50400
34
+ :starts_at: 2010-08-08 08:00:00 +04:00
35
+ :period: 86400
36
+ :
37
+ :klass: DenyFilter
38
+ ?
39
+ :length: 482400
40
+ :starts_at: 2010-08-09 08:00:00 +04:00
41
+ :period: 604800
42
+ :
43
+ :klass: DenyFilter
44
+ CONFIG
45
+ TarifBuilder.build Settings.new(settings), tariff_id
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,50 @@
1
+ module UniqSysOmega
2
+ module CustomTariffs
3
+ module Specs
4
+
5
+ def self.night_packet_weekday_tariff(tariff_id)
6
+ settings = YAML.load <<-CONFIG
7
+ ---
8
+ :klass: RepeatingFilter
9
+ :settings:
10
+ ?
11
+ :length: 381600
12
+ :starts_at: 2010-08-02 22:00:00 +04:00
13
+ :period: 604800
14
+ :
15
+ :klass: RepeatingFilter
16
+ :settings:
17
+ ?
18
+ :length: 36000
19
+ :starts_at: 2010-08-02 22:00:00 +04:00
20
+ :period: 86400
21
+ :
22
+ :klass: ComputerClassFilter
23
+ :settings:
24
+ 1:
25
+ :klass: SingleTimeFilter
26
+ :settings:
27
+ :price: 995
28
+ 2:
29
+ :klass: SingleTimeFilter
30
+ :settings:
31
+ :price: 1495
32
+ ?
33
+ :length: 50400
34
+ :starts_at: 2010-08-02 08:00:00 +04:00
35
+ :period: 86400
36
+ :
37
+ :klass: DenyFilter
38
+ ?
39
+ :length: 223200
40
+ :starts_at: 2010-08-07 08:00:00 +04:00
41
+ :period: 604800
42
+ :
43
+ :klass: DenyFilter
44
+ CONFIG
45
+ TarifBuilder.build Settings.new(settings), tariff_id
46
+ end
47
+
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require 'common_tariff'
2
+ require 'morning_packet_tariff'
3
+ require 'night_packet_holiday_tariff'
4
+ require 'night_packet_weekday_tariff'
5
+
6
+ require 'rubygems'
7
+ require 'omega-tariffs-base'
8
+ require 'yaml'
9
+
10
+ module UniqSysOmega
11
+ module CustomTariffs
12
+ module Specs
13
+ include UniqSysOmega::Tariffs
14
+ end
15
+
16
+ class CustomTariffBuilder
17
+ class <<self
18
+
19
+ def build(symbol, tariff_id)
20
+ Specs.send(symbol, tariff_id)
21
+ end
22
+
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{omega-tariffs-custom}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Uniq Systems"]
9
+ s.date = %q{2010-09-22}
10
+ s.description = %q{Just a test sky gem}
11
+ s.email = %q{ivan@uniqsystems.ru}
12
+ s.extra_rdoc_files = ["README.rdoc", "lib/common_tariff.rb", "lib/morning_packet_tariff.rb", "lib/night_packet_holiday_tariff.rb", "lib/night_packet_weekday_tariff.rb", "lib/omega-tariffs-custom.rb"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/common_tariff.rb", "lib/morning_packet_tariff.rb", "lib/night_packet_holiday_tariff.rb", "lib/night_packet_weekday_tariff.rb", "lib/omega-tariffs-custom.rb", "spec/common_tariff_spec.rb", "spec/lib/active_record_classes_stub.rb", "spec/lib/init.rb", "spec/lib/spec_helper.rb", "spec/morning_packet_tariff_spec.rb", "spec/night_packet_holiday_tariff_spec.rb", "spec/night_packet_weekday_tariff_spec.rb", "omega-tariffs-custom.gemspec"]
14
+ s.homepage = %q{http://uniqsystems.ru}
15
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Omega-tariffs-custom", "--main", "README.rdoc"]
16
+ s.require_paths = ["lib"]
17
+ s.rubyforge_project = %q{omega-tariffs-custom}
18
+ s.rubygems_version = %q{1.3.7}
19
+ s.summary = %q{Just a test sky gem}
20
+
21
+ if s.respond_to? :specification_version then
22
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
27
+ s.add_development_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
28
+ else
29
+ s.add_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
30
+ s.add_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
34
+ s.add_dependency(%q<omega-tariffs-base>, [">= 0.1.4"])
35
+ end
36
+ end
@@ -0,0 +1,95 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'init'))
2
+
3
+ describe "Common tarif" do
4
+ before do
5
+ tarif_start = Time.local(2010, 8, 1, 0, 0, 0)
6
+ @filter = UniqSysOmega::CustomTariffs::CustomTariffBuilder.build :common_tariff, 1
7
+ FinTransaction.reset_transactions
8
+ end
9
+
10
+ it "should perform cost calculations correctly" do
11
+ moment = Time.local(2010, 8, 23, 0, 0, 0)
12
+ [
13
+ { :computer_id => 1, :start => moment, :length => 3.hours, :target => 395*3 },
14
+ { :computer_id => 1, :start => moment, :length => 9.hours, :target => 395*8 + 195*1},
15
+ { :computer_id => 1, :start => moment, :length => 8.hours + 2.hours, :target => (395*8 + 195*2) },
16
+ { :computer_id => 1, :start => moment + 2.hours, :length => 10.hours, :target => (395*6 + 195*4) },
17
+ { :computer_id => 1, :start => moment, :length => 24.hours, :target => (395*18 + 195*6) },
18
+
19
+ { :computer_id => 2, :start => moment, :length => 3.hours, :target => 495*3 },
20
+ { :computer_id => 2, :start => moment, :length => 9.hours, :target => 495*8 + 295*1 },
21
+ { :computer_id => 2, :start => moment, :length => 8.hours + 2.hours, :target => (495*8 + 295*2) },
22
+ { :computer_id => 2, :start => moment + 2.hours, :length => 10.hours, :target => (495*6 + 295*4) },
23
+ { :computer_id => 2, :start => moment, :length => 24.hours, :target => (495*18 + 295*6) }
24
+ ].each do |t|
25
+ @filter.calculate_cost(t[:computer_id], DOESNT_MATTER, t[:start], t[:length]).should == t[:target]
26
+ end
27
+ end
28
+
29
+ it "should allow to start session and continue it all through the day" do
30
+ now = moment = Time.local(2010, 8, 23, 0, 0, 0)
31
+ while now < moment + 1.day
32
+ @filter.permitted?(DOESNT_MATTER, 1, DOESNT_MATTER).should == true
33
+ @filter.start_permitted?(DOESNT_MATTER, 1, DOESNT_MATTER).should == true
34
+ now += 30.seconds
35
+ end
36
+ end
37
+
38
+ it "should make correct financial transactions for ordinary class" do
39
+ activity = Activity.new
40
+ activity.created_at = Time.now
41
+ activity.amount = 24.hours
42
+ activity.computer_id = 1
43
+
44
+ ft = FinTransaction.template
45
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
46
+
47
+ @filter.process_activity activity, ft
48
+
49
+ FinTransaction.transactions.sum.should == (395*18 + 195*6)
50
+ end
51
+
52
+ it "should make correct financial transactions for ordinary class #2" do
53
+ activity = Activity.new
54
+ activity.created_at = Time.local(2010, 8, 25, 3, 12, 15)
55
+ activity.amount = 23.hours
56
+ activity.computer_id = 1
57
+
58
+ ft = FinTransaction.template
59
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
60
+
61
+ @filter.process_activity activity, ft
62
+
63
+ FinTransaction.transactions.map(&:round).should == [ 1894, 1170, 4821 ]
64
+ FinTransaction.transactions.sum.round.should == 7885
65
+ end
66
+
67
+ it "should make correct financial transactions for VIP class" do
68
+ activity = Activity.new
69
+ activity.created_at = Time.now
70
+ activity.amount = 24.hours
71
+ activity.computer_id = 2
72
+
73
+ ft = FinTransaction.template
74
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
75
+
76
+ @filter.process_activity activity, ft
77
+
78
+ FinTransaction.transactions.sum.should == (495*18 + 295*6)
79
+ end
80
+
81
+ it "should make correct financial transactions for VIP class #2" do
82
+ activity = Activity.new
83
+ activity.created_at = Time.local(2010, 8, 25, 3, 12, 15)
84
+ activity.amount = 23.hours
85
+ activity.computer_id = 2
86
+
87
+ ft = FinTransaction.template
88
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
89
+
90
+ @filter.process_activity activity, ft
91
+
92
+ FinTransaction.transactions.map(&:round).should == [ 2374, 1770, 6041 ]
93
+ FinTransaction.transactions.sum.round.should == 10185
94
+ end
95
+ end
@@ -0,0 +1,151 @@
1
+ require 'rubygems'
2
+ gem 'activerecord', '=2.3.8'
3
+ require 'active_record'
4
+
5
+ class MockBase < ActiveRecord::Base
6
+ end
7
+
8
+ MockBase.class_eval do
9
+ alias_method :save, :valid?
10
+
11
+ def new_save!
12
+ raise "Invalid record: #{errors.full_messages.join("\n")}" unless valid?
13
+ end
14
+
15
+ alias_method :save!, :new_save!
16
+
17
+ def self.columns()
18
+ @columns ||= [];
19
+ end
20
+
21
+ def self.column(name, sql_type = nil, default = nil, null = true)
22
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type, null)
23
+ end
24
+
25
+ def self.template
26
+ instance = new
27
+ instance.instance_eval %Q{
28
+ def save
29
+ raise "Cannot save a template transaction"
30
+ end
31
+
32
+ def save!
33
+ raise "Cannot save a template transaction"
34
+ end
35
+ }
36
+
37
+ instance.expects(:save).never
38
+ instance.expects(:save!).never
39
+
40
+ instance
41
+ end
42
+ end
43
+
44
+ class FinTransaction < MockBase
45
+ column :club_id, :integer
46
+ column :activity_id, :integer
47
+ column :author_id, :integer
48
+ column :src_account_id, :integer
49
+ column :dst_account_id, :integer
50
+ column :created_at, :timestamp
51
+ column :volume, :decimal
52
+
53
+ validates_presence_of :club_id
54
+ validates_presence_of :author_id
55
+ validates_presence_of :volume
56
+
57
+ class << self
58
+ attr_accessor :transactions
59
+
60
+ def reset_transactions
61
+ self.transactions = [ ]
62
+ end
63
+
64
+ def charge_login(login_id, volume)
65
+ FinTransaction.new(:author_id => 1, :club_id => 2,
66
+ :src_account_id => login_id, :dst_account_id => -1,
67
+ :volume => volume)
68
+ end
69
+ end
70
+
71
+ def save
72
+ return false unless valid?
73
+ do_save
74
+ true
75
+ end
76
+
77
+ def save!
78
+ raise "Validation error" unless valid?
79
+ do_save
80
+ end
81
+
82
+ protected
83
+ def do_save
84
+ self.class.transactions ||= []
85
+ self.class.transactions << volume
86
+ end
87
+ end
88
+
89
+ class Activity < MockBase
90
+ column :created_at
91
+ column :login_id, :decimal
92
+ column :amount
93
+ column :computer_id
94
+ end
95
+
96
+ class Login
97
+ attr_accessor :id
98
+
99
+ def balance
100
+ return 0 unless self.class.balance
101
+ self.class.balance[id] || 0
102
+ end
103
+
104
+ class <<self
105
+ def find(id)
106
+ Login.new.tap do |login|
107
+ login.id = id
108
+ end
109
+ end
110
+
111
+ attr_accessor :balance
112
+
113
+ def set_login_balance(id, balance)
114
+ self.balance ||= { }
115
+ self.balance[id] = balance
116
+ end
117
+ end
118
+ end
119
+
120
+ class Computer
121
+ attr_accessor :id
122
+ attr_accessor :computer_class_id
123
+
124
+ class <<self
125
+ def find(computer_id)
126
+ Computer.new.tap do |computer|
127
+ computer.id = computer_id
128
+ computer.computer_class_id = computer_id
129
+ end
130
+ end
131
+ end
132
+ end
133
+
134
+ class ComputerClass
135
+ attr_accessor :id
136
+
137
+ class <<self
138
+ def count
139
+ all.size
140
+ end
141
+
142
+ def all
143
+ [ 1, 2 ].map { |id| with_id(id) }
144
+ end
145
+
146
+ private
147
+ def with_id(id)
148
+ ComputerClass.new.tap { |me| me.id = id}
149
+ end
150
+ end
151
+ end
data/spec/lib/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'spec'
2
+ require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper'))
3
+
4
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'lib', 'omega-tariffs-custom'))
@@ -0,0 +1,9 @@
1
+ require 'mocha'
2
+ require 'timecop'
3
+ require File.expand_path(File.join(File.dirname(__FILE__), 'active_record_classes_stub'))
4
+
5
+ Spec::Runner.configure do |config|
6
+ config.mock_with :mocha
7
+ end
8
+
9
+ DOESNT_MATTER = nil
@@ -0,0 +1,127 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'init'))
2
+
3
+ describe "Morning packet tarif" do
4
+ before do
5
+ @filter = UniqSysOmega::CustomTariffs::CustomTariffBuilder.build :morning_packet_tariff, 1
6
+ FinTransaction.reset_transactions
7
+ end
8
+
9
+ it "should deny login and usage at the wrong time" do
10
+ Login.set_login_balance(1, 1000)
11
+ moment = Time.parse('2011-03-11 03:59:59 +04:00')
12
+
13
+ [[0, false, false], [1, true, true], [3600*6, true, true], [1+3600*6, false, false]].each do |variant|
14
+ Timecop.freeze(moment+variant[0]) do
15
+ @filter.start_permitted?(3, 1, 1).should == variant[1]
16
+ @filter.permitted?(3, 1, 1).should == variant[2]
17
+ end
18
+ end
19
+ end
20
+
21
+ it "should deny login and usage with the lack of balance" do
22
+ Login.set_login_balance(1, 300)
23
+ moment = Time.parse('2011-03-11 03:59:59 +04:00')
24
+ period_end = Time.parse('2011-03-14 23:59:59 +04:00')
25
+ while moment < period_end
26
+ Timecop.freeze(moment) do
27
+ @filter.start_permitted?(3, 1, 1).should == false
28
+ @filter.permitted?(3, 1, 1).should == false
29
+ end
30
+ moment += 60
31
+ end
32
+ end
33
+
34
+ it "should deny login and usage with the balance that is not enough for the class" do
35
+ Login.set_login_balance(1, 699)
36
+ moment = Time.parse('2011-03-11 03:59:59 +04:00')
37
+
38
+ [0, 1, 3600*10, 1+3600*10].each do |variant|
39
+ Timecop.freeze(moment+variant[0]) do
40
+ @filter.start_permitted?(3, 2, 1).should == false
41
+ @filter.permitted?(3, 2, 1).should == false
42
+ end
43
+ end
44
+ end
45
+
46
+ def do_several_activities(offsets=[0], class_id = 1)
47
+ ft = FinTransaction.template
48
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
49
+
50
+ offsets.each do |offset|
51
+ activity = Activity.new
52
+ activity.computer_id = class_id
53
+ activity.created_at = Time.now + offset
54
+ activity.login_id = 1
55
+ activity.amount = 30
56
+ @filter.process_activity(activity, ft).should == true
57
+ end
58
+ end
59
+
60
+ def actualize_balance!
61
+ Login.set_login_balance(1, Login.find(1).balance - FinTransaction.transactions.sum)
62
+ FinTransaction.reset_transactions
63
+ end
64
+
65
+ it "should charge correctly" do
66
+ Login.set_login_balance(1, 705)
67
+ moment = Time.parse('2011-03-27 03:59:59 +04:00')
68
+ Timecop.freeze(moment) do
69
+ @filter.start_permitted?(3, 2, 1).should == false
70
+ @filter.permitted?(3, 2, 1).should == false
71
+ end
72
+
73
+ Timecop.freeze(moment+1) do
74
+ @filter.start_permitted?(3, 2, 1).should == true
75
+ do_several_activities [0, 30, 60], 2
76
+ actualize_balance!
77
+ @filter.permitted?(3, 2, 1).should == true
78
+ Login.find(1).balance.should == 5
79
+ end
80
+
81
+ Timecop.freeze(moment+6.hours) do
82
+ @filter.start_permitted?(3, 2, 1).should == true
83
+ @filter.permitted?(3, 2, 1).should == true
84
+ end
85
+
86
+ Timecop.freeze(moment+6.hours+1) do
87
+ @filter.start_permitted?(3, 2, 1).should == false
88
+ @filter.permitted?(3, 2, 1).should == false
89
+ end
90
+ end
91
+
92
+ it "should correctly calculate the balance long after the activity's done" do
93
+ Login.set_login_balance(1, 505)
94
+ moment = Time.parse('2010-10-11 04:30:00 +04:00')
95
+
96
+ ft = FinTransaction.template
97
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
98
+
99
+ activity = Activity.new
100
+ activity.computer_id = 1
101
+ activity.created_at = moment
102
+ activity.login_id = 1
103
+ activity.amount = 666
104
+ @filter.process_activity(activity, ft).should == true
105
+
106
+ actualize_balance!
107
+ Login.find(1).balance.should == (505-500)
108
+ end
109
+
110
+ it "should accept activities that are slightly longer than the filter period" do
111
+ Login.set_login_balance(1, 505)
112
+ moment = Time.parse('2010-10-14 04:30:00 +04:00')
113
+
114
+ ft = FinTransaction.template
115
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
116
+
117
+ activity = Activity.new
118
+ activity.computer_id = 1
119
+ activity.created_at = moment
120
+ activity.login_id = 1
121
+ activity.amount = 36000
122
+ @filter.process_activity(activity, ft).should == true
123
+
124
+ actualize_balance!
125
+ Login.find(1).balance.should == (505-500)
126
+ end
127
+ end
@@ -0,0 +1,145 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'init'))
2
+
3
+ describe "Night packet holiday tarif" do
4
+ before do
5
+ @filter = UniqSysOmega::CustomTariffs::CustomTariffBuilder.build :night_packet_holiday_tariff, 1
6
+ FinTransaction.reset_transactions
7
+ end
8
+
9
+ it "should deny login and usage at the wrong time" do
10
+ Login.set_login_balance(1, 1500)
11
+ moment = Time.parse('2010-09-18 21:59:59')
12
+
13
+ [0, 1].each do |weekday|
14
+ [[0, false, false], [1, true, true], [3600*10, true, true], [1+3600*10, false, false]].each do |variant|
15
+ Timecop.freeze(moment+variant[0]+weekday*24*3600) do
16
+ @filter.start_permitted?(3, 1, 1).should == variant[1]
17
+ @filter.permitted?(3, 1, 1).should == variant[2]
18
+ end
19
+ end
20
+ end
21
+
22
+ [2, 3, 4, 5, 6].each do |weekday|
23
+ [0, 1, 3600*10, 1+3600*10].each do |offset|
24
+ Timecop.freeze(moment+offset+weekday*24*3600) do
25
+ @filter.start_permitted?(3, 1, 1).should == false
26
+ @filter.permitted?(3, 1, 1).should == false
27
+ end
28
+ end
29
+ end
30
+
31
+ Timecop.freeze(moment+1) do
32
+ @filter.start_permitted?(3, 1, 1).should == true
33
+ @filter.permitted?(3, 1, 1).should == true
34
+ end
35
+ end
36
+
37
+ it "should deny login and usage with the lack of balance" do
38
+ Login.set_login_balance(1, 1494)
39
+ moment = Time.parse('2010-09-25 21:59:59')
40
+ period_end = Time.parse('2010-09-27 23:59:59')
41
+ while moment < period_end
42
+ Timecop.freeze(moment) do
43
+ @filter.start_permitted?(3, 1, 1).should == false
44
+ @filter.permitted?(3, 1, 1).should == false
45
+ end
46
+ moment += 60
47
+ end
48
+ end
49
+
50
+ it "should deny login and usage with the balance that is not enough for the class" do
51
+ Login.set_login_balance(1, 1994)
52
+ moment = Time.parse('2010-09-18 21:59:59')
53
+
54
+ [0, 1, 2, 3, 4, 5, 6].each do |weekday|
55
+ [0, 1, 3600*10, 1+3600*10].each do |variant|
56
+ Timecop.freeze(moment+variant[0]+weekday*24*3600) do
57
+ @filter.start_permitted?(3, 2, 1).should == false
58
+ @filter.permitted?(3, 2, 1).should == false
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def do_several_activities(offsets=[0], class_id = 1)
65
+ ft = FinTransaction.template
66
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
67
+
68
+ offsets.each do |offset|
69
+ activity = Activity.new
70
+ activity.computer_id = class_id
71
+ activity.created_at = Time.now + offset
72
+ activity.login_id = 1
73
+ activity.amount = 30
74
+ @filter.process_activity(activity, ft).should == true
75
+ end
76
+ end
77
+
78
+ def actualize_balance!
79
+ Login.set_login_balance(1, Login.find(1).balance - FinTransaction.transactions.sum)
80
+ FinTransaction.reset_transactions
81
+ end
82
+
83
+ it "should charge correctly" do
84
+ Login.set_login_balance(1, 2000)
85
+ moment = Time.parse('2010-10-16 21:59:59')
86
+ Timecop.freeze(moment) do
87
+ @filter.start_permitted?(3, 2, 1).should == false
88
+ @filter.permitted?(3, 2, 1).should == false
89
+ end
90
+
91
+ Timecop.freeze(moment+1) do
92
+ @filter.start_permitted?(3, 2, 1).should == true
93
+ do_several_activities [0, 30, 60], 2
94
+ actualize_balance!
95
+ @filter.permitted?(3, 2, 1).should == true
96
+ Login.find(1).balance.should == 5
97
+ end
98
+
99
+ Timecop.freeze(moment+10.hours) do
100
+ @filter.start_permitted?(3, 2, 1).should == true
101
+ @filter.permitted?(3, 2, 1).should == true
102
+ end
103
+
104
+ Timecop.freeze(moment+10.hours+1) do
105
+ @filter.start_permitted?(3, 2, 1).should == false
106
+ @filter.permitted?(3, 2, 1).should == false
107
+ end
108
+ end
109
+
110
+ it "should correctly calculate the balance long after the activity's done" do
111
+ Login.set_login_balance(1, 1500)
112
+ moment = Time.parse('2010-10-30 22:30:00')
113
+
114
+ ft = FinTransaction.template
115
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
116
+
117
+ activity = Activity.new
118
+ activity.computer_id = 1
119
+ activity.created_at = moment
120
+ activity.login_id = 1
121
+ activity.amount = 666
122
+ @filter.process_activity(activity, ft).should == true
123
+
124
+ actualize_balance!
125
+ Login.find(1).balance.should == (1500-1495)
126
+ end
127
+
128
+ it "should accept activities that are slightly longer than the filter period" do
129
+ Login.set_login_balance(1, 1500)
130
+ moment = Time.parse('2010-11-06 22:30:00')
131
+
132
+ ft = FinTransaction.template
133
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
134
+
135
+ activity = Activity.new
136
+ activity.computer_id = 1
137
+ activity.created_at = moment
138
+ activity.login_id = 1
139
+ activity.amount = 36000
140
+ @filter.process_activity(activity, ft).should == true
141
+
142
+ actualize_balance!
143
+ Login.find(1).balance.should == (1500-1495)
144
+ end
145
+ end
@@ -0,0 +1,145 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'lib', 'init'))
2
+
3
+ describe "Night packet tarif" do
4
+ before do
5
+ @filter = UniqSysOmega::CustomTariffs::CustomTariffBuilder.build :night_packet_weekday_tariff, 1
6
+ FinTransaction.reset_transactions
7
+ end
8
+
9
+ it "should deny login and usage at the wrong time" do
10
+ Login.set_login_balance(1, 1000)
11
+ moment = Time.parse('2010-09-20 21:59:59')
12
+
13
+ [0, 1, 2, 3, 4].each do |weekday|
14
+ [[0, false, false], [1, true, true], [3600*10, true, true], [1+3600*10, false, false]].each do |variant|
15
+ Timecop.freeze(moment+variant[0]+weekday*24*3600) do
16
+ @filter.start_permitted?(3, 1, 1).should == variant[1]
17
+ @filter.permitted?(3, 1, 1).should == variant[2]
18
+ end
19
+ end
20
+ end
21
+
22
+ [5, 6].each do |weekday|
23
+ [0, 1, 3600*10, 1+3600*10].each do |offset|
24
+ Timecop.freeze(moment+offset+weekday*24*3600) do
25
+ @filter.start_permitted?(3, 1, 1).should == false
26
+ @filter.permitted?(3, 1, 1).should == false
27
+ end
28
+ end
29
+ end
30
+
31
+ Timecop.freeze(moment+1) do
32
+ @filter.start_permitted?(3, 1, 1).should == true
33
+ @filter.permitted?(3, 1, 1).should == true
34
+ end
35
+ end
36
+
37
+ it "should deny login and usage with the lack of balance" do
38
+ Login.set_login_balance(1, 994)
39
+ moment = Time.parse('2010-09-20 21:59:59')
40
+ period_end = Time.parse('2010-09-28 23:59:59')
41
+ while moment < period_end
42
+ Timecop.freeze(moment) do
43
+ @filter.start_permitted?(3, 1, 1).should == false
44
+ @filter.permitted?(3, 1, 1).should == false
45
+ end
46
+ moment += 60
47
+ end
48
+ end
49
+
50
+ it "should deny login and usage with the balance that is not enough for the class" do
51
+ Login.set_login_balance(1, 1200)
52
+ moment = Time.parse('2010-09-20 21:59:59')
53
+
54
+ [0, 1, 2, 3, 4, 5, 6].each do |weekday|
55
+ [0, 1, 3600*10, 1+3600*10].each do |variant|
56
+ Timecop.freeze(moment+variant[0]+weekday*24*3600) do
57
+ @filter.start_permitted?(3, 2, 1).should == false
58
+ @filter.permitted?(3, 2, 1).should == false
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ def do_several_activities(offsets=[0], class_id = 1)
65
+ ft = FinTransaction.template
66
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
67
+
68
+ offsets.each do |offset|
69
+ activity = Activity.new
70
+ activity.computer_id = class_id
71
+ activity.created_at = Time.now + offset
72
+ activity.login_id = 1
73
+ activity.amount = 30
74
+ @filter.process_activity(activity, ft).should == true
75
+ end
76
+ end
77
+
78
+ def actualize_balance!
79
+ Login.set_login_balance(1, Login.find(1).balance - FinTransaction.transactions.sum)
80
+ FinTransaction.reset_transactions
81
+ end
82
+
83
+ it "should charge correctly" do
84
+ Login.set_login_balance(1, 1500)
85
+ moment = Time.parse('2010-10-18 21:59:59')
86
+ Timecop.freeze(moment) do
87
+ @filter.start_permitted?(3, 2, 1).should == false
88
+ @filter.permitted?(3, 2, 1).should == false
89
+ end
90
+
91
+ Timecop.freeze(moment+1) do
92
+ @filter.start_permitted?(3, 2, 1).should == true
93
+ do_several_activities [0, 30, 60], 2
94
+ actualize_balance!
95
+ @filter.permitted?(3, 2, 1).should == true
96
+ Login.find(1).balance.should == 5
97
+ end
98
+
99
+ Timecop.freeze(moment+10.hours) do
100
+ @filter.start_permitted?(3, 2, 1).should == true
101
+ @filter.permitted?(3, 2, 1).should == true
102
+ end
103
+
104
+ Timecop.freeze(moment+10.hours+1) do
105
+ @filter.start_permitted?(3, 2, 1).should == false
106
+ @filter.permitted?(3, 2, 1).should == false
107
+ end
108
+ end
109
+
110
+ it "should correctly calculate the balance long after the activity's done" do
111
+ Login.set_login_balance(1, 1500)
112
+ moment = Time.parse('2010-10-11 22:30:00')
113
+
114
+ ft = FinTransaction.template
115
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
116
+
117
+ activity = Activity.new
118
+ activity.computer_id = 1
119
+ activity.created_at = moment
120
+ activity.login_id = 1
121
+ activity.amount = 666
122
+ @filter.process_activity(activity, ft).should == true
123
+
124
+ actualize_balance!
125
+ Login.find(1).balance.should == (1500-995)
126
+ end
127
+
128
+ it "should accept activities that are slightly longer than the filter period" do
129
+ Login.set_login_balance(1, 1500)
130
+ moment = Time.parse('2010-11-01 22:30:00')
131
+
132
+ ft = FinTransaction.template
133
+ ft.club_id, ft.author_id, ft.src_account_id, ft.dst_account_id = 1, 2, 3, 4
134
+
135
+ activity = Activity.new
136
+ activity.computer_id = 1
137
+ activity.created_at = moment
138
+ activity.login_id = 1
139
+ activity.amount = 36000
140
+ @filter.process_activity(activity, ft).should == true
141
+
142
+ actualize_balance!
143
+ Login.find(1).balance.should == (1500-995)
144
+ end
145
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omega-tariffs-custom
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Uniq Systems
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-22 00:00:00 +04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: omega-tariffs-base
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 0
32
+ - 1
33
+ - 4
34
+ version: 0.1.4
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: omega-tariffs-base
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 0
48
+ - 1
49
+ - 4
50
+ version: 0.1.4
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: Just a test sky gem
54
+ email: ivan@uniqsystems.ru
55
+ executables: []
56
+
57
+ extensions: []
58
+
59
+ extra_rdoc_files:
60
+ - README.rdoc
61
+ - lib/common_tariff.rb
62
+ - lib/morning_packet_tariff.rb
63
+ - lib/night_packet_holiday_tariff.rb
64
+ - lib/night_packet_weekday_tariff.rb
65
+ - lib/omega-tariffs-custom.rb
66
+ files:
67
+ - Manifest
68
+ - README.rdoc
69
+ - Rakefile
70
+ - lib/common_tariff.rb
71
+ - lib/morning_packet_tariff.rb
72
+ - lib/night_packet_holiday_tariff.rb
73
+ - lib/night_packet_weekday_tariff.rb
74
+ - lib/omega-tariffs-custom.rb
75
+ - spec/common_tariff_spec.rb
76
+ - spec/lib/active_record_classes_stub.rb
77
+ - spec/lib/init.rb
78
+ - spec/lib/spec_helper.rb
79
+ - spec/morning_packet_tariff_spec.rb
80
+ - spec/night_packet_holiday_tariff_spec.rb
81
+ - spec/night_packet_weekday_tariff_spec.rb
82
+ - omega-tariffs-custom.gemspec
83
+ has_rdoc: true
84
+ homepage: http://uniqsystems.ru
85
+ licenses: []
86
+
87
+ post_install_message:
88
+ rdoc_options:
89
+ - --line-numbers
90
+ - --inline-source
91
+ - --title
92
+ - Omega-tariffs-custom
93
+ - --main
94
+ - README.rdoc
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ hash: 3
103
+ segments:
104
+ - 0
105
+ version: "0"
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ hash: 11
112
+ segments:
113
+ - 1
114
+ - 2
115
+ version: "1.2"
116
+ requirements: []
117
+
118
+ rubyforge_project: omega-tariffs-custom
119
+ rubygems_version: 1.3.7
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Just a test sky gem
123
+ test_files: []
124
+