To edit pages or tickets please login with username/password: aaf/aaf

Changeset 14

Show
Ignore:
Timestamp:
03/12/06 11:22:45 (3 years ago)
Author:
jk
Message:

sqlite schema, added tests for rebuild_index and custom to_doc method

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/demo/app/models/comment.rb

    r10 r14  
     1require 'ferret' 
     2 
    13class Comment < ActiveRecord::Base 
    24  belongs_to :parent, :class_name => 'Content' 
     
    46  acts_as_ferret  
    57  #acts_as_ferret :fields => ['author', 'content' ] 
     8 
     9  # you can override the default to_doc method  
     10  # to customize what gets into your index.  
     11  def to_doc 
     12    # doc now has all the fields of our model instance, we  
     13    # just add another field to it: 
     14    doc = super 
     15    # add a field containing the current time 
     16    doc <<  Ferret::Document::Field.new( 
     17              'added', Time.now.to_i.to_s,  
     18              Ferret::Document::Field::Store::YES,  
     19              Ferret::Document::Field::Index::UNTOKENIZED) 
     20    return doc 
     21  end 
    622end 
  • trunk/demo/config/database.yml

    r5 r14  
    66# And be sure to use new-style password hashing: 
    77#   http://dev.mysql.com/doc/refman/5.0/en/old-client.html 
     8# 
     9# mysql config 
    810development: 
    911  adapter: mysql 
     
    2123 
    2224# since this is a demo, we won't need this one 
    23 production: 
    24   adapter: mysql 
    25   database: ferret_production 
    26   username: root 
    27   password:  
    28   socket: /var/run/mysqld/mysqld.sock 
     25#production: 
     26#  adapter: mysql 
     27#  database: ferret_production 
     28#  username: root 
     29#  password:  
     30#  socket: /var/run/mysqld/mysqld.sock 
    2931 
     32# SQLITE 
     33#development: 
     34#  adapter: sqlite3 
     35#  dbfile: db/dev.db 
     36 
     37#test: 
     38#  adapter: sqlite3 
     39#  dbfile: db/test.db 
     40 
  • trunk/demo/test/fixtures/comments.yml

    r5 r14  
    22first: 
    33  id: 1 
     4  content: comment from fixture 
    45another: 
    56  id: 2 
     7  content: second comment from fixture 
  • trunk/demo/test/test_helper.rb

    r5 r14  
    1616  # don't care one way or the other, switching from MyISAM to InnoDB tables 
    1717  # is recommended. 
    18   self.use_transactional_fixtures = true 
     18  #self.use_transactional_fixtures = true 
     19  self.use_transactional_fixtures = false 
    1920 
    2021  # Instantiated fixtures are slow, but give you @david where otherwise you 
  • trunk/demo/test/unit/comment_test.rb

    r10 r14  
    1111  def test_class_index_dir 
    1212    assert_equal "#{RAILS_ROOT}/index/test/Comment", Comment.class_index_dir 
     13  end 
     14 
     15  # tests the automatic building of an index when none exists 
     16  # delete index/test/* before running rake to make this useful 
     17  def test_index_rebuild 
     18    comments_from_ferret = Comment.find_by_contents('"comment from fixture"') 
     19    assert_equal 2, comments_from_ferret.size 
     20    assert comments_from_ferret.include?(comments(:first)) 
     21    assert comments_from_ferret.include?(comments(:another)) 
     22  end 
     23 
     24  # tests the custom to_doc method defined in comment.rb 
     25  def test_custom_to_doc 
     26    top_docs = Comment.ferret_index.search('"comment from fixture"') 
     27    assert_equal 2, top_docs.score_docs.size 
     28    doc = Comment.ferret_index.doc(top_docs.score_docs[0].doc) 
     29    # check for the special field added by the custom to_doc method 
     30    assert_not_nil doc[:added] 
     31    # still a valid int ? 
     32    assert doc[:added].to_i > 0 
    1333  end 
    1434 
     
    4565    # ...unless you connect them by OR 
    4666    comments_from_ferret = Comment.find_by_contents('monkey OR comment') 
    47     assert_equal 1, comments_from_ferret.size 
    48     assert_equal comment.id, comments_from_ferret.first.id 
     67    assert_equal 3, comments_from_ferret.size 
     68    assert comments_from_ferret.include?(comment) 
     69    assert comments_from_ferret.include?(comments(:first)) 
     70    assert comments_from_ferret.include?(comments(:another)) 
    4971 
    5072    # multiple terms, each term has to occur in a document to be found,  

To edit pages or tickets please login with username/password: aaf/aaf