Commit e50674fe27ed01e7f77546fbb28bd61fe81984ad
Committed by
João M. M. da Silva
1 parent
8f142887
Exists in
master
and in
28 other branches
[Mezuro] completed reading fixtures, tests and implementation and fixed
reading_group_test
Showing
6 changed files
with
104 additions
and
6 deletions
Show diff stats
plugins/mezuro/lib/kalibro/model.rb
@@ -50,7 +50,7 @@ class Kalibro::Model | @@ -50,7 +50,7 @@ class Kalibro::Model | ||
50 | 50 | ||
51 | def save | 51 | def save |
52 | begin | 52 | begin |
53 | - self.class.request(save_endpoint, save_action, save_params) | 53 | + self.id = self.class.request(save_endpoint, save_action, save_params) |
54 | true | 54 | true |
55 | rescue Exception => exception | 55 | rescue Exception => exception |
56 | add_error exception | 56 | add_error exception |
@@ -0,0 +1,17 @@ | @@ -0,0 +1,17 @@ | ||
1 | +class Kalibro::Reading < Kalibro::Model | ||
2 | + | ||
3 | + attr_accessor :id, :label, :grade, :color | ||
4 | + | ||
5 | + def self.find(id) | ||
6 | + new request("Reading", :get_reading, {:reading_id => id})[:reading] | ||
7 | + end | ||
8 | + | ||
9 | + def self.readings_of( group_id ) | ||
10 | + request("Reading", :readings_of, {:group_id => group_id})[:reading].to_a.map { |reading| new reading } | ||
11 | + end | ||
12 | + | ||
13 | + def self.reading_of( range_id ) | ||
14 | + new request("Reading", :reading_of, {:range_id => range_id} )[:reading] | ||
15 | + end | ||
16 | + | ||
17 | +end |
@@ -0,0 +1,16 @@ | @@ -0,0 +1,16 @@ | ||
1 | +class ReadingFixtures | ||
2 | + | ||
3 | + def self.reading | ||
4 | + Kalibro::Reading.new reading_hash | ||
5 | + end | ||
6 | + | ||
7 | + def self.created_reading # A created object has no id before being sent to kalibro | ||
8 | + Kalibro::Reading.new :label => "Reading Test Label", :grade => 10.5, :color => "AABBCC" | ||
9 | + end | ||
10 | + | ||
11 | + def self.reading_hash | ||
12 | + {:id => 42, :label => "Reading Test Label", :grade => 10.5, :color => "AABBCC" } | ||
13 | + end | ||
14 | + | ||
15 | +end | ||
16 | + |
plugins/mezuro/test/fixtures/reading_group_fixtures.rb
@@ -4,6 +4,10 @@ class ReadingGroupFixtures | @@ -4,6 +4,10 @@ class ReadingGroupFixtures | ||
4 | Kalibro::ReadingGroup.new reading_group_hash | 4 | Kalibro::ReadingGroup.new reading_group_hash |
5 | end | 5 | end |
6 | 6 | ||
7 | + def self.created_reading_group # A created object has no id before being sent to kalibro | ||
8 | + Kalibro::ReadingGroup.new :name => "Reading Group Test", :description => "Reading group in the fixtures" | ||
9 | + end | ||
10 | + | ||
7 | def self.reading_group_hash | 11 | def self.reading_group_hash |
8 | {:id => 42, :name => "Reading Group Test", :description => "Reading group in the fixtures"} | 12 | {:id => 42, :name => "Reading Group Test", :description => "Reading group in the fixtures"} |
9 | end | 13 | end |
plugins/mezuro/test/unit/kalibro/reading_group_test.rb
@@ -6,6 +6,7 @@ class ReadingGroupTest < ActiveSupport::TestCase | @@ -6,6 +6,7 @@ class ReadingGroupTest < ActiveSupport::TestCase | ||
6 | def setup | 6 | def setup |
7 | @hash = ReadingGroupFixtures.reading_group_hash | 7 | @hash = ReadingGroupFixtures.reading_group_hash |
8 | @reading_group = ReadingGroupFixtures.reading_group | 8 | @reading_group = ReadingGroupFixtures.reading_group |
9 | + @created_reading_group = ReadingGroupFixtures.created_reading_group | ||
9 | end | 10 | end |
10 | 11 | ||
11 | should 'create reading group from hash' do | 12 | should 'create reading group from hash' do |
@@ -29,7 +30,7 @@ class ReadingGroupTest < ActiveSupport::TestCase | @@ -29,7 +30,7 @@ class ReadingGroupTest < ActiveSupport::TestCase | ||
29 | 30 | ||
30 | should 'get all reading groups' do | 31 | should 'get all reading groups' do |
31 | Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :all_reading_groups).returns({:reading_group => [@hash]}) | 32 | Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :all_reading_groups).returns({:reading_group => [@hash]}) |
32 | - assert_equal @hash[:name], Kalibro::ReadingGroup.all[0].name | 33 | + assert_equal @hash[:name], Kalibro::ReadingGroup.all.first.name |
33 | end | 34 | end |
34 | 35 | ||
35 | should 'get reading group of a metric configuration' do | 36 | should 'get reading group of a metric configuration' do |
@@ -39,13 +40,16 @@ class ReadingGroupTest < ActiveSupport::TestCase | @@ -39,13 +40,16 @@ class ReadingGroupTest < ActiveSupport::TestCase | ||
39 | end | 40 | end |
40 | 41 | ||
41 | should 'return true when reading group is saved successfully' do | 42 | should 'return true when reading group is saved successfully' do |
42 | - Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :save_reading_group, {:reading_group => @reading_group.to_hash}).returns(-1) | ||
43 | - assert @reading_group.save | 43 | + id_from_kalibro = 1 |
44 | + Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :save_reading_group, {:reading_group => @created_reading_group.to_hash}).returns(id_from_kalibro) | ||
45 | + assert @created_reading_group.save | ||
46 | + assert_equal id_from_kalibro, @created_reading_group.id | ||
44 | end | 47 | end |
45 | 48 | ||
46 | should 'return false when reading group is not saved successfully' do | 49 | should 'return false when reading group is not saved successfully' do |
47 | - Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :save_reading_group, {:reading_group => @reading_group.to_hash}).raises(Exception.new) | ||
48 | - assert !(@reading_group.save) | 50 | + Kalibro::ReadingGroup.expects(:request).with("ReadingGroup", :save_reading_group, {:reading_group => @created_reading_group.to_hash}).raises(Exception.new) |
51 | + assert !(@created_reading_group.save) | ||
52 | + assert_nil @created_reading_group.id | ||
49 | end | 53 | end |
50 | 54 | ||
51 | should 'destroy reading group by id' do | 55 | should 'destroy reading group by id' do |
@@ -0,0 +1,57 @@ | @@ -0,0 +1,57 @@ | ||
1 | +require "test_helper" | ||
2 | +require "#{RAILS_ROOT}/plugins/mezuro/test/fixtures/reading_fixtures" | ||
3 | + | ||
4 | +class ReadingTest < ActiveSupport::TestCase | ||
5 | + | ||
6 | + def setup | ||
7 | + @hash = ReadingFixtures.reading_hash | ||
8 | + @reading = ReadingFixtures.reading | ||
9 | + @created_reading = ReadingFixtures.created_reading | ||
10 | + end | ||
11 | + | ||
12 | + should 'create reading from hash' do | ||
13 | + assert_equal @hash[:label], Kalibro::Reading.new(@hash).label | ||
14 | + end | ||
15 | + | ||
16 | + should 'convert reading to hash' do | ||
17 | + assert_equal @hash, @reading.to_hash | ||
18 | + end | ||
19 | + | ||
20 | + should 'get reading' do | ||
21 | + Kalibro::Reading.expects(:request).with("Reading", :get_reading, {:reading_id => @hash[:id]}). | ||
22 | + returns({:reading => @hash}) | ||
23 | + assert_equal @hash[:label], Kalibro::Reading.find(@hash[:id]).label | ||
24 | + end | ||
25 | + | ||
26 | + should 'get reading of a range' do | ||
27 | + range_id = 31 | ||
28 | + Kalibro::Reading.expects(:request).with("Reading", :reading_of, {:range_id => range_id}).returns({:reading => @hash}) | ||
29 | + assert_equal @hash[:label], Kalibro::Reading.reading_of(range_id).label | ||
30 | + end | ||
31 | + | ||
32 | + should 'get readings of a reading group' do | ||
33 | + reading_group_id = 31 | ||
34 | + Kalibro::Reading.expects(:request).with("Reading", :readings_of, {:group_id => reading_group_id}).returns({:reading => [@hash]}) | ||
35 | + assert_equal @hash[:label], Kalibro::Reading.readings_of(reading_group_id).first.label | ||
36 | + end | ||
37 | + | ||
38 | + should 'return true when reading is saved successfully' do | ||
39 | + id_from_kalibro = 1 | ||
40 | + Kalibro::Reading.expects(:request).with("Reading", :save_reading, {:reading => @created_reading.to_hash}).returns(id_from_kalibro) | ||
41 | + assert @created_reading.save | ||
42 | + assert_equal id_from_kalibro, @created_reading.id | ||
43 | + end | ||
44 | + | ||
45 | + should 'return false when reading is not saved successfully' do | ||
46 | + Kalibro::Reading.expects(:request).with("Reading", :save_reading, {:reading => @created_reading.to_hash}).raises(Exception.new) | ||
47 | + assert !(@created_reading.save) | ||
48 | + assert_nil @created_reading.id | ||
49 | + end | ||
50 | + | ||
51 | + should 'destroy reading by id' do | ||
52 | + Kalibro::Reading.expects(:request).with("Reading", :delete_reading, {:reading_id => @reading.id}) | ||
53 | + @reading.destroy | ||
54 | + end | ||
55 | + | ||
56 | +end | ||
57 | + |