<commit_before>require 'spec_helper'
require 'active_support/core_ext/hash/indifferent_access'

module MongoModel
  describe Document do
    AttributeTypes = {
      String => "my string",
      Integer => 99,
      Float => 45.123,
      Symbol => :foobar,
      Boolean => false,
      Array => [ 1, 2, 3, "hello", :world, [99, 100] ],
      Hash => { :rabbit => 'hat', 'hello' => 12345 }.with_indifferent_access,
      Date => lambda { Date.today },
      Time => lambda { Time.now },
      CustomClass => CustomClass.new('hello')
    }
    
    AttributeTypes.each do |type, value|
      describe "setting #{type} attributes" do
        define_class(:TestDocument, Document) do
          property :test_property, type
        end
      
        before(:each) do
          @value = value.respond_to?(:call) ? value.call : value
        end
      
        subject { TestDocument.create!(:test_property => @value) }
      
        it "should read the correct value from attributes" do
          subject.test_property.should == @value
        end
      
        it "should read the correct value after reloading" do
          TestDocument.find(subject.id).test_property.should == subject.test_property
        end
      end
    end
    
    describe "setting attributes with hash" do
      define_class(:TestDocument, Document) do
        property :test_property, String
        
        def test_property=(value)
          write_attribute(:test_property, 'set from method')
        end
      end
      
      subject { TestDocument.new }<commit_msg>Attribute specs for Document and EmbeddedDocument<commit_after>require 'spec_helper'
require 'active_support/core_ext/hash/indifferent_access'

module MongoModel
  AttributeTypes = {
    String => "my string",
    Integer => 99,
    Float => 45.123,
    Symbol => :foobar,
    Boolean => false,
    Array => [ 1, 2, 3, "hello", :world, [99, 100] ],
    Hash => { :rabbit => 'hat', 'hello' => 12345 }.with_indifferent_access,
    Date => Date.today,
    Time => Time.now,
    CustomClass => CustomClass.new('hello')
  }
  
  specs_for(Document, EmbeddedDocument) do
    AttributeTypes.each do |type, value|
      describe "setting #{type} attributes" do
        define_class(:TestDocument, described_class) do
          property :test_property, type
        end
        
        if specing?(EmbeddedDocument)
          define_class(:ParentDocument, Document) do
            property :child, TestDocument
          end
          
          let(:parent) { ParentDocument.create!(:child => TestDocument.new(:test_property => value)) }
          let(:child) { parent.child }
          let(:reloaded) { ParentDocument.find(parent.id).child }
          
          subject { child }
        else
          subject { TestDocument.create!(:test_property => value) }
          
          let(:reloaded) { TestDocument.find(subject.id) }
        end
      
        
      
        it "should read the correct value from attributes" do
          subject.test_property.should == value
        end
      
        it "should read the correct value after reloading" do
          reloaded.test_property.should == subject.test_property
        end
      end
    end
    
    describe "setting attributes with hash" do
      define_class(:TestDocument, described_class) do
        property :test_property, String
        
        def test_property=(value)
          write_attribute(:test_property, 'set from method')
        end
      end
      
      subject { TestDocument.new }
{'fraction_non_alphanumeric': 0.10028490028490028, 'fraction_numerical': 0.011396011396011397, 'mean_word_length': 2.2181484876260313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1061378', 'n_tokens_mistral': 997, 'n_tokens_neox': 953, 'n_words': 253}