test_populate.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import pytest
  2. from sqlalchemy.exc import OperationalError
  3. from sqlalchemy_utils.functions import create_database, drop_database
  4. from flaskbb.extensions import alembic, db
  5. from flaskbb.utils.populate import delete_settings_from_fixture, \
  6. create_settings_from_fixture, update_settings_from_fixture, \
  7. create_default_groups, create_test_data, insert_bulk_data, \
  8. create_welcome_forum, create_user
  9. from flaskbb.fixtures.groups import fixture as group_fixture
  10. from flaskbb.fixtures.settings import fixture as settings_fixture
  11. from flaskbb.user.models import Group, User
  12. from flaskbb.forum.models import Category, Topic, Post
  13. from flaskbb.management.models import Setting, SettingsGroup
  14. def _individual_settings(update_result):
  15. """Helper that returns the number of settings that were updated."""
  16. return sum(
  17. len(settings_in_a_group)
  18. for settings_in_a_group in update_result.values()
  19. )
  20. def test_delete_settings_from_fixture(default_settings):
  21. groups_count = SettingsGroup.query.count()
  22. assert len(settings_fixture) == groups_count
  23. deleted = delete_settings_from_fixture(settings_fixture)
  24. assert len(settings_fixture) == len(deleted)
  25. assert not SettingsGroup.query.count()
  26. assert not Setting.query.count()
  27. def test_create_settings_from_fixture(database):
  28. assert not SettingsGroup.query.count()
  29. assert not Setting.query.count()
  30. created = create_settings_from_fixture(settings_fixture)
  31. assert len(settings_fixture) == len(created)
  32. assert SettingsGroup.query.count() == len(created)
  33. def test_update_settings_from_fixture(database):
  34. settings_fixture_group_count = len(settings_fixture)
  35. settings_fixture_setting_count = sum(
  36. len(settings_fixture[k][1]['settings'])
  37. for k in range(len(settings_fixture))
  38. )
  39. assert not SettingsGroup.query.count()
  40. assert not Setting.query.count()
  41. # No force-overwrite - the fixtures will be created because they
  42. # do not exist.
  43. updated = update_settings_from_fixture(settings_fixture)
  44. assert settings_fixture_group_count == len(updated)
  45. assert settings_fixture_group_count == SettingsGroup.query.count()
  46. assert settings_fixture_setting_count == _individual_settings(updated)
  47. assert settings_fixture_setting_count == Setting.query.count()
  48. def test_update_settings_from_fixture_overwrite(database, default_settings,
  49. updated_fixture):
  50. # should add groups: testgroup
  51. # should add testgroup/monty_python, general/test_fixture
  52. pre_update_group_count = SettingsGroup.query.count()
  53. pre_update_setting_count = Setting.query.count()
  54. updated = update_settings_from_fixture(updated_fixture)
  55. assert len(updated) == 2
  56. assert _individual_settings(updated) == 2
  57. assert pre_update_group_count + 1 == SettingsGroup.query.count()
  58. assert pre_update_setting_count + 2 == Setting.query.count()
  59. def test_update_settings_from_fixture_force(database, default_settings,
  60. updated_fixture):
  61. # force-overwrite - nothing changed so nothing should happen here
  62. pre_update_group_count = SettingsGroup.query.count()
  63. pre_update_setting_count = Setting.query.count()
  64. force_updated = update_settings_from_fixture(settings_fixture,
  65. overwrite_group=True,
  66. overwrite_setting=True)
  67. assert len(force_updated) == 0
  68. assert _individual_settings(force_updated) == 0
  69. assert pre_update_group_count == SettingsGroup.query.count()
  70. assert pre_update_setting_count == Setting.query.count()
  71. # should update groups: general
  72. # should update settings: 2 in general, 1 in testgroup
  73. force_updated_1 = update_settings_from_fixture(updated_fixture,
  74. overwrite_group=True,
  75. overwrite_setting=True)
  76. assert len(force_updated_1) == 2
  77. assert _individual_settings(force_updated_1) == 3
  78. assert pre_update_group_count + 1 == SettingsGroup.query.count()
  79. assert pre_update_setting_count + 2 == Setting.query.count()
  80. def test_create_user(default_groups):
  81. user = User.query.filter_by(username="admin").first()
  82. assert not user
  83. user = create_user(username="admin", password="test",
  84. email="test@example.org", groupname="admin")
  85. assert user.username == "admin"
  86. assert user.permissions["admin"]
  87. def test_create_welcome_forum(default_groups):
  88. assert not create_welcome_forum()
  89. create_user(username="admin", password="test",
  90. email="test@example.org", groupname="admin")
  91. assert create_welcome_forum()
  92. def test_create_test_data(database):
  93. assert Category.query.count() == 0
  94. data_created = create_test_data()
  95. assert Category.query.count() == data_created['categories']
  96. def test_insert_bulk_data(database):
  97. assert not insert_bulk_data(topic_count=1, post_count=1)
  98. create_test_data(categories=1, forums=1, topics=0)
  99. assert Topic.query.count() == 0
  100. topics, posts = insert_bulk_data(topic_count=1, post_count=1)
  101. assert Topic.query.count() == topics
  102. # -1 bc the topic post also counts as post
  103. assert Post.query.count() - 1 == posts
  104. def test_create_default_groups(database):
  105. """Test that the default groups are created correctly."""
  106. assert Group.query.count() == 0
  107. create_default_groups()
  108. assert Group.query.count() == len(group_fixture)
  109. for key, attributes in group_fixture.items():
  110. group = Group.query.filter_by(name=key).first()
  111. for attribute, value in attributes.items():
  112. assert getattr(group, attribute) == value
  113. def test_migrations_upgrade():
  114. with pytest.raises(OperationalError):
  115. User.query.all()
  116. # ensure that the database is created
  117. create_database(db.engine.url)
  118. alembic.upgrade()
  119. assert len(User.query.all()) == 0
  120. drop_database(db.engine.url)