128 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
	
	
	
#!/usr/bin/env python3
 | 
						|
import unittest
 | 
						|
import requests
 | 
						|
from azure.identity import DefaultAzureCredential
 | 
						|
from devops import DEVOPS_SCOPE, Organization, Repository, Project, Item
 | 
						|
 | 
						|
# Get the token outside the test class to speed up tests.
 | 
						|
# Each Unit test instantinates the class, so doing it here avoids repeated authentication.
 | 
						|
token = DefaultAzureCredential().get_token(DEVOPS_SCOPE).token
 | 
						|
 | 
						|
class Test01(unittest.TestCase):
 | 
						|
    def setUp(self):
 | 
						|
        self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
 | 
						|
 | 
						|
    def test_01(self):
 | 
						|
        """Listing projects in the organization"""
 | 
						|
        self.assertGreater(len(list(self.org.projects)), 0)
 | 
						|
    def test_02(self):
 | 
						|
        """Getting a specific project by ID (object instantiation)"""
 | 
						|
        project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        self.assertEqual(project.id, "bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        self.assertEqual(project.name, "ADO Sandbox")
 | 
						|
    def test_03(self):
 | 
						|
        """Getting a specific project by name using org indexing (object retrieval)"""
 | 
						|
        project = self.org["ADO Sandbox"]
 | 
						|
        self.assertEqual(project.id, "bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        self.assertEqual(project.name, "ADO Sandbox")
 | 
						|
    def test_04(self):
 | 
						|
        """Getting a specific project by ID using org indexing (object retrieval)"""
 | 
						|
        project = self.org["bafe0cf1-6c97-4088-864a-ea6dc02b2727"]
 | 
						|
        self.assertEqual(project.id, "bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        self.assertEqual(project.name, "ADO Sandbox")
 | 
						|
 | 
						|
class Test02(unittest.TestCase):
 | 
						|
    def setUp(self):
 | 
						|
        self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
 | 
						|
 | 
						|
    def test_01(self):
 | 
						|
        """Listing repositories in a project"""
 | 
						|
        project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        repos = list(project.repositories)
 | 
						|
        self.assertGreater(len(repos), 0)
 | 
						|
 | 
						|
    def test_02(self):
 | 
						|
        """Getting a specific repository by ID (object instantiation)"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        self.assertEqual(repo.name, "ado-auth-lab")
 | 
						|
 | 
						|
    def test_03(self):
 | 
						|
        """Getting a specific repository by name using project indexing (object retrieval)"""
 | 
						|
        project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        repo = project["ado-auth-lab"]
 | 
						|
        self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        self.assertEqual(repo.name, "ado-auth-lab")
 | 
						|
 | 
						|
    def test_04(self):
 | 
						|
        """Getting a specific repository by ID using project indexing (object retrieval)"""
 | 
						|
        project = Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727")
 | 
						|
        repo = project["feac266f-84d2-41bc-839b-736925a85eaa"]
 | 
						|
        self.assertEqual(repo.id, "feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        self.assertEqual(repo.name, "ado-auth-lab")
 | 
						|
 | 
						|
class Test03(unittest.TestCase):
 | 
						|
    def setUp(self):
 | 
						|
        self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
 | 
						|
 | 
						|
    def test_01(self):
 | 
						|
        """Getting details of a specific item in a repository"""
 | 
						|
        item = Item(Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa"), path="/generate-pat.py")
 | 
						|
        self.assertEqual(item.path, "/generate-pat.py")
 | 
						|
        self.assertIsNotNone(item.commit_id)
 | 
						|
 | 
						|
    def test_02(self):
 | 
						|
        """Listing items in a folder within a repository"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        docs_item = Item(repo, path="/container")
 | 
						|
        children = list(docs_item.children)
 | 
						|
        self.assertGreater(len(children), 0)
 | 
						|
 | 
						|
    def test_03(self):
 | 
						|
        """Getting a specific item from a repository using indexing"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        item = repo["/container"]
 | 
						|
        self.assertEqual(item.path, "/container")
 | 
						|
        self.assertTrue(item.is_folder)
 | 
						|
 | 
						|
    def test_04(self):
 | 
						|
        """Getting a specific item from a repository using indexing"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        item = repo["/generate-pat.py"]
 | 
						|
        self.assertEqual(item.path, "/generate-pat.py")
 | 
						|
        self.assertFalse(item.is_folder)
 | 
						|
 | 
						|
    def test_05(self):
 | 
						|
        """Attempting to get a non-existent item from a repository using indexing"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        with self.assertRaises(KeyError):
 | 
						|
            repo["/non-existent-file.txt"] # This file does not exist
 | 
						|
 | 
						|
class Test04(unittest.TestCase):
 | 
						|
    def setUp(self):
 | 
						|
        self.org = Organization("https://dev.azure.com/mcovsandbox", token=token)
 | 
						|
 | 
						|
    def test_01(self):
 | 
						|
        """Getting details of a specific item in a repository"""
 | 
						|
        item = Item(Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa"), path="/generate-pat.py")
 | 
						|
        self.assertEqual(item.path, "/generate-pat.py")
 | 
						|
        self.assertIsNotNone(item.commit_id)
 | 
						|
 | 
						|
    def test_02(self):
 | 
						|
        """Trying to instantiate Item for a item that does not exist"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        with self.assertRaises(requests.exceptions.HTTPError):
 | 
						|
            item = Item(repo, path="/non-existent-file.txt")
 | 
						|
            self.assertEqual(item.path, "/non-existent-file.txt")
 | 
						|
            commit_id = item.commit_id # This will raise HTTPError when trying to fetch details of a non-existent item
 | 
						|
 | 
						|
    def test_03(self):
 | 
						|
        """Listing items in a folder within a repository"""
 | 
						|
        repo = Repository(Project(self.org, id="bafe0cf1-6c97-4088-864a-ea6dc02b2727"), id="feac266f-84d2-41bc-839b-736925a85eaa")
 | 
						|
        item = Item(repo, path="/container")
 | 
						|
        children = list(item.children)
 | 
						|
        self.assertGreater(len(children), 0)
 | 
						|
 | 
						|
if __name__ == "__main__":
 | 
						|
    unittest.main()
 |