server/python/django2/renkanmanager/tests/test_models.py
author ymh <ymh.work@gmail.com>
Sun, 14 Jul 2024 22:00:08 +0200
changeset 666 9d6550026232
parent 665 69d13e7dd286
permissions -rw-r--r--
Added tag V00.13.04 for changeset 69d13e7dd286
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
665
69d13e7dd286 add file encoding
ymh <ymh.work@gmail.com>
parents: 626
diff changeset
     1
# -*- coding: utf-8 -*-
626
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
import json,os
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from django.test import TestCase
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from renkanmanager.models import content_copy
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
from django.contrib.auth import get_user_model
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
User = get_user_model()
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
class ModelsTest(TestCase):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
    def setUp(self):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
        test_json_path = os.path.join(os.path.dirname(__file__), 'test_revision_copy.json')
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
        with open(test_json_path) as json_file:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
            self.test_json = json_file.read()
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    def test_content_copy_call(self):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        c_copy = content_copy(self.test_json)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        self.assertIsNotNone(c_copy)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    def test_content_copy_id_key(self):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        c_copy = content_copy(self.test_json)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        self.assertIn('id', c_copy)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        self.assertNotIn('_id', c_copy)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        for n in c_copy['nodes']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
            self.assertIn('id', n)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
            self.assertNotIn('_id', n)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        for u in c_copy['users']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            self.assertIn('id', u)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
            self.assertNotIn('_id', u)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        for v in c_copy['views']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
            self.assertIn('id', v)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
            self.assertNotIn('_id', v)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        for e in c_copy['edges']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
            self.assertIn('id', e)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
            self.assertNotIn('_id', e)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    def test_content_copy_content(self):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
        c_copy = content_copy(self.test_json)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        c_json = json.loads(self.test_json)
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        for k in (c_copy.keys() - ['nodes', 'users', 'views', 'edges', 'id']):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
            self.assertEquals(c_copy[k], c_json[k])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
        self.assertEquals(len(c_copy['nodes']), len(c_json['nodes']))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        for node in c_copy['nodes']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            original_nodes = [ n for n in c_json['nodes'] if n['title'] == node['title'] ]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            self.assertEquals(1,len(original_nodes))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
            for k in (node.keys() - ['id']):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
                self.assertEquals(node[k], original_nodes[0][k])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        self.assertEquals(len(c_copy['edges']), len(c_json['edges']))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
        for edge in c_copy['edges']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            original_edges = [ e for e in c_json['edges'] if e['title'] == edge['title'] ]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            self.assertEquals(1,len(original_edges))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            orig_edge = original_edges[0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
            for k in (edge.keys() - ['id', 'from', 'to']):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
                self.assertEquals(edge[k], orig_edge[k])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            from_copy = [e for e in c_copy['nodes'] if e['id'] == edge['from']][0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            from_orig = [e for e in c_json['nodes'] if e['_id'] == orig_edge['from']][0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
            self.assertEquals(from_copy['title'], from_orig['title'])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            to_copy = [e for e in c_copy['nodes'] if e['id'] == edge['to']][0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
            to_orig = [e for e in c_json['nodes'] if e['_id'] == orig_edge['to']][0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
            self.assertEquals(to_copy['title'], to_orig['title'])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        self.assertEquals(len(c_copy['users']), len(c_json['users']))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        for user in c_copy['users']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            original_users = [ u for u in c_json['users'] if u['title'] == user['title'] ]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
            self.assertEquals(1,len(original_users))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            for k in (user.keys() - ['id']):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
                self.assertEquals(user[k], original_users[0][k])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        self.assertEquals(len(c_copy['views']), len(c_json['views']))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        for view in c_copy['views']:
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            original_views = [ v for v in c_json['views'] if v['title'] == view['title'] ]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
            self.assertEquals(1,len(original_views))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            orig_view = original_views[0]
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            for k in (view.keys() - ['id', 'hidden_nodes']):
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                self.assertEquals(view[k], orig_view[k])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            self.assertEquals(len(view['hidden_nodes']), len(orig_view['hidden_nodes']))
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
            copy_hidden_nodes_titles = sorted([ [n for n in c_copy['nodes'] if n['id'] == n_id][0]['title'] for n_id in view['hidden_nodes']])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
            orig_hidden_nodes_titles = sorted([ [n for n in c_json['nodes'] if n['_id'] == n_id][0]['title'] for n_id in orig_view['hidden_nodes']])
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
            self.assertEquals(copy_hidden_nodes_titles, orig_hidden_nodes_titles)