diff options
Diffstat (limited to 'activeresource/test/cases/base')
-rw-r--r-- | activeresource/test/cases/base/custom_methods_test.rb | 101 | ||||
-rw-r--r-- | activeresource/test/cases/base/equality_test.rb | 52 | ||||
-rw-r--r-- | activeresource/test/cases/base/load_test.rb | 199 | ||||
-rw-r--r-- | activeresource/test/cases/base/schema_test.rb | 411 |
4 files changed, 0 insertions, 763 deletions
diff --git a/activeresource/test/cases/base/custom_methods_test.rb b/activeresource/test/cases/base/custom_methods_test.rb deleted file mode 100644 index f7aa7a4a09..0000000000 --- a/activeresource/test/cases/base/custom_methods_test.rb +++ /dev/null @@ -1,101 +0,0 @@ -require 'abstract_unit' -require 'fixtures/person' -require 'fixtures/street_address' -require 'active_support/core_ext/hash/conversions' - -class CustomMethodsTest < ActiveSupport::TestCase - def setup - @matz = { :person => { :id => 1, :name => 'Matz' } }.to_json - @matz_deep = { :person => { :id => 1, :name => 'Matz', :other => 'other' } }.to_json - @matz_array = { :people => [{ :person => { :id => 1, :name => 'Matz' } }] }.to_json - @ryan = { :person => { :name => 'Ryan' } }.to_json - @addy = { :address => { :id => 1, :street => '12345 Street' } }.to_json - @addy_deep = { :address => { :id => 1, :street => '12345 Street', :zip => "27519" } }.to_json - - ActiveResource::HttpMock.respond_to do |mock| - mock.get "/people/1.json", {}, @matz - mock.get "/people/1/shallow.json", {}, @matz - mock.get "/people/1/deep.json", {}, @matz_deep - mock.get "/people/retrieve.json?name=Matz", {}, @matz_array - mock.get "/people/managers.json", {}, @matz_array - mock.post "/people/hire.json?name=Matz", {}, nil, 201 - mock.put "/people/1/promote.json?position=Manager", {}, nil, 204 - mock.put "/people/promote.json?name=Matz", {}, nil, 204, {} - mock.put "/people/sort.json?by=name", {}, nil, 204 - mock.delete "/people/deactivate.json?name=Matz", {}, nil, 200 - mock.delete "/people/1/deactivate.json", {}, nil, 200 - mock.post "/people/new/register.json", {}, @ryan, 201, 'Location' => '/people/5.json' - mock.post "/people/1/register.json", {}, @matz, 201 - mock.get "/people/1/addresses/1.json", {}, @addy - mock.get "/people/1/addresses/1/deep.json", {}, @addy_deep - mock.put "/people/1/addresses/1/normalize_phone.json?locale=US", {}, nil, 204 - mock.put "/people/1/addresses/sort.json?by=name", {}, nil, 204 - mock.post "/people/1/addresses/new/link.json", {}, { :address => { :street => '12345 Street' } }.to_json, 201, 'Location' => '/people/1/addresses/2.json' - end - - Person.user = nil - Person.password = nil - end - - def teardown - ActiveResource::HttpMock.reset! - end - - def test_custom_collection_method - # GET - assert_equal([{ "id" => 1, "name" => 'Matz' }], Person.get(:retrieve, :name => 'Matz')) - - # POST - assert_equal(ActiveResource::Response.new("", 201, {}), Person.post(:hire, :name => 'Matz')) - - # PUT - assert_equal ActiveResource::Response.new("", 204, {}), - Person.put(:promote, {:name => 'Matz'}, 'atestbody') - assert_equal ActiveResource::Response.new("", 204, {}), Person.put(:sort, :by => 'name') - - # DELETE - Person.delete :deactivate, :name => 'Matz' - - # Nested resource - assert_equal ActiveResource::Response.new("", 204, {}), StreetAddress.put(:sort, :person_id => 1, :by => 'name') - end - - def test_custom_element_method - # Test GET against an element URL - assert_equal Person.find(1).get(:shallow), {"id" => 1, "name" => 'Matz'} - assert_equal Person.find(1).get(:deep), {"id" => 1, "name" => 'Matz', "other" => 'other'} - - # Test PUT against an element URL - assert_equal ActiveResource::Response.new("", 204, {}), Person.find(1).put(:promote, {:position => 'Manager'}, 'body') - - # Test DELETE against an element URL - assert_equal ActiveResource::Response.new("", 200, {}), Person.find(1).delete(:deactivate) - - # With nested resources - assert_equal StreetAddress.find(1, :params => { :person_id => 1 }).get(:deep), - { "id" => 1, "street" => '12345 Street', "zip" => "27519" } - assert_equal ActiveResource::Response.new("", 204, {}), - StreetAddress.find(1, :params => { :person_id => 1 }).put(:normalize_phone, :locale => 'US') - end - - def test_custom_new_element_method - # Test POST against a new element URL - ryan = Person.new(:name => 'Ryan') - assert_equal ActiveResource::Response.new(@ryan, 201, { 'Location' => '/people/5.json' }), ryan.post(:register) - expected_request = ActiveResource::Request.new(:post, '/people/new/register.json', @ryan) - assert_equal expected_request.body, ActiveResource::HttpMock.requests.first.body - - # Test POST against a nested collection URL - addy = StreetAddress.new(:street => '123 Test Dr.', :person_id => 1) - assert_equal ActiveResource::Response.new({ :address => { :street => '12345 Street' } }.to_json, - 201, { 'Location' => '/people/1/addresses/2.json' }), - addy.post(:link) - - matz = Person.find(1) - assert_equal ActiveResource::Response.new(@matz, 201), matz.post(:register) - end - - def test_find_custom_resources - assert_equal 'Matz', Person.find(:all, :from => :managers).first.name - end -end diff --git a/activeresource/test/cases/base/equality_test.rb b/activeresource/test/cases/base/equality_test.rb deleted file mode 100644 index fffd8b75c3..0000000000 --- a/activeresource/test/cases/base/equality_test.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/street_address" - -class BaseEqualityTest < ActiveSupport::TestCase - def setup - @new = Person.new - @one = Person.new(:id => 1) - @two = Person.new(:id => 2) - @street = StreetAddress.new(:id => 2) - end - - def test_should_equal_self - assert @new == @new, '@new == @new' - assert @one == @one, '@one == @one' - end - - def test_shouldnt_equal_new_resource - assert @new != @one, '@new != @one' - assert @one != @new, '@one != @new' - end - - def test_shouldnt_equal_different_class - assert @two != @street, 'person != street_address with same id' - assert @street != @two, 'street_address != person with same id' - end - - def test_eql_should_alias_equals_operator - assert_equal @new == @new, @new.eql?(@new) - assert_equal @new == @one, @new.eql?(@one) - - assert_equal @one == @one, @one.eql?(@one) - assert_equal @one == @new, @one.eql?(@new) - - assert_equal @one == @street, @one.eql?(@street) - end - - def test_hash_should_be_id_hash - [@new, @one, @two, @street].each do |resource| - assert_equal resource.id.hash, resource.hash - end - end - - def test_with_prefix_options - assert_equal @one == @one, @one.eql?(@one) - assert_equal @one == @one.dup, @one.eql?(@one.dup) - new_one = @one.dup - new_one.prefix_options = {:foo => 'bar'} - assert_not_equal @one, new_one - end - -end diff --git a/activeresource/test/cases/base/load_test.rb b/activeresource/test/cases/base/load_test.rb deleted file mode 100644 index f07e1ea16b..0000000000 --- a/activeresource/test/cases/base/load_test.rb +++ /dev/null @@ -1,199 +0,0 @@ -require 'abstract_unit' -require "fixtures/person" -require "fixtures/street_address" -require 'active_support/core_ext/hash/conversions' - -module Highrise - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - module Deeply - module Nested - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - class Comment < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - - module TestDifferentLevels - class Note < ActiveResource::Base - self.site = "http://37s.sunrise.i:3000" - end - end - end - end -end - - -class BaseLoadTest < ActiveSupport::TestCase - def setup - @matz = { :id => 1, :name => 'Matz' } - - @first_address = { :address => { :id => 1, :street => '12345 Street' } } - @addresses = [@first_address, { :address => { :id => 2, :street => '67890 Street' } }] - @addresses_from_json = { :street_addresses => @addresses } - @addresses_from_json_single = { :street_addresses => [ @first_address ] } - - @deep = { :id => 1, :street => { - :id => 1, :state => { :id => 1, :name => 'Oregon', - :notable_rivers => [ - { :id => 1, :name => 'Willamette' }, - { :id => 2, :name => 'Columbia', :rafted_by => @matz }], - :postal_codes => [ 97018, 1234567890 ], - :dates => [ Time.now ], - :votes => [ true, false, true ], - :places => [ "Columbia City", "Unknown" ]}}} - - - # List of books formated as [{timestamp_of_publication => name}, ...] - @books = {:books => [ - {1009839600 => "Ruby in a Nutshell"}, - {1199142000 => "The Ruby Programming Language"} - ]} - - @books_date = {:books => [ - {Time.at(1009839600) => "Ruby in a Nutshell"}, - {Time.at(1199142000) => "The Ruby Programming Language"} - ]} - @person = Person.new - end - - def test_load_hash_with_integers_as_keys - assert_nothing_raised{@person.load(@books)} - end - - def test_load_hash_with_dates_as_keys - assert_nothing_raised{@person.load(@books_date)} - end - - def test_load_expects_hash - assert_raise(ArgumentError) { @person.load nil } - assert_raise(ArgumentError) { @person.load '<person id="1"/>' } - end - - def test_load_simple_hash - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - end - - def test_after_load_attributes_are_accessible - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - assert_equal @matz[:name], @person.attributes['name'] - end - - def test_after_load_attributes_are_accessible_via_indifferent_access - assert_equal Hash.new, @person.attributes - assert_equal @matz.stringify_keys, @person.load(@matz).attributes - assert_equal @matz[:name], @person.attributes['name'] - assert_equal @matz[:name], @person.attributes[:name] - end - - def test_load_one_with_existing_resource - address = @person.load(:street_address => @first_address.values.first).street_address - assert_kind_of StreetAddress, address - assert_equal @first_address.values.first.stringify_keys, address.attributes - end - - def test_load_one_with_unknown_resource - address = silence_warnings { @person.load(@first_address).address } - assert_kind_of Person::Address, address - assert_equal @first_address.values.first.stringify_keys, address.attributes - end - - def test_load_collection_with_existing_resource - addresses = @person.load(@addresses_from_json).street_addresses - assert_kind_of Array, addresses - addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) - end - - def test_load_collection_with_unknown_resource - Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) - assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" - addresses = silence_warnings { @person.load(:addresses => @addresses).addresses } - assert Person.const_defined?(:Address), "Address should have been autocreated" - addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal @addresses.map { |a| a[:address].stringify_keys }, addresses.map(&:attributes) - end - - def test_load_collection_with_single_existing_resource - addresses = @person.load(@addresses_from_json_single).street_addresses - assert_kind_of Array, addresses - addresses.each { |address| assert_kind_of StreetAddress, address } - assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) - end - - def test_load_collection_with_single_unknown_resource - Person.__send__(:remove_const, :Address) if Person.const_defined?(:Address) - assert !Person.const_defined?(:Address), "Address shouldn't exist until autocreated" - addresses = silence_warnings { @person.load(:addresses => [ @first_address ]).addresses } - assert Person.const_defined?(:Address), "Address should have been autocreated" - addresses.each { |address| assert_kind_of Person::Address, address } - assert_equal [ @first_address.values.first ].map(&:stringify_keys), addresses.map(&:attributes) - end - - def test_recursively_loaded_collections - person = @person.load(@deep) - assert_equal @deep[:id], person.id - - street = person.street - assert_kind_of Person::Street, street - assert_equal @deep[:street][:id], street.id - - state = street.state - assert_kind_of Person::Street::State, state - assert_equal @deep[:street][:state][:id], state.id - - rivers = state.notable_rivers - assert_kind_of Array, rivers - assert_kind_of Person::Street::State::NotableRiver, rivers.first - assert_equal @deep[:street][:state][:notable_rivers].first[:id], rivers.first.id - assert_equal @matz[:id], rivers.last.rafted_by.id - - postal_codes = state.postal_codes - assert_kind_of Array, postal_codes - assert_equal 2, postal_codes.size - assert_kind_of Fixnum, postal_codes.first - assert_equal @deep[:street][:state][:postal_codes].first, postal_codes.first - assert_kind_of Numeric, postal_codes.last - assert_equal @deep[:street][:state][:postal_codes].last, postal_codes.last - - places = state.places - assert_kind_of Array, places - assert_kind_of String, places.first - assert_equal @deep[:street][:state][:places].first, places.first - - dates = state.dates - assert_kind_of Array, dates - assert_kind_of Time, dates.first - assert_equal @deep[:street][:state][:dates].first, dates.first - - votes = state.votes - assert_kind_of Array, votes - assert_kind_of TrueClass, votes.first - assert_equal @deep[:street][:state][:votes].first, votes.first - end - - def test_nested_collections_within_the_same_namespace - n = Highrise::Note.new(:comments => [{ :comment => { :name => "1" } }]) - assert_kind_of Highrise::Comment, n.comments.first - end - - def test_nested_collections_within_deeply_nested_namespace - n = Highrise::Deeply::Nested::Note.new(:comments => [{ :name => "1" }]) - assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first - end - - def test_nested_collections_in_different_levels_of_namespaces - n = Highrise::Deeply::Nested::TestDifferentLevels::Note.new(:comments => [{ :name => "1" }]) - assert_kind_of Highrise::Deeply::Nested::Comment, n.comments.first - end -end diff --git a/activeresource/test/cases/base/schema_test.rb b/activeresource/test/cases/base/schema_test.rb deleted file mode 100644 index d29eaf5fb6..0000000000 --- a/activeresource/test/cases/base/schema_test.rb +++ /dev/null @@ -1,411 +0,0 @@ -require 'abstract_unit' -require 'active_support/core_ext/hash/conversions' -require "fixtures/person" -require "fixtures/street_address" - -######################################################################## -# Testing the schema of your Active Resource models -######################################################################## -class SchemaTest < ActiveModel::TestCase - def setup - setup_response # find me in abstract_unit - end - - def teardown - Person.schema = nil # hack to stop test bleedthrough... - end - - ##################################################### - # Passing in a schema directly and returning it - #### - - test "schema on a new model should be empty" do - assert Person.schema.blank?, "should have a blank class schema" - assert Person.new.schema.blank?, "should have a blank instance schema" - end - - test "schema should only accept a hash" do - ["blahblah", ['one','two'], [:age, :name], Person.new].each do |bad_schema| - assert_raises(ArgumentError,"should only accept a hash (or nil), but accepted: #{bad_schema.inspect}") do - Person.schema = bad_schema - end - end - end - - test "schema should accept a simple hash" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema - end - - test "schema should accept a hash with simple values" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema - end - - test "schema should accept all known attribute types as values" do - # I'd prefer to use here... - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - assert_nothing_raised("should have accepted #{the_type.inspect}"){ Person.schema = {'my_key' => the_type }} - end - end - - test "schema should not accept unknown values" do - bad_values = [ :oogle, :blob, 'thing'] - - bad_values.each do |bad_value| - assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do - Person.schema = {'key' => bad_value} - end - end - end - - test "schema should accept nil and remove the schema" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - assert_equal new_schema, Person.schema # sanity check - - - assert_nothing_raised { Person.schema = nil } - assert_nil Person.schema, "should have nulled out the schema, but still had: #{Person.schema.inspect}" - end - - - test "schema should be with indifferent access" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - new_schema_syms = new_schema.keys - - assert_nothing_raised { Person.schema = new_schema } - new_schema_syms.each do |col| - assert Person.new.respond_to?(col.to_s), "should respond to the schema's string key, but failed on: #{col.to_s}" - assert Person.new.respond_to?(col.to_sym), "should respond to the schema's symbol key, but failed on: #{col.to_sym}" - end - end - - - test "schema on a fetched resource should return all the attributes of that model instance" do - p = Person.find(1) - s = p.schema - - assert s.present?, "should have found a non-empty schema!" - - p.attributes.each do |the_attr, val| - assert s.has_key?(the_attr), "should have found attr: #{the_attr} in schema, but only had: #{s.inspect}" - end - end - - test "with two instances, default schema should match the attributes of the individual instances - even if they differ" do - matz = Person.find(1) - rick = Person.find(6) - - m_attrs = matz.attributes.keys.sort - r_attrs = rick.attributes.keys.sort - - assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" - - assert_not_equal matz.schema, rick.schema, "should have had different schemas too" - end - - test "defining a schema should return it when asked" do - assert Person.schema.blank?, "should have a blank class schema" - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { - Person.schema = new_schema - assert_equal new_schema, Person.schema, "should have saved the schema on the class" - assert_equal new_schema, Person.new.schema, "should have made the schema available to every instance" - } - end - - test "defining a schema, then fetching a model should still match the defined schema" do - # sanity checks - assert Person.schema.blank?, "should have a blank class schema" - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - matz = Person.find(1) - assert !matz.schema.blank?, "should have some sort of schema on an instance variable" - assert_not_equal new_schema, matz.schema, "should not have the class-level schema until it's been added to the class!" - - assert_nothing_raised { - Person.schema = new_schema - assert_equal new_schema, matz.schema, "class-level schema should override instance-level schema" - } - end - - - ##################################################### - # Using the schema syntax - #### - - test "should be able to use schema" do - assert_respond_to Person, :schema, "should at least respond to the schema method" - - assert_nothing_raised("Should allow the schema to take a block") do - Person.schema { } - end - end - - test "schema definition should store and return attribute set" do - assert_nothing_raised do - s = nil - Person.schema do - s = self - attribute :foo, :string - end - assert_respond_to s, :attrs, "should return attributes in theory" - assert_equal({'foo' => 'string' }, s.attrs, "should return attributes in practice") - end - end - - test "should be able to add attributes through schema" do - assert_nothing_raised do - s = nil - Person.schema do - s = self - attribute('foo', 'string') - end - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal 'string', s.attrs['foo'], "should have saved the attribute type" - end - end - - test "should convert symbol attributes to strings" do - assert_nothing_raised do - s = nil - Person.schema do - attribute(:foo, :integer) - s = self - end - - assert s.attrs.has_key?('foo'), "should have saved the attribute name as a string" - assert_equal 'integer', s.attrs['foo'], "should have saved the attribute type as a string" - end - end - - test "should be able to add all known attribute types" do - assert_nothing_raised do - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - s = nil - Person.schema do - s = self - attribute('foo', the_type) - end - assert s.attrs.has_key?('foo'), "should have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" - end - end - end - - test "attributes should not accept unknown values" do - bad_values = [ :oogle, :blob, 'thing'] - - bad_values.each do |bad_value| - s = nil - assert_raises(ArgumentError,"should only accept a known attribute type, but accepted: #{bad_value.inspect}") do - Person.schema do - s = self - attribute 'key', bad_value - end - end - assert !self.respond_to?(bad_value), "should only respond to a known attribute type, but accepted: #{bad_value.inspect}" - assert_raises(NoMethodError,"should only have methods for known attribute types, but accepted: #{bad_value.inspect}") do - Person.schema do - send bad_value, 'key' - end - end - end - end - - - test "should accept attribute types as the type's name as the method" do - ActiveResource::Schema::KNOWN_ATTRIBUTE_TYPES.each do |the_type| - s = nil - Person.schema do - s = self - send(the_type,'foo') - end - assert s.attrs.has_key?('foo'), "should now have saved the attribute name" - assert_equal the_type.to_s, s.attrs['foo'], "should have saved the attribute type of: #{the_type}" - end - end - - test "should accept multiple attribute names for an attribute method" do - names = ['foo','bar','baz'] - s = nil - Person.schema do - s = self - string(*names) - end - names.each do |the_name| - assert s.attrs.has_key?(the_name), "should now have saved the attribute name: #{the_name}" - assert_equal 'string', s.attrs[the_name], "should have saved the attribute as a string" - end - end - - ##################################################### - # What a schema does for us - #### - - # respond_to? - - test "should respond positively to attributes that are only in the schema" do - new_attr_name = :my_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" - - assert_nothing_raised do - Person.schema = {new_attr_name.to_s => 'string'} - Person.schema { string new_attr_name_two } - end - - assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}" - assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" - end - - test "should not care about ordering of schema definitions" do - new_attr_name = :my_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert !Person.new.respond_to?(new_attr_name), "sanity check - should not respond to the brand-new attribute yet" - assert !Person.new.respond_to?(new_attr_name_two), "sanity check - should not respond to the brand-new attribute yet" - - assert_nothing_raised do - Person.schema { string new_attr_name_two } - Person.schema = {new_attr_name.to_s => 'string'} - end - - assert_respond_to Person.new, new_attr_name, "should respond to the attribute in a passed-in schema, but failed on: #{new_attr_name}" - assert_respond_to Person.new, new_attr_name_two, "should respond to the attribute from the schema, but failed on: #{new_attr_name_two}" - end - - # method_missing effects - - test "should not give method_missing for attribute only in schema" do - new_attr_name = :another_new_schema_attribute - new_attr_name_two = :another_new_schema_attribute - - assert Person.schema.blank?, "sanity check - should have a blank class schema" - - assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name} as a method") do - Person.new.send(new_attr_name) - end - assert_raises(NoMethodError, "should not have found the attribute: #{new_attr_name_two} as a method") do - Person.new.send(new_attr_name_two) - end - - Person.schema = {new_attr_name.to_s => :float} - Person.schema { string new_attr_name_two } - - assert_nothing_raised do - Person.new.send(new_attr_name) - Person.new.send(new_attr_name_two) - end - end - - - ######## - # Known attributes - # - # Attributes can be known to be attributes even if they aren't actually - # 'set' on a particular instance. - # This will only differ from 'attributes' if a schema has been set. - - test "new model should have no known attributes" do - assert Person.known_attributes.blank?, "should have no known attributes" - assert Person.new.known_attributes.blank?, "should have no known attributes on a new instance" - end - - test "setting schema should set known attributes on class and instance" do - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - - assert_equal new_schema.keys.sort, Person.known_attributes.sort - assert_equal new_schema.keys.sort, Person.new.known_attributes.sort - end - - test "known attributes on a fetched resource should return all the attributes of the instance" do - p = Person.find(1) - attrs = p.known_attributes - - assert attrs.present?, "should have found some attributes!" - - p.attributes.each do |the_attr, val| - assert attrs.include?(the_attr), "should have found attr: #{the_attr} in known attributes, but only had: #{attrs.inspect}" - end - end - - test "with two instances, known attributes should match the attributes of the individual instances - even if they differ" do - matz = Person.find(1) - rick = Person.find(6) - - m_attrs = matz.attributes.keys.sort - r_attrs = rick.attributes.keys.sort - - assert_not_equal m_attrs, r_attrs, "should have different attributes on each model" - - assert_not_equal matz.known_attributes, rick.known_attributes, "should have had different known attributes too" - end - - test "setting schema then fetching should add schema attributes to the instance attributes" do - # an attribute in common with fetched instance and one that isn't - new_schema = {'age' => 'integer', 'name' => 'string', - 'height' => 'float', 'bio' => 'text', - 'weight' => 'decimal', 'photo' => 'binary', - 'alive' => 'boolean', 'created_at' => 'timestamp', - 'thetime' => 'time', 'thedate' => 'date', 'mydatetime' => 'datetime'} - - assert_nothing_raised { Person.schema = new_schema } - - matz = Person.find(1) - known_attrs = matz.known_attributes - - matz.attributes.keys.each do |the_attr| - assert known_attrs.include?(the_attr), "should have found instance attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" - end - new_schema.keys.each do |the_attr| - assert known_attrs.include?(the_attr), "should have found schema attr: #{the_attr} in known attributes, but only had: #{known_attrs.inspect}" - end - end - - -end |