class AixprtApp(App):
    """
    Main application, the central part of a kivy application.
    """
    # List of Workload objects
    workloads_obj = aixprt.get_workloads()

    # Names of all the workloads in the system
    workloads = aixprt.get_workloads().keys()

    # List of suites objects
    suites_obj = aixprt.load_suites()

    # Names of all the suites in the system
    suites = aixprt.load_suites().keys()

    # wl to edit on edit suite page
    wl_to_edit = None

    # List of suite statuses
    suite_status_list = []

    # Text to display on the message page
    message_text = ''

    # Passed to ProgressScreen to run
    suite_to_run = ''

    # Passed to ProgressScreen to run
    workload_to_run = ''

    # Number of workload iterations
    workload_iterations = 1

    # Count of workloads on the page
    wl_counter = 0

    # Page you just came from.  ScreenManager has a previous() function, but it only works based
    # on the order of the screens in the ScreenManager.  Thus, for dynamic screen returning we need to store
    # the name ourselves.
    source_page = ''

    # Need to manually refresh the workload list.  This does that every half-a-second
    def refresh_workloads(self):
        """
        Refresh the workloads in the App.
        """
        App.get_running_app().workloads_obj = aixprt.get_workloads()
        App.get_running_app().workloads = aixprt.get_workloads().keys()

    # Need to manually refresh the suites list.  This does that every half-a-second
    def refresh_suites(self):
        """
        Refresh the suites in the App.
        """
        App.get_running_app().suites_obj = aixprt.load_suites()
        App.get_running_app().suites = aixprt.load_suites().keys()

    Clock.schedule_interval(refresh_workloads, 0.5)
    Clock.schedule_interval(refresh_suites, 0.5)
def test_remove_workload_from_suite():
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2 
    # Check for target suite
    assert "Small_Suite" in SU_data
    assert len(SU_data["Small_Suite"]) == 2
    # Try to remove workloads from an index that is out of bounds
    assert aixprt.remove_workload_from_suite(-1, "Small_Suite") == 1
    assert len(SU_data["Small_Suite"]) == 2 
    assert aixprt.remove_workload_from_suite(2, "Small_Suite") == 1
    assert len(SU_data["Small_Suite"]) == 2 
    # Remove workloads from a suite sucessfully
    workload1 = {
            "name": "Small_Workload",
            "iterations": "1"
        }
    workload2 = {
            "name": "Smaller_Workload",
            "iterations": "2"
        }    
    assert workload2== aixprt.remove_workload_from_suite(1, "Small_Suite")
    SU_data = aixprt.load_suites()
    assert len(SU_data["Small_Suite"]) == 1
    assert workload1 == aixprt.remove_workload_from_suite(0, "Small_Suite")
    SU_data = aixprt.load_suites()
    assert len(SU_data["Small_Suite"]) == 0
    # Try to remove a workload from an empty suite 
    assert aixprt.remove_workload_from_suite(0, "Small_Suite") == 1
    SU_data = aixprt.load_suites()
    assert len(SU_data["Small_Suite"]) == 0
    # Try to remove from a suite that does not exist
    assert aixprt.remove_workload_from_suite(1, "Not_in_system") == 1
def test_remove_suite():
    # Expected suites removed 
    suite1 = {    
        "Small_Suite": [
            {
                "name": "Small_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            }
        ]
    }
    suite1_list = [
            {
                "name": "Small_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            }
        ]
    suite2 = {    
        "Smaller_Suite": [
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            }
        ]
    }
    suite2_list = [
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            }
        ]
    # Load Suites and ensure there are two suites in the file
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2    
    # Try to remove a suite that does not exist
    assert aixprt.remove_suite("Not_in_system") == 1
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2  
    # Remove the two suites that do exist 
    assert suite1_list == aixprt.remove_suite(suite1)    
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 1
    assert suite2_list == aixprt.remove_suite(suite2)    
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 0           
def test_edit_workload_in_suite():
    # Load suites
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2   
    # Check original iterations for workloads
    assert int(SU_data["Small_Suite"][0]['iterations']) == 1
    assert int(SU_data["Small_Suite"][1]['iterations']) == 2
    # Edit iterations
    aixprt.edit_workload_in_suite(0, 5, "Small_Suite")
    aixprt.edit_workload_in_suite(1, 3, "Small_Suite")
    SU_data = aixprt.load_suites()
    assert int(SU_data["Small_Suite"][0]['iterations']) == 5
    assert int(SU_data["Small_Suite"][1]['iterations']) == 3
    # Try to edit with invalid iterations
    assert aixprt.edit_workload_in_suite(0, -5, "Small_Suite") == 1
    assert aixprt.edit_workload_in_suite(1, 0, "Small_Suite") == 1
    SU_data = aixprt.load_suites()
    assert int(SU_data["Small_Suite"][0]['iterations']) == 5
    assert int(SU_data["Small_Suite"][1]['iterations']) == 3
    # Try to edit from a suite that does not exist
    assert aixprt.edit_workload_in_suite(0, 4, "Not_in_system") == 1
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2  
    # Try to edit from a suite with no workloads
    aixprt.remove_workload_from_suite(0, "Small_Suite")
    aixprt.remove_workload_from_suite(0, "Small_Suite")
    SU_data = aixprt.load_suites()
    assert len(SU_data["Small_Suite"]) == 0
    assert aixprt.edit_workload_in_suite(0, 4, "Small_Suite") == 1
    assert aixprt.edit_workload_in_suite(1, 2, "Small_Suite") == 1
def test_add_suite():
    # Load Suites and ensure there are only two suites in the file
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2
    # Add a new suite sucessfully
    new_suite = {
        "new_suite": [
            {
                "name": "test_workload(I_v3)",
                "iterations": "3"
            },
            {
                "name": "Small_Workload",
                "iterations": "2"
            }
        ]
    }
    new_suite_list = [
            {
                "name": "test_workload(I_v3)",
                "iterations": "3"
            },
            {
                "name": "Small_Workload",
                "iterations": "2"
            }
        ]
    assert aixprt.add_suite(new_suite) == 0
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 3
    assert new_suite_list == aixprt.get_suite("new_suite")
    # Now try to add that same suite again and ensure it is not added
    assert aixprt.add_suite(new_suite) == 1
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 3
    # Try to add a suite with a name that exceeds the character limit (20)
    long_suite = {
        "this_name_is_wayyyyyyyyyyyyyyyyyyyyyyyyyyy_longer_than_twenty_characters": [
            {
                "name": "test_workload(I_v3)",
                "iterations": "2"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            }
        ]
    }   
    assert aixprt.add_suite(long_suite) == 1
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 3   
def test_load_suites():
    # Suites.json copy to test against 
    test_SU_dict = {
        "Small_Suite": [
            {
                "name": "Small_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            }
        ],
        "Smaller_Suite": [
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            }
        ]
    }    
    # Run load_suites
    SU_data = aixprt.load_suites()
    assert test_SU_dict == SU_data
def test_is_workload_in_suites():
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2
    # Check for a workload not in any suites
    assert aixprt.is_workload_in_suites("test_workload(I_v3)") == 1    
    # Check for a workload in only 1 suite
    assert aixprt.is_workload_in_suites("Small_Workload") == [["Small_Suite", 0]]    
    # Check for a workload in multiple suites
    assert aixprt.is_workload_in_suites("Smaller_Workload") == [["Small_Suite", 1], ["Smaller_Suite", 0], ["Smaller_Suite", 1]] 
 def refresh_suites(self):
     """
     Refresh the suites in the App.
     """
     App.get_running_app().suites_obj = aixprt.load_suites()
     App.get_running_app().suites = aixprt.load_suites().keys()
def test_edit_suite():
    # Load Suites and ensure there are only two suites in the file
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2

    # Edit a suite sucessfully
    edited_suite1 = {    
        "Smaller_Suite": [
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            }
        ]
    }
    edited_suite_list1 = [
            {
                "name": "Smaller_Workload",
                "iterations": "2"
            },
            {
                "name": "Smaller_Workload",
                "iterations": "1"
            }
        ]
    assert aixprt.edit_suite("Smaller_Suite", edited_suite1) == 0
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2
    assert edited_suite_list1 == aixprt.get_suite("Smaller_Suite")

    # Edit a suite with a new name sucessfully 
    edited_suite2 = {    
        "New_Suite": [
            {
                "name": "Small_Workload",
                "iterations": "3"
            },
            {
                "name": "Small_Workload",
                "iterations": "1"
            }
        ]
    }
    edited_suite_list2 = [
            {
                "name": "Small_Workload",
                "iterations": "3"
            },
            {
                "name": "Small_Workload",
                "iterations": "1"
            }
        ]
    assert aixprt.edit_suite("Small_Suite", edited_suite2) == 0
    SU_data = aixprt.load_suites()
    assert len(SU_data) == 2
    assert edited_suite_list2 == aixprt.get_suite("New_Suite")

    # Try to edit a suite that doesn't exist 
    edited_suite3 = {    
        "Not_exist": [
            {
                "name": "Small_Workload",
                "iterations": "3"
            },
            {
                "name": "Small_Workload",
                "iterations": "1"
            }
        ]
    }
    assert aixprt.edit_suite("Not_in_system", edited_suite3) == 1
    assert len(SU_data) == 2

    # Try to edit a suite with a new name that is too long    
    assert aixprt.edit_suite("this_name_is_wayyyyyyyyyyyyyyyyyyyy_longer_than_twenty_characters", edited_suite3) == 1
    assert len(SU_data) == 2