server/python/django2/renkanmanager/tests/test_models.py
author ymh <ymh.work@gmail.com>
Wed, 29 Jun 2016 23:11:56 +0200
changeset 626 112912309726
child 665 69d13e7dd286
permissions -rw-r--r--
ensure that we change the ids of elements when copying/creating a new renkan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
626
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
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
     2
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
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
     4
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
     5
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
     6
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
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
     8
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
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
    10
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
    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
    12
        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
    13
        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
    14
            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
    15
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    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
    17
        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
    18
        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
    19
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    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
    21
        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
    22
        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
    23
        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
    24
        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
    25
            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
    26
            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
    27
        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
    28
            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
    29
            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
    30
        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
    31
            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
    32
            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
    33
        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
    34
            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
    35
            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
    36
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    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
    38
        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
    39
        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
    40
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        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
    42
            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
    43
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        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
    45
        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
    46
            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
    47
            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
    48
            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
    49
                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
    50
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        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
    52
        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
    53
            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
    54
            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
    55
            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
    56
            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
    57
                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
    58
            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
    59
            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
    60
            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
    61
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            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
    63
            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
    64
            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
    65
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        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
    67
        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
    68
            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
    69
            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
    70
            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
    71
                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
    72
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        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
    74
        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
    75
            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
    76
            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
    77
            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
    78
            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
    79
                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
    80
            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
    81
            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
    82
            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
    83
112912309726 ensure that we change the ids of elements when copying/creating a new renkan
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
            self.assertEquals(copy_hidden_nodes_titles, orig_hidden_nodes_titles)