14844: Adds collections controller tests and updates collection model tests
authorEric Biagiotti <ebiagiotti@veritasgenetics.com>
Mon, 1 Apr 2019 18:49:42 +0000 (14:49 -0400)
committerEric Biagiotti <ebiagiotti@veritasgenetics.com>
Mon, 1 Apr 2019 18:49:42 +0000 (14:49 -0400)
Arvados-DCO-1.1-Signed-off-by: Eric Biagiotti <ebiagiotti@veritasgenetics.com>

services/api/test/functional/arvados/v1/collections_controller_test.rb
services/api/test/unit/collection_test.rb

index 997d89d5cd13d72c97dd3edcaa177bca36e1efed..3bcb1ec1b1acb8f4fbecdf237e18a08ee2398581 100644 (file)
@@ -937,6 +937,81 @@ EOS
     assert_equal 'value_1', json_response['properties']['property_1']
   end
 
+  [
+    [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", 1, 34],
+    [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:30:foo.txt 0:30:foo1.txt 0:30:foo2.txt 0:30:foo3.txt 0:30:foo4.txt\n", 5, 184],
+    [". d41d8cd98f00b204e9800998ecf8427e 0:0:.\n", 0, 0]
+  ].each do |manifest, count, size|
+    test "create collection with valid manifest #{manifest} and expect file stats" do
+      authorize_with :active
+      post :create, {
+        collection: {
+          manifest_text: manifest
+        }
+      }
+      assert_response 200
+      assert_equal count, json_response['file_count']
+      assert_equal size, json_response['file_size_total']
+    end
+  end
+
+  test "create collection with file stats and expect overwrite" do
+    authorize_with :active
+    post :create, {
+      collection: {
+        manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n",
+        file_count: 10,
+        file_size_total: 100
+      }
+    }
+    assert_response 200
+    assert_equal 1, json_response['file_count']
+    assert_equal 34, json_response['file_size_total']
+  end
+
+  test "update collection manifest and expect file stats" do
+    authorize_with :active
+    post :update, {
+      id: 'zzzzz-4zz18-bv31uwvy3neko21',
+      collection: {
+        manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n"
+      }
+    }
+    assert_response 200
+    assert_equal 1, json_response['file_count']
+    assert_equal 34, json_response['file_size_total']
+  end
+
+  test "update collection file count and expect error" do
+    authorize_with :active
+    post :update, {
+      id: 'zzzzz-4zz18-znfnqtbbv4spc3w',
+      collection: {
+        file_count: 10
+      }
+    }
+    assert_response 422
+    response_errors = json_response['errors']
+    assert_not_nil response_errors, 'Expected error in response'
+    assert(response_errors.first.include?('File count cannot be changed'),
+           "Expected file count error in #{response_errors.first}")
+  end
+
+  test "update collection file size and expect error" do
+    authorize_with :active
+    post :update, {
+      id: 'zzzzz-4zz18-znfnqtbbv4spc3w',
+      collection: {
+        file_size_total: 10
+      }
+    }
+    assert_response 422
+    response_errors = json_response['errors']
+    assert_not_nil response_errors, 'Expected error in response'
+    assert(response_errors.first.include?('File size total cannot be changed'),
+           "Expected file size total error in #{response_errors.first}")
+  end
+
   [
     ". 0:0:foo.txt",
     ". d41d8cd98f00b204e9800998ecf8427e foo.txt",
index a0fa3cac9fc9a44edce0f77002a0e27f1e660fea..8538b35b35c7ba20738144b166326ee7257e4df9 100644 (file)
@@ -64,31 +64,40 @@ class CollectionTest < ActiveSupport::TestCase
     [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", 1, 34],
     [". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:30:foo.txt 0:30:foo1.txt 0:30:foo2.txt 0:30:foo3.txt 0:30:foo4.txt\n", 5, 184],
     [". d41d8cd98f00b204e9800998ecf8427e 0:0:.\n", 0, 0]
-  ].each do |t|
-    test "file stats on create collection with #{t[0]}" do
+  ].each do |manifest, count, size|
+    test "file stats on create collection with #{manifest}" do
       act_as_system_user do
-        c = Collection.create(manifest_text: t[0])
-        assert_equal t[1], c.file_count
-        assert_equal t[2], c.file_size_total
+        c = Collection.create(manifest_text: manifest)
+        assert_equal count, c.file_count
+        assert_equal size, c.file_size_total
       end
     end
   end
 
   test "file stats cannot be changed unless through manifest change" do
     act_as_system_user do
+      # Changing file stats via an update should fail validation
       c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n")
-      assert c.valid?
       c.file_count = 6
       c.file_size_total = 30
       assert !c.valid?
       c.reload
       assert_equal 1, c.file_count
       assert_equal 34, c.file_size_total
-      c.update(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:10:foo.txt 0:10:foo2.txt\n")
-      assert c.valid?
+
+      # Changing the file stats via manifest change validates
+      c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt 0:34:foo2.txt\n")
       c.reload
+      assert c.valid?
       assert_equal 2, c.file_count
-      assert_equal 20, c.file_size_total
+      assert_equal 68, c.file_size_total
+
+      # Changing file stats at create, will silently overwrite
+      c = Collection.create(manifest_text: ". d41d8cd98f00b204e9800998ecf8427e 0:34:foo.txt\n", file_count: 10, file_size_total: 10)
+      c.reload
+      assert c.valid?
+      assert_equal 1, c.file_count
+      assert_equal 34, c.file_size_total
     end
   end