File: my-sequel_test.rb

package info (click to toggle)
tdiary 5.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,092 kB
  • sloc: ruby: 23,031; javascript: 1,029; xml: 325; makefile: 26; sh: 2
file content (238 lines) | stat: -rw-r--r-- 6,411 bytes parent folder | download | duplicates (6)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
require File.expand_path('../test_helper', __FILE__)
require 'my-sequel'
require 'fileutils'
require 'tmpdir'

# test cases for updates of links: run this file as a Ruby script
class TestMySequel < Test::Unit::TestCase
	OrigLinks = {
		'20061231#p01' => '20061101#p01',
		'20061231#p02' => '20061001#p01',
		'20061130#p01' => '20061001#p01',
	}

	def setup
		@cache_path = File.join(Dir.tmpdir, "#{__FILE__}-#{$$}")
		FileUtils.mkdir_p(@cache_path)

		@orig_src_dates = OrigLinks.keys.map{|a| MySequel.date(a)}
		@orig_dst_dates = OrigLinks.values.map{|a| MySequel.date(a)}
		orig = MySequel.new(@cache_path)
		orig.restore(@orig_src_dates)
		@orig_src_dates.each do |date|
			orig.clean_dsts(Time.local(*(date.scan(/(\d{4,4})(\d\d)(\d\d)/)[0])))
		end
		OrigLinks.each_pair do |src, dst|
			orig.add(src, dst)
		end
		orig.clean_srcs
		orig.commit
	end

	def testsrcs	# confirms setup really cached the OrigLinks
		cached = MySequel.new(@cache_path)
		cached.restore(@orig_dst_dates)
		OrigLinks.values.uniq.each do |dst|
			srcs = OrigLinks.find_all{|e| dst == e[1]}.map{|e| e[0]}
			assert_equal(srcs.sort, cached.srcs(dst).sort)
		end
	end

	def testadd	# confirms addition of a day
		# write the diary for 2007-01-01
		cached = MySequel.new(@cache_path)
		cached.restore(@orig_dst_dates)
		cached.clean_dsts(Time.local(2007,1,1))
		cached.add('20070101#p01', '20060101#p01')
		assert_equal(['20070101#p01'], cached.srcs('20060101#p01'))
		cached.clean_srcs
		cached.commit
		# display the diary for 2006-01-01
		cached = MySequel.new(@cache_path)
		cached.restore(['20060101'])
		assert_equal(['20070101#p01'], cached.srcs('20060101#p01'))
		# confirmation of other links
		testsrcs
	end

	def testadd_two_months	# http://zunda.freeshell.org/d/20070122.html#c01
		# write the diary for 2007-02-15
		cached = MySequel.new(@cache_path)
		cached.restore('20070215')
		cached.clean_dsts(Time.local(2007,2,15))
		cached.add('20070215#p01', '20070115#p01')
		cached.clean_srcs
		cached.commit
		# write the diary for 2007-03-10
		cached = MySequel.new(@cache_path)
		cached.restore('20070310')
		cached.clean_dsts(Time.local(2007,3,10))
		cached.add('20070310#p01', '20070115#p01')
		cached.clean_srcs
		cached.commit
		# display the diary on 2007-01-15
		cached = MySequel.new(@cache_path)
		cached.restore('20070115')
		assert_equal(['20070215#p01', '20070310#p01'], cached.srcs('20070115#p01'))

	end

	def testedit	# confirms edition of a link
		# edit the diary for 2006-11-31
		cached = MySequel.new(@cache_path)
		cached.restore(@orig_dst_dates)
		cached.clean_dsts(Time.local(2006,11,30))
		cached.add('20061130#p01', '20060901#p01')
		cached.clean_srcs
		cached.commit
		# display the diary on 2006-09-01
		cached = MySequel.new(@cache_path)
		cached.restore(['20060901'])
		assert_equal(['20061130#p01'], cached.srcs('20060901#p01'))
		# display the diary on 2006-10-01
		cached.restore(['20061001'])
		assert_equal(['20061231#p02'], cached.srcs('20061001#p01'))
	end

	def testdelete	# confirms deletion of a link
		# edit the diary for 2006-11-31
		cached = MySequel.new(@cache_path)
		cached.restore(@orig_dst_dates)
		cached.clean_dsts(Time.local(2006,11,30))
		cached.clean_srcs
		cached.commit
		# display the diary on 2006-10-01
		cached.restore(['20061001'])
		assert_equal(['20061231#p02'], cached.srcs('20061001#p01'))
	end

	def teardown
		FileUtils.rmtree(@cache_path)
	end
end

class TestMySequelCss < Test::Unit::TestCase
	def test_usual
		assert_equal(<<"_TARGET", MySequel::css(<<'_INNER'))
\t<style type="text/css" media="all"><!--
\tdiv.sequel {
\t\thogehoge: &lt;mogemoge&gt;
\t}
\t--></style>
_TARGET
hogehoge: <mogemoge>
_INNER
	end

	def test_empty
		assert_equal('', MySequel::css(''))
	end

	def test_space
		assert_equal('', MySequel::css(' '))
	end

	def test_crlf
		assert_equal('', MySequel::css("\r\n"))
	end

end

class TestMySequelConf < Test::Unit::TestCase
	include ERB::Util

	def setup
		@defaults = {
			:label => {
				:title => 'Link label',
				:default => 'default label',
				:index => 1,
			},
			:format => {
				:title => 'Date format<sup>*</sup>',
				:description => 'Format of the dates of the link',
				:default => '<date>',
				:index => 2,
			},
			:textarea => {
				:title => 'Test to show text area',
				:default => "a\nb\ncc",
				:index => 3,
				:textarea => true,
			},
			:textarea_with_size => {
				:title => 'Test to show text area',
				:default => "a\nb\ncc",
				:index => 4,
				:textarea => {rows: 2},
			}
		}
		@my_sequel_conf = MySequel::Conf.new(@defaults)
	end

	def testdefaults	# retrieve default configuration
		assert_equal('default label', @my_sequel_conf[:label])
	end

	def testmerge
		options = {'my_sequel.label' => 'configured label'}
		@my_sequel_conf.merge_hash(options)
		assert_equal('configured label', @my_sequel_conf[:label])
	end

	def testparams
		options = {'label' => ['configured label']}
		@my_sequel_conf.merge_params(options)
		assert_equal('configured label', @my_sequel_conf[:label])
	end

	def testparams_with_empty_array
		options = {'label' => ['configured label'], 'label.reset' => []}
		@my_sequel_conf.merge_params(options)
		assert_equal('configured label', @my_sequel_conf[:label])
	end

	def testtohash
		testmerge
		conf_hash = {'dummy' => 'dummy'}
		@my_sequel_conf.to_conf_hash(conf_hash)
		assert_equal({'my_sequel.label' => 'configured label', 'dummy' => 'dummy'}, conf_hash)
	end

	def testparams_with_empty
		testmerge
		options = {'label' => ['']}
		@my_sequel_conf.merge_params(options)
		assert_equal('', @my_sequel_conf[:label])
	end

	def testparams_with_reset
		testmerge
		options = {'label' => ['any value'], 'label.reset' => 't'}
		@my_sequel_conf.merge_params(options)
		assert_equal('default label', @my_sequel_conf[:label])
	end

	def test_delete_confhash
		options = {'my_sequel.label' => 'configured label'}
		@my_sequel_conf.merge_hash(options)
		params = {'label' => ['any value'],'label.reset' => 't'}
		@my_sequel_conf.merge_params(params)
		@my_sequel_conf.to_conf_hash(options)
		assert(!options.has_key?('my_sequel.label'))
	end

	def testparams_with_nil
		testmerge
		options = {'label' => nil}
		@my_sequel_conf.merge_params(options)
		assert_equal('configured label', @my_sequel_conf[:label])
	end

	def testparams_with_nokey
		testmerge
		options = {}
		@my_sequel_conf.merge_params(options)
		assert_equal('configured label', @my_sequel_conf[:label])
	end
end