|
| 1 | +""" |
| 2 | +GitLab API: https://docs.gitlab.com/ce/api/bulk_imports.html |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | +import responses |
| 7 | + |
| 8 | +from gitlab.v4.objects import BulkImport, BulkImportAllEntity, BulkImportEntity |
| 9 | + |
| 10 | +migration_content = { |
| 11 | + "id": 1, |
| 12 | + "status": "finished", |
| 13 | + "source_type": "gitlab", |
| 14 | + "created_at": "2021-06-18T09:45:55.358Z", |
| 15 | + "updated_at": "2021-06-18T09:46:27.003Z", |
| 16 | +} |
| 17 | +entity_content = { |
| 18 | + "id": 1, |
| 19 | + "bulk_import_id": 1, |
| 20 | + "status": "finished", |
| 21 | + "source_full_path": "source_group", |
| 22 | + "destination_slug": "destination_slug", |
| 23 | + "destination_namespace": "destination_path", |
| 24 | + "parent_id": None, |
| 25 | + "namespace_id": 1, |
| 26 | + "project_id": None, |
| 27 | + "created_at": "2021-06-18T09:47:37.390Z", |
| 28 | + "updated_at": "2021-06-18T09:47:51.867Z", |
| 29 | + "failures": [], |
| 30 | +} |
| 31 | + |
| 32 | + |
| 33 | +@pytest.fixture |
| 34 | +def resp_create_bulk_import(): |
| 35 | + with responses.RequestsMock() as rsps: |
| 36 | + rsps.add( |
| 37 | + method=responses.POST, |
| 38 | + url="http://localhost/api/v4/bulk_imports", |
| 39 | + json=migration_content, |
| 40 | + content_type="application/json", |
| 41 | + status=201, |
| 42 | + ) |
| 43 | + yield rsps |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def resp_list_bulk_imports(): |
| 48 | + with responses.RequestsMock() as rsps: |
| 49 | + rsps.add( |
| 50 | + method=responses.GET, |
| 51 | + url="http://localhost/api/v4/bulk_imports", |
| 52 | + json=[migration_content], |
| 53 | + content_type="application/json", |
| 54 | + status=200, |
| 55 | + ) |
| 56 | + yield rsps |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture |
| 60 | +def resp_get_bulk_import(): |
| 61 | + with responses.RequestsMock() as rsps: |
| 62 | + rsps.add( |
| 63 | + method=responses.GET, |
| 64 | + url="http://localhost/api/v4/bulk_imports/1", |
| 65 | + json=migration_content, |
| 66 | + content_type="application/json", |
| 67 | + status=200, |
| 68 | + ) |
| 69 | + yield rsps |
| 70 | + |
| 71 | + |
| 72 | +@pytest.fixture |
| 73 | +def resp_list_all_bulk_import_entities(): |
| 74 | + with responses.RequestsMock() as rsps: |
| 75 | + rsps.add( |
| 76 | + method=responses.GET, |
| 77 | + url="http://localhost/api/v4/bulk_imports/entities", |
| 78 | + json=[entity_content], |
| 79 | + content_type="application/json", |
| 80 | + status=200, |
| 81 | + ) |
| 82 | + yield rsps |
| 83 | + |
| 84 | + |
| 85 | +@pytest.fixture |
| 86 | +def resp_list_bulk_import_entities(): |
| 87 | + with responses.RequestsMock() as rsps: |
| 88 | + rsps.add( |
| 89 | + method=responses.GET, |
| 90 | + url="http://localhost/api/v4/bulk_imports/1/entities", |
| 91 | + json=[entity_content], |
| 92 | + content_type="application/json", |
| 93 | + status=200, |
| 94 | + ) |
| 95 | + yield rsps |
| 96 | + |
| 97 | + |
| 98 | +@pytest.fixture |
| 99 | +def resp_get_bulk_import_entity(): |
| 100 | + with responses.RequestsMock() as rsps: |
| 101 | + rsps.add( |
| 102 | + method=responses.GET, |
| 103 | + url="http://localhost/api/v4/bulk_imports/1/entities/1", |
| 104 | + json=entity_content, |
| 105 | + content_type="application/json", |
| 106 | + status=200, |
| 107 | + ) |
| 108 | + yield rsps |
| 109 | + |
| 110 | + |
| 111 | +def test_create_bulk_import(gl, resp_create_bulk_import): |
| 112 | + configuration = { |
| 113 | + "url": gl.url, |
| 114 | + "access_token": "test-token", |
| 115 | + } |
| 116 | + migration_entity = { |
| 117 | + "source_full_path": "source", |
| 118 | + "source_type": "group_entity", |
| 119 | + "destination_slug": "destination", |
| 120 | + "destination_namespace": "destination", |
| 121 | + } |
| 122 | + migration = gl.bulk_imports.create( |
| 123 | + { |
| 124 | + "configuration": configuration, |
| 125 | + "entities": [migration_entity], |
| 126 | + } |
| 127 | + ) |
| 128 | + assert isinstance(migration, BulkImport) |
| 129 | + assert migration.status == "finished" |
| 130 | + |
| 131 | + |
| 132 | +def test_list_bulk_imports(gl, resp_list_bulk_imports): |
| 133 | + migrations = gl.bulk_imports.list() |
| 134 | + assert isinstance(migrations[0], BulkImport) |
| 135 | + assert migrations[0].status == "finished" |
| 136 | + |
| 137 | + |
| 138 | +def test_get_bulk_import(gl, resp_get_bulk_import): |
| 139 | + migration = gl.bulk_imports.get(1) |
| 140 | + assert isinstance(migration, BulkImport) |
| 141 | + assert migration.status == "finished" |
| 142 | + |
| 143 | + |
| 144 | +def test_list_all_bulk_import_entities(gl, resp_list_all_bulk_import_entities): |
| 145 | + entities = gl.bulk_import_entities.list() |
| 146 | + assert isinstance(entities[0], BulkImportAllEntity) |
| 147 | + assert entities[0].bulk_import_id == 1 |
| 148 | + |
| 149 | + |
| 150 | +def test_list_bulk_import_entities(gl, migration, resp_list_bulk_import_entities): |
| 151 | + entities = migration.entities.list() |
| 152 | + assert isinstance(entities[0], BulkImportEntity) |
| 153 | + assert entities[0].bulk_import_id == 1 |
| 154 | + |
| 155 | + |
| 156 | +def test_get_bulk_import_entity(gl, migration, resp_get_bulk_import_entity): |
| 157 | + entity = migration.entities.get(1) |
| 158 | + assert isinstance(entity, BulkImportEntity) |
| 159 | + assert entity.bulk_import_id == 1 |
0 commit comments