sam-dm-core 0.9.10 → 0.9.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. data/.gitignore +18 -0
  2. data/History.txt +10 -0
  3. data/MIT-LICENSE +1 -1
  4. data/Rakefile +4 -4
  5. data/dm-core.gemspec +40 -0
  6. data/lib/dm-core.rb +1 -1
  7. data/lib/dm-core/adapters/data_objects_adapter.rb +2 -2
  8. data/lib/dm-core/adapters/in_memory_adapter.rb +87 -0
  9. data/lib/dm-core/adapters/mysql_adapter.rb +1 -1
  10. data/lib/dm-core/adapters/postgres_adapter.rb +1 -1
  11. data/lib/dm-core/adapters/sqlite3_adapter.rb +1 -1
  12. data/lib/dm-core/associations/many_to_one.rb +1 -1
  13. data/lib/dm-core/associations/one_to_many.rb +18 -18
  14. data/lib/dm-core/associations/relationship.rb +9 -3
  15. data/lib/dm-core/associations/relationship_chain.rb +1 -1
  16. data/lib/dm-core/collection.rb +3 -3
  17. data/lib/dm-core/model.rb +5 -4
  18. data/lib/dm-core/resource.rb +11 -5
  19. data/lib/dm-core/version.rb +1 -1
  20. data/script/all +3 -4
  21. data/spec/integration/association_spec.rb +18 -18
  22. data/spec/integration/association_through_spec.rb +4 -4
  23. data/spec/integration/associations/many_to_many_spec.rb +9 -9
  24. data/spec/integration/associations/many_to_one_spec.rb +1 -1
  25. data/spec/integration/associations/one_to_many_spec.rb +3 -3
  26. data/spec/integration/collection_spec.rb +2 -2
  27. data/spec/integration/dependency_queue_spec.rb +1 -1
  28. data/spec/integration/model_spec.rb +1 -1
  29. data/spec/integration/mysql_adapter_spec.rb +1 -1
  30. data/spec/integration/postgres_adapter_spec.rb +16 -16
  31. data/spec/integration/property_spec.rb +10 -6
  32. data/spec/integration/query_spec.rb +4 -4
  33. data/spec/integration/repository_spec.rb +1 -1
  34. data/spec/integration/sqlite3_adapter_spec.rb +7 -7
  35. data/spec/integration/sti_spec.rb +6 -6
  36. data/spec/integration/strategic_eager_loading_spec.rb +14 -11
  37. data/spec/integration/type_spec.rb +10 -6
  38. data/spec/lib/logging_helper.rb +11 -11
  39. data/spec/models/content.rb +16 -0
  40. data/spec/spec_helper.rb +8 -4
  41. data/spec/unit/adapters/data_objects_adapter_spec.rb +9 -5
  42. data/spec/unit/adapters/in_memory_adapter_spec.rb +98 -0
  43. data/spec/unit/adapters/postgres_adapter_spec.rb +5 -5
  44. data/spec/unit/associations/many_to_many_spec.rb +2 -2
  45. data/spec/unit/associations/many_to_one_spec.rb +3 -3
  46. data/spec/unit/associations/one_to_many_spec.rb +2 -2
  47. data/spec/unit/associations/relationship_spec.rb +6 -6
  48. data/spec/unit/associations_spec.rb +21 -21
  49. data/spec/unit/identity_map_spec.rb +3 -3
  50. data/spec/unit/is_spec.rb +7 -7
  51. data/spec/unit/property_spec.rb +7 -7
  52. data/spec/unit/resource_spec.rb +12 -12
  53. data/spec/unit/transaction_spec.rb +1 -1
  54. data/tasks/dm.rb +1 -1
  55. metadata +10 -5
@@ -1,11 +1,15 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
- gem 'fastercsv', '~>1.4.0'
4
- require 'fastercsv'
3
+ if RUBY_VERSION >= '1.9.0'
4
+ require 'csv'
5
+ else
6
+ gem 'fastercsv', '~>1.4.0'
7
+ require 'fastercsv'
8
+ end
5
9
 
6
10
  describe DataMapper::Property do
7
11
  before do
8
- module PropertySpec
12
+ module ::PropertySpec
9
13
  class Resource
10
14
  include DataMapper::Resource
11
15
  end
@@ -23,7 +27,7 @@ if ADAPTER
23
27
  describe DataMapper::Property, "with #{ADAPTER}" do
24
28
  describe " tracking strategies" do
25
29
  before :all do
26
- class Actor
30
+ class ::Actor
27
31
  include DataMapper::Resource
28
32
 
29
33
  property :id, Serial
@@ -145,7 +149,7 @@ if ADAPTER
145
149
 
146
150
  describe "lazy loading" do
147
151
  before :all do
148
- class RowBoat
152
+ class ::RowBoat
149
153
  include DataMapper::Resource
150
154
  property :id, Serial
151
155
  property :notes, String, :lazy => [:notes]
@@ -198,7 +202,7 @@ if ADAPTER
198
202
 
199
203
  describe 'defaults' do
200
204
  before :all do
201
- class Catamaran
205
+ class ::Catamaran
202
206
  include DataMapper::Resource
203
207
  property :id, Serial
204
208
  property :name, String
@@ -424,8 +424,8 @@ if ADAPTER
424
424
  factory = DataMapper::Associations::Relationship.new(
425
425
  :factory,
426
426
  ADAPTER,
427
- 'QuerySpec::Vehicle',
428
- 'QuerySpec::Factory',
427
+ QuerySpec::Vehicle,
428
+ QuerySpec::Factory,
429
429
  { :child_key => [ :factory_id ], :parent_key => [ :id ] }
430
430
  )
431
431
  results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ factory ]) }
@@ -446,8 +446,8 @@ if ADAPTER
446
446
  region = DataMapper::Associations::Relationship.new(
447
447
  :region,
448
448
  ADAPTER,
449
- 'QuerySpec::Factory',
450
- 'QuerySpec::Region',
449
+ QuerySpec::Factory,
450
+ QuerySpec::Region,
451
451
  { :child_key => [ :region_id ], :parent_key => [ :id ] }
452
452
  )
453
453
  results = repository(ADAPTER) { QuerySpec::Vehicle.all(:links => [ 'factory', region ]) }
@@ -3,7 +3,7 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
3
3
  if ADAPTER
4
4
  describe DataMapper::Repository, "with #{ADAPTER}" do
5
5
  before :all do
6
- class SerialFinderSpec
6
+ class ::SerialFinderSpec
7
7
  include DataMapper::Resource
8
8
 
9
9
  property :id, Serial
@@ -8,7 +8,7 @@ if HAS_SQLITE3
8
8
 
9
9
  describe "auto migrating" do
10
10
  before :all do
11
- class Sputnik
11
+ class ::Sputnik
12
12
  include DataMapper::Resource
13
13
 
14
14
  property :id, Serial
@@ -30,7 +30,7 @@ if HAS_SQLITE3
30
30
 
31
31
  describe "querying metadata" do
32
32
  before :all do
33
- class Sputnik
33
+ class ::Sputnik
34
34
  include DataMapper::Resource
35
35
 
36
36
  property :id, Serial
@@ -76,7 +76,7 @@ if HAS_SQLITE3
76
76
 
77
77
  describe "handling transactions" do
78
78
  before :all do
79
- class Sputnik
79
+ class ::Sputnik
80
80
  include DataMapper::Resource
81
81
 
82
82
  property :id, Serial
@@ -108,7 +108,7 @@ if HAS_SQLITE3
108
108
 
109
109
  describe "reading & writing a database" do
110
110
  before :all do
111
- class User
111
+ class ::User
112
112
  include DataMapper::Resource
113
113
 
114
114
  property :id, Serial
@@ -134,7 +134,7 @@ if HAS_SQLITE3
134
134
  result.should be_kind_of(Array)
135
135
  row = result.first
136
136
  row.should be_kind_of(Struct)
137
- row.members.should == %w{id name}
137
+ row.members.map { |m| m.to_s }.should == %w{id name}
138
138
 
139
139
  row.id.should == 1
140
140
  row.name.should == 'Paul'
@@ -153,7 +153,7 @@ if HAS_SQLITE3
153
153
 
154
154
  describe "CRUD for serial Key" do
155
155
  before :all do
156
- class VideoGame
156
+ class ::VideoGame
157
157
  include DataMapper::Resource
158
158
 
159
159
  property :id, Serial
@@ -255,7 +255,7 @@ if HAS_SQLITE3
255
255
 
256
256
  describe "CRUD for Composite Key" do
257
257
  before :all do
258
- class BankCustomer
258
+ class ::BankCustomer
259
259
  include DataMapper::Resource
260
260
 
261
261
  property :bank, String, :key => true
@@ -8,7 +8,7 @@ if HAS_SQLITE3
8
8
 
9
9
  @property_class = Struct.new(:name, :type, :nullable, :default, :serial)
10
10
 
11
- class Book
11
+ class ::Book
12
12
  include DataMapper::Resource
13
13
 
14
14
  property :id, Serial
@@ -17,23 +17,23 @@ if HAS_SQLITE3
17
17
  property :class_type, Discriminator
18
18
  end
19
19
 
20
- class Propaganda < Book
20
+ class ::Propaganda < Book
21
21
  property :marxist, Boolean, :nullable => false, :default => false
22
22
  end
23
23
 
24
- class Fiction < Book
24
+ class ::Fiction < Book
25
25
  property :series, String
26
26
  end
27
27
 
28
- class ShortStory < Fiction
28
+ class ::ShortStory < Fiction
29
29
  property :moral, String
30
30
  end
31
31
 
32
- class ScienceFiction < Fiction
32
+ class ::ScienceFiction < Fiction
33
33
  property :aliens, Boolean
34
34
  end
35
35
 
36
- class SpaceWestern < ScienceFiction
36
+ class ::SpaceWestern < ScienceFiction
37
37
  property :cowboys, Boolean
38
38
  end
39
39
  end
@@ -4,7 +4,7 @@ describe "Strategic Eager Loading" do
4
4
  include LoggingHelper
5
5
 
6
6
  before :all do
7
- class Zoo
7
+ class ::Zoo
8
8
  include DataMapper::Resource
9
9
  def self.default_repository_name; ADAPTER end
10
10
 
@@ -14,7 +14,7 @@ describe "Strategic Eager Loading" do
14
14
  has n, :exhibits
15
15
  end
16
16
 
17
- class Exhibit
17
+ class ::Exhibit
18
18
  include DataMapper::Resource
19
19
  def self.default_repository_name; ADAPTER end
20
20
 
@@ -26,7 +26,7 @@ describe "Strategic Eager Loading" do
26
26
  has n, :animals
27
27
  end
28
28
 
29
- class Animal
29
+ class ::Animal
30
30
  include DataMapper::Resource
31
31
  def self.default_repository_name; ADAPTER end
32
32
 
@@ -67,11 +67,12 @@ describe "Strategic Eager Loading" do
67
67
  logger do |log|
68
68
  dallas.exhibits.entries # load all exhibits for zoos in identity_map
69
69
  dallas.exhibits.size.should == 1
70
+
70
71
  log.readlines.size.should == 1
71
- end
72
72
 
73
- repository.identity_map(Zoo).keys.sort.should == zoo_ids
74
- repository.identity_map(Exhibit).keys.sort.should == exhibit_ids
73
+ repository.identity_map(Zoo).keys.sort.should == zoo_ids
74
+ repository.identity_map(Exhibit).keys.sort.should == exhibit_ids
75
+ end
75
76
 
76
77
  logger do |log|
77
78
  zoos.each { |zoo| zoo.exhibits.entries } # issues no queries
@@ -97,16 +98,17 @@ describe "Strategic Eager Loading" do
97
98
  logger do |log|
98
99
  reptiles = dallas.exhibits(:name => 'Reptiles')
99
100
  reptiles.size.should == 1
101
+
100
102
  log.readlines.size.should == 1
101
103
  end
102
104
 
103
105
  logger do |log|
104
106
  primates = dallas.exhibits(:name => 'Primates')
105
107
  primates.size.should == 1
108
+ primates.should_not == reptiles
109
+
106
110
  log.readlines.size.should == 1
107
111
  end
108
-
109
- primates.should_not == reptiles
110
112
  end
111
113
  end
112
114
 
@@ -121,11 +123,12 @@ describe "Strategic Eager Loading" do
121
123
 
122
124
  logger do |log|
123
125
  bear.exhibit
126
+
127
+ repository.identity_map(Animal).keys.sort.should == animal_ids
128
+ repository.identity_map(Exhibit).keys.sort.should == exhibit_ids
129
+
124
130
  log.readlines.size.should == 1
125
131
  end
126
-
127
- repository.identity_map(Animal).keys.sort.should == animal_ids
128
- repository.identity_map(Exhibit).keys.sort.should == exhibit_ids
129
132
  end
130
133
  end
131
134
 
@@ -1,10 +1,14 @@
1
1
  require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper'))
2
2
 
3
- gem 'fastercsv', '~>1.4.0'
4
- require 'fastercsv'
3
+ if RUBY_VERSION >= '1.9.0'
4
+ require 'csv'
5
+ else
6
+ gem 'fastercsv', '~>1.4.0'
7
+ require 'fastercsv'
8
+ end
5
9
 
6
10
  if ADAPTER
7
- module TypeTests
11
+ module ::TypeTests
8
12
  class Impostor < DataMapper::Type
9
13
  primitive String
10
14
  end
@@ -25,7 +29,7 @@ if ADAPTER
25
29
  end
26
30
  end
27
31
 
28
- class Lemon
32
+ class ::Lemon
29
33
  include DataMapper::Resource
30
34
 
31
35
  def self.default_repository_name
@@ -37,7 +41,7 @@ if ADAPTER
37
41
  property :deleted_at, DataMapper::Types::ParanoidDateTime
38
42
  end
39
43
 
40
- class Lime
44
+ class ::Lime
41
45
  include DataMapper::Resource
42
46
 
43
47
  def self.default_repository_name
@@ -199,7 +203,7 @@ if ADAPTER
199
203
  DataMapper::Repository.adapters[:alternate_paranoid] = repository(ADAPTER).adapter.dup
200
204
 
201
205
  Object.send(:remove_const, :Orange) if defined?(Orange)
202
- class Orange
206
+ class ::Orange
203
207
  include DataMapper::Resource
204
208
 
205
209
  def self.default_repository_name
@@ -1,18 +1,18 @@
1
1
  module LoggingHelper
2
- def logger(adapter = ADAPTER, &block)
3
- current_adapter = DataObjects.const_get(repository(adapter).adapter.uri.scheme.capitalize)
4
- old_logger = current_adapter.logger
2
+ def logger
3
+ class << DataMapper.logger
4
+ attr_writer :log
5
+ end
6
+
7
+ old_log = DataMapper.logger.log
5
8
 
6
- log_path = File.join(SPEC_ROOT, "tmp.log")
7
- handle = File.open(log_path, "a+")
8
- current_adapter.logger = DataObjects::Logger.new(log_path, 0)
9
9
  begin
10
- yield(handle)
10
+ StringIO.new('') do |io|
11
+ DataMapper.logger.log = io
12
+ yield io
13
+ end
11
14
  ensure
12
- handle.truncate(0)
13
- handle.close
14
- current_adapter.logger = old_logger
15
- File.delete(log_path)
15
+ DataMapper.logger.log = old_log
16
16
  end
17
17
  end
18
18
  end
@@ -0,0 +1,16 @@
1
+ module Content
2
+ class Dialect
3
+ include DataMapper::Resource
4
+
5
+ property :id, Serial
6
+ property :name, String
7
+ property :code, String
8
+ end
9
+
10
+ class Locale
11
+ include DataMapper::Resource
12
+
13
+ property :id, Serial
14
+ property :name, String
15
+ end
16
+ end
@@ -8,12 +8,11 @@ SPEC_ROOT = Pathname(__FILE__).dirname.expand_path
8
8
  require SPEC_ROOT.parent + 'lib/dm-core'
9
9
 
10
10
  # Load the various helpers for the spec suite
11
- Dir[DataMapper.root / 'spec' / 'lib' / '*.rb'].each do |file|
11
+ Dir[(DataMapper.root / 'spec' / 'lib' / '*.rb').to_s].each do |file|
12
12
  require file
13
13
  end
14
14
 
15
15
  # setup mock adapters
16
- DataMapper.setup(:default, "sqlite3::memory:")
17
16
  DataMapper.setup(:default2, "sqlite3::memory:")
18
17
 
19
18
  [ :mock, :legacy, :west_coast, :east_coast ].each do |repository_name|
@@ -30,8 +29,13 @@ end
30
29
  #
31
30
  def setup_adapter(name, default_uri)
32
31
  begin
33
- DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
34
- Object.const_set('ADAPTER', ENV['ADAPTER'].to_sym) if name.to_s == ENV['ADAPTER']
32
+ adapter = DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
33
+
34
+ if name.to_s == ENV['ADAPTER']
35
+ Object.const_set('ADAPTER', ENV['ADAPTER'].to_sym)
36
+ DataMapper::Repository.adapters[:default] = adapter
37
+ end
38
+
35
39
  true
36
40
  rescue Exception => e
37
41
  if name.to_s == ENV['ADAPTER']
@@ -7,7 +7,7 @@ require DataMapper.root / 'spec' / 'unit' / 'adapters' / 'adapter_shared_spec'
7
7
 
8
8
  describe DataMapper::Adapters::DataObjectsAdapter do
9
9
  before :all do
10
- class Cheese
10
+ class ::Cheese
11
11
  include DataMapper::Resource
12
12
  property :id, Serial
13
13
  property :name, String, :nullable => false
@@ -26,7 +26,7 @@ describe DataMapper::Adapters::DataObjectsAdapter do
26
26
  describe "#find_by_sql" do
27
27
 
28
28
  before do
29
- class Plupp
29
+ class ::Plupp
30
30
  include DataMapper::Resource
31
31
  property :id, Integer, :key => true
32
32
  property :name, String
@@ -34,7 +34,7 @@ describe DataMapper::Adapters::DataObjectsAdapter do
34
34
  end
35
35
 
36
36
  it "should be added to DataMapper::Model" do
37
- DataMapper::Model.instance_methods.include?("find_by_sql").should == true
37
+ DataMapper::Model.instance_methods.map { |m| m.to_s }.include?("find_by_sql").should == true
38
38
  Plupp.should respond_to(:find_by_sql)
39
39
  end
40
40
 
@@ -73,7 +73,11 @@ describe DataMapper::Adapters::DataObjectsAdapter do
73
73
  end
74
74
 
75
75
  it "should accept a Query argument with or without options hash" do
76
- @connection.should_receive(:create_command).twice.with('SELECT "name" FROM "plupps" WHERE ("name" = ?) ORDER BY "id"').and_return(@command)
76
+ if ADAPTER == :mysql
77
+ @connection.should_receive(:create_command).twice.with('SELECT `name` FROM `plupps` WHERE (`name` = ?) ORDER BY `id`').and_return(@command)
78
+ else
79
+ @connection.should_receive(:create_command).twice.with('SELECT "name" FROM "plupps" WHERE ("name" = ?) ORDER BY "id"').and_return(@command)
80
+ end
77
81
  @command.should_receive(:execute_reader).twice.with('my pretty plur').and_return(@reader)
78
82
  Plupp.should_receive(:repository).any_number_of_times.and_return(@repository)
79
83
  Plupp.should_receive(:repository).any_number_of_times.with(:plupp_repo).and_return(@repository)
@@ -573,7 +577,7 @@ describe DataMapper::Adapters::DataObjectsAdapter do
573
577
 
574
578
  result = @adapter.query('SQL STRING')
575
579
 
576
- result.first.members.should == %w{id user_name age}
580
+ result.first.members.map { |m| m.to_s }.should == %w[ id user_name age ]
577
581
  end
578
582
 
579
583
  it 'should convert each row into the struct' do
@@ -0,0 +1,98 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', "..", 'spec_helper'))
2
+
3
+ describe DataMapper::Adapters::InMemoryAdapter do
4
+ before do
5
+ DataMapper.setup(:inmem, :adapter => 'in_memory')
6
+
7
+ class ::Heffalump
8
+ include DataMapper::Resource
9
+
10
+ def self.default_repository_name
11
+ :inmem
12
+ end
13
+
14
+ property :color, String, :key => true # TODO: Drop the 'must have a key' limitation
15
+ property :num_spots, Integer
16
+ property :striped, Boolean
17
+ end
18
+
19
+ @heff1 = Heffalump.create(:color => 'Black', :num_spots => 0, :striped => true)
20
+ @heff2 = Heffalump.create(:color => 'Brown', :num_spots => 25, :striped => false)
21
+ @heff3 = Heffalump.create(:color => 'Blue', :num_spots => nil, :striped => false)
22
+ end
23
+
24
+ it 'should successfully save an object' do
25
+ @heff1.new_record?.should be_false
26
+ end
27
+
28
+ it 'should be able to get the object' do
29
+ Heffalump.get('Black').should == @heff1
30
+ end
31
+
32
+ it 'should be able to get all the objects' do
33
+ Heffalump.all.should == [@heff1, @heff2, @heff3]
34
+ end
35
+
36
+ it 'should be able to search for objects with equal value' do
37
+ Heffalump.all(:striped => true).should == [@heff1]
38
+ end
39
+
40
+ it 'should be able to search for objects included in an array of values' do
41
+ Heffalump.all(:num_spots => [ 25, 50, 75, 100 ]).should == [@heff2]
42
+ end
43
+
44
+ it 'should be able to search for objects included in a range of values' do
45
+ Heffalump.all(:num_spots => 25..100).should == [@heff2]
46
+ end
47
+
48
+ it 'should be able to search for objects with nil value' do
49
+ Heffalump.all(:num_spots => nil).should == [@heff3]
50
+ end
51
+
52
+ it 'should be able to search for objects with not equal value' do
53
+ Heffalump.all(:striped.not => true).should == [@heff2, @heff3]
54
+ end
55
+
56
+ it 'should be able to search for objects not included in an array of values' do
57
+ Heffalump.all(:num_spots.not => [ 25, 50, 75, 100 ]).should == [@heff1, @heff3]
58
+ end
59
+
60
+ it 'should be able to search for objects not included in a range of values' do
61
+ Heffalump.all(:num_spots.not => 25..100).should == [@heff1, @heff3]
62
+ end
63
+
64
+ it 'should be able to search for objects with not nil value' do
65
+ Heffalump.all(:num_spots.not => nil).should == [@heff1, @heff2]
66
+ end
67
+
68
+ it 'should be able to search for objects that match value' do
69
+ Heffalump.all(:color.like => 'Bl').should == [@heff1, @heff3]
70
+ end
71
+
72
+ it 'should be able to search for objects with value greater than' do
73
+ Heffalump.all(:num_spots.gt => 0).should == [@heff2]
74
+ end
75
+
76
+ it 'should be able to search for objects with value greater than or equal to' do
77
+ Heffalump.all(:num_spots.gte => 0).should == [@heff1, @heff2]
78
+ end
79
+
80
+ it 'should be able to search for objects with value less than' do
81
+ Heffalump.all(:num_spots.lt => 1).should == [@heff1]
82
+ end
83
+
84
+ it 'should be able to search for objects with value less than or equal to' do
85
+ Heffalump.all(:num_spots.lte => 0).should == [@heff1]
86
+ end
87
+
88
+ it 'should be able to update an object' do
89
+ @heff1.num_spots = 10
90
+ @heff1.save
91
+ Heffalump.get('Black').num_spots.should == 10
92
+ end
93
+
94
+ it 'should be able to destroy an object' do
95
+ @heff1.destroy
96
+ Heffalump.all.size.should == 2
97
+ end
98
+ end