test_populate.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import pytest
  2. from sqlalchemy.exc import OperationalError
  3. from flaskbb.extensions import alembic, db
  4. from flaskbb.utils.populate import delete_settings_from_fixture, \
  5. create_settings_from_fixture, update_settings_from_fixture, \
  6. create_default_groups, create_test_data, insert_bulk_data, \
  7. create_welcome_forum, create_user
  8. from flaskbb.fixtures.groups import fixture as group_fixture
  9. from flaskbb.fixtures.settings import fixture as settings_fixture
  10. from flaskbb.user.models import Group, User
  11. from flaskbb.forum.models import Category, Topic, Post
  12. from flaskbb.management.models import Setting, SettingsGroup
  13. def test_delete_settings_from_fixture(default_settings):
  14. groups_count = SettingsGroup.query.count()
  15. assert len(settings_fixture) == groups_count
  16. deleted = delete_settings_from_fixture(settings_fixture)
  17. assert len(settings_fixture) == len(deleted)
  18. assert not SettingsGroup.query.count()
  19. assert not Setting.query.count()
  20. def test_create_settings_from_fixture(database):
  21. assert not SettingsGroup.query.count()
  22. assert not Setting.query.count()
  23. created = create_settings_from_fixture(settings_fixture)
  24. assert len(settings_fixture) == len(created)
  25. assert SettingsGroup.query.count() == len(created)
  26. def test_update_settings_from_fixture(database):
  27. def individual_settings(update_result):
  28. """helper that returns the number of settings that were updated"""
  29. return sum(len(settings_in_a_group) for settings_in_a_group in update_result.values())
  30. settings_fixture_group_count = len(settings_fixture)
  31. settings_fixture_setting_count = sum(
  32. len(settings_fixture[k][1]['settings']) for k in range(len(settings_fixture))
  33. )
  34. assert not SettingsGroup.query.count()
  35. assert not Setting.query.count()
  36. # No force-overwrite - the fixtures will be created because they do not exist.
  37. updated_1 = update_settings_from_fixture(settings_fixture)
  38. assert settings_fixture_group_count == len(updated_1)
  39. assert settings_fixture_group_count == SettingsGroup.query.count()
  40. assert settings_fixture_setting_count == individual_settings(updated_1)
  41. assert settings_fixture_setting_count == Setting.query.count()
  42. # force-overwrite - nothing changed so nothing should happen here
  43. force_updated_1 = update_settings_from_fixture(settings_fixture,
  44. overwrite_group=True,
  45. overwrite_setting=True)
  46. assert len(force_updated_1) == 0
  47. assert individual_settings(force_updated_1) == 0
  48. assert settings_fixture_group_count == SettingsGroup.query.count()
  49. assert settings_fixture_setting_count == Setting.query.count()
  50. fixture_to_update_with = (
  51. # a group where we change a lot
  52. ('general', {
  53. 'name': "General Settings",
  54. 'description': "This description is wrong.",
  55. 'settings': (
  56. # change value
  57. ('project_title', {
  58. 'value': "FlaskBB is cool!",
  59. 'value_type': "string",
  60. 'name': "Project title",
  61. 'description': "The title of the project.",
  62. }),
  63. # change name
  64. ('project_subtitle', {
  65. 'value': "A lightweight forum software in Flask",
  66. 'value_type': "string",
  67. 'name': "Subtitle of the project",
  68. 'description': "A short description of the project.",
  69. }),
  70. # change options (extra)
  71. ('posts_per_page', {
  72. 'value': 10,
  73. 'value_type': "integer",
  74. 'extra': {'min': 1},
  75. 'name': "Posts per page",
  76. 'description': "Number of posts displayed per page.",
  77. }),
  78. # change description
  79. ('topics_per_page', {
  80. 'value': 10,
  81. 'value_type': "integer",
  82. 'extra': {'min': 5},
  83. 'name': "Topics per page",
  84. 'description': "The number of topics to be displayed per page.",
  85. }),
  86. # add
  87. ('test_fixture', {
  88. 'description': 'This is a test fixture',
  89. 'name': 'Test Fixture',
  90. 'value': 'FlaskBBTest',
  91. 'value_type': 'string'
  92. }),
  93. )
  94. }),
  95. # a group where we change nothing
  96. ('auth', {
  97. 'name': 'Authentication Settings',
  98. 'description': 'Configurations for the Login and Register process!',
  99. # the same as in flaskbb/settings/fixtures/settings.py
  100. 'settings': (
  101. ('registration_enabled', {
  102. 'value': True,
  103. 'value_type': "boolean",
  104. 'name': "Enable Registration",
  105. 'description': "Enable or disable the registration",
  106. }),
  107. )
  108. }),
  109. # a wholly new group
  110. ('testgroup', {
  111. 'name': "Important settings",
  112. 'description': "Some settings without which the world would not work.",
  113. 'settings': (
  114. # change value
  115. ('monty_python', {
  116. 'value': "And now for something completely different...",
  117. 'value_type': "string",
  118. 'name': "Monty Python",
  119. 'description': "A random quote from Monty Python.",
  120. }),
  121. )
  122. })
  123. )
  124. # should add groups: testgroup
  125. # should add testgroup/monty_python, general/test_fixture
  126. updated_2 = update_settings_from_fixture(fixture_to_update_with)
  127. assert len(updated_2) == 2
  128. assert individual_settings(updated_2) == 2
  129. assert settings_fixture_group_count + 1 == SettingsGroup.query.count()
  130. assert settings_fixture_setting_count + 2 == Setting.query.count()
  131. fixture_to_update_with[2][1]['settings'][0][1]['description'] = "Something meaningless."
  132. # should update groups: general
  133. # should update settings: 4 in general, 1 in testgroup
  134. force_updated_2 = update_settings_from_fixture(fixture_to_update_with,
  135. overwrite_group=True,
  136. overwrite_setting=True)
  137. assert len(force_updated_2) == 2
  138. assert individual_settings(force_updated_2) == 5
  139. assert settings_fixture_group_count + 1 == SettingsGroup.query.count()
  140. assert settings_fixture_setting_count + 2 == Setting.query.count()
  141. modified_settings_fixture = [item for item in settings_fixture]
  142. modified_settings_fixture.append(
  143. # another wholly new group
  144. ('testgroup2', {
  145. 'name': "Important settings",
  146. 'description': "Some settings without which the world would not work.",
  147. 'settings': (
  148. # change value
  149. ('monty_python_reborn', {
  150. 'value': "And now for something completely different...",
  151. 'value_type': "string",
  152. 'name': "Monty Python",
  153. 'description': "A random quote from Monty Python.",
  154. }),
  155. )
  156. })
  157. )
  158. # should revert 4 in general
  159. # should add testgroup2 and one subitem
  160. force_updated_3 = update_settings_from_fixture(modified_settings_fixture,
  161. overwrite_group=True,
  162. overwrite_setting=True)
  163. assert len(force_updated_3) == 2
  164. assert individual_settings(force_updated_3) == 5
  165. assert settings_fixture_group_count + 2 == SettingsGroup.query.count()
  166. assert settings_fixture_setting_count + 3 == Setting.query.count()
  167. def test_create_user(default_groups):
  168. user = User.query.filter_by(username="admin").first()
  169. assert not user
  170. user = create_user(username="admin", password="test",
  171. email="test@example.org", groupname="admin")
  172. assert user.username == "admin"
  173. assert user.permissions["admin"]
  174. def test_create_welcome_forum(default_groups):
  175. assert not create_welcome_forum()
  176. create_user(username="admin", password="test",
  177. email="test@example.org", groupname="admin")
  178. assert create_welcome_forum()
  179. def test_create_test_data(database):
  180. assert Category.query.count() == 0
  181. data_created = create_test_data()
  182. assert Category.query.count() == data_created['categories']
  183. def test_insert_bulk_data(database):
  184. assert not insert_bulk_data(topic_count=1, post_count=1)
  185. create_test_data(categories=1, forums=1, topics=0)
  186. assert Topic.query.count() == 0
  187. topics, posts = insert_bulk_data(topic_count=1, post_count=1)
  188. assert Topic.query.count() == topics
  189. # -1 bc the topic post also counts as post
  190. assert Post.query.count() - 1 == posts
  191. def test_create_default_groups(database):
  192. """Test that the default groups are created correctly."""
  193. assert Group.query.count() == 0
  194. create_default_groups()
  195. assert Group.query.count() == len(group_fixture)
  196. for key, attributes in group_fixture.items():
  197. group = Group.query.filter_by(name=key).first()
  198. for attribute, value in attributes.items():
  199. assert getattr(group, attribute) == value
  200. def test_migrations_upgrade():
  201. with pytest.raises(OperationalError):
  202. User.query.all()
  203. alembic.upgrade()
  204. assert len(User.query.all()) == 0