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

     1  import http
     2  
     3  import lakefs_sdk
     4  import pytest
     5  
     6  from tests.utests.common import get_test_client, expect_exception_context
     7  from lakefs.repository import Repository
     8  from lakefs.exceptions import ConflictException
     9  
    10  
    11  def get_test_branch():
    12      client = get_test_client()
    13      repo = Repository(repository_id="test_repo", client=client)
    14      return repo.branch("test_branch")
    15  
    16  
    17  def test_branch_creation():
    18      """
    19      Ensure branches can only be created in repo context
    20      """
    21      client = get_test_client()
    22      repo = Repository(repository_id="test_repo", client=client)
    23      branch = repo.branch("test_branch")
    24      assert branch.repo_id == "test_repo"
    25      assert branch.id == "test_branch"
    26  
    27  
    28  def test_branch_create(monkeypatch):
    29      branch = get_test_branch()
    30      source = "main"
    31      with monkeypatch.context():
    32          def monkey_create_branch(_self, repo_name, branch_creation, *_):
    33              assert repo_name == branch.repo_id
    34              assert branch_creation.name == branch.id
    35              assert branch_creation.source == source
    36              return lakefs_sdk.BranchCreation(name=branch.id, source=source)
    37  
    38          monkeypatch.setattr(lakefs_sdk.BranchesApi, "create_branch", monkey_create_branch)
    39          branch.create(source)
    40  
    41  
    42  def test_branch_create_already_exists(monkeypatch):
    43      branch = get_test_branch()
    44      source = "main"
    45      ex = lakefs_sdk.exceptions.ApiException(status=http.HTTPStatus.CONFLICT.value)
    46  
    47      with monkeypatch.context():
    48          def monkey_create_branch(_self, repo_name, branch_creation, *_):
    49              raise ex
    50  
    51          monkeypatch.setattr(lakefs_sdk.BranchesApi, "create_branch", monkey_create_branch)
    52  
    53          # Expect success when exist_ok = True
    54          res = branch.create(source, exist_ok=True)
    55          assert res.id == branch.id
    56          assert res.repo_id == branch.repo_id
    57  
    58          # Expect fail on exists
    59          with expect_exception_context(ConflictException):
    60              branch.create(source)
    61  
    62  
    63  def test_branch_head(monkeypatch):
    64      branch = get_test_branch()
    65      commit_id = "1234"
    66      with monkeypatch.context():
    67          def monkey_get_branch(repo_name, branch_name, *_):
    68              assert repo_name == branch.repo_id
    69              assert branch_name == branch.id
    70              return lakefs_sdk.Ref(commit_id=commit_id, id=branch.id)
    71  
    72          monkeypatch.setattr(branch._client.sdk_client.branches_api, "get_branch", monkey_get_branch)
    73          res = branch.head
    74          assert res.id == commit_id
    75          assert res.repo_id == branch.repo_id
    76  
    77  
    78  def test_branch_commit(monkeypatch):
    79      branch = get_test_branch()
    80      md = {"key": "value"}
    81      commit_id = "1234"
    82      commit_message = "test message"
    83      with monkeypatch.context():
    84          def monkey_commit(repo_name, branch_name, commits_creation, *_):
    85              assert repo_name == branch.repo_id
    86              assert branch_name == branch.id
    87              assert commits_creation.message == commit_message
    88              assert commits_creation.metadata == md
    89              assert commits_creation.allow_empty
    90              return lakefs_sdk.Commit(
    91                  id=commit_id,
    92                  parents=[""],
    93                  committer="Committer",
    94                  message=commit_message,
    95                  creation_date=123,
    96                  meta_range_id="",
    97              )
    98  
    99          monkeypatch.setattr(branch._client.sdk_client.commits_api, "commit", monkey_commit)
   100          res = branch.commit(commit_message, metadata=md, allow_empty=True, ignored_field="test")
   101          assert res.id == commit_id
   102  
   103  
   104  def test_branch_delete(monkeypatch):
   105      branch = get_test_branch()
   106      with monkeypatch.context():
   107          def monkey_delete_branch(repo_name, branch_name, *_):
   108              assert repo_name == branch.repo_id
   109              assert branch_name == branch.id
   110  
   111          monkeypatch.setattr(branch._client.sdk_client.branches_api, "delete_branch", monkey_delete_branch)
   112          branch.delete()
   113  
   114  
   115  def test_branch_revert(monkeypatch):
   116      branch = get_test_branch()
   117      ref_id = "ab1234"
   118      expected_parent = 0
   119      with monkeypatch.context():
   120          def monkey_revert_branch(repo_name, branch_name, revert_branch_creation, *_):
   121              assert repo_name == branch.repo_id
   122              assert branch_name == branch.id
   123              assert revert_branch_creation.ref == ref_id
   124              assert revert_branch_creation.parent_number == expected_parent  # default value
   125  
   126          def monkey_get_commit(repo_name, ref_name, **_):
   127              assert repo_name == branch.repo_id
   128              assert ref_name == branch.id
   129              return lakefs_sdk.Commit(
   130                  id=ref_id,
   131                  parents=[""],
   132                  committer="Committer",
   133                  message="Message",
   134                  creation_date=0,
   135                  meta_range_id=""
   136              )
   137  
   138          # Test default parent number
   139          monkeypatch.setattr(branch._client.sdk_client.branches_api, "revert_branch", monkey_revert_branch)
   140          monkeypatch.setattr(branch._client.sdk_client.commits_api, "get_commit", monkey_get_commit)
   141          branch.revert(ref_id)
   142          expected_parent = 2
   143          # Test set parent number
   144          branch.revert(ref_id, 2)
   145  
   146          # Test set invalid parent number
   147          with expect_exception_context(ValueError):
   148              branch.revert(ref_id, -1)
   149  
   150          expected_parent = 0
   151          # reference_id passed, but not reference
   152          with pytest.warns(DeprecationWarning, match="reference_id is deprecated.*"):
   153              branch.revert(None, reference_id=ref_id)
   154  
   155          # neither reference nor reference_id passed
   156          with pytest.raises(ValueError, match=".* must be specified"):
   157              branch.revert(None)
   158  
   159          # both are passed, prefer ``reference_id``
   160          with pytest.warns(DeprecationWarning, match="reference_id is deprecated.*"):
   161              # this is not a high-quality test, but it would throw if the revert API
   162              # was called with reference "hello" due to the monkey-patching above
   163              # always returning "ab1234" as ref ID.
   164              c = branch.revert(ref_id, reference_id="hello")
   165              assert c.id == ref_id