epugh-sequel 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (134) hide show
  1. data/README.rdoc +652 -0
  2. data/VERSION.yml +4 -0
  3. data/bin/sequel +104 -0
  4. data/lib/sequel.rb +1 -0
  5. data/lib/sequel/adapters/ado.rb +85 -0
  6. data/lib/sequel/adapters/db2.rb +132 -0
  7. data/lib/sequel/adapters/dbi.rb +101 -0
  8. data/lib/sequel/adapters/do.rb +197 -0
  9. data/lib/sequel/adapters/do/mysql.rb +38 -0
  10. data/lib/sequel/adapters/do/postgres.rb +92 -0
  11. data/lib/sequel/adapters/do/sqlite.rb +31 -0
  12. data/lib/sequel/adapters/firebird.rb +307 -0
  13. data/lib/sequel/adapters/informix.rb +75 -0
  14. data/lib/sequel/adapters/jdbc.rb +485 -0
  15. data/lib/sequel/adapters/jdbc/h2.rb +62 -0
  16. data/lib/sequel/adapters/jdbc/mysql.rb +56 -0
  17. data/lib/sequel/adapters/jdbc/oracle.rb +23 -0
  18. data/lib/sequel/adapters/jdbc/postgresql.rb +101 -0
  19. data/lib/sequel/adapters/jdbc/sqlite.rb +43 -0
  20. data/lib/sequel/adapters/mysql.rb +370 -0
  21. data/lib/sequel/adapters/odbc.rb +184 -0
  22. data/lib/sequel/adapters/openbase.rb +57 -0
  23. data/lib/sequel/adapters/oracle.rb +140 -0
  24. data/lib/sequel/adapters/postgres.rb +453 -0
  25. data/lib/sequel/adapters/shared/mssql.rb +93 -0
  26. data/lib/sequel/adapters/shared/mysql.rb +341 -0
  27. data/lib/sequel/adapters/shared/oracle.rb +62 -0
  28. data/lib/sequel/adapters/shared/postgres.rb +743 -0
  29. data/lib/sequel/adapters/shared/progress.rb +34 -0
  30. data/lib/sequel/adapters/shared/sqlite.rb +263 -0
  31. data/lib/sequel/adapters/sqlite.rb +243 -0
  32. data/lib/sequel/adapters/utils/date_format.rb +21 -0
  33. data/lib/sequel/adapters/utils/stored_procedures.rb +75 -0
  34. data/lib/sequel/adapters/utils/unsupported.rb +62 -0
  35. data/lib/sequel/connection_pool.rb +258 -0
  36. data/lib/sequel/core.rb +204 -0
  37. data/lib/sequel/core_sql.rb +185 -0
  38. data/lib/sequel/database.rb +687 -0
  39. data/lib/sequel/database/schema_generator.rb +324 -0
  40. data/lib/sequel/database/schema_methods.rb +164 -0
  41. data/lib/sequel/database/schema_sql.rb +324 -0
  42. data/lib/sequel/dataset.rb +422 -0
  43. data/lib/sequel/dataset/convenience.rb +237 -0
  44. data/lib/sequel/dataset/prepared_statements.rb +220 -0
  45. data/lib/sequel/dataset/sql.rb +1105 -0
  46. data/lib/sequel/deprecated.rb +529 -0
  47. data/lib/sequel/exceptions.rb +44 -0
  48. data/lib/sequel/extensions/blank.rb +42 -0
  49. data/lib/sequel/extensions/inflector.rb +288 -0
  50. data/lib/sequel/extensions/pagination.rb +96 -0
  51. data/lib/sequel/extensions/pretty_table.rb +78 -0
  52. data/lib/sequel/extensions/query.rb +48 -0
  53. data/lib/sequel/extensions/string_date_time.rb +47 -0
  54. data/lib/sequel/metaprogramming.rb +44 -0
  55. data/lib/sequel/migration.rb +212 -0
  56. data/lib/sequel/model.rb +142 -0
  57. data/lib/sequel/model/association_reflection.rb +263 -0
  58. data/lib/sequel/model/associations.rb +1024 -0
  59. data/lib/sequel/model/base.rb +911 -0
  60. data/lib/sequel/model/deprecated.rb +188 -0
  61. data/lib/sequel/model/deprecated_hooks.rb +103 -0
  62. data/lib/sequel/model/deprecated_inflector.rb +335 -0
  63. data/lib/sequel/model/deprecated_validations.rb +384 -0
  64. data/lib/sequel/model/errors.rb +37 -0
  65. data/lib/sequel/model/exceptions.rb +7 -0
  66. data/lib/sequel/model/inflections.rb +230 -0
  67. data/lib/sequel/model/plugins.rb +74 -0
  68. data/lib/sequel/object_graph.rb +230 -0
  69. data/lib/sequel/plugins/caching.rb +122 -0
  70. data/lib/sequel/plugins/hook_class_methods.rb +122 -0
  71. data/lib/sequel/plugins/schema.rb +53 -0
  72. data/lib/sequel/plugins/single_table_inheritance.rb +63 -0
  73. data/lib/sequel/plugins/validation_class_methods.rb +373 -0
  74. data/lib/sequel/sql.rb +854 -0
  75. data/lib/sequel/version.rb +11 -0
  76. data/lib/sequel_core.rb +1 -0
  77. data/lib/sequel_model.rb +1 -0
  78. data/spec/adapters/ado_spec.rb +46 -0
  79. data/spec/adapters/firebird_spec.rb +376 -0
  80. data/spec/adapters/informix_spec.rb +96 -0
  81. data/spec/adapters/mysql_spec.rb +875 -0
  82. data/spec/adapters/oracle_spec.rb +272 -0
  83. data/spec/adapters/postgres_spec.rb +692 -0
  84. data/spec/adapters/spec_helper.rb +10 -0
  85. data/spec/adapters/sqlite_spec.rb +550 -0
  86. data/spec/core/connection_pool_spec.rb +526 -0
  87. data/spec/core/core_ext_spec.rb +156 -0
  88. data/spec/core/core_sql_spec.rb +528 -0
  89. data/spec/core/database_spec.rb +1214 -0
  90. data/spec/core/dataset_spec.rb +3513 -0
  91. data/spec/core/expression_filters_spec.rb +363 -0
  92. data/spec/core/migration_spec.rb +261 -0
  93. data/spec/core/object_graph_spec.rb +280 -0
  94. data/spec/core/pretty_table_spec.rb +58 -0
  95. data/spec/core/schema_generator_spec.rb +167 -0
  96. data/spec/core/schema_spec.rb +778 -0
  97. data/spec/core/spec_helper.rb +82 -0
  98. data/spec/core/version_spec.rb +7 -0
  99. data/spec/extensions/blank_spec.rb +67 -0
  100. data/spec/extensions/caching_spec.rb +201 -0
  101. data/spec/extensions/hook_class_methods_spec.rb +470 -0
  102. data/spec/extensions/inflector_spec.rb +122 -0
  103. data/spec/extensions/pagination_spec.rb +99 -0
  104. data/spec/extensions/pretty_table_spec.rb +91 -0
  105. data/spec/extensions/query_spec.rb +85 -0
  106. data/spec/extensions/schema_spec.rb +111 -0
  107. data/spec/extensions/single_table_inheritance_spec.rb +53 -0
  108. data/spec/extensions/spec_helper.rb +90 -0
  109. data/spec/extensions/string_date_time_spec.rb +93 -0
  110. data/spec/extensions/validation_class_methods_spec.rb +1054 -0
  111. data/spec/integration/dataset_test.rb +160 -0
  112. data/spec/integration/eager_loader_test.rb +683 -0
  113. data/spec/integration/prepared_statement_test.rb +130 -0
  114. data/spec/integration/schema_test.rb +183 -0
  115. data/spec/integration/spec_helper.rb +75 -0
  116. data/spec/integration/type_test.rb +96 -0
  117. data/spec/model/association_reflection_spec.rb +93 -0
  118. data/spec/model/associations_spec.rb +1780 -0
  119. data/spec/model/base_spec.rb +494 -0
  120. data/spec/model/caching_spec.rb +217 -0
  121. data/spec/model/dataset_methods_spec.rb +78 -0
  122. data/spec/model/eager_loading_spec.rb +1165 -0
  123. data/spec/model/hooks_spec.rb +472 -0
  124. data/spec/model/inflector_spec.rb +126 -0
  125. data/spec/model/model_spec.rb +588 -0
  126. data/spec/model/plugins_spec.rb +142 -0
  127. data/spec/model/record_spec.rb +1243 -0
  128. data/spec/model/schema_spec.rb +92 -0
  129. data/spec/model/spec_helper.rb +124 -0
  130. data/spec/model/validations_spec.rb +1080 -0
  131. data/spec/rcov.opts +6 -0
  132. data/spec/spec.opts +0 -0
  133. data/spec/spec_config.rb.example +10 -0
  134. metadata +202 -0
@@ -0,0 +1,122 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe String do
4
+ it "#camelize and #camelcase should transform the word to CamelCase" do
5
+ "egg_and_hams".camelize.should == "EggAndHams"
6
+ "egg_and_hams".camelize(false).should == "eggAndHams"
7
+ "post".camelize.should == "Post"
8
+ "post".camelcase.should == "Post"
9
+ end
10
+
11
+ it "#constantize should eval the string to get a constant" do
12
+ "String".constantize.should == String
13
+ "String::Inflections".constantize.should == String::Inflections
14
+ proc{"BKSDDF".constantize}.should raise_error
15
+ proc{"++A++".constantize}.should raise_error
16
+ end
17
+
18
+ it "#dasherize should transform underscores to dashes" do
19
+ "egg_and_hams".dasherize.should == "egg-and-hams"
20
+ "post".dasherize.should == "post"
21
+ end
22
+
23
+ it "#demodulize should remove any preceding modules" do
24
+ "String::Inflections::Blah".demodulize.should == "Blah"
25
+ "String::Inflections".demodulize.should == "Inflections"
26
+ "String".demodulize.should == "String"
27
+ end
28
+
29
+ it "#humanize should remove _i, transform underscore to spaces, and capitalize" do
30
+ "egg_and_hams".humanize.should == "Egg and hams"
31
+ "post".humanize.should == "Post"
32
+ "post_id".humanize.should == "Post"
33
+ end
34
+
35
+ it "#titleize and #titlecase should underscore, humanize, and capitalize all words" do
36
+ "egg-and: hams".titleize.should == "Egg And: Hams"
37
+ "post".titleize.should == "Post"
38
+ "post".titlecase.should == "Post"
39
+ end
40
+
41
+ it "#underscore should add underscores between CamelCased words, change :: to / and - to _, and downcase" do
42
+ "EggAndHams".underscore.should == "egg_and_hams"
43
+ "EGGAndHams".underscore.should == "egg_and_hams"
44
+ "Egg::And::Hams".underscore.should == "egg/and/hams"
45
+ "post".underscore.should == "post"
46
+ "post-id".underscore.should == "post_id"
47
+ end
48
+
49
+ it "#pluralize should transform words from singular to plural" do
50
+ "post".pluralize.should == "posts"
51
+ "octopus".pluralize.should =="octopi"
52
+ "the blue mailman".pluralize.should == "the blue mailmen"
53
+ "CamelOctopus".pluralize.should == "CamelOctopi"
54
+ end
55
+
56
+ it "#singularize should transform words from plural to singular" do
57
+ "posts".singularize.should == "post"
58
+ "octopi".singularize.should == "octopus"
59
+ "the blue mailmen".singularize.should == "the blue mailman"
60
+ "CamelOctopi".singularize.should == "CamelOctopus"
61
+ end
62
+
63
+ it "#tableize should transform class names to table names" do
64
+ "RawScaledScorer".tableize.should == "raw_scaled_scorers"
65
+ "egg_and_ham".tableize.should == "egg_and_hams"
66
+ "fancyCategory".tableize.should == "fancy_categories"
67
+ end
68
+
69
+ it "#classify should tranform table names to class names" do
70
+ "egg_and_hams".classify.should == "EggAndHam"
71
+ "post".classify.should == "Post"
72
+ end
73
+
74
+ it "#foreign_key should create a foreign key name from a class name" do
75
+ "Message".foreign_key.should == "message_id"
76
+ "Message".foreign_key(false).should == "messageid"
77
+ "Admin::Post".foreign_key.should == "post_id"
78
+ end
79
+ end
80
+
81
+ describe String::Inflections do
82
+ before do
83
+ @plurals, @singulars, @uncountables = String.inflections.plurals.dup, String.inflections.singulars.dup, String.inflections.uncountables.dup
84
+ end
85
+ after do
86
+ String.inflections.plurals.replace(@plurals)
87
+ String.inflections.singulars.replace(@singulars)
88
+ String.inflections.uncountables.replace(@uncountables)
89
+ Sequel.inflections.plurals.replace(@plurals)
90
+ Sequel.inflections.singulars.replace(@singulars)
91
+ Sequel.inflections.uncountables.replace(@uncountables)
92
+ end
93
+
94
+ it "should be possible to clear the list of singulars, plurals, and uncountables" do
95
+ String.inflections.clear(:plurals)
96
+ String.inflections.plurals.should == []
97
+ String.inflections.plural('blah', 'blahs')
98
+ String.inflections.clear
99
+ String.inflections.plurals.should == []
100
+ String.inflections.singulars.should == []
101
+ String.inflections.uncountables.should == []
102
+ end
103
+
104
+ it "should be able to specify new inflection rules" do
105
+ String.inflections do |i|
106
+ i.plural(/xx$/i, 'xxx')
107
+ i.singular(/ttt$/i, 'tt')
108
+ i.irregular('yy', 'yyy')
109
+ i.uncountable(%w'zz')
110
+ end
111
+ 'roxx'.pluralize.should == 'roxxx'
112
+ 'rottt'.singularize.should == 'rott'
113
+ 'yy'.pluralize.should == 'yyy'
114
+ 'yyy'.singularize.should == 'yy'
115
+ 'zz'.pluralize.should == 'zz'
116
+ 'zz'.singularize.should == 'zz'
117
+ end
118
+
119
+ it "should be yielded and returned by String.inflections" do
120
+ String.inflections{|i| i.should == String::Inflections}.should == String::Inflections
121
+ end
122
+ end
@@ -0,0 +1,99 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ context "A paginated dataset" do
4
+ setup do
5
+ @d = Sequel::Dataset.new(nil)
6
+ @d.meta_def(:count) {153}
7
+
8
+ @paginated = @d.paginate(1, 20)
9
+ end
10
+
11
+ specify "should raise an error if the dataset already has a limit" do
12
+ proc{@d.limit(10).paginate(1,10)}.should raise_error(Sequel::Error)
13
+ proc{@paginated.paginate(2,20)}.should raise_error(Sequel::Error)
14
+ end
15
+
16
+ specify "should set the limit and offset options correctly" do
17
+ @paginated.opts[:limit].should == 20
18
+ @paginated.opts[:offset].should == 0
19
+ end
20
+
21
+ specify "should set the page count correctly" do
22
+ @paginated.page_count.should == 8
23
+ @d.paginate(1, 50).page_count.should == 4
24
+ end
25
+
26
+ specify "should set the current page number correctly" do
27
+ @paginated.current_page.should == 1
28
+ @d.paginate(3, 50).current_page.should == 3
29
+ end
30
+
31
+ specify "should return the next page number or nil if we're on the last" do
32
+ @paginated.next_page.should == 2
33
+ @d.paginate(4, 50).next_page.should be_nil
34
+ end
35
+
36
+ specify "should return the previous page number or nil if we're on the last" do
37
+ @paginated.prev_page.should be_nil
38
+ @d.paginate(4, 50).prev_page.should == 3
39
+ end
40
+
41
+ specify "should return the page range" do
42
+ @paginated.page_range.should == (1..8)
43
+ @d.paginate(4, 50).page_range.should == (1..4)
44
+ end
45
+
46
+ specify "should return the record range for the current page" do
47
+ @paginated.current_page_record_range.should == (1..20)
48
+ @d.paginate(4, 50).current_page_record_range.should == (151..153)
49
+ @d.paginate(5, 50).current_page_record_range.should == (0..0)
50
+ end
51
+
52
+ specify "should return the record count for the current page" do
53
+ @paginated.current_page_record_count.should == 20
54
+ @d.paginate(3, 50).current_page_record_count.should == 50
55
+ @d.paginate(4, 50).current_page_record_count.should == 3
56
+ @d.paginate(5, 50).current_page_record_count.should == 0
57
+ end
58
+
59
+ specify "should know if current page is last page" do
60
+ @paginated.last_page?.should be_false
61
+ @d.paginate(2, 20).last_page?.should be_false
62
+ @d.paginate(5, 30).last_page?.should be_false
63
+ @d.paginate(6, 30).last_page?.should be_true
64
+ end
65
+
66
+ specify "should know if current page is first page" do
67
+ @paginated.first_page?.should be_true
68
+ @d.paginate(1, 20).first_page?.should be_true
69
+ @d.paginate(2, 20).first_page?.should be_false
70
+ end
71
+
72
+ specify "should work with fixed sql" do
73
+ ds = @d.clone(:sql => 'select * from blah')
74
+ ds.meta_def(:count) {150}
75
+ ds.paginate(2, 50).sql.should == 'SELECT * FROM (select * from blah) AS t1 LIMIT 50 OFFSET 50'
76
+ end
77
+ end
78
+
79
+ context "Dataset#each_page" do
80
+ setup do
81
+ @d = Sequel::Dataset.new(nil).from(:items)
82
+ @d.meta_def(:count) {153}
83
+ end
84
+
85
+ specify "should raise an error if the dataset already has a limit" do
86
+ proc{@d.limit(10).each_page(10){}}.should raise_error(Sequel::Error)
87
+ end
88
+
89
+ specify "should iterate over each page in the resultset as a paginated dataset" do
90
+ a = []
91
+ @d.each_page(50) {|p| a << p}
92
+ a.map {|p| p.sql}.should == [
93
+ 'SELECT * FROM items LIMIT 50 OFFSET 0',
94
+ 'SELECT * FROM items LIMIT 50 OFFSET 50',
95
+ 'SELECT * FROM items LIMIT 50 OFFSET 100',
96
+ 'SELECT * FROM items LIMIT 50 OFFSET 150',
97
+ ]
98
+ end
99
+ end
@@ -0,0 +1,91 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ require 'stringio'
4
+
5
+ context "Dataset#print" do
6
+ setup do
7
+ @output = StringIO.new
8
+ @orig_stdout = $stdout
9
+ $stdout = @output
10
+ @dataset = Sequel::Dataset.new(nil).from(:items)
11
+ def @dataset.fetch_rows(sql)
12
+ yield({:a=>1, :b=>2})
13
+ yield({:a=>3, :b=>4})
14
+ yield({:a=>5, :b=>6})
15
+ end
16
+ end
17
+
18
+ teardown do
19
+ $stdout = @orig_stdout
20
+ end
21
+
22
+ specify "should print out a table with the values" do
23
+ @dataset.print(:a, :b)
24
+ @output.rewind
25
+ @output.read.should == \
26
+ "+-+-+\n|a|b|\n+-+-+\n|1|2|\n|3|4|\n|5|6|\n+-+-+\n"
27
+ end
28
+
29
+ specify "should default to the dataset's columns" do
30
+ @dataset.meta_def(:columns) {[:a, :b]}
31
+ @dataset.print
32
+ @output.rewind
33
+ @output.read.should == \
34
+ "+-+-+\n|a|b|\n+-+-+\n|1|2|\n|3|4|\n|5|6|\n+-+-+\n"
35
+ end
36
+ end
37
+
38
+ context "PrettyTable" do
39
+ setup do
40
+ @data1 = [
41
+ {:x => 3, :y => 4}
42
+ ]
43
+
44
+ @data2 = [
45
+ {:a => 23, :b => 45},
46
+ {:a => 45, :b => 2377}
47
+ ]
48
+
49
+ @data3 = [
50
+ {:aaa => 1},
51
+ {:bb => 2},
52
+ {:c => 3}
53
+ ]
54
+
55
+ @output = StringIO.new
56
+ @orig_stdout = $stdout
57
+ $stdout = @output
58
+ end
59
+
60
+ teardown do
61
+ $stdout = @orig_stdout
62
+ end
63
+
64
+ specify "should infer the columns if not given" do
65
+ Sequel::PrettyTable.print(@data1)
66
+ @output.rewind
67
+ @output.read.should =~ \
68
+ /\n(\|x\|y\|)|(\|y\|x\|)\n/
69
+ end
70
+
71
+ specify "should calculate the maximum width of each column correctly" do
72
+ Sequel::PrettyTable.print(@data2, [:a, :b])
73
+ @output.rewind
74
+ @output.read.should == \
75
+ "+--+----+\n|a |b |\n+--+----+\n|23| 45|\n|45|2377|\n+--+----+\n"
76
+ end
77
+
78
+ specify "should also take header width into account" do
79
+ Sequel::PrettyTable.print(@data3, [:aaa, :bb, :c])
80
+ @output.rewind
81
+ @output.read.should == \
82
+ "+---+--+-+\n|aaa|bb|c|\n+---+--+-+\n| 1| | |\n| | 2| |\n| | |3|\n+---+--+-+\n"
83
+ end
84
+
85
+ specify "should print only the specified columns" do
86
+ Sequel::PrettyTable.print(@data2, [:a])
87
+ @output.rewind
88
+ @output.read.should == \
89
+ "+--+\n|a |\n+--+\n|23|\n|45|\n+--+\n"
90
+ end
91
+ end
@@ -0,0 +1,85 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ context "Database#dataset" do
4
+ setup do
5
+ @db = Sequel::Database.new
6
+ end
7
+
8
+ specify "should delegate to Dataset#query if block is provided" do
9
+ @d = @db.query {select :x; from :y}
10
+ @d.should be_a_kind_of(Sequel::Dataset)
11
+ @d.sql.should == "SELECT x FROM y"
12
+ end
13
+ end
14
+
15
+ context "Dataset#query" do
16
+ setup do
17
+ @d = Sequel::Dataset.new(nil)
18
+ end
19
+
20
+ specify "should support #from" do
21
+ q = @d.query {from :xxx}
22
+ q.class.should == @d.class
23
+ q.sql.should == "SELECT * FROM xxx"
24
+ end
25
+
26
+ specify "should support #select" do
27
+ q = @d.query do
28
+ select :a, :b___mongo
29
+ from :yyy
30
+ end
31
+ q.class.should == @d.class
32
+ q.sql.should == "SELECT a, b AS mongo FROM yyy"
33
+ end
34
+
35
+ specify "should support #where" do
36
+ q = @d.query do
37
+ from :zzz
38
+ where(:x + 2 > :y + 3)
39
+ end
40
+ q.class.should == @d.class
41
+ q.sql.should == "SELECT * FROM zzz WHERE ((x + 2) > (y + 3))"
42
+
43
+ q = @d.from(:zzz).query do
44
+ where((:x.sql_number > 1) & (:y.sql_number > 2))
45
+ end
46
+ q.class.should == @d.class
47
+ q.sql.should == "SELECT * FROM zzz WHERE ((x > 1) AND (y > 2))"
48
+
49
+ q = @d.from(:zzz).query do
50
+ where :x => 33
51
+ end
52
+ q.class.should == @d.class
53
+ q.sql.should == "SELECT * FROM zzz WHERE (x = 33)"
54
+ end
55
+
56
+ specify "should support #group_by and #having" do
57
+ q = @d.query do
58
+ from :abc
59
+ group_by :id
60
+ having(:x.sql_number >= 2)
61
+ end
62
+ q.class.should == @d.class
63
+ q.sql.should == "SELECT * FROM abc GROUP BY id HAVING (x >= 2)"
64
+ end
65
+
66
+ specify "should support #order, #order_by" do
67
+ q = @d.query do
68
+ from :xyz
69
+ order_by :stamp
70
+ end
71
+ q.class.should == @d.class
72
+ q.sql.should == "SELECT * FROM xyz ORDER BY stamp"
73
+ end
74
+
75
+ specify "should raise on non-chainable method calls" do
76
+ proc {@d.query {first_source}}.should raise_error(Sequel::Error)
77
+ end
78
+
79
+ specify "should raise on each, insert, update, delete" do
80
+ proc {@d.query {each}}.should raise_error(Sequel::Error)
81
+ proc {@d.query {insert(:x => 1)}}.should raise_error(Sequel::Error)
82
+ proc {@d.query {update(:x => 1)}}.should raise_error(Sequel::Error)
83
+ proc {@d.query {delete}}.should raise_error(Sequel::Error)
84
+ end
85
+ end
@@ -0,0 +1,111 @@
1
+ require File.join(File.dirname(__FILE__), "spec_helper")
2
+
3
+ describe Sequel::Model, "dataset & schema" do
4
+ before do
5
+ @model = Class.new(Sequel::Model(:items))
6
+ end
7
+
8
+ specify "sets schema with implicit table name" do
9
+ @model.set_schema do
10
+ primary_key :ssn, :string
11
+ end
12
+ @model.primary_key.should == :ssn
13
+ @model.table_name.should == :items
14
+ end
15
+
16
+ specify "sets schema with explicit table name" do
17
+ @model.set_schema :foo do
18
+ primary_key :id
19
+ end
20
+ @model.primary_key.should == :id
21
+ @model.table_name.should == :foo
22
+ end
23
+ end
24
+
25
+ describe Sequel::Model, "table_exists?" do
26
+
27
+ before(:each) do
28
+ MODEL_DB.reset
29
+ @model = Class.new(Sequel::Model(:items))
30
+ end
31
+
32
+ it "should get the table name and question the model's db if table_exists?" do
33
+ @model.should_receive(:table_name).and_return(:items)
34
+ @model.db.should_receive(:table_exists?)
35
+ @model.table_exists?
36
+ end
37
+ end
38
+
39
+ describe Sequel::Model, "create_table and schema" do
40
+
41
+ before(:each) do
42
+ MODEL_DB.reset
43
+ @model = Class.new(Sequel::Model) do
44
+ set_schema(:items) do
45
+ text :name
46
+ float :price, :null => false
47
+ end
48
+ end
49
+ end
50
+
51
+ it "should get the create table SQL list from the db and execute it line by line" do
52
+ @model.create_table
53
+ MODEL_DB.sqls.should == ['CREATE TABLE items (name text, price float NOT NULL)']
54
+ end
55
+
56
+ it "should reload the schema from the database" do
57
+ schem = {:name=>{:type=>:string}, :price=>{:type=>:float}}
58
+ @model.db.should_receive(:schema).with(:items, :reload=>true).and_return(schem.to_a.sort_by{|x| x[0].to_s})
59
+ @model.create_table
60
+ @model.db_schema.should == schem
61
+ @model.instance_variable_get(:@columns).should == [:name, :price]
62
+ end
63
+
64
+ it "should return the schema generator via schema" do
65
+ @model.schema.should be_a_kind_of(Sequel::Schema::Generator)
66
+ end
67
+
68
+ it "should use the superclasses schema if it exists" do
69
+ @submodel = Class.new(@model)
70
+ @submodel.schema.should be_a_kind_of(Sequel::Schema::Generator)
71
+ end
72
+
73
+ it "should return nil if no schema is present" do
74
+ @model = Class.new(Sequel::Model)
75
+ @model.schema.should == nil
76
+ @submodel = Class.new(@model)
77
+ @submodel.schema.should == nil
78
+ end
79
+ end
80
+
81
+ describe Sequel::Model, "drop_table" do
82
+
83
+ before(:each) do
84
+ MODEL_DB.reset
85
+ @model = Class.new(Sequel::Model(:items))
86
+ end
87
+
88
+ it "should get the drop table SQL for the associated table and then execute the SQL." do
89
+ @model.should_receive(:table_name).and_return(:items)
90
+ @model.db.should_receive(:drop_table_sql).with(:items)
91
+ @model.db.should_receive(:execute).and_return(:true)
92
+ @model.drop_table
93
+ end
94
+
95
+ end
96
+
97
+ describe Sequel::Model, "create_table!" do
98
+
99
+ before(:each) do
100
+ MODEL_DB.reset
101
+ @model = Class.new(Sequel::Model(:items))
102
+ end
103
+
104
+ it "should drop table if it exists and then create the table" do
105
+ @model.should_receive(:drop_table).and_return(true)
106
+ @model.should_receive(:create_table).and_return(true)
107
+
108
+ @model.create_table!
109
+ end
110
+
111
+ end