github.com/treeverse/lakefs@v1.24.1-0.20240520134607-95648127bfb0/clients/python-wrapper/tests/utests/test_reference.py (about)

     1  import lakefs_sdk
     2  
     3  from lakefs import ObjectInfo, CommonPrefix
     4  from lakefs.repository import Repository
     5  from tests.utests.common import get_test_client, expect_exception_context
     6  
     7  
     8  def get_test_ref():
     9      client = get_test_client()
    10      repo = Repository(repository_id="test_repo", client=client)
    11      return repo.ref("test_reference")
    12  
    13  
    14  def test_reference_creation():
    15      ref = get_test_ref()
    16      assert ref._repo_id == "test_repo"
    17      assert ref.id == "test_reference"
    18  
    19  
    20  def test_reference_log(monkeypatch):
    21      ref = get_test_ref()
    22      idx = 0
    23      pages = 10
    24      items_per_page = 100
    25  
    26      def monkey_log_commits(*_, **__):
    27          nonlocal idx
    28          results = []
    29          for pid in range(items_per_page):
    30              index = items_per_page * idx + pid
    31              results.append(lakefs_sdk.Commit(
    32                  id=str(index),
    33                  parents=[""],
    34                  committer="Committer-" + str(index),
    35                  message="Message-" + str(index),
    36                  creation_date=index,
    37                  meta_range_id="",
    38              ))
    39          idx += 1
    40          pagination = lakefs_sdk.Pagination(
    41              has_more=idx < pages,
    42              next_offset="",
    43              max_per_page=items_per_page,
    44              results=items_per_page
    45          )
    46  
    47          return lakefs_sdk.CommitList(
    48              pagination=pagination,
    49              results=results
    50          )
    51  
    52      with monkeypatch.context():
    53          monkeypatch.setattr(ref._client.sdk_client.refs_api, "log_commits", monkey_log_commits)
    54          i = 0
    55          # Test log entire history
    56          for i, c in enumerate(ref.log()):
    57              assert i == int(c.id)
    58  
    59          assert i + 1 == pages * items_per_page
    60  
    61          # Test log with limit
    62          idx = 0
    63          max_amount = 123
    64          assert len(list(ref.log(max_amount=max_amount))) == max_amount
    65  
    66          # Test limit more than amount
    67          idx = 0
    68          max_amount = pages * items_per_page * 2
    69          assert len(list(ref.log(max_amount=max_amount))) == pages * items_per_page
    70  
    71  
    72  def test_reference_diff(monkeypatch):
    73      ref = get_test_ref()
    74      idx = 0
    75      pages = 10
    76      items_per_page = 100
    77  
    78      def monkey_diff_refs(*_, **__):
    79          nonlocal idx
    80          results = []
    81          for pid in range(items_per_page):
    82              index = items_per_page * idx + pid
    83              results.append(lakefs_sdk.Diff(
    84                  type="added",
    85                  path=str(index),
    86                  path_type="object",
    87                  size_bytes=index,
    88              ))
    89          idx += 1
    90          pagination = lakefs_sdk.Pagination(
    91              has_more=idx < pages,
    92              next_offset="",
    93              max_per_page=items_per_page,
    94              results=items_per_page
    95          )
    96  
    97          return lakefs_sdk.DiffList(
    98              pagination=pagination,
    99              results=results
   100          )
   101  
   102      with monkeypatch.context():
   103          monkeypatch.setattr(ref._client.sdk_client.refs_api, "diff_refs", monkey_diff_refs)
   104          # Test log entire history
   105          i = 0
   106          for i, c in enumerate(ref.diff(other_ref="other_ref")):
   107              assert i == c.size_bytes
   108              assert i == int(c.path)
   109  
   110          assert i + 1 == pages * items_per_page
   111  
   112          # Test log with limit
   113          idx = 0
   114          max_amount = 123
   115          assert len(list(ref.diff(other_ref="other_ref", max_amount=max_amount))) == max_amount
   116  
   117          # Test limit more than amount
   118          idx = 0
   119          max_amount = pages * items_per_page * 2
   120          assert len(list(ref.diff(other_ref="other_ref", max_amount=max_amount))) == pages * items_per_page
   121  
   122  
   123  def test_reference_objects(monkeypatch):
   124      ref = get_test_ref()
   125      with monkeypatch.context():
   126          def monkey_list_objects(*_, **__):
   127              results = []
   128              for i in range(10):
   129                  if i % 2:
   130                      results.append(lakefs_sdk.ObjectStats(
   131                          path=f"path-{i}",
   132                          path_type="object",
   133                          physical_address=f"address-{i}",
   134                          checksum=f"{i}",
   135                          size_bytes=i,
   136                          mtime=i,
   137                      ))
   138                  else:
   139                      results.append(lakefs_sdk.ObjectStats(
   140                          path=f"path-{i}",
   141                          path_type="common_prefix",
   142                          physical_address="?",
   143                          checksum="",
   144                          mtime=i,
   145                      ))
   146              return lakefs_sdk.ObjectStatsList(pagination=lakefs_sdk.Pagination(
   147                  has_more=False,
   148                  next_offset="",
   149                  max_per_page=1,
   150                  results=1),
   151                  results=results)
   152  
   153          monkeypatch.setattr(ref._client.sdk_client.objects_api, "list_objects", monkey_list_objects)
   154  
   155          for i, item in enumerate(ref.objects()):
   156              if i % 2:
   157                  assert isinstance(item, ObjectInfo)
   158                  assert item.size_bytes == i
   159              else:
   160                  assert isinstance(item, CommonPrefix)
   161                  with expect_exception_context(AttributeError):
   162                      item.checksum  # pylint: disable=pointless-statement
   163  
   164              assert item.path == f"path-{i}"