From 0864ddadb0fad01b11a2e8f276e783a969b1b351 Mon Sep 17 00:00:00 2001 From: Taka Date: Sun, 8 Dec 2019 16:49:25 +0000 Subject: [PATCH] Changed how the tests initialize the database. --- asl_articles/tests/test_articles.py | 17 +++---- asl_articles/tests/test_basic.py | 5 +-- asl_articles/tests/test_publications.py | 17 +++---- asl_articles/tests/test_publishers.py | 20 +++------ asl_articles/tests/utils.py | 59 ++++++++++++++----------- 5 files changed, 54 insertions(+), 64 deletions(-) diff --git a/asl_articles/tests/test_articles.py b/asl_articles/tests/test_articles.py index b9e0be5..24a3aac 100644 --- a/asl_articles/tests/test_articles.py +++ b/asl_articles/tests/test_articles.py @@ -1,6 +1,6 @@ """ Test article operations. """ -from asl_articles.tests.utils import init_tests, init_db, do_search, get_result_names, \ +from asl_articles.tests.utils import init_tests, do_search, get_result_names, \ wait_for, wait_for_elem, find_child, find_children, set_elem_text, \ set_toast_marker, check_toast, check_ask_dialog, check_error_msg @@ -10,8 +10,7 @@ def test_edit_article( webdriver, flask_app, dbconn ): """Test editing articles.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "articles.json" ) + init_tests( webdriver, flask_app, dbconn, "articles.json" ) # edit "What To Do If You Have A Tin Can" results = do_search( "tin can" ) @@ -57,8 +56,7 @@ def test_create_article( webdriver, flask_app, dbconn ): """Test creating new articles.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # try creating a article with no name (should fail) _create_article( {}, toast_type=None ) @@ -95,8 +93,7 @@ def test_delete_article( webdriver, flask_app, dbconn ): """Test deleting articles.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "articles.json" ) + init_tests( webdriver, flask_app, dbconn, "articles.json" ) # start to delete article "Smoke Gets In Your Eyes", but cancel the operation results = do_search( "smoke" ) @@ -136,8 +133,7 @@ def test_unicode( webdriver, flask_app, dbconn ): """Test Unicode content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a article with Unicode content _create_article( { @@ -163,8 +159,7 @@ def test_clean_html( webdriver, flask_app, dbconn ): """Test cleaning HTML content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a article with HTML content _create_article( { diff --git a/asl_articles/tests/test_basic.py b/asl_articles/tests/test_basic.py index 0faf437..fa745c1 100644 --- a/asl_articles/tests/test_basic.py +++ b/asl_articles/tests/test_basic.py @@ -1,6 +1,6 @@ """ Basic tests. """ -from asl_articles.tests.utils import init_tests, init_db, do_search, find_child +from asl_articles.tests.utils import init_tests, do_search, find_child # --------------------------------------------------------------------- @@ -8,8 +8,7 @@ def test_basic( webdriver, flask_app, dbconn ): """Basic tests.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn, "basic.json" ) # make sure the home page loaded correctly elem = find_child( "#search-form .caption" ) diff --git a/asl_articles/tests/test_publications.py b/asl_articles/tests/test_publications.py index e5571ed..ba207bf 100644 --- a/asl_articles/tests/test_publications.py +++ b/asl_articles/tests/test_publications.py @@ -10,8 +10,7 @@ def test_edit_publication( webdriver, flask_app, dbconn ): """Test editing publications.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "publications.json" ) + init_tests( webdriver, flask_app, dbconn, "publications.json" ) # edit "ASL Journal #2" results = do_search( "asl journal" ) @@ -57,8 +56,7 @@ def test_create_publication( webdriver, flask_app, dbconn ): """Test creating new publications.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # try creating a publication with no name (should fail) _create_publication( {}, toast_type=None ) @@ -95,8 +93,7 @@ def test_delete_publication( webdriver, flask_app, dbconn ): """Test deleting publications.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "publications.json" ) + init_tests( webdriver, flask_app, dbconn, "publications.json" ) # start to delete publication "ASL Journal #1", but cancel the operation results = do_search( "ASL Journal" ) @@ -138,7 +135,7 @@ def test_cascading_deletes( webdriver, flask_app, dbconn ): """Test cascading deletes.""" # initialize - init_tests( webdriver, flask_app ) + init_tests( webdriver, flask_app, None ) def do_test( pub_name, expected_warning, expected_articles ): @@ -180,8 +177,7 @@ def test_unicode( webdriver, flask_app, dbconn ): """Test Unicode content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "publications.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a publication with Unicode content _create_publication( { @@ -206,8 +202,7 @@ def test_clean_html( webdriver, flask_app, dbconn ): """Test cleaning HTML content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "publications.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a publication with HTML content _create_publication( { diff --git a/asl_articles/tests/test_publishers.py b/asl_articles/tests/test_publishers.py index 1afbb9d..dc8952b 100644 --- a/asl_articles/tests/test_publishers.py +++ b/asl_articles/tests/test_publishers.py @@ -10,8 +10,7 @@ def test_edit_publisher( webdriver, flask_app, dbconn ): """Test editing publishers.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn, "basic.json" ) # edit "Avalon Hill" results = do_search( "" ) @@ -56,11 +55,7 @@ def test_create_publisher( webdriver, flask_app, dbconn ): """Test creating new publishers.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) - - # load up some search results - do_search( "" ) + init_tests( webdriver, flask_app, dbconn ) # try creating a publisher with no name (should fail) _create_publisher( {}, toast_type=None ) @@ -94,8 +89,7 @@ def test_delete_publisher( webdriver, flask_app, dbconn ): """Test deleting publishers.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn, "basic.json" ) # start to delete publisher "Le Franc Tireur", but cancel the operation results = do_search( "" ) @@ -136,7 +130,7 @@ def test_cascading_deletes( webdriver, flask_app, dbconn ): """Test cascading deletes.""" # initialize - init_tests( webdriver, flask_app ) + init_tests( webdriver, flask_app, None ) def check_results( results, sr_type, expected, expected_deletions ): expected = [ "{} {}".format( sr_type, p ) for p in expected ] @@ -216,8 +210,7 @@ def test_unicode( webdriver, flask_app, dbconn ): """Test Unicode content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a publisher with Unicode content _create_publisher( { @@ -241,8 +234,7 @@ def test_clean_html( webdriver, flask_app, dbconn ): """Test cleaning HTML content.""" # initialize - init_tests( webdriver, flask_app ) - init_db( dbconn, "basic.json" ) + init_tests( webdriver, flask_app, dbconn ) # create a publisher with HTML content _create_publisher( { diff --git a/asl_articles/tests/utils.py b/asl_articles/tests/utils.py index 293e586..379708e 100644 --- a/asl_articles/tests/utils.py +++ b/asl_articles/tests/utils.py @@ -21,7 +21,7 @@ _flask_app = None # nb: this may not be set (if we're talking to an existing Fla # --------------------------------------------------------------------- -def init_tests( webdriver, flask_app ): +def init_tests( webdriver, flask_app, dbconn, fixtures_fname=None ): """Prepare to run tests.""" # initialize @@ -29,10 +29,43 @@ def init_tests( webdriver, flask_app ): _webdriver = webdriver _flask_app = flask_app + # initialize the database + if dbconn: + init_db( dbconn, fixtures_fname ) + else: + assert fixtures_fname is None + # load the home page webdriver.get( webdriver.make_url( "/" ) ) wait_for_elem( 2, "#search-form" ) +# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + +def init_db( dbconn, fixtures_fname ): + """Load the database with test data.""" + + # create a database session + Session = sqlalchemy.orm.sessionmaker( bind=dbconn ) + session = Session() + + # load the test data + if fixtures_fname: + dname = os.path.join( os.path.split(__file__)[0], "fixtures/" ) + fname = os.path.join( dname, fixtures_fname ) + data = json.load( open( fname, "r" ) ) + else: + data = {} + + # load the test data into the database + for table_name in ["publisher","publication","article"]: + model = getattr( asl_articles.models, table_name.capitalize() ) + session.query( model ).delete() + if table_name in data: + session.bulk_insert_mappings( model, data[table_name] ) + session.commit() + + return session + # --------------------------------------------------------------------- def do_search( query ): @@ -73,30 +106,6 @@ def get_result_names( results ): # --------------------------------------------------------------------- -def init_db( engine, fname ): - """Load the database with test data.""" - - # create a database session - Session = sqlalchemy.orm.sessionmaker( bind=engine ) - session = Session() - - # load the test data - dname = os.path.join( os.path.split(__file__)[0], "fixtures/" ) - fname = os.path.join( dname, fname ) - data = json.load( open( fname, "r" ) ) - - # load the test data into the database - for table_name in ["publisher","publication","article"]: - model = getattr( asl_articles.models, table_name.capitalize() ) - session.query( model ).delete() - if table_name in data: - session.bulk_insert_mappings( model, data[table_name] ) - session.commit() - - return session - -# --------------------------------------------------------------------- - def wait_for( timeout, func ): """Wait for a condition to become true.""" return WebDriverWait( _webdriver, timeout, 0.1 ).until(