|
2 | 2 | require File.dirname(__FILE__) + '/../spec_helper.rb'
|
3 | 3 | require 'annotate/annotate_models'
|
4 | 4 | require 'annotate/active_record_patch'
|
| 5 | +require 'active_support/core_ext/string' |
5 | 6 |
|
6 | 7 | describe AnnotateModels do
|
7 | 8 | def mock_foreign_key(name, from_column, to_table, to_column = 'id')
|
@@ -60,19 +61,21 @@ def mock_column(name, type, options={})
|
60 | 61 | it { expect(AnnotateModels.quote(BigDecimal.new("1.2"))).to eql("1.2") }
|
61 | 62 | it { expect(AnnotateModels.quote([BigDecimal.new("1.2")])).to eql(["1.2"]) }
|
62 | 63 |
|
63 |
| - it "should get schema info" do |
| 64 | + it "should get schema info with default options" do |
64 | 65 | klass = mock_class(:users, :id, [
|
65 |
| - mock_column(:id, :integer), |
66 |
| - mock_column(:name, :string, :limit => 50) |
| 66 | + mock_column(:id, :integer, :limit => 8), |
| 67 | + mock_column(:name, :string, :limit => 50), |
| 68 | + mock_column(:notes, :text, :limit => 55), |
67 | 69 | ])
|
68 | 70 |
|
69 | 71 | expect(AnnotateModels.get_schema_info(klass, "Schema Info")).to eql(<<-EOS)
|
70 | 72 | # Schema Info
|
71 | 73 | #
|
72 | 74 | # Table name: users
|
73 | 75 | #
|
74 |
| -# id :integer not null, primary key |
75 |
| -# name :string(50) not null |
| 76 | +# id :integer not null, primary key |
| 77 | +# name :string(50) not null |
| 78 | +# notes :text(55) not null |
76 | 79 | #
|
77 | 80 | EOS
|
78 | 81 | end
|
@@ -192,6 +195,61 @@ def mock_column(name, type, options={})
|
192 | 195 | EOS
|
193 | 196 | end
|
194 | 197 |
|
| 198 | + describe "#get_schema_info with custom options" do |
| 199 | + def self.when_called_with(options = {}) |
| 200 | + expected = options.delete(:returns) |
| 201 | + |
| 202 | + it "should work with options = #{options}" do |
| 203 | + klass = mock_class(:users, :id, [ |
| 204 | + mock_column(:id, :integer, :limit => 8), |
| 205 | + mock_column(:active, :boolean, :limit => 1), |
| 206 | + mock_column(:name, :string, :limit => 50), |
| 207 | + mock_column(:notes, :text, :limit => 55), |
| 208 | + ]) |
| 209 | + schema_info = AnnotateModels.get_schema_info(klass, "Schema Info", options) |
| 210 | + expect(schema_info).to eql(expected) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + when_called_with hide_limit_column_types: '', returns: <<-EOS.strip_heredoc |
| 215 | + # Schema Info |
| 216 | + # |
| 217 | + # Table name: users |
| 218 | + # |
| 219 | + # id :integer not null, primary key |
| 220 | + # active :boolean not null |
| 221 | + # name :string(50) not null |
| 222 | + # notes :text(55) not null |
| 223 | + # |
| 224 | + EOS |
| 225 | + |
| 226 | + when_called_with hide_limit_column_types: 'integer,boolean', returns: |
| 227 | + <<-EOS.strip_heredoc |
| 228 | + # Schema Info |
| 229 | + # |
| 230 | + # Table name: users |
| 231 | + # |
| 232 | + # id :integer not null, primary key |
| 233 | + # active :boolean not null |
| 234 | + # name :string(50) not null |
| 235 | + # notes :text(55) not null |
| 236 | + # |
| 237 | + EOS |
| 238 | + |
| 239 | + when_called_with hide_limit_column_types: 'integer,boolean,string,text', returns: |
| 240 | + <<-EOS.strip_heredoc |
| 241 | + # Schema Info |
| 242 | + # |
| 243 | + # Table name: users |
| 244 | + # |
| 245 | + # id :integer not null, primary key |
| 246 | + # active :boolean not null |
| 247 | + # name :string not null |
| 248 | + # notes :text not null |
| 249 | + # |
| 250 | + EOS |
| 251 | + end |
| 252 | + |
195 | 253 | describe "#get_model_class" do
|
196 | 254 | require "tmpdir"
|
197 | 255 |
|
|
0 commit comments