File: test_dbase.tcl

package info (click to toggle)
tkrat 1%3A2.2cvs20100105-true-dfsg-6.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,340 kB
  • ctags: 9,259
  • sloc: ansic: 96,057; tcl: 25,667; makefile: 1,638; sh: 282
file content (140 lines) | stat: -rw-r--r-- 4,291 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
puts "$HEAD Test dbase operations"

namespace eval test_dbase {
    variable subjects
}

proc test_dbase::verify_search {search_exp expected} {
    variable subjects

    set fh [RatOpenFolder [list Dbase dbase {} remove +1 $search_exp]]
    set real [$fh list %s]

    set exp {}
    foreach s $expected {
        lappend exp [lindex $subjects $s]
    }
    if {$exp != $real} {
        puts [format "%-20s  %-20s" "Expected result" "Actual result"]
        for {set i 0} {$i < [llength $exp] || $i < [llength $real]} {incr i} {
            puts [format "%-20s  %-20s" [lindex $exp $i] [lindex $real $i]]
        }
        ReportError "Dbase search result mismatch"
    }
    $fh close
}

proc test_dbase::verify_dbinfo {dbinfo exp src} {
    global verbose

    if {$exp != $dbinfo} {
        if {$verbose} {
            puts "     Got: $dbinfo"
            puts "Expected: $exp"
        }
        ReportError "$src dbinfo mismatch"
    }
}

proc test_dbase::test_dbase {} {
    global option dir hdr
    variable subjects

    set option(dbase_dir) $dir/db
    file delete -force $option(dbase_dir)

    StartTest "Creating empty database"
    verify_search {or} {}

    StartTest "Inserting messages"
    # Prepare test messages
    set fn $dir/folder.[pid]
    set def [list Test file {} $fn]
    set fh [open $fn w]
    puts $fh $hdr
    for {set i 1} {$i < 21} {incr i} {
        upvar \#0 msg$i m
	puts $fh $m
    }
    close $fh
    set f1 [RatOpenFolder $def]
    set file_subjects [$f1 list %s]
    set dates [$f1 list %D]

    set exp {}
    set start_time [clock seconds]
    for {set i 0} {$i < 20} {incr i} {
        set msg [$f1 get $i]
        lappend subjects [lindex $file_subjects $i]
        set key [format "key%02d lock%02d" $i $i]
        RatInsert $msg $key +1 remove
        lappend exp $i
        verify_search {or} $exp
        verify_search {and} {}
    }
    $f1 close

    StartTest "Single keyword search"
    for {set i 0} {$i < 20} {incr i} {
        set key [format "key%02d" $i]
        verify_search [list or keywords $key] $i
        verify_search [list and keywords $key] $i
    }

    StartTest "Multi keyword search (or)"
    verify_search [list or keywords [list key01 key02]] {1 2}
    verify_search [list or keywords [list key03 key02]] {2 3}

    StartTest "Multi keyword search (and)"
    verify_search [list and keywords [list key03 lock03]] {3}

    StartTest "Time interval search"
    # We have to add an hour here because the $dates is in GMT
    set d2 [expr [lindex $dates 2]+3600]
    set d3 [expr [lindex $dates 3]+3600]
    verify_search [list int $d2 $d2 or] {2}
    verify_search [list int $d2 $d3 or] {2 3}
    verify_search [list int $d2 $d2 and] {}
    verify_search [list int $d2 $d3 and] {}

    StartTest "Time interval and keyword search"
    verify_search [list int $d2 $d3 and keywords key02] {2}
    verify_search [list int $d2 $d3 or keywords [list key02 key01]] {2}

    StartTest "Folder dbinfo"
    set search_exp [list or keywords [list key01 key02]]
    set fh [RatOpenFolder [list Dbase dbase {} remove +1 $search_exp]]
    verify_dbinfo [$fh dbinfo_get] "{key01 key02} 1 remove" "Folder"

    StartTest "Message dbinfo"
    set msg [$fh get 0]
    set expire [expr $start_time+24*60*60]
    verify_dbinfo [$msg dbinfo_get] "{key01 lock01} $expire remove" "Message"

    StartTest "Update expiration"
    set new_expire [expr $start_time+2*24*60*60]
    $fh dbinfo_set 0 "new_key" $new_expire backup

    StartTest "Folder dbinfo again"
    set search_exp [list or keywords [list key01 key02]]
    set fh [RatOpenFolder [list Dbase dbase {} remove +1 $search_exp]]
    verify_dbinfo [$fh dbinfo_get] "{key01 key02} 1 remove" "Folder"

    StartTest "Message dbinfo again"
    set msg [$fh get 0]
    verify_dbinfo [$msg dbinfo_get] "new_key $new_expire backup" "Message"

    StartTest "Update expiration for list"
    set new_expire [expr $start_time+2*24*60*60]
    $fh dbinfo_set {0 1} "newer_key" $new_expire backup

    StartTest "Message 0 dbinfo"
    set msg [$fh get 0]
    verify_dbinfo [$msg dbinfo_get] "newer_key $new_expire backup" "Message"

    StartTest "Message 1 dbinfo"
    set msg [$fh get 1]
    verify_dbinfo [$msg dbinfo_get] "newer_key $new_expire backup" "Message"
}

test_dbase::test_dbase