<commit_before>"""
tests for make_magic
"""

import wx
import unittest
import os
#import ErMagicBuilder
import make_magic
import builder
import grid_frame
import pmag_widgets

WD = os.getcwd()

class TestMainFrame(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #WD = os.path.join(os.getcwd(), 'unittests', 'examples', 'my_project')
        self.frame = make_magic.MainFrame(WD, "zebra")
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        for wind in wx.GetTopLevelWindows():
            wind.Destroy()
            del wind
        self.app.Destroy()
        os.chdir(WD)

    def test_main_panel_is_created(self):
        """
        test for existence of main panel
        """
        self.assertTrue(self.pnl.IsEnabled())
        self.assertEqual("main panel", str(self.pnl.GetName()))

    def test_data_object_is_created(self):
        self.assertTrue(self.frame.er_magic)

        self.assertFalse(self.frame.er_magic.specimens)
        self.assertFalse(self.frame.er_magic.samples)
        self.assertFalse(self.frame.er_magic.sites)
        self.assertFalse(self.frame.er_magic.locations)

        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][0])
        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][1])
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][0])
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][1])

    def test_pmag_results(self):
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][0])#pmag_results_header)
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][1])#pmag_results_reqd_header)

    def test_specimen_button(self):
        window = self.does_top_window_exist(self.pnl, 'specimen_btn', 'specimen')
        self.assertTrue(window, 'er_specimens grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_sample_button(self):
        window = self.does_top_window_exist(self.pnl, 'sample_btn', 'sample')
        self.assertTrue(window, 'er_samples grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_site_button(self):
        window = self.does_top_window_exist(self.pnl, 'site_btn', 'site')
        self.assertTrue(window, 'er_sites grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_location_button(self):
        window = self.does_top_window_exist(self.pnl, 'location_btn', 'location')
        self.assertTrue(window, 'er_locations grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_ages_button(self):
        window = self.does_top_window_exist(self.pnl, 'age_btn', 'age')
        self.assertTrue(window, 'er_ages grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_results_button(self):
        window = self.does_top_window_exist(self.pnl, 'result_btn', 'result')
        self.assertTrue(window, 'er_results grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())


    def does_window_exist(self, parent, btn_name, window_name):
        """
        produces a click event on the button called btn_name,
        see if it produces the window called window_name
        """
        btn, window = None, None
        children = parent.GetChildren()
        for child in children:
            if child.GetName() == btn_name:
                btn = child
                break
        if not btn:
            return None
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
        btn.GetEventHandler().ProcessEvent(event)
        for child in parent.GetChildren():
            if child.GetName() == window_name and not isinstance(child, wx.lib.buttons.GenButton):
                window = child
                break
        if not window:
            return None
        else:
            return window


    def does_top_window_exist(self, parent, btn_name, window_name):
        """
        produces a click event on the button called btn_name, 
        see if it produces a top-level window called window_name
        """
        btn = None
        children = parent.GetChildren()
        for child in children:
            if child.GetName() == btn_name:
                btn = child
                break
        if not btn:
            return None
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
        btn.GetEventHandler().ProcessEvent(event)
        for wind in wx.GetTopLevelWindows():
            if wind.GetName() == window_name:
                return wind
        return None

class TestMainFrameWithData(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #WD = os.path.join(os.getcwd(), 'unittests', 'examples', 'my_project')
        self.frame = make_magic.MainFrame(os.path.join(WD, 'Datafiles', 'copy_ErMagicBuilder'))
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        self.app.Destroy()
        os.chdir(WD)

    def test_main_panel_is_created(self):
        """
        test for existence of main panel
        """
        self.assertTrue(self.pnl.IsEnabled())
        self.assertEqual("main panel", str(self.pnl.GetName()))

    def test_data_object_is_created(self):
        self.assertTrue(self.frame.er_magic)
        #self.assertTrue
        #self.assertTrue(self.frame.er_magic.specimens)
        

        self.assertTrue(self.frame.er_magic.specimens)
        self.assertTrue(self.frame.er_magic.samples)
        self.assertTrue(self.frame.er_magic.sites)
        self.assertTrue(self.frame.er_magic.locations)

        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][0])#er_specimens_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][1])#er_specimens_reqd_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][0])#pmag_specimens_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][1])#pmag_specimens_reqd_header)
        self.assertTrue(self.frame.er_magic.headers['sample']['pmag'][0])#pmag_samples_header)
        self.assertTrue(self.frame.er_magic.headers['sample']['er'][1])#er_samples_header)
        self.assertTrue(self.frame.er_magic.headers['site']['pmag'][0])#pmag_sites_header)#
        self.assertTrue(self.frame.er_magic.headers['site']['er'][0])#er_sites_header)

        specimen = self.frame.er_magic.specimens[0]
        sample = self.frame.er_magic.samples[0]
        site = self.frame.er_magic.sites[0]
        location = self.frame.er_magic.locations[0]
        self.assertFalse(specimen.results_data)
        self.assertTrue(specimen.er_data)
        self.assertTrue(specimen.pmag_data)
        self.assertTrue(sample.er_data)
        self.assertTrue(sample.pmag_data)
        self.assertTrue(site.er_data)
        self.assertTrue(site.pmag_data)
        self.assertTrue(location.er_data)
        self.assertFalse(location.pmag_data)

        #self.assertTrue(self.frame.er_magic.pmag_results_header)
        #self.assertTrue(self.frame.er_magic.pmag_results_reqd_header)

    def test_pmag_results(self):
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][0])#pmag_results_header)
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][1])#pmag_results_reqd_header)



class TestMakeMagicGridFrame(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #self.grid = GridFrame(self.ErMagic, self.WD, grid_type, grid_type, self.panel)
        ErMagic = builder.ErMagicBuilder(WD)
        ErMagic.init_default_headers()
        ErMagic.init_actual_headers()
        self.frame = grid_frame.GridFrame(ErMagic, WD, "specimen", "specimen")
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        self.app.Destroy()
        os.chdir(WD)

    def test_grid_is_created(self):
        """
        """
        self.assertTrue(self.frame.grid)

    @unittest.skip('This test calls ShowModal and stops everything')
    def test_add_cols(self):

        #event = wx.CommandEvent(wx.EVT_MENU.evtType[0], help_id)
        #self.frame.GetEventHandler().ProcessEvent(event)
        btn_id = self.frame.add_cols_button.Id
        
        event = wx.CommandEvent(wx.EVT_BUTTON.evtType[0], btn_id)
        self.frame.GetEventHandler().ProcessEvent(event)
        # oh, this sucks.  showmodal and all that


class TestMakeMagicMenu(unittest.TestCase):

    
    def setUp(self):
        self.app = wx.App()
        #self.grid = GridFrame(self.ErMagic, self.WD, grid_type, grid_type, self.panel)
        self.ErMagic = builder.ErMagicBuilder(WD)
        self.ErMagic.init_default_headers()
        self.ErMagic.init_actual_headers()
        self.frame = make_magic.MainFrame(WD)
        #self.frame = grid_frame.GridFrame(ErMagic, WD, "specimen", "specimen")
        #self.pnl = self.frame.GetChildren()[0]


    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        for wind in wx.GetTopLevelWindows():
            res = wind.Destroy()
        self.app.Destroy()
        os.chdir(WD)

    def test_that_all_menus_exist(self):
        """
        check that all expected menus were created
        and that each menu item is enabled
        """
        menu_names = ['File']
        menus = self.frame.MenuBar.Menus
        for menu, menu_name in menus:
            self.assertIsInstance(menu, wx.Menu)
            for item in menu.GetMenuItems():
                self.assertTrue(item.IsEnabled())
            self.assertIn(menu_name, menu_names)

    def test_click_help(self):
        """
        Test that help HtmlFrame is created
        """
        menus = self.frame.MenuBar.Menus
        fmenu, fmenu_name = menus[0]

        # once you have the correct menu
        help_id = fmenu.FindItem('Help')
        help_item = fmenu.FindItemById(help_id)

        top_windows = wx.GetTopLevelWindows()
        event = wx.CommandEvent(wx.EVT_MENU.evtType[0], help_id)
        self.frame.GetEventHandler().ProcessEvent(event)
        top_windows = wx.GetTopLevelWindows()
        help_window = False
        for window in top_windows:
            if window.Label == 'Help Window':
                help_window = window
        self.assertTrue(help_window)
        self.assertTrue(help_window.IsEnabled())<commit_msg>troubleshoot travis-ci failing tests<commit_after>"""
tests for make_magic
"""

import wx
import unittest
import os
#import ErMagicBuilder
import make_magic
import builder
import grid_frame
import pmag_widgets

WD = os.getcwd()

class TestMainFrame(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #WD = os.path.join(os.getcwd(), 'unittests', 'examples', 'my_project')
        self.frame = make_magic.MainFrame(WD, "zebra")
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        for wind in wx.GetTopLevelWindows():
            wind.Destroy()
            del wind
        self.app.Destroy()
        os.chdir(WD)

    def test_main_panel_is_created(self):
        """
        test for existence of main panel
        """
        self.assertTrue(self.pnl.IsEnabled())
        self.assertEqual("main panel", str(self.pnl.GetName()))

    def test_data_object_is_created(self):
        self.assertTrue(self.frame.er_magic)

        self.assertFalse(self.frame.er_magic.specimens)
        self.assertFalse(self.frame.er_magic.samples)
        self.assertFalse(self.frame.er_magic.sites)
        self.assertFalse(self.frame.er_magic.locations)

        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][0])
        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][1])
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][0])
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][1])

    def test_pmag_results(self):
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][0])#pmag_results_header)
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][1])#pmag_results_reqd_header)

    def test_specimen_button(self):
        window = self.does_top_window_exist(self.pnl, 'specimen_btn', 'specimen')
        self.assertTrue(window, 'er_specimens grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_sample_button(self):
        window = self.does_top_window_exist(self.pnl, 'sample_btn', 'sample')
        self.assertTrue(window, 'er_samples grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_site_button(self):
        window = self.does_top_window_exist(self.pnl, 'site_btn', 'site')
        self.assertTrue(window, 'er_sites grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_location_button(self):
        window = self.does_top_window_exist(self.pnl, 'location_btn', 'location')
        self.assertTrue(window, 'er_locations grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_ages_button(self):
        window = self.does_top_window_exist(self.pnl, 'age_btn', 'age')
        self.assertTrue(window, 'er_ages grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())

    def test_results_button(self):
        window = self.does_top_window_exist(self.pnl, 'result_btn', 'result')
        self.assertTrue(window, 'er_results grid window was not created')
        self.assertIsInstance(window, grid_frame.GridFrame)
        self.assertTrue(window.IsEnabled())
        self.assertTrue(window.IsShown())


    def does_window_exist(self, parent, btn_name, window_name):
        """
        produces a click event on the button called btn_name,
        see if it produces the window called window_name
        """
        btn, window = None, None
        children = parent.GetChildren()
        for child in children:
            if child.GetName() == btn_name:
                btn = child
                break
        if not btn:
            return None
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
        btn.GetEventHandler().ProcessEvent(event)
        for child in parent.GetChildren():
            if child.GetName() == window_name and not isinstance(child, wx.lib.buttons.GenButton):
                window = child
                break
        if not window:
            return None
        else:
            return window


    def does_top_window_exist(self, parent, btn_name, window_name):
        """
        produces a click event on the button called btn_name, 
        see if it produces a top-level window called window_name
        """
        btn = None
        children = parent.GetChildren()
        for child in children:
            if child.GetName() == btn_name:
                btn = child
                break
        if not btn:
            return None
        event = wx.CommandEvent(wx.wxEVT_COMMAND_BUTTON_CLICKED, btn.GetId())
        btn.GetEventHandler().ProcessEvent(event)
        for wind in wx.GetTopLevelWindows():
            if wind.GetName() == window_name:
                return wind
        return None

class TestMainFrameWithData(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #WD = os.path.join(os.getcwd(), 'unittests', 'examples', 'my_project')
        self.frame = make_magic.MainFrame(os.path.join(WD, 'Datafiles', 'copy_ErMagicBuilder'))
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        self.app.Destroy()
        os.chdir(WD)

    def test_main_panel_is_created(self):
        """
        test for existence of main panel
        """
        self.assertTrue(self.pnl.IsEnabled())
        self.assertEqual("main panel", str(self.pnl.GetName()))

    def test_data_object_is_created(self):
        self.assertTrue(self.frame.er_magic)
        #self.assertTrue
        #self.assertTrue(self.frame.er_magic.specimens)
        

        self.assertTrue(self.frame.er_magic.specimens)
        self.assertTrue(self.frame.er_magic.samples)
        self.assertTrue(self.frame.er_magic.sites)
        self.assertTrue(self.frame.er_magic.locations)

        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][0])#er_specimens_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['er'][1])#er_specimens_reqd_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][0])#pmag_specimens_header)
        self.assertTrue(self.frame.er_magic.headers['specimen']['pmag'][1])#pmag_specimens_reqd_header)
        self.assertTrue(self.frame.er_magic.headers['sample']['pmag'][0])#pmag_samples_header)
        self.assertTrue(self.frame.er_magic.headers['sample']['er'][1])#er_samples_header)
        self.assertTrue(self.frame.er_magic.headers['site']['pmag'][0])#pmag_sites_header)#
        self.assertTrue(self.frame.er_magic.headers['site']['er'][0])#er_sites_header)

        specimen = self.frame.er_magic.specimens[0]
        sample = self.frame.er_magic.samples[0]
        site = self.frame.er_magic.sites[0]
        location = self.frame.er_magic.locations[0]
        self.assertFalse(specimen.results_data)
        self.assertTrue(specimen.er_data)
        self.assertTrue(specimen.pmag_data)
        self.assertTrue(sample.er_data)
        self.assertTrue(sample.pmag_data)
        self.assertTrue(site.er_data)
        self.assertTrue(site.pmag_data)
        self.assertTrue(location.er_data)
        self.assertFalse(location.pmag_data)

        #self.assertTrue(self.frame.er_magic.pmag_results_header)
        #self.assertTrue(self.frame.er_magic.pmag_results_reqd_header)

    def test_pmag_results(self):
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][0])#pmag_results_header)
        self.assertTrue(self.frame.er_magic.headers['result']['pmag'][1])#pmag_results_reqd_header)



class TestMakeMagicGridFrame(unittest.TestCase):

    def setUp(self):
        self.app = wx.App()
        #self.grid = GridFrame(self.ErMagic, self.WD, grid_type, grid_type, self.panel)
        ErMagic = builder.ErMagicBuilder(WD)
        ErMagic.init_default_headers()
        ErMagic.init_actual_headers()
        self.frame = grid_frame.GridFrame(ErMagic, WD, "specimen", "specimen")
        self.pnl = self.frame.GetChildren()[0]

    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        self.app.Destroy()
        os.chdir(WD)

    def test_grid_is_created(self):
        """
        """
        self.assertTrue(self.frame.grid)

    @unittest.skip('This test calls ShowModal and stops everything')
    def test_add_cols(self):

        #event = wx.CommandEvent(wx.EVT_MENU.evtType[0], help_id)
        #self.frame.GetEventHandler().ProcessEvent(event)
        btn_id = self.frame.add_cols_button.Id
        
        event = wx.CommandEvent(wx.EVT_BUTTON.evtType[0], btn_id)
        self.frame.GetEventHandler().ProcessEvent(event)
        # oh, this sucks.  showmodal and all that


class TestMakeMagicMenu(unittest.TestCase):

    
    def setUp(self):
        self.app = wx.App()
        #self.grid = GridFrame(self.ErMagic, self.WD, grid_type, grid_type, self.panel)
        self.ErMagic = builder.ErMagicBuilder(WD)
        self.ErMagic.init_default_headers()
        self.ErMagic.init_actual_headers()
        self.frame = make_magic.MainFrame(WD)
        #self.frame = grid_frame.GridFrame(ErMagic, WD, "specimen", "specimen")
        #self.pnl = self.frame.GetChildren()[0]


    def tearDown(self):
        #self.frame.Destroy() # this does not work and causes strange errors
        for wind in wx.GetTopLevelWindows():
            res = wind.Destroy()
        self.app.Destroy()
        os.chdir(WD)

    def test_that_all_menus_exist(self):
        """
        check that all expected menus were created
        and that each menu item is enabled
        """
        menu_names = ['File']
        menus = self.frame.MenuBar.Menus
        for menu, menu_name in menus:
            self.assertIsInstance(menu, wx.Menu)
            for item in menu.GetMenuItems():
                self.assertTrue(item.IsEnabled())
            self.assertIn(menu_name, menu_names)

    def test_click_help(self):
        """
        Test that help HtmlFrame is created
        """
        menus = self.frame.MenuBar.Menus
        fmenu, fmenu_name = menus[0]

        # once you have the correct menu
        help_id = fmenu.FindItem('Help')
        help_item = fmenu.FindItemById(help_id)

        top_windows = wx.GetTopLevelWindows()
        print 'before'
        for window in top_windows:
            print 'top-level window:', window
            print 'name:', window.Label
        print 'after'
        event = wx.CommandEvent(wx.EVT_MENU.evtType[0], help_id)
        self.frame.GetEventHandler().ProcessEvent(event)
        top_windows = wx.GetTopLevelWindows()
        help_window = False
        for window in top_windows:
            print 'top-level window:', window
            print 'name:', window.Label
            if window.Label == 'Help Window':
                help_window = window
        self.assertTrue(help_window)
        self.assertTrue(help_window.IsEnabled())
{'fraction_non_alphanumeric': 0.11646215419800325, 'fraction_numerical': 0.0024629458591722744, 'mean_word_length': 2.7940931086267313, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6932920', 'n_tokens_mistral': 7048, 'n_tokens_neox': 6971, 'n_words': 1190}