Просмотр исходного кода

Split test into multiple smaller ones

Peter Justin 7 лет назад
Родитель
Сommit
1a6c1beaed
3 измененных файлов с 101 добавлено и 135 удалено
  1. 1 0
      tests/conftest.py
  2. 56 0
      tests/fixtures/settings_fixture.py
  3. 44 135
      tests/unit/utils/test_populate.py

+ 1 - 0
tests/conftest.py

@@ -2,3 +2,4 @@ from tests.fixtures.app import *  # noqa
 from tests.fixtures.forum import *  # noqa
 from tests.fixtures.user import *  # noqa
 from tests.fixtures.message import *  # noqa
+from tests.fixtures.settings_fixture import *  # noqa

+ 56 - 0
tests/fixtures/settings_fixture.py

@@ -0,0 +1,56 @@
+import pytest
+
+
+@pytest.fixture
+def updated_fixture():
+    return (
+        # a group where we change a lot
+        ('general', {
+            'name': "General Settings",
+            'description': "This description is wrong.",
+            'settings': (
+                # change value
+                ('project_title', {
+                    'value': "FlaskBB is cool!",
+                    'value_type': "string",
+                    'name': "Project title",
+                    'description': "The title of the project.",
+                }),
+                # add
+                ('test_fixture', {
+                    'description': 'This is a test fixture',
+                    'name': 'Test Fixture',
+                    'value': 'FlaskBBTest',
+                    'value_type': 'string'
+                }),
+            )
+        }),
+        # a group where we change nothing
+        ('auth', {
+            'name': 'Authentication Settings',
+            'description': 'Settings for the Login and Register process.',
+            # the same as in flaskbb/settings/fixtures/settings.py
+            'settings': (
+                ('registration_enabled', {
+                    'value': True,
+                    'value_type': "boolean",
+                    'name': "Enable Registration",
+                    'description': "Enable or disable the registration",
+                }),
+            )
+        }),
+        # a wholly new group
+        ('testgroup', {
+            'name': "Important settings",
+            'description': "Some settings without the world would not work.",
+            'settings': (
+                # change value
+                ('monty_python', {
+                    'value': "And now for something completely different...",
+                    'value_type': "string",
+                    'name': "Monty Python",
+                    'description': "A random quote from Monty Python.",
+                }),
+            )
+        })
+    )

+ 44 - 135
tests/unit/utils/test_populate.py

@@ -14,6 +14,14 @@ from flaskbb.forum.models import Category, Topic, Post
 from flaskbb.management.models import Setting, SettingsGroup
 
 
+def _individual_settings(update_result):
+    """Helper that returns the number of settings that were updated."""
+    return sum(
+        len(settings_in_a_group)
+        for settings_in_a_group in update_result.values()
+    )
+
+
 def test_delete_settings_from_fixture(default_settings):
     groups_count = SettingsGroup.query.count()
     assert len(settings_fixture) == groups_count
@@ -36,159 +44,60 @@ def test_create_settings_from_fixture(database):
 
 
 def test_update_settings_from_fixture(database):
-    def individual_settings(update_result):
-        """helper that returns the number of settings that were updated"""
-        return sum(len(settings_in_a_group) for settings_in_a_group in update_result.values())
-
     settings_fixture_group_count = len(settings_fixture)
     settings_fixture_setting_count = sum(
-        len(settings_fixture[k][1]['settings']) for k in range(len(settings_fixture))
+        len(settings_fixture[k][1]['settings'])
+        for k in range(len(settings_fixture))
     )
 
-
     assert not SettingsGroup.query.count()
     assert not Setting.query.count()
 
-    # No force-overwrite - the fixtures will be created because they do not exist.
-    updated_1 = update_settings_from_fixture(settings_fixture)
-    assert settings_fixture_group_count == len(updated_1)
+    # No force-overwrite - the fixtures will be created because they
+    # do not exist.
+    updated = update_settings_from_fixture(settings_fixture)
+    assert settings_fixture_group_count == len(updated)
     assert settings_fixture_group_count == SettingsGroup.query.count()
-    assert settings_fixture_setting_count == individual_settings(updated_1)
+    assert settings_fixture_setting_count == _individual_settings(updated)
     assert settings_fixture_setting_count == Setting.query.count()
 
-    # force-overwrite - nothing changed so nothing should happen here
-    force_updated_1 = update_settings_from_fixture(settings_fixture,
-                                                   overwrite_group=True,
-                                                   overwrite_setting=True)
-
-    assert len(force_updated_1) == 0
-    assert individual_settings(force_updated_1) == 0
-    assert settings_fixture_group_count == SettingsGroup.query.count()
-    assert settings_fixture_setting_count == Setting.query.count()
-
-    fixture_to_update_with = (
-        # a group where we change a lot
-        ('general', {
-            'name': "General Settings",
-            'description': "This description is wrong.",
-            'settings': (
-                # change value
-                ('project_title', {
-                    'value': "FlaskBB is cool!",
-                    'value_type': "string",
-                    'name': "Project title",
-                    'description': "The title of the project.",
-                }),
-                # change name
-                ('project_subtitle', {
-                    'value':        "A lightweight forum software in Flask",
-                    'value_type':   "string",
-                    'name':         "Subtitle of the project",
-                    'description':  "A short description of the project.",
-                }),
-                # change options (extra)
-                ('posts_per_page', {
-                    'value':        10,
-                    'value_type':   "integer",
-                    'extra':        {'min': 1},
-                    'name':         "Posts per page",
-                    'description':  "Number of posts displayed per page.",
-                }),
-                # change description
-                ('topics_per_page', {
-                    'value':        10,
-                    'value_type':   "integer",
-                    'extra':        {'min': 5},
-                    'name':         "Topics per page",
-                    'description':  "The number of topics to be displayed per page.",
-                }),
-                # add
-                ('test_fixture', {
-                    'description': 'This is a test fixture',
-                    'name': 'Test Fixture',
-                    'value': 'FlaskBBTest',
-                    'value_type': 'string'
-                }),
-            )
-        }),
-        # a group where we change nothing
-        ('auth', {
-            'name': 'Authentication Settings',
-            'description': 'Configurations for the Login and Register process!',
-            # the same as in flaskbb/settings/fixtures/settings.py
-            'settings': (
-                ('registration_enabled', {
-                    'value':        True,
-                    'value_type':   "boolean",
-                    'name':         "Enable Registration",
-                    'description':  "Enable or disable the registration",
-                }),
-            )
-        }),
-        # a wholly new group
-        ('testgroup', {
-            'name': "Important settings",
-            'description': "Some settings without which the world would not work.",
-            'settings': (
-                # change value
-                ('monty_python', {
-                    'value': "And now for something completely different...",
-                    'value_type': "string",
-                    'name': "Monty Python",
-                    'description': "A random quote from Monty Python.",
-                }),
-            )
-        })
-    )
 
+def test_update_settings_from_fixture_overwrite(database, default_settings,
+                                                updated_fixture):
     # should add groups: testgroup
     # should add testgroup/monty_python, general/test_fixture
-    updated_2 = update_settings_from_fixture(fixture_to_update_with)
-    assert len(updated_2) == 2
-    assert individual_settings(updated_2) == 2
-    assert settings_fixture_group_count + 1 == SettingsGroup.query.count()
-    assert settings_fixture_setting_count + 2 == Setting.query.count()
+    pre_update_group_count = SettingsGroup.query.count()
+    pre_update_setting_count = Setting.query.count()
+    updated = update_settings_from_fixture(updated_fixture)
+    assert len(updated) == 2
+    assert _individual_settings(updated) == 2
+    assert pre_update_group_count + 1 == SettingsGroup.query.count()
+    assert pre_update_setting_count + 2 == Setting.query.count()
 
-    fixture_to_update_with[2][1]['settings'][0][1]['description'] = "Something meaningless."
 
-    # should update groups: general
-    # should update settings: 4 in general, 1 in testgroup
-    force_updated_2 = update_settings_from_fixture(fixture_to_update_with,
-                                                   overwrite_group=True,
-                                                   overwrite_setting=True)
-    assert len(force_updated_2) == 2
-    assert individual_settings(force_updated_2) == 5
-    assert settings_fixture_group_count + 1 == SettingsGroup.query.count()
-    assert settings_fixture_setting_count + 2 == Setting.query.count()
-
-    modified_settings_fixture = [item for item in settings_fixture]
-    modified_settings_fixture.append(
-        # another wholly new group
-        ('testgroup2', {
-            'name': "Important settings",
-            'description': "Some settings without which the world would not work.",
-            'settings': (
-                # change value
-                ('monty_python_reborn', {
-                    'value': "And now for something completely different...",
-                    'value_type': "string",
-                    'name': "Monty Python",
-                    'description': "A random quote from Monty Python.",
-                }),
-            )
-        })
-    )
+def test_update_settings_from_fixture_force(database, default_settings,
+                                            updated_fixture):
+    # force-overwrite - nothing changed so nothing should happen here
+    pre_update_group_count = SettingsGroup.query.count()
+    pre_update_setting_count = Setting.query.count()
+    force_updated = update_settings_from_fixture(settings_fixture,
+                                                 overwrite_group=True,
+                                                 overwrite_setting=True)
+
+    assert len(force_updated) == 0
+    assert _individual_settings(force_updated) == 0
+    assert pre_update_group_count == SettingsGroup.query.count()
+    assert pre_update_setting_count == Setting.query.count()
 
-    # should revert 4 in general
-    # should add testgroup2 and one subitem
-    force_updated_3 = update_settings_from_fixture(modified_settings_fixture,
+    # should update groups: general
+    # should update settings: 2 in general, 1 in testgroup
+    force_updated_1 = update_settings_from_fixture(updated_fixture,
                                                    overwrite_group=True,
                                                    overwrite_setting=True)
-
-    assert len(force_updated_3) == 2
-    assert individual_settings(force_updated_3) == 5
-    assert settings_fixture_group_count + 2 == SettingsGroup.query.count()
-    assert settings_fixture_setting_count + 3 == Setting.query.count()
+    assert len(force_updated_1) == 2
+    assert _individual_settings(force_updated_1) == 3
+    assert pre_update_group_count + 1 == SettingsGroup.query.count()
+    assert pre_update_setting_count + 2 == Setting.query.count()
 
 
 def test_create_user(default_groups):