test_content_revision.py 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # -*- coding: utf-8 -*-
  2. from collections import OrderedDict
  3. from sqlalchemy import inspect
  4. from tracim_backend.models import ContentRevisionRO
  5. from tracim_backend.models import User
  6. from tracim_backend.models.data import ContentType
  7. from tracim_backend.tests import DefaultTest
  8. from tracim_backend.tests import eq_
  9. class TestContentRevision(DefaultTest):
  10. def _new_from(self, revision):
  11. excluded_columns = (
  12. 'revision_id',
  13. '_sa_instance_state',
  14. 'depot_file',
  15. 'node',
  16. 'revision_read_statuses',
  17. )
  18. revision_columns = [attr.key for attr in inspect(revision).attrs
  19. if attr.key not in excluded_columns]
  20. new_revision = ContentRevisionRO()
  21. for revision_column in revision_columns:
  22. old_revision_column_value = getattr(revision, revision_column)
  23. setattr(new_revision, revision_column, old_revision_column_value)
  24. return new_revision
  25. def _get_dict_representation(self, revision):
  26. keys_to_remove = ('updated', '_sa_instance_state')
  27. dict_repr = OrderedDict(sorted(revision.__dict__.items()))
  28. for key_to_remove in keys_to_remove:
  29. dict_repr.pop(key_to_remove, None)
  30. return dict_repr
  31. def test_new_revision(self):
  32. admin = self.session.query(User).filter(
  33. User.email == 'admin@admin.admin'
  34. ).one()
  35. workspace = self._create_workspace_and_test(
  36. name='workspace_1',
  37. user=admin
  38. )
  39. folder = self._create_content_and_test(
  40. name='folder_1',
  41. workspace=workspace,
  42. type=ContentType.Folder
  43. )
  44. page = self._create_content_and_test(
  45. workspace=workspace,
  46. parent=folder,
  47. name='file_1',
  48. description='content of file_1',
  49. type=ContentType.Page,
  50. owner=admin
  51. )
  52. self.session.flush()
  53. # Model create a new instance with list of column
  54. new_revision_by_model = ContentRevisionRO.new_from(page.revision)
  55. # Test create a new instance from dynamic listing of model
  56. # columns mapping
  57. new_revision_by_test = self._new_from(page.revision)
  58. new_revision_by_model_dict = self._get_dict_representation(
  59. new_revision_by_model
  60. )
  61. new_revision_by_test_dict = self._get_dict_representation(
  62. new_revision_by_test
  63. )
  64. # They must be identical
  65. eq_(new_revision_by_model_dict, new_revision_by_test_dict)