4523: some more test cases
[arvados.git] / services / api / test / unit / collection_test.rb
1 require 'test_helper'
2
3 class CollectionTest < ActiveSupport::TestCase
4   def create_collection name, enc=nil
5     txt = ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:#{name}.txt\n"
6     txt.force_encoding(enc) if enc
7     return Collection.create(manifest_text: txt)
8   end
9
10   test 'accept ASCII manifest_text' do
11     act_as_system_user do
12       c = create_collection 'foo', Encoding::US_ASCII
13       assert c.valid?
14     end
15   end
16
17   test 'accept UTF-8 manifest_text' do
18     act_as_system_user do
19       c = create_collection "f\xc3\x98\xc3\x98", Encoding::UTF_8
20       assert c.valid?
21     end
22   end
23
24   test 'refuse manifest_text with invalid UTF-8 byte sequence' do
25     act_as_system_user do
26       c = create_collection "f\xc8o", Encoding::UTF_8
27       assert !c.valid?
28       assert_equal [:manifest_text], c.errors.messages.keys
29       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
30     end
31   end
32
33   test 'refuse manifest_text with non-UTF-8 encoding' do
34     act_as_system_user do
35       c = create_collection "f\xc8o", Encoding::ASCII_8BIT
36       assert !c.valid?
37       assert_equal [:manifest_text], c.errors.messages.keys
38       assert_match /UTF-8/, c.errors.messages[:manifest_text].first
39     end
40   end
41
42   test 'create and update collection and verify file_names' do
43     act_as_system_user do
44       c = create_collection 'foo', Encoding::US_ASCII
45       assert c.valid?
46       created_file_names = c.file_names
47       assert created_file_names
48       assert_match /foo.txt/, c.file_names
49
50       c.update_attribute 'manifest_text', ". d41d8cd98f00b204e9800998ecf8427e+0 0:0:foo2.txt\n"
51       assert_not_equal created_file_names, c.file_names
52       assert_match /foo2.txt/, c.file_names
53     end
54   end
55
56   [
57     [2**8, false],
58     [2**18, true],
59   ].each do |manifest_size, gets_truncated|
60     test "create collection with manifest size #{manifest_size} which gets truncated #{gets_truncated},
61           and not expect exceptions even on very large manifest texts" do
62       # file_names has a max size, hence there will be no errors even on large manifests
63       act_as_system_user do
64         manifest_text = './blurfl d41d8cd98f00b204e9800998ecf8427e+0'
65         index = 0
66         while manifest_text.length < manifest_size
67           manifest_text += ' ' + "0:0:veryverylongfilename000000000000#{index}.txt\n./subdir1"
68           index += 1
69         end
70         manifest_text += "\n"
71         c = Collection.create(manifest_text: manifest_text)
72
73         assert c.valid?
74         assert c.file_names
75         assert_match /veryverylongfilename0000000000001.txt/, c.file_names
76         assert_match /veryverylongfilename0000000000002.txt/, c.file_names
77         if !gets_truncated
78           assert_match /blurfl/, c.file_names
79           assert_match /subdir1/, c.file_names
80         end
81       end
82     end
83   end
84
85   [
86     ['foo', true],
87     ['foo bar', true],
88     ['foox barx', false],                               # no match for both
89     ['foox bar', true],                                 # bar matches
90     ['foo barx', true],
91     ['file2_in_subdir4', true],                         # whole string match
92     ['filex_in_subdir4', false],                        # looks for the whole string and fails
93     ['filex in subdir4', true],                         # matches subdir4
94     ['6a4ff0499484c6c79c95cd8c566bd25f+249025', true],
95     ['6a4ff0499484c6c79c95cd8c566bd25f+249024', false], # matches the whole string and fails
96     ['6a4ff0499484c6c79c95cd8', true],                  # prefix matches    
97     ['499484c6c79c95cd8c566bd', false],                 # not a prefix match
98     ['no-such-file', false],                            # looks for whole string and fails
99     ['no such file', true],                             # matches "file"
100   ].each do |search_filter, expect_results|
101     test "full text search collection for #{search_filter} and expect results #{expect_results}" do
102       search_filters = search_filter.split.each {|s| s.concat(':*')}
103       results = Collection.where("to_tsvector('english', translate(manifest_text, '/.', '  ')) @@ to_tsquery(?)",
104                                  "#{search_filters.join('|')}")
105       if expect_results
106         assert_equal true, results.length>0
107       else
108         assert_equal 0, results.length
109       end
110     end
111   end
112 end