def test_deploy(fs): MockedFiles._mockCorrectFileSystem(fs) def mock_return_None(*args, **kwargs): return None az = AzDevOps(org='foo', token='bar') az._create_project = MagicMock(return_value=mock_return_None) az._get_project = MagicMock(return_value=SimpleNamespace(id=1)) az._enable_epics = MagicMock(return_value=mock_return_None) az._create_work_item = MagicMock(return_value=mock_return_None) backlog = Backlog() config = backlog._getConfig('workitems/correct') work_items = backlog._buildWorkItems(MockedFiles._mockParsedFileList(), config) args = argparse.Namespace() args.org = 'foo' args.repo = 'testUser' args.project = 'testProject' args.backlog = 'correct' az.deploy(args, work_items) az._create_project.assert_called_with('testProject') az._enable_epics.assert_called() assert az._create_work_item.call_count == 20
def test_build_work_items(fs): backlog = helpers.Backlog() backlog._build_epic = MagicMock(return_value=None) work_items = backlog._build_work_items( MockedFiles._mock_parsed_file_list(), MockedFiles._mock_config()) backlog._build_epic.assert_any_call( MockedFiles._mock_parsed_file_list()[0], MockedFiles._mock_config()) backlog._build_epic.assert_any_call( MockedFiles._mock_parsed_file_list()[1], MockedFiles._mock_config()) backlog._build_epic.assert_any_call( MockedFiles._mock_parsed_file_list()[2], MockedFiles._mock_config()) assert work_items == [] epic = entities.Epic() epic.title = "Foobar" epic.description = "Some Description" backlog._build_epic = MagicMock(return_value=epic) work_items = backlog._build_work_items( [MockedFiles._mock_parsed_file_list()[0]], MockedFiles._mock_config()) assert len(work_items) == 1 assert work_items[0] == epic assert work_items[0].title == "Foobar" assert work_items[0].description == "Some Description"
def test_get_files_parent_path_has_file_file_system(fs): MockedFiles._mock_parent_path_has_file_file_system(fs) f = helpers.FileSystem() with pytest.raises(FileExistsError) as exc: files = f.get_files('./parentPathHasFile') # NOQA assert "parent path should not contain any files" in str(exc.value)
def test_deploy(fs): MockedFiles._mock_correct_file_system(fs) def mock_return_None(*args, **kwargs): return None az = AzDevOps(org='foo', token='bar') az._create_project = MagicMock(return_value=mock_return_None) az._get_project = MagicMock(return_value=SimpleNamespace(id=1)) az._enable_epics = MagicMock(return_value=mock_return_None) az._create_work_item = MagicMock(return_value=mock_return_None) backlog = Backlog() config = backlog._get_config('workitems/correct', 'azure') config["_repository_path"] = './workitems/correct' work_items = backlog._build_work_items( MockedFiles._mock_parsed_file_list(), config) args = argparse.Namespace() args.org = 'foo' args.repo = 'testUser' args.project = 'testProject' args.backlog = 'correct' az.deploy(args, work_items, config, []) az._create_project.assert_called_with('testProject', 'Sample description') az._enable_epics.assert_called() assert az._create_work_item.call_count == 20
def test_getConfig(monkeypatch, fs): MockedFiles._mockCorrectFileSystem(fs) def mockFileSystemReadFileReturnNone(*args, **kwargs): return None def mockParserJsonReturnJson(*args, **kwargs): content = None with open('./workitems/correct/config.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mockValidationValidateConfigReturnTrue(*args, **kwargs): return True def mockValidationValidateConfigRaiseError(*args, **kwargs): return (False, "there's an error") monkeypatch.setattr(helpers.FileSystem, "readFile", mockFileSystemReadFileReturnNone) monkeypatch.setattr(helpers.Parser, "json", mockParserJsonReturnJson) backlog = helpers.Backlog() monkeypatch.setattr(helpers.Validation, "validateConfig", mockValidationValidateConfigReturnTrue) assert backlog._getConfig('.') == mockParserJsonReturnJson() monkeypatch.setattr(helpers.Validation, "validateConfig", mockValidationValidateConfigRaiseError) with pytest.raises(ValueError) as exc: backlog._getConfig('.') assert "configuration file not valid: there's an error" in str(exc.value)
def test_getFiles_PathHasNoMetadata(fs): MockedFiles._mockPathHasNoMetadataFileSystem(fs) f = helpers.FileSystem() with pytest.raises(FileNotFoundError) as exc: files = f.getFiles('./pathHasNoMetadata') # NOQA assert "'metadata.json' does not exist in path './pathHasNoMetadata/01_folder'" in str(exc.value)
def test_buildFeature(fs): MockedFiles._mockCorrectFileSystem(fs) def mockGetConfigReturnConfig(*args, **kwargs): content = None with open('./workitems/correct/config.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mockParserJsonReturnFeatureJson(*args, **kwargs): content = None with open('./workitems/correct/01_epic/02_feature/metadata.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mockBacklogBuildStoryReturnStory(*args, **kwargs): story = entities.UserStory() story.title = "Some Story" story.description = "Some Description" return story def mockFeature(*args, **kwargs): return MockedFiles._mockParsedFileList()[0]["features"][0] backlog = helpers.Backlog() backlog._getAndValidateJson = MagicMock(return_value=False) feature = backlog._buildFeature(mockFeature(), mockGetConfigReturnConfig()) assert feature is None backlog._getAndValidateJson = MagicMock( return_value=mockParserJsonReturnFeatureJson()) backlog._buildStory = MagicMock(return_value=None) feature = backlog._buildFeature(mockFeature(), mockGetConfigReturnConfig()) assert feature.title == "Foo bar" assert feature.description == "Lorem Ipsum 01_folder/02_folder" assert len(feature.tags) == 3 assert Lists.contains(feature.tags, lambda tag: tag.title == "01_Folder") is True assert Lists.contains(feature.tags, lambda tag: tag.title == "02_Folder") is True assert Lists.contains(feature.tags, lambda tag: tag.title == "AppDev") is True assert len(feature.userStories) == 0 backlog._buildStory = MagicMock( return_value=mockBacklogBuildStoryReturnStory()) feature = backlog._buildFeature(mockFeature(), mockGetConfigReturnConfig()) assert len( feature.userStories ) == 2 # should return 2 instances of the mocked feature since the mocked feature has 2 user stories assert feature.userStories[0].title == "Some Story" assert feature.userStories[0].description == "Some Description"
def test_build_epic(fs): MockedFiles._mock_correct_file_system(fs) def mock_get_config_returns_config(*args, **kwargs): content = None with open('./workitems/correct/config.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mock_parser_parse_json_returns_epic_json(*args, **kwargs): content = None with open('./workitems/correct/01_epic/02_feature/metadata.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mock_backlog_build_feature_returns_feature(*args, **kwargs): feature = entities.Feature() feature.title = "Some Feature" feature.description = "Some Description" return feature backlog = helpers.Backlog() backlog._get_and_validate_json = MagicMock(return_value=False) epic = backlog._build_epic(MockedFiles._mock_parsed_file_list()[0], mock_get_config_returns_config()) assert epic is None backlog._get_and_validate_json = MagicMock( return_value=mock_parser_parse_json_returns_epic_json()) backlog._build_feature = MagicMock(return_value=None) epic = backlog._build_epic(MockedFiles._mock_parsed_file_list()[0], mock_get_config_returns_config()) assert epic.title == "Foo bar" assert epic.description == "Lorem Ipsum 01_folder/02_folder" assert len(epic.tags) == 3 assert Lists.contains(epic.tags, lambda tag: tag.title == "01_Folder") is True assert Lists.contains(epic.tags, lambda tag: tag.title == "02_Folder") is True assert Lists.contains(epic.tags, lambda tag: tag.title == "AppDev") is True assert len(epic.features) == 0 backlog._build_feature = MagicMock( return_value=mock_backlog_build_feature_returns_feature()) epic = backlog._build_epic(MockedFiles._mock_parsed_file_list()[0], mock_get_config_returns_config()) assert len( epic.features ) == 3 # should return 3 instances of the mocked feature since the mocked epic has 3 features assert epic.features[0].title == "Some Feature" assert epic.features[0].description == "Some Description"
def test_build_initialize_repo(fs, mock_Github): MockedFiles._mock_correct_file_system(fs) gh = GitHub(token='foo') gh.github = mock_Github repo = gh._get_user().create_repo() gh._initialize_repo(repo, './workitems/correct', [ './workitems/correct/README.md', './workitems/correct/feature_01/attachment.doc' ])
def test_initialize_repo(fs): az = AzDevOps(org='foo', token='bar') project = SimpleNamespace(id=1) path = './workitems/correct' MockedFiles._mock_correct_file_system(fs) attachments = MockedFiles._mock_file_list() az._initialize_repo('foo', project, path, attachments) assert az.clients.get_git_client.return_value.create_push.called assert az.connection._client.send.called
def test_gather_work_items(monkeypatch): def mock_gather_work_items_returns_file_list(*args, **kwargs): return MockedFiles._mock_file_list() monkeypatch.setattr(helpers.FileSystem, "get_files", mock_gather_work_items_returns_file_list) backlog = helpers.Backlog() assert backlog._gather_work_items('.') == MockedFiles._mock_file_list()
def test_deployAzure(fs): # patchedInit.return_value = None # patchedDeploy.return_value = None MockedFiles._mockCorrectFileSystem(fs) backlog = helpers.Backlog() config = backlog._getConfig('workitems/correct') workItems = backlog._buildWorkItems(MockedFiles._mockParsedFileList(), config) args = Namespace(org='testOrg', repo=None, project='testProject', backlog='correct', token='testToken') backlog._deployAzure(args, workItems)
def test_parseWorkItems(monkeypatch): def mockParseWorkItemsReturnFileList(*args, **kwargs): return MockedFiles._mockParsedFileList() monkeypatch.setattr(helpers.Parser, "fileHierarchy", mockParseWorkItemsReturnFileList) backlog = helpers.Backlog() assert backlog._parseWorkItems('.') == MockedFiles._mockParsedFileList()
def test_gatherWorkItems(monkeypatch): def mockGatherWorkItemsReturnFileList(*args, **kwargs): return MockedFiles._mockFileList() monkeypatch.setattr(helpers.FileSystem, "getFiles", mockGatherWorkItemsReturnFileList) backlog = helpers.Backlog() assert backlog._gatherWorkItems('.') == MockedFiles._mockFileList()
def test_validate_roles(): v = helpers.Validation() assert v._validate_roles( './somepath/metadata.json', {}, MockedFiles._mock_config() ) == (False, "'roles' property not found in metadata './somepath/metadata.json'") assert v._validate_roles('./somepath/metadata.json', { 'roles': 'lorem ipsum' }, MockedFiles._mock_config()) == ( False, "'roles' property is not in correct format in metadata './somepath/metadata.json'" ) assert v._validate_roles('./somepath/metadata.json', { 'roles': ['lorem ipsum'] }, MockedFiles._mock_config()) == ( False, "invalid role 'lorem ipsum' in metadata './somepath/metadata.json'") assert v._validate_roles('./somepath/metadata.json', {'roles': ['AppDev']}, MockedFiles._mock_config()) is True
def test_deployGitHub(patched_github, patched_deploy, fs): patched_github.return_value = MagicMock() patched_deploy.return_value = None MockedFiles._mockCorrectFileSystem(fs) backlog = helpers.Backlog() config = backlog._getConfig('workitems/correct') workItems = backlog._buildWorkItems(MockedFiles._mockParsedFileList(), config) args = Namespace(org='testOrg', repo=None, project='testProject', backlog='correct', token='testToken') backlog._deployGitHub(args, workItems) patched_deploy.assert_called_with(args, workItems)
def test_validateTags(): v = helpers.Validation() assert v._validateTags( './somepath/metadata.json', {}, MockedFiles._mockConfig()) == ( False, "'tags' property not found in metadata './somepath/metadata.json'") assert v._validateTags('./somepath/metadata.json', { 'tags': 'lorem ipsum' }, MockedFiles._mockConfig()) == ( False, "'tags' property is not in correct format in metadata './somepath/metadata.json'" ) assert v._validateTags( './somepath/metadata.json', {'tags': ['lorem ipsum']}, MockedFiles._mockConfig()) == ( False, "invalid tag 'lorem ipsum' in metadata './somepath/metadata.json'") assert v._validateTags('./somepath/metadata.json', {'tags': ['01_Folder']}, MockedFiles._mockConfig()) is True
def test_parse_work_items(monkeypatch): def mock_parse_work_items_returns_file_list(*args, **kwargs): return MockedFiles._mock_parsed_file_list() monkeypatch.setattr(helpers.Parser, "parse_file_hierarchy", mock_parse_work_items_returns_file_list) backlog = helpers.Backlog() assert backlog._parse_work_items( '.') == MockedFiles._mock_parsed_file_list()
def test_deploy_github(patched_github, patched_deploy, fs): patched_github.return_value = MagicMock() patched_deploy.return_value = None MockedFiles._mock_correct_file_system(fs) backlog = helpers.Backlog() config = backlog._get_config('workitems/correct', 'github') work_items = backlog._build_work_items( MockedFiles._mock_parsed_file_list(), config) args = Namespace(org='testOrg', repo=None, project='testProject', backlog='correct', token='testToken') backlog._deploy_github(args, work_items, config, []) patched_deploy.assert_called_with(args, work_items, config, [])
def test_deploy_azure(patched_deploy, fs, monkeypatch): monkeypatch.setattr(services.AzDevOps, "_auth", mock_auth) patched_deploy.return_value = None MockedFiles._mock_correct_file_system(fs) backlog = helpers.Backlog() config = backlog._get_config('workitems/correct', 'azure') work_items = backlog._build_work_items( MockedFiles._mock_parsed_file_list(), config) args = Namespace(org='testOrg', repo=None, project='testProject', backlog='correct', token='testToken') backlog._deploy_azure(args, work_items, config, []) patched_deploy.assert_called_with(args, work_items, config, [])
def test_build_task(fs): MockedFiles._mock_correct_file_system(fs) def mock_get_config_returns_config(*args, **kwargs): content = None with open('./workitems/correct/config.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mock_parser_parse_json_returns_task_json(*args, **kwargs): content = None with open('./workitems/correct/01_epic/02_feature/metadata.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mock_task(*args, **kwargs): return MockedFiles._mock_parsed_file_list( )[0]["features"][0]["stories"][0]["tasks"][0] backlog = helpers.Backlog() backlog._get_and_validate_json = MagicMock(return_value=False) task = backlog._build_task(mock_task(), mock_get_config_returns_config()) assert task is None backlog._get_and_validate_json = MagicMock( return_value=mock_parser_parse_json_returns_task_json()) task = backlog._build_task(mock_task(), mock_get_config_returns_config()) assert task.title == "Foo bar" assert task.description == "Lorem Ipsum 01_folder/02_folder" assert len(task.tags) == 3 assert Lists.contains(task.tags, lambda tag: tag.title == "01_Folder") is True assert Lists.contains(task.tags, lambda tag: tag.title == "02_Folder") is True assert Lists.contains(task.tags, lambda tag: tag.title == "AppDev") is True
def test_deploy_with_repo(fs): MockedFiles._mock_correct_file_system(fs) def mock_return_None(*args, **kwargs): return None gh = GitHub(token='foo') gh._create_org_repo = MagicMock(return_value=mock_return_None) gh._create_user_repo = MagicMock(return_value=mock_return_None) gh._initialize_repo = MagicMock(return_value=mock_return_None) gh._create_labels = MagicMock(return_value=mock_return_None) gh._delete_labels = MagicMock(return_value=mock_return_None) gh._create_project = MagicMock(return_value=mock_return_None) gh._create_columns = MagicMock(return_value=mock_return_None) gh._create_milestone = MagicMock(return_value=mock_return_None) gh._create_issue = MagicMock(return_value=mock_return_None) gh._create_card = MagicMock(return_value=mock_return_None) gh._build_description = MagicMock(return_value=mock_return_None) backlog = Backlog() config = backlog._get_config('workitems/correct', 'github') config["_repository_path"] = 'workitems/correct' work_items = backlog._build_work_items( MockedFiles._mock_parsed_file_list(), config) args = argparse.Namespace() args.org = None args.repo = 'testUser' args.project = 'testProject' args.backlog = 'correct' gh.deploy(args, work_items, config, []) gh._create_user_repo.assert_called_with('testProject', 'Sample description') gh._delete_labels.assert_called() assert gh._create_project.call_count == 4 assert gh._create_columns.call_count == 4 assert gh._create_milestone.call_count == 6 assert gh._create_issue.call_count == 4 assert gh._create_card.call_count == 4
def test_get_and_validate_json(monkeypatch, fs): MockedFiles._mock_correct_file_system(fs) def mock_file_system_read_file_returns_None(*args, **kwargs): return None def mock_parser_json_returns_json(*args, **kwargs): content = None with open('./workitems/correct/01_epic/metadata.json', 'r') as reader: content = reader.read() reader.close() return json.loads(content) def mock_validation_validate_metadata_returns_True(*args, **kwargs): return True def mock_validation_validate_metadata_returns_False(*args, **kwargs): return (False, "there's an error") monkeypatch.setattr(helpers.FileSystem, "read_file", mock_file_system_read_file_returns_None) monkeypatch.setattr(helpers.Parser, "parse_json", mock_parser_json_returns_json) backlog = helpers.Backlog() monkeypatch.setattr(helpers.Validation, "validate_metadata", mock_validation_validate_metadata_returns_True) assert backlog._get_and_validate_json( '.', MockedFiles._mock_config()) == mock_parser_json_returns_json() monkeypatch.setattr(helpers.Validation, "validate_metadata", mock_validation_validate_metadata_returns_False) with pytest.raises(ValueError) as exc: backlog._get_and_validate_json('.', MockedFiles._mock_config()) assert "metadata not valid: there's an error" in str(exc.value)
def test_deploy_withRepo(fs): MockedFiles._mockCorrectFileSystem(fs) def mock_returnNone(*args, **kwargs): return None gh = GitHub(token='foo') gh._createOrgRepo = MagicMock(return_value=mock_returnNone) gh._createUserRepo = MagicMock(return_value=mock_returnNone) gh._deleteLabels = MagicMock(return_value=mock_returnNone) gh._createProject = MagicMock(return_value=mock_returnNone) gh._createColumns = MagicMock(return_value=mock_returnNone) gh._createMilestone = MagicMock(return_value=mock_returnNone) gh._createIssue = MagicMock(return_value=mock_returnNone) gh._createCard = MagicMock(return_value=mock_returnNone) gh._buildDescription = MagicMock(return_value=mock_returnNone) backlog = Backlog() config = backlog._getConfig('workitems/correct') workItems = backlog._buildWorkItems(MockedFiles._mockParsedFileList(), config) args = argparse.Namespace() args.org = None args.repo = 'testUser' args.project = 'testProject' args.backlog = 'correct' gh.deploy(args, workItems) gh._createUserRepo.assert_called_with('testProject') gh._deleteLabels.assert_called() assert gh._createProject.call_count == 4 assert gh._createColumns.call_count == 4 assert gh._createMilestone.call_count == 6 assert gh._createIssue.call_count == 4 assert gh._createCard.call_count == 4
def test_validate_metadata(): v = helpers.Validation() assert v.validate_metadata( './somepath/metadata.json', None, MockedFiles._mock_config()) == ( False, "metadata in './somepath/metadata.json' is empty") v._validate_title = MagicMock(return_value=(False, "no title")) assert v.validate_metadata('./somepath/metadata.json', { 'description': 'lorem desc', 'tags': ['01_Folder'], 'roles': [] }, MockedFiles._mock_config()) == (False, "no title") v._validate_title = MagicMock(return_value=True) v._validate_description = MagicMock(return_value=(False, "no description")) assert v.validate_metadata('./somepath/metadata.json', { 'title': 'lorem ipsum', 'tags': ['01_Folder'], 'roles': [] }, MockedFiles._mock_config()) == (False, "no description") v._validate_title = MagicMock(return_value=True) v._validate_description = MagicMock(return_value=True) v._validate_tags = MagicMock(return_value=(False, "no tags")) assert v.validate_metadata('./somepath/metadata.json', { 'title': 'lorem ipsum', 'description': 'lorem desc', 'roles': [] }, MockedFiles._mock_config()) == (False, "no tags") v._validate_title = MagicMock(return_value=True) v._validate_description = MagicMock(return_value=True) v._validate_tags = MagicMock(return_value=True) v._validate_roles = MagicMock(return_value=(False, "no roles")) assert v.validate_metadata('./somepath/metadata.json', { 'title': 'lorem ipsum', 'description': 'lorem desc', 'tags': ['01_Folder'] }, MockedFiles._mock_config()) == (False, "no roles") v._validate_title = MagicMock(return_value=True) v._validate_description = MagicMock(return_value=True) v._validate_tags = MagicMock(return_value=True) v._validate_roles = MagicMock(return_value=True) assert v.validate_metadata( './somepath/metadata.json', { 'title': 'lorem ipsum', 'description': 'lorem desc', 'tags': ['01_Folder'], 'roles': [] }, MockedFiles._mock_config()) is True
def test_fileHierarchy(): p = helpers.Parser() parsedFiles = p.fileHierarchy(MockedFiles._mockFileList()) assert parsedFiles == MockedFiles._mockParsedFileList()
def test_get_files_correct_file_system(fs): MockedFiles._mock_correct_file_system(fs) f = helpers.FileSystem() files = f.get_files('./workitems/correct') assert len(files) == 23
def test_filter_attachments(fs): backlog = helpers.Backlog() result = backlog._filter_attachments(MockedFiles._mock_file_list()) assert len(result) == 3
def test_filter_work_items(fs): backlog = helpers.Backlog() result = backlog._filter_work_items(MockedFiles._mock_file_list()) assert len(result) == 20
def mock_parse_work_items_returns_file_list(*args, **kwargs): return MockedFiles._mock_parsed_file_list()