Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add correlation_id to models and make public_id auto incrementing.

  • Loading branch information...
commit 24f0c5751a15c15275ccbc809fa600e8dbb31313 1 parent dccead9
Rob Young robyoung authored
21 app/models/activity.rb
@@ -2,11 +2,15 @@ class Activity
2 2 include Mongoid::Document
3 3 field :public_id, :type => Integer
4 4 index :public_id, :unique => true
  5 + field :correlation_id, :type => Integer
  6 + index :correlation_id, :unique => true
5 7 field :name, :type => String
6 8 has_and_belongs_to_many :sectors
7 9
8 10 validates :name, :presence => true
9 11
  12 + before_save :set_public_id
  13 +
10 14 def self.find_by_public_id(public_id)
11 15 where(public_id: public_id).first
12 16 end
@@ -15,6 +19,10 @@ def self.find_by_public_ids(public_ids)
15 19 self.any_in public_id: public_ids
16 20 end
17 21
  22 + def self.find_by_correlation_id(correlation_id)
  23 + where(correlation_id: correlation_id).first
  24 + end
  25 +
18 26 def self.find_by_sectors(sectors)
19 27 activity_ids = sectors.map(&:activity_ids).flatten
20 28 self.any_in _id: activity_ids
@@ -23,4 +31,17 @@ def self.find_by_sectors(sectors)
23 31 def to_s
24 32 self.name
25 33 end
  34 +
  35 + def set_public_id
  36 + # TODO: factor out
  37 + if self.public_id.nil?
  38 + counter = Mongoid.database["counters"].find_and_modify(
  39 + :query => {:_id => "activity"},
  40 + :update => {:$inc => {:count => 1}},
  41 + :new => true,
  42 + :upsert => true
  43 + )["count"]
  44 + self.public_id = counter
  45 + end
  46 + end
26 47 end
21 app/models/licence.rb
@@ -2,6 +2,8 @@ class Licence
2 2 include Mongoid::Document
3 3 field :public_id, :type => Integer
4 4 index :public_id, :unique => true
  5 + field :correlation_id, :type => Integer
  6 + index :correlation_id, :unique => true
5 7 field :name, :type => String
6 8 field :regulation_area, :type => String
7 9 field :da_england, :type => Boolean
@@ -12,13 +14,32 @@ class Licence
12 14 validates :name, :presence => true
13 15 validates :regulation_area, :presence => true
14 16
  17 + before_save :set_public_id
  18 +
15 19 def self.find_by_public_id(public_id)
16 20 where(public_id: public_id).first
17 21 end
18 22
  23 + def self.find_by_correlation_id(correlation_id)
  24 + where(correlation_id: correlation_id).first
  25 + end
  26 +
19 27 def self.find_by_sectors_activities_and_location(sectors, activities, location)
20 28 # consider moving location into licence_link to avoid two queries
21 29 licence_links = LicenceLink.find_by_sectors_and_activities(sectors, activities)
22 30 where(:_id.in => licence_links.map(&:licence_id), "da_#{location}".to_sym => true)
23 31 end
  32 +
  33 + def set_public_id
  34 + # TODO: factor out
  35 + if self.public_id.nil?
  36 + counter = Mongoid.database["counters"].find_and_modify(
  37 + :query => {:_id => "licence"},
  38 + :update => {:$inc => {:count => 1}},
  39 + :new => true,
  40 + :upsert => true
  41 + )["count"]
  42 + self.public_id = counter
  43 + end
  44 + end
24 45 end
21 app/models/sector.rb
@@ -2,6 +2,8 @@ class Sector
2 2 include Mongoid::Document
3 3 field :public_id, :type => Integer
4 4 index :public_id, :unique => true
  5 + field :correlation_id, :type => Integer
  6 + index :correlation_id, :unique => true
5 7 field :name, :type => String
6 8 field :layer1_id, :type => Integer # Only for the purpose of importing
7 9 field :layer2_id, :type => Integer # Only for the purpose of importing
@@ -9,6 +11,8 @@ class Sector
9 11
10 12 validates :name, :presence => true
11 13
  14 + before_save :set_public_id
  15 +
12 16 def self.find_by_public_id(public_id)
13 17 where(public_id: public_id).first
14 18 end
@@ -17,6 +21,23 @@ def self.find_by_public_ids(public_ids)
17 21 self.any_in public_id: public_ids
18 22 end
19 23
  24 + def self.find_by_correlation_id(correlation_id)
  25 + where(correlation_id: correlation_id).first
  26 + end
  27 +
  28 + def set_public_id
  29 + # TODO: factor out
  30 + if self.public_id.nil?
  31 + counter = Mongoid.database["counters"].find_and_modify(
  32 + :query => {:_id => "sector"},
  33 + :update => {:$inc => {:count => 1}},
  34 + :new => true,
  35 + :upsert => true
  36 + )['count']
  37 + self.public_id = counter
  38 + end
  39 + end
  40 +
20 41 def to_s
21 42 self.name
22 43 end
6 lib/data_importer/activities.rb
@@ -8,13 +8,13 @@ def self.open_data_file
8 8 private
9 9
10 10 def process_row(row)
11   - unless sector = Sector.find_by_public_id(row['SECTOR_OID'].to_i)
  11 + unless sector = Sector.find_by_correlation_id(row['SECTOR_OID'].to_i)
12 12 Rails.logger.info "Can't find Sector #{row['SECTOR_OID']} (#{row['SECTOR']})"
13 13 return
14 14 end
15   - unless activity = Activity.find_by_public_id(row['BUSINSS_ACT_ID'].to_i)
  15 + unless activity = Activity.find_by_correlation_id(row['BUSINSS_ACT_ID'].to_i)
16 16 activity = Activity.new
17   - activity.public_id = row['BUSINSS_ACT_ID'].to_i
  17 + activity.correlation_id = row['BUSINSS_ACT_ID'].to_i
18 18 activity.name = row['ACTIVITY_TITLE']
19 19 Rails.logger.debug "Creating BusinessActivity #{activity.id}(#{activity.name})"
20 20 activity.safely.save!
8 lib/data_importer/licences.rb
@@ -12,12 +12,12 @@ def process_row(row)
12 12 if sectors.length == 0
13 13 raise "Could not find sector #{row['SECTOR_OID']}, failing."
14 14 end
15   - unless activity = Activity.find_by_public_id(row['BUSINESSACT_ID'].to_i)
  15 + unless activity = Activity.find_by_correlation_id(row['BUSINESSACT_ID'].to_i)
16 16 raise "Could not find activity #{row['BUSINESSACT_ID']}, failing."
17 17 end
18   - unless licence = Licence.find_by_public_id(row['LICENCE_OID'].to_i)
  18 + unless licence = Licence.find_by_correlation_id(row['LICENCE_OID'].to_i)
19 19 licence = Licence.new
20   - licence.public_id = row['LICENCE_OID'].to_i
  20 + licence.correlation_id = row['LICENCE_OID'].to_i
21 21 Rails.logger.debug "Creating licence #{licence.id}(#{licence.name})"
22 22 end
23 23 licence.name = row['LICENCE']
@@ -46,7 +46,7 @@ def find_licence_link(sector, activity, licence)
46 46 end
47 47
48 48 def find_sectors(sector_id)
49   - if sector = Sector.find_by_public_id(sector_id)
  49 + if sector = Sector.find_by_correlation_id(sector_id)
50 50 [sector]
51 51 else
52 52 Sector.any_of({layer1_id: sector_id}, {layer2_id: sector_id})
4 lib/data_importer/sectors.rb
@@ -8,11 +8,11 @@ def self.open_data_file
8 8 private
9 9
10 10 def process_row(row)
11   - if Sector.find_by_public_id(row['LAYER3_OID'].to_i)
  11 + if Sector.find_by_correlation_id(row['LAYER3_OID'].to_i)
12 12 Rails.logger.info "Skipping Layer3 sector #{row['LAYER3_OID']}(#{row['LAYER3']})"
13 13 else
14 14 layer3 = Sector.new
15   - layer3.public_id = row['LAYER3_OID'].to_i
  15 + layer3.correlation_id = row['LAYER3_OID'].to_i
16 16 layer3.name = row['LAYER3']
17 17 layer3.layer1_id = row['LAYER1_OID'].to_i
18 18 layer3.layer2_id = row['LAYER2_OID'].to_i
1  spec/factories/activities.rb
@@ -3,6 +3,7 @@
3 3 FactoryGirl.define do
4 4 factory :activity do
5 5 sequence(:public_id)
  6 + sequence(:correlation_id)
6 7 name "Test Activity"
7 8 end
8 9 end
1  spec/factories/licences.rb
@@ -3,6 +3,7 @@
3 3 FactoryGirl.define do
4 4 factory :licence do
5 5 sequence(:public_id)
  6 + sequence(:correlation_id)
6 7 name "Test Licence"
7 8 regulation_area "Test Regulation Area"
8 9 da_england true
1  spec/factories/sectors.rb
@@ -3,6 +3,7 @@
3 3 FactoryGirl.define do
4 4 factory :sector do
5 5 sequence(:public_id)
  6 + sequence(:correlation_id)
6 7 name "Test Sector"
7 8 end
8 9 end
14 spec/lib/data_importer/activities_spec.rb
@@ -10,17 +10,17 @@
10 10 "1000431","Nutritionist services","1002","Use CCTV systems"
11 11 END
12 12
13   - sector = FactoryGirl.create(:sector, public_id: 1000431)
  13 + sector = FactoryGirl.create(:sector, correlation_id: 1000431)
14 14
15 15 importer = DataImporter::Activities.new(source)
16 16 importer.run
17 17
18   - imported_activity1 = Activity.find_by_public_id(362)
19   - imported_activity1.public_id.should == 362
  18 + imported_activity1 = Activity.find_by_correlation_id(362)
  19 + imported_activity1.correlation_id.should == 362
20 20 imported_activity1.name.should == "Practise as a dietitian "
21 21
22   - imported_activity2 = Activity.find_by_public_id(1002)
23   - imported_activity2.public_id.should == 1002
  22 + imported_activity2 = Activity.find_by_correlation_id(1002)
  23 + imported_activity2.correlation_id.should == 1002
24 24 imported_activity2.name.should == "Use CCTV systems"
25 25
26 26 sector.reload
@@ -34,12 +34,12 @@
34 34 "1000431","Nutritionist services","362","Practise as a dietitian "
35 35 END
36 36
37   - sector = FactoryGirl.create(:sector, public_id: 1000431)
  37 + sector = FactoryGirl.create(:sector, correlation_id: 1000431)
38 38
39 39 importer = DataImporter::Activities.new(source)
40 40 importer.run
41 41
42   - Activity.where(public_id: 362).length.should == 1
  42 + Activity.where(correlation_id: 362).length.should == 1
43 43 end
44 44 end
45 45
36 spec/lib/data_importer/licences_spec.rb
@@ -3,8 +3,8 @@
3 3
4 4 describe DataImporter::Licences do
5 5 before :each do
6   - @sector = FactoryGirl.create(:sector, public_id: 1)
7   - @activity = FactoryGirl.create(:activity, public_id: 1)
  6 + @sector = FactoryGirl.create(:sector, correlation_id: 1)
  7 + @activity = FactoryGirl.create(:activity, correlation_id: 1)
8 8 end
9 9
10 10 describe "clean import" do
@@ -17,8 +17,8 @@
17 17 importer = DataImporter::Licences.new(source)
18 18 importer.run
19 19
20   - imported_licence = Licence.find_by_public_id(1)
21   - imported_licence.public_id.should == 1
  20 + imported_licence = Licence.find_by_correlation_id(1)
  21 + imported_licence.correlation_id.should == 1
22 22 imported_licence.name.should == "Licences to play music in a theatre (All UK)"
23 23 imported_licence.regulation_area.should == "Copyright"
24 24 imported_licence.da_england == true
@@ -32,8 +32,8 @@
32 32 imported_licence_link.licence.should == imported_licence
33 33 LicenceLink.all.length.should == 1
34 34 end
35   - it "should update the licence if one with the same public_id already exists" do
36   - FactoryGirl.create(:licence, public_id: 1, name: "Test Name", da_england: false)
  35 + it "should update the licence if one with the same correlation_id already exists" do
  36 + FactoryGirl.create(:licence, correlation_id: 1, name: "Test Name", da_england: false)
37 37
38 38 source = StringIO.new(<<-END)
39 39 "SECTOR_OID","SECTOR","BUSINESSACT_ID","ACTIVITY_TITLE","LICENCE_OID","LICENCE","REGULATION_AREA","DA_ENGLAND","DA_SCOTLAND","DA_WALES","DA_NIRELAND","ALL_OF_UK"
@@ -43,8 +43,8 @@
43 43 importer = DataImporter::Licences.new(source)
44 44 importer.run
45 45
46   - imported_licence = Licence.find_by_public_id(1)
47   - imported_licence.public_id.should == 1
  46 + imported_licence = Licence.find_by_correlation_id(1)
  47 + imported_licence.correlation_id.should == 1
48 48 imported_licence.name.should == "Licences to play music in a theatre (All UK)"
49 49 imported_licence.da_england.should be_true
50 50 end
@@ -58,7 +58,7 @@
58 58 lambda do
59 59 importer.run
60 60 end.should raise_error
61   - imported_licence = Licence.find_by_public_id(1)
  61 + imported_licence = Licence.find_by_correlation_id(1)
62 62 imported_licence.should == nil
63 63 end
64 64 it "should fail early if the activity does not exist" do
@@ -71,13 +71,13 @@
71 71 lambda do
72 72 importer.run
73 73 end.should raise_error
74   - imported_licence = Licence.find_by_public_id(1)
  74 + imported_licence = Licence.find_by_correlation_id(1)
75 75 imported_licence.should == nil
76 76 end
77 77 it "should add links for all layer3 sectors if a layer2 sector id is provided" do
78   - sector1 = FactoryGirl.create(:sector, public_id: 2, layer2_id: 101)
79   - sector2 = FactoryGirl.create(:sector, public_id: 3, layer2_id: 101)
80   - sector3 = FactoryGirl.create(:sector, public_id: 4, layer2_id: 102)
  78 + sector1 = FactoryGirl.create(:sector, correlation_id: 2, layer2_id: 101)
  79 + sector2 = FactoryGirl.create(:sector, correlation_id: 3, layer2_id: 101)
  80 + sector3 = FactoryGirl.create(:sector, correlation_id: 4, layer2_id: 102)
81 81
82 82 source = StringIO.new(<<-END)
83 83 "SECTOR_OID","SECTOR","BUSINESSACT_ID","ACTIVITY_TITLE","LICENCE_OID","LICENCE","REGULATION_AREA","DA_ENGLAND","DA_SCOTLAND","DA_WALES","DA_NIRELAND","ALL_OF_UK"
@@ -95,9 +95,9 @@
95 95 sectors.should_not include(sector3)
96 96 end
97 97 it "should add links for all layer3 sectors if a layer1 sector id is provided" do
98   - sector1 = FactoryGirl.create(:sector, public_id: 2, layer1_id: 101)
99   - sector2 = FactoryGirl.create(:sector, public_id: 3, layer1_id: 101)
100   - sector3 = FactoryGirl.create(:sector, public_id: 4, layer1_id: 102)
  98 + sector1 = FactoryGirl.create(:sector, correlation_id: 2, layer1_id: 101)
  99 + sector2 = FactoryGirl.create(:sector, correlation_id: 3, layer1_id: 101)
  100 + sector3 = FactoryGirl.create(:sector, correlation_id: 4, layer1_id: 102)
101 101
102 102 source = StringIO.new(<<-END)
103 103 "SECTOR_OID","SECTOR","BUSINESSACT_ID","ACTIVITY_TITLE","LICENCE_OID","LICENCE","REGULATION_AREA","DA_ENGLAND","DA_SCOTLAND","DA_WALES","DA_NIRELAND","ALL_OF_UK"
@@ -116,7 +116,7 @@
116 116 end
117 117 it "should not create a new licence_link if it already exists" do
118 118 require 'ruby-debug'
119   - @licence = FactoryGirl.create(:licence, public_id: 1, name: "Test Name", da_england: false)
  119 + @licence = FactoryGirl.create(:licence, correlation_id: 1, name: "Test Name", da_england: false)
120 120 licence_link = FactoryGirl.create(:licence_link, sector: @sector, activity: @activity, licence: @licence)
121 121
122 122 source = StringIO.new(<<-END)
@@ -141,7 +141,7 @@
141 141 importer = DataImporter::Licences.new(source)
142 142 importer.run
143 143
144   - imported_licence = Licence.find_by_public_id(1)
  144 + imported_licence = Licence.find_by_correlation_id(1)
145 145 imported_licence.da_england.should == true
146 146 imported_licence.da_scotland.should == true
147 147 imported_licence.da_wales.should == true
10 spec/lib/data_importer/sectors_spec.rb
@@ -9,13 +9,13 @@
9 9 "1000001","A0","Agriculture, forestry and fishing","1000002","A0.010","Agriculture","1000011","A0.010.090","Animal farming support services"
10 10 END
11 11
12   - Sector.find_by_public_id(1000011).should == nil
  12 + Sector.find_by_correlation_id(1000011).should == nil
13 13
14 14 importer = DataImporter::Sectors.new(source)
15 15 importer.run
16 16
17   - imported_sector = Sector.find_by_public_id(1000011)
18   - imported_sector.public_id.should == 1000011
  17 + imported_sector = Sector.find_by_correlation_id(1000011)
  18 + imported_sector.correlation_id.should == 1000011
19 19 imported_sector.name.should == "Animal farming support services"
20 20 imported_sector.layer1_id.should == 1000001
21 21 imported_sector.layer2_id.should == 1000002
@@ -28,12 +28,12 @@
28 28 "1000001","A0","Agriculture, forestry and fishing","1000002","A0.010","Agriculture","1000011","A0.010.090","Animal farming support services"
29 29 END
30 30
31   - Sector.find_by_public_id(1000011).should == nil
  31 + Sector.find_by_correlation_id(1000011).should == nil
32 32
33 33 importer = DataImporter::Sectors.new(source)
34 34 importer.run
35 35
36   - Sector.where(public_id: 1000011).length.should == 1
  36 + Sector.where(correlation_id: 1000011).length.should == 1
37 37 end
38 38 end
39 39
49 spec/models/activity_spec.rb
@@ -4,10 +4,12 @@
4 4 it "should use the correct field types on the model" do
5 5 Activity.safely.create!(
6 6 :public_id => 42,
  7 + :correlation_id => 24,
7 8 :name => "Some Activity"
8 9 )
9 10 activity = Activity.first
10 11 activity.public_id.should == 42
  12 + activity.correlation_id.should == 24
11 13 activity.name.should == "Some Activity"
12 14 end
13 15
@@ -24,6 +26,14 @@
24 26 end.should raise_error(Mongo::OperationFailure)
25 27 end
26 28
  29 + it "should have a database level uniqueness constraint on correlation_id" do
  30 + FactoryGirl.create(:activity, :correlation_id => 42)
  31 + @activity.correlation_id = 42
  32 + lambda do
  33 + @activity.safely.save
  34 + end.should raise_error(Mongo::OperationFailure)
  35 + end
  36 +
27 37 it "should require a name" do
28 38 @activity.name = ''
29 39 @activity.should_not be_valid
@@ -80,6 +90,22 @@
80 90 end
81 91 end
82 92
  93 + describe "find_by_correlation_id" do
  94 + before :each do
  95 + @activity = FactoryGirl.create(:activity)
  96 + end
  97 +
  98 + it "should be able to retrieve by correlation_id" do
  99 + found_activity = Activity.find_by_correlation_id(@activity.correlation_id)
  100 + found_activity.should == @activity
  101 + end
  102 +
  103 + it "should fail to retrieve a non-existent correlation_id" do
  104 + found_activity = Activity.find_by_correlation_id(@activity.correlation_id + 1)
  105 + found_activity.should == nil
  106 + end
  107 + end
  108 +
83 109 describe "find_by_sectors" do
84 110 before :each do
85 111 @s1 = FactoryGirl.create(:sector, :name => "Fooey Sector")
@@ -111,4 +137,27 @@
111 137 a = FactoryGirl.build(:activity, :name => "Foo Activity")
112 138 a.to_s.should == "Foo Activity"
113 139 end
  140 +
  141 + describe "auto incrementing public_id" do
  142 + def create_and_test_activity(correlation_id, public_id, &block)
  143 + activity = Activity.new
  144 + activity.correlation_id = correlation_id
  145 + activity.name = "Test Activity"
  146 + activity.public_id.should == nil
  147 + block.call(activity)
  148 + activity.public_id.should == public_id
  149 + end
  150 +
  151 + it "should set the public_id to the next free public_id when set_public_id is called" do
  152 + create_and_test_activity(12, 1) {|activity| activity.set_public_id }
  153 + create_and_test_activity(13, 2) {|activity| activity.set_public_id }
  154 + create_and_test_activity(14, 3) {|activity| activity.set_public_id }
  155 + end
  156 +
  157 + it "should set the public_id to the next free public_id on save" do
  158 + create_and_test_activity(12, 1) {|activity| activity.save }
  159 + create_and_test_activity(13, 2) {|activity| activity.save }
  160 + create_and_test_activity(14, 3) {|activity| activity.save }
  161 + end
  162 + end
114 163 end
52 spec/models/licence_spec.rb
@@ -4,11 +4,13 @@
4 4 it "should use the correct field types on the model" do
5 5 Licence.safely.create!(
6 6 :public_id => 42,
  7 + :correlation_id => 24,
7 8 :name => "Some Licence",
8 9 :regulation_area => "Some Regulation Area"
9 10 )
10 11 licence = Licence.first
11 12 licence.public_id.should == 42
  13 + licence.correlation_id.should == 24
12 14 licence.name.should == "Some Licence"
13 15 end
14 16
@@ -25,6 +27,14 @@
25 27 end.should raise_error(Mongo::OperationFailure)
26 28 end
27 29
  30 + it "should have a database level uniqueness constraint on correlation_id" do
  31 + FactoryGirl.create(:licence, :correlation_id => 42)
  32 + @licence.correlation_id = 42
  33 + lambda do
  34 + @licence.safely.save
  35 + end.should raise_error(Mongo::OperationFailure)
  36 + end
  37 +
28 38 it "should require a name" do
29 39 @licence.name = ''
30 40 @licence.should_not be_valid
@@ -52,6 +62,22 @@
52 62 end
53 63 end
54 64
  65 + describe "find_by_correlation_id" do
  66 + before :each do
  67 + @licence = FactoryGirl.create(:licence)
  68 + end
  69 +
  70 + it "should be able to retrieve by correlation_id" do
  71 + found_licence = Licence.find_by_correlation_id(@licence.correlation_id)
  72 + found_licence.should == @licence
  73 + end
  74 +
  75 + it "should fail to retrieve a non-existent correlation_id" do
  76 + found_licence = Licence.find_by_correlation_id(@licence.correlation_id + 1)
  77 + found_licence.should == nil
  78 + end
  79 + end
  80 +
55 81 describe "find_by_sectors_activities_and_location" do
56 82 before :each do
57 83 @s1 = FactoryGirl.create(:sector, name: "Sector One")
@@ -89,4 +115,30 @@
89 115 found_licences.to_a.should =~ [@l1, @l2]
90 116 end
91 117 end
  118 +
  119 + describe "auto incrementing public_id" do
  120 + def create_and_test_licence(correlation_id, public_id, &block)
  121 + # Surely this can be handled by FactoryGirl?
  122 + licence = Licence.new
  123 + licence.correlation_id = correlation_id
  124 + licence.name = "Test licence"
  125 + licence.regulation_area = "England"
  126 + licence.public_id.should == nil
  127 + block.call(licence)
  128 + licence.public_id.should == public_id
  129 +
  130 + end
  131 + it "should set the public_id to the next free public_id when set_public_id is called" do
  132 + create_and_test_licence(12, 1) {|licence| licence.set_public_id }
  133 + create_and_test_licence(13, 2) {|licence| licence.set_public_id }
  134 + create_and_test_licence(14, 3) {|licence| licence.set_public_id }
  135 + end
  136 +
  137 + it "should set the public_id to the next free public_id on save" do
  138 + create_and_test_licence(12, 1) {|licence| licence.save }
  139 + create_and_test_licence(13, 2) {|licence| licence.save }
  140 + create_and_test_licence(14, 3) {|licence| licence.save}
  141 + end
  142 +
  143 + end
92 144 end
46 spec/models/sector_spec.rb
@@ -4,10 +4,12 @@
4 4 it "should use the correct field types on the model" do
5 5 Sector.safely.create!(
6 6 :public_id => 42,
  7 + :correlation_id => 24,
7 8 :name => "Some Sector"
8 9 )
9 10 sector = Sector.first
10 11 sector.public_id.should == 42
  12 + sector.correlation_id.should == 24
11 13 sector.name.should == "Some Sector"
12 14 end
13 15
@@ -24,6 +26,14 @@
24 26 end.should raise_error(Mongo::OperationFailure)
25 27 end
26 28
  29 + it "should have a database level uniqueness constraint on correlation_id" do
  30 + FactoryGirl.create(:sector, :correlation_id => 42)
  31 + @sector.correlation_id = 42
  32 + lambda do
  33 + @sector.safely.save
  34 + end.should raise_error(Mongo::OperationFailure)
  35 + end
  36 +
27 37 it "should require a name" do
28 38 @sector.name = ''
29 39 @sector.should_not be_valid
@@ -79,10 +89,46 @@
79 89 sectors.to_a.should =~ [@s1, @s2]
80 90 end
81 91 end
  92 +
  93 + describe "#find_by_correlation_id" do
  94 + before :each do
  95 + @sector = FactoryGirl.create(:sector)
  96 + end
  97 +
  98 + it "should be able to retrieve by correlation_id" do
  99 + found_sector = Sector.find_by_correlation_id(@sector.correlation_id)
  100 + found_sector.should == @sector
  101 + end
  102 + end
82 103 end
83 104
84 105 specify "to_s returns the name" do
85 106 s = FactoryGirl.build(:sector, :name => "Foo Sector")
86 107 s.to_s.should == "Foo Sector"
87 108 end
  109 +
  110 + describe "auto incrementing public_id" do
  111 +
  112 + def create_and_test_sector(correlation_id, public_id, &block)
  113 + # Surely this can be handled by FactoryGirl?
  114 + sector = Sector.new
  115 + sector.correlation_id = correlation_id
  116 + sector.name = "Test Sector"
  117 + sector.public_id.should == nil
  118 + block.call(sector)
  119 + sector.public_id.should == public_id
  120 +
  121 + end
  122 + it "should set the public_id to the next free public_id when set_public_id is called" do
  123 + create_and_test_sector(12, 1) {|sector| sector.set_public_id }
  124 + create_and_test_sector(13, 2) {|sector| sector.set_public_id }
  125 + create_and_test_sector(14, 3) {|sector| sector.set_public_id }
  126 + end
  127 +
  128 + it "should set the public_id to the next free public_id on save" do
  129 + create_and_test_sector(12, 1) {|sector| sector.save }
  130 + create_and_test_sector(13, 2) {|sector| sector.save }
  131 + create_and_test_sector(14, 3) {|sector| sector.save}
  132 + end
  133 + end
88 134 end

0 comments on commit 24f0c57

Please sign in to comment.
Something went wrong with that request. Please try again.