Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / apps / workbench / test / unit / arvados_resource_list_test.rb
index a5681d24f4d9970b8af1a54af291f6457c4e9148..270b96203b249276be213c7a14b7346abf4487ca 100644 (file)
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 require 'test_helper'
 
 class ResourceListTest < ActiveSupport::TestCase
 
+  reset_api_fixtures :after_each_test, false
+
   test 'links_for on a resource list that does not return links' do
     use_token :active
     results = Specimen.all
     assert_equal [], results.links_for(api_fixture('users')['active']['uuid'])
   end
 
-  test 'links_for on non-empty resource list' do
+  test 'get all items by default' do
+    use_token :admin
+    a = 0
+    Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
+      a += 1
+    end
+    assert_equal 201, a
+  end
+
+  test 'prefetch all items' do
+    use_token :admin
+    a = 0
+    Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').each do
+      a += 1
+    end
+    assert_equal 201, a
+  end
+
+  test 'get limited items' do
+    use_token :admin
+    a = 0
+    Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(51).each do
+      a += 1
+    end
+    assert_equal 51, a
+  end
+
+  test 'get limited items, limit % page_size != 0' do
+    skip "Requires server MAX_LIMIT < 200 which is not currently the default"
+
+    use_token :admin
+    max_page_size = Collection.
+      where(owner_uuid: 'zzzzz-j7d0g-0201collections').
+      limit(1000000000).
+      fetch_multiple_pages(false).
+      count
+    # Conditions necessary for this test to be valid:
+    assert_operator 200, :>, max_page_size
+    assert_operator 1, :<, max_page_size
+    # Verify that the server really sends max_page_size when asked for max_page_size+1
+    assert_equal max_page_size, Collection.
+      where(owner_uuid: 'zzzzz-j7d0g-0201collections').
+      limit(max_page_size+1).
+      fetch_multiple_pages(false).
+      results.
+      count
+    # Now that we know the max_page_size+1 is in the middle of page 2,
+    # make sure #each returns page 1 and only the requested part of
+    # page 2.
+    a = 0
+    saw_uuid = {}
+    Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').limit(max_page_size+1).each do |item|
+      a += 1
+      saw_uuid[item.uuid] = true
+    end
+    assert_equal max_page_size+1, a
+    # Ensure no overlap between pages
+    assert_equal max_page_size+1, saw_uuid.size
+  end
+
+  test 'get single page of items' do
+    use_token :admin
+    a = 0
+    c = Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').fetch_multiple_pages(false)
+    c.each do
+      a += 1
+    end
+
+    assert_operator a, :<, 201
+    assert_equal c.result_limit, a
+  end
+
+  test 'get empty set' do
+    use_token :admin
+    c = Collection.
+      where(owner_uuid: 'doesn-texis-tdoesntexistdoe').
+      fetch_multiple_pages(false)
+    # Important: check c.result_offset before calling c.results here.
+    assert_equal 0, c.result_offset
+    assert_equal 0, c.items_available
+    assert_empty c.results
+  end
+
+  test 'count=none' do
     use_token :active
-    results = Group.find(api_fixture('groups')['afolder']['uuid']).contents(include_linked: true)
-    assert_equal [], results.links_for(api_fixture('users')['active']['uuid'])
-    assert_equal [], results.links_for(api_fixture('jobs')['running_cancelled']['uuid'])
-    assert_equal [], results.links_for(api_fixture('jobs')['running']['uuid'], 'bogus-link-class')
-    assert_equal true, results.links_for(api_fixture('jobs')['running']['uuid'], 'name').any?
-  end
-
-  test 'links_for returns all link classes (simulated results)' do
-    folder_uuid = api_fixture('groups')['afolder']['uuid']
-    specimen_uuid = api_fixture('specimens')['in_afolder']['uuid']
-    api_response = {
-      kind: 'arvados#specimenList',
-      links: [{kind: 'arvados#link',
-                uuid: 'zzzzz-o0j2j-asdfasdfasdfas0',
-                tail_uuid: folder_uuid,
-                head_uuid: specimen_uuid,
-                link_class: 'name',
-                name: 'Alice'},
-              {kind: 'arvados#link',
-                uuid: 'zzzzz-o0j2j-asdfasdfasdfas1',
-                tail_uuid: folder_uuid,
-                head_uuid: specimen_uuid,
-                link_class: 'foo',
-                name: 'Bob'},
-              {kind: 'arvados#link',
-                uuid: 'zzzzz-o0j2j-asdfasdfasdfas2',
-                tail_uuid: folder_uuid,
-                head_uuid: specimen_uuid,
-                link_class: nil,
-                name: 'Clydesdale'}],
-      items: [{kind: 'arvados#specimen',
-                uuid: specimen_uuid}]
-    }
-    arl = ArvadosResourceList.new
-    arl.results = ArvadosApiClient.new.unpack_api_response(api_response)
-    assert_equal(['name', 'foo', nil],
-                 (arl.
-                  links_for(specimen_uuid).
-                  collect { |x| x.link_class }),
-                 "Expected links_for to return all link_classes")
+    c = Collection.with_count('none')
+    assert_nil c.items_available
+    refute_empty c.results
   end
 
+  test 'cache results across each(&block) calls' do
+    use_token :admin
+    c = Collection.where(owner_uuid: 'zzzzz-j7d0g-0201collections').with_count('none')
+    c.each do |x|
+      x.description = 'foo'
+    end
+    found = 0
+    c.each do |x|
+      found += 1
+      # We should get the same objects we modified in the loop above
+      # -- not new objects built from another set of API responses.
+      assert_equal 'foo', x.description
+    end
+    assert_equal 201, found
+  end
 end