~ubuntu-branches/ubuntu/saucy/dhelp/saucy-proposed

« back to all changes in this revision

Viewing changes to tmp/dhelp-ruby/test/tc_dhelpdbase.rb

  • Committer: Bazaar Package Importer
  • Author(s): Colin Watson
  • Date: 2008-06-19 01:25:07 UTC
  • Revision ID: james.westby@ubuntu.com-20080619012507-adt75omul1shucde
Tags: 0.6.9ubuntu1
* Resynchronise with Debian. Remaining changes:
  - Recommends: firefox-3.0.
  - Exit zero if the bdb module is not available; this usually indicates
    that dhelp is not configured yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
require 'test/unit'
2
 
require 'dhelp'
3
 
require 'fileutils'
4
 
 
5
 
class TC_DhelpDB < Test::Unit::TestCase
6
 
   TMP_DIR = 'test/tmp'
7
 
 
8
 
   def setup
9
 
      FileUtils.rm_r TMP_DIR if File.exists? TMP_DIR
10
 
      FileUtils.mkdir_p TMP_DIR
11
 
      @db = Dhelp::Database.open(BDB::CREATE|BDB::TRUNCATE, {}, 0644,
12
 
                                 File.join(TMP_DIR, 'dhelpbase'))
13
 
   end
14
 
 
15
 
   def test_insert
16
 
      @db.write(Dhelp::ItemData.new(:file    => 'foofile',
17
 
                                    :dir     => 'foodir',
18
 
                                    :name    => 'fooname',
19
 
                                    :descrip => 'Description for foo'))
20
 
      assert_equal(1, @db.keys.size,         "Number of keys")
21
 
   end
22
 
 
23
 
   def test_remove
24
 
      fooItem = Dhelp::ItemData.new(:file    => 'foofile',
25
 
                                    :dir     => 'foodir',
26
 
                                    :name    => 'fooname',
27
 
                                    :descrip => 'Description for foo')
28
 
      almostFooItem = Dhelp::ItemData.new(:file    => 'anotherfile',
29
 
                                          :dir     => 'foodir',
30
 
                                          :name    => 'fooname',
31
 
                                          :descrip => 'Description for foo')
32
 
      barItem = Dhelp::ItemData.new(:file    => 'barfile',
33
 
                                    :dir     => 'bardir',
34
 
                                    :name    => 'barname',
35
 
                                    :descrip => 'Description for bar')
36
 
      fooItemWithAltDesc = Dhelp::ItemData.new(:file    => 'foofile',
37
 
                                    :dir     => 'foodir',
38
 
                                    :name    => 'fooname',
39
 
                                    :descrip => 'Alternative description')
40
 
      @db.write(fooItem)
41
 
      @db.write(barItem)
42
 
      assert_equal(2, @db.keys.size,         "Number of keys")
43
 
      ret = @db.del(almostFooItem)
44
 
      assert_equal(nil, ret,                 "Deleting nothing")
45
 
      assert_equal(2, @db.keys.size,         "Number of keys after deleting nothing")
46
 
      ret = @db.del(fooItem)
47
 
      assert_not_nil(ret,                    "Really deleting one key")
48
 
      assert_equal(1, @db.keys.size,         "Number of keys after really deleting")
49
 
      # Add it again, check it's deleted with the same key, but different
50
 
      # description
51
 
      @db.write(fooItem)
52
 
      assert_equal(2, @db.keys.size,         "Number of keys after really deleting")
53
 
      ret = @db.del(fooItemWithAltDesc)
54
 
      assert_not_nil(ret,                    "Deleting same key, different data")
55
 
      assert_equal(1, @db.keys.size,         "Number of keys after really deleting")
56
 
   end
57
 
 
58
 
   def test_categories
59
 
      testCategories = %w(cat1 cat2 supercat supercow powers)
60
 
      numberEntries = 0
61
 
      testCategories.each_with_index do |cat, i|
62
 
         # A couple of items for each category
63
 
         0.upto(i) do |j|
64
 
            numberEntries += 1
65
 
            @db.write(Dhelp::ItemData.new(:file => "#{cat}#{j}",
66
 
                                          :dir  => cat,
67
 
                                          :name => "File #{j} for cat #{cat}",
68
 
                                          :descrip => "Long description for #{cat}/#{j}"))
69
 
         end
70
 
      end
71
 
      assert_equal(numberEntries, @db.size,  "Files in categories inserted")
72
 
 
73
 
      # Check that every category has the correct files
74
 
      @db.each_category do |cat, itemList|
75
 
         # If the category is in index n, then it has n+1 items
76
 
         size = testCategories.index(cat) + 1
77
 
         assert_equal(size, itemList.size,   "Number of categories in #{cat}")
78
 
      end
79
 
   end
80
 
 
81
 
   def teardown
82
 
      @db.close
83
 
      FileUtils.rm_rf TMP_DIR
84
 
   end
85
 
end