Merge branch '8567-moar-docker' refs #8567
[arvados.git] / services / api / test / unit / collection_test.rb
index d1cb9785938bec789c9ff7098d4747005f6b89a0..882e26059ced5a30df507143e0216e6695bc1e08 100644 (file)
@@ -370,21 +370,26 @@ class CollectionTest < ActiveSupport::TestCase
     end
   end
 
+  now = Time.now
   [['trash-to-delete interval negative',
     :collection_owned_by_active,
-    {trash_at: Time.now+2.weeks, delete_at: Time.now},
+    {trash_at: now+2.weeks, delete_at: now},
     {state: :invalid}],
-   ['trash-to-delete interval too short',
+   ['now-to-delete interval short',
     :collection_owned_by_active,
-    {trash_at: Time.now+3.days, delete_at: Time.now+7.days},
-    {state: :invalid}],
+    {trash_at: now+3.days, delete_at: now+7.days},
+    {state: :trash_future}],
+   ['now-to-delete interval short, trash=delete',
+    :collection_owned_by_active,
+    {trash_at: now+3.days, delete_at: now+3.days},
+    {state: :trash_future}],
    ['trash-to-delete interval ok',
     :collection_owned_by_active,
-    {trash_at: Time.now, delete_at: Time.now+15.days},
+    {trash_at: now, delete_at: now+15.days},
     {state: :trash_now}],
    ['trash-to-delete interval short, but far enough in future',
     :collection_owned_by_active,
-    {trash_at: Time.now+13.days, delete_at: Time.now+15.days},
+    {trash_at: now+13.days, delete_at: now+15.days},
     {state: :trash_future}],
    ['trash by setting is_trashed bool',
     :collection_owned_by_active,
@@ -392,11 +397,11 @@ class CollectionTest < ActiveSupport::TestCase
     {state: :trash_now}],
    ['trash in future by setting just trash_at',
     :collection_owned_by_active,
-    {trash_at: Time.now+1.week},
+    {trash_at: now+1.week},
     {state: :trash_future}],
    ['trash in future by setting trash_at and delete_at',
     :collection_owned_by_active,
-    {trash_at: Time.now+1.week, delete_at: Time.now+4.weeks},
+    {trash_at: now+1.week, delete_at: now+4.weeks},
     {state: :trash_future}],
    ['untrash by clearing is_trashed bool',
     :expired_collection,
@@ -416,7 +421,7 @@ class CollectionTest < ActiveSupport::TestCase
         end
         updates_ok = c.update_attributes(updates)
         expect_valid = expect[:state] != :invalid
-        assert_equal updates_ok, expect_valid, c.errors.full_messages.to_s
+        assert_equal expect_valid, updates_ok, c.errors.full_messages.to_s
         case expect[:state]
         when :invalid
           refute c.valid?
@@ -470,9 +475,8 @@ class CollectionTest < ActiveSupport::TestCase
   end
 
   test "move to trash in SweepTrashedCollections" do
-    uuid = 'zzzzz-4zz18-4guozfh77ewd2f0'
-    c = Collection.where('uuid=? and is_trashed=false', uuid).first
-    assert c
+    c = collections(:trashed_on_next_sweep)
+    refute_empty Collection.where('uuid=? and is_trashed=false', c.uuid)
     assert_raises(ActiveRecord::RecordNotUnique) do
       act_as_user users(:active) do
         Collection.create!(owner_uuid: c.owner_uuid,
@@ -480,8 +484,8 @@ class CollectionTest < ActiveSupport::TestCase
       end
     end
     SweepTrashedCollections.sweep_now
-    c = Collection.unscoped.find_by_uuid(uuid)
-    assert_equal true, c.is_trashed
+    c = Collection.unscoped.where('uuid=? and is_trashed=true', c.uuid).first
+    assert c
     act_as_user users(:active) do
       assert Collection.create!(owner_uuid: c.owner_uuid,
                                 name: c.name)
@@ -489,7 +493,23 @@ class CollectionTest < ActiveSupport::TestCase
   end
 
   test "delete in SweepTrashedCollections" do
-    uuid = 'zzzzz-4zz18-3u1p5umicfpqszp'
+    uuid = 'zzzzz-4zz18-3u1p5umicfpqszp' # deleted_on_next_sweep
+    assert_not_empty Collection.unscoped.where(uuid: uuid)
+    SweepTrashedCollections.sweep_now
+    assert_empty Collection.unscoped.where(uuid: uuid)
+  end
+
+  test "delete referring links in SweepTrashedCollections" do
+    uuid = collections(:trashed_on_next_sweep).uuid
+    act_as_system_user do
+      Link.create!(head_uuid: uuid,
+                   tail_uuid: system_user_uuid,
+                   link_class: 'whatever',
+                   name: 'something')
+    end
+    past = db_current_time
+    Collection.unscoped.where(uuid: uuid).
+      update_all(is_trashed: true, trash_at: past, delete_at: past)
     assert_not_empty Collection.unscoped.where(uuid: uuid)
     SweepTrashedCollections.sweep_now
     assert_empty Collection.unscoped.where(uuid: uuid)