From 11c8af724d9369c92199870af2cc69704017d3a0 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 10 Jan 2016 21:00:39 -0800 Subject: [PATCH 1/4] Add more tests for dbcore --- test/test_dbcore.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/test_dbcore.py b/test/test_dbcore.py index a69ffedbe..204398eaa 100644 --- a/test/test_dbcore.py +++ b/test/test_dbcore.py @@ -116,6 +116,16 @@ class TestDatabaseTwoModels(dbcore.Database): pass +class TestModelWithGetters(dbcore.Model): + + @classmethod + def _getters(cls): + return {'aComputedField': (lambda s: 'thing')} + + def _template_funcs(self): + return {} + + class MigrationTest(unittest.TestCase): """Tests the ability to change the database schema between versions. @@ -274,6 +284,40 @@ class ModelTest(unittest.TestCase): model2.load() self.assertNotIn('flex_field', model2) + def test_check_db_fails(self): + with self.assertRaisesRegexp(ValueError, 'no database'): + dbcore.Model()._check_db() + with self.assertRaisesRegexp(ValueError, 'no id'): + TestModel1(self.db)._check_db() + + dbcore.Model(self.db)._check_db(need_id=False) + + def test_missing_field(self): + with self.assertRaises(AttributeError): + TestModel1(self.db).nonExistingKey + + def test_computed_field(self): + model = TestModelWithGetters() + self.assertEqual(model.aComputedField, 'thing') + with self.assertRaisesRegexp(KeyError, 'computed field .+ deleted'): + del model.aComputedField + + def test_items(self): + model = TestModel1(self.db) + model.id = 5 + self.assertEqual({('id', 5), ('field_one', None)}, + set(model.items())) + + def test_delete_internal_field(self): + model = dbcore.Model() + del model._db + with self.assertRaises(AttributeError): + model._db + + def test_parse_nonstring(self): + with self.assertRaisesRegexp(TypeError, "must be a string"): + dbcore.Model._parse(None, 42) + class FormatTest(unittest.TestCase): def test_format_fixed_field(self): @@ -588,6 +632,15 @@ class ResultsIteratorTest(unittest.TestCase): objs = self.db._fetch(TestModel1) self.assertEqual(len(objs), 2) + def test_out_of_range(self): + objs = self.db._fetch(TestModel1) + with self.assertRaises(IndexError): + objs[100] + + def test_no_results(self): + self.assertIsNone(self.db._fetch( + TestModel1, dbcore.query.FalseQuery()).get()) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__) From f770125e3cf3873f9812ab99a76a9f9fdd753186 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 10 Jan 2016 21:01:28 -0800 Subject: [PATCH 2/4] Avoid repeating expensive db creation Saves about 3 seconds --- test/test_dbcore.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/test/test_dbcore.py b/test/test_dbcore.py index 204398eaa..3c1a3599b 100644 --- a/test/test_dbcore.py +++ b/test/test_dbcore.py @@ -19,6 +19,7 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) import os +import shutil import sqlite3 from test._common import unittest @@ -130,11 +131,13 @@ class MigrationTest(unittest.TestCase): """Tests the ability to change the database schema between versions. """ - def setUp(self): - handle, self.libfile = mkstemp('db') + + @classmethod + def setUpClass(cls): + handle, cls.orig_libfile = mkstemp('orig_db') os.close(handle) # Set up a database with the two-field schema. - old_lib = TestDatabase2(self.libfile) + old_lib = TestDatabase2(cls.orig_libfile) # Add an item to the old library. old_lib._connection().execute( @@ -143,6 +146,15 @@ class MigrationTest(unittest.TestCase): old_lib._connection().commit() del old_lib + @classmethod + def tearDownClass(cls): + os.remove(cls.orig_libfile) + + def setUp(self): + handle, self.libfile = mkstemp('db') + os.close(handle) + shutil.copyfile(self.orig_libfile, self.libfile) + def tearDown(self): os.remove(self.libfile) From dda1173e02d73e5cfe07434238bd99dbd9e1ae45 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 10 Jan 2016 22:01:13 -0800 Subject: [PATCH 3/4] Escape quotes to make old version of emacs font-lock happy --- test/test_art.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test_art.py b/test/test_art.py index e19c7d211..4954106b5 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -209,10 +209,10 @@ class AAOTest(UseThePlugin): def test_aao_scraper_finds_image(self): body = b"""
- - View larger image + + \"View """ self.mock_response(self.AAO_URL, body) album = _common.Bag(asin=self.ASIN) From 760298b8e464d0821d937cfdade895ed0a4c72d6 Mon Sep 17 00:00:00 2001 From: Jesse Weinstein Date: Sun, 10 Jan 2016 23:18:47 -0800 Subject: [PATCH 4/4] Mark slow tests to be skipped if SKIP_SLOW_TESTS env var is defined Saves over 100 seconds. --- test/test_art.py | 2 ++ test/test_convert.py | 6 ++++++ test/test_dbcore.py | 2 ++ test/test_edit.py | 3 +++ test/test_importer.py | 2 ++ test/test_logging.py | 3 +++ test/test_ui.py | 6 ++++++ 7 files changed, 24 insertions(+) diff --git a/test/test_art.py b/test/test_art.py index 4954106b5..d4ca814a5 100644 --- a/test/test_art.py +++ b/test/test_art.py @@ -261,6 +261,8 @@ class GoogleImageTest(UseThePlugin): self.assertEqual(list(result_url), []) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class ArtImporterTest(UseThePlugin): def setUp(self): super(ArtImporterTest, self).setUp() diff --git a/test/test_convert.py b/test/test_convert.py index ab1fa2504..e6f75c0ad 100644 --- a/test/test_convert.py +++ b/test/test_convert.py @@ -64,6 +64,8 @@ class TestHelper(helper.TestHelper): .format(path, tag)) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class ImportConvertTest(unittest.TestCase, TestHelper): def setUp(self): @@ -99,6 +101,8 @@ class ImportConvertTest(unittest.TestCase, TestHelper): self.assertTrue(os.path.isfile(item.path)) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class ConvertCliTest(unittest.TestCase, TestHelper): def setUp(self): @@ -186,6 +190,8 @@ class ConvertCliTest(unittest.TestCase, TestHelper): self.assertFalse(os.path.exists(converted)) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class NeverConvertLossyFilesTest(unittest.TestCase, TestHelper): """Test the effect of the `never_convert_lossy_files` option. """ diff --git a/test/test_dbcore.py b/test/test_dbcore.py index 3c1a3599b..00ef2f1fb 100644 --- a/test/test_dbcore.py +++ b/test/test_dbcore.py @@ -127,6 +127,8 @@ class TestModelWithGetters(dbcore.Model): return {} +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class MigrationTest(unittest.TestCase): """Tests the ability to change the database schema between versions. diff --git a/test/test_edit.py b/test/test_edit.py index 522437eaa..34bad25a7 100644 --- a/test/test_edit.py +++ b/test/test_edit.py @@ -15,6 +15,7 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) import codecs +import os from mock import patch from test._common import unittest @@ -62,6 +63,8 @@ class ModifyFileMocker(object): f.write(contents) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class EditCommandTest(unittest.TestCase, TestHelper): """ Black box tests for `beetsplug.edit`. Command line interaction is simulated using `test.helper.control_stdin()`, and yaml editing via an diff --git a/test/test_importer.py b/test/test_importer.py index b9f75f7ef..27dcd5a12 100644 --- a/test/test_importer.py +++ b/test/test_importer.py @@ -236,6 +236,8 @@ class ImportHelper(TestHelper): self.assertEqual(len(os.listdir(self.libdir)), 0) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class NonAutotaggedImportTest(_common.TestCase, ImportHelper): def setUp(self): self.setup_beets(disk=True) diff --git a/test/test_logging.py b/test/test_logging.py index 81df95a78..8517f0a3f 100644 --- a/test/test_logging.py +++ b/test/test_logging.py @@ -5,6 +5,7 @@ from __future__ import (division, absolute_import, print_function, unicode_literals) import sys +import os import threading import logging as log from StringIO import StringIO @@ -163,6 +164,8 @@ class LoggingLevelTest(unittest.TestCase, helper.TestHelper): self.assertIn('dummy: debug import_stage', logs) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class ConcurrentEventsTest(TestCase, helper.TestHelper): """Similar to LoggingLevelTest but lower-level and focused on multiple events interaction. Since this is a bit heavy we don't do it in diff --git a/test/test_ui.py b/test/test_ui.py index b32a4c452..08097b3f2 100644 --- a/test/test_ui.py +++ b/test/test_ui.py @@ -594,6 +594,8 @@ class InputTest(_common.TestCase): self.assertEqual(album, u'\xc2me') +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class ConfigTest(unittest.TestCase, TestHelper): def setUp(self): self.setup_beets() @@ -1035,6 +1037,8 @@ class PathFormatTest(_common.TestCase): self.assertEqual(pf[1:], default_formats) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class PluginTest(_common.TestCase): def test_plugin_command_from_pluginpath(self): config['pluginpath'] = [os.path.join(_common.RSRC, 'beetsplug')] @@ -1042,6 +1046,8 @@ class PluginTest(_common.TestCase): ui._raw_main(['test']) +@unittest.skipIf('SKIP_SLOW_TESTS' in os.environ, + 'Skipping because test is slow') class CompletionTest(_common.TestCase): def test_completion(self): # Load plugin commands