test_action.py 6.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import asyncio
import os
import json
from copy import deepcopy

from aiohttp import test_utils
from motor import motor_asyncio

from registry_mirror.main import App
from prozorro_sale.registry.migration import _migrate
from prozorro_sale.registry.model_generator import load_action_object

REPLICA_DB = os.environ['REPLICA_DB']
CLIENT_DB = os.environ['CLIENT_DB']
CLIENT_MONGO_URL = os.environ['CLIENT_MONGO_URL']
MONGO_URL = os.environ['MONGO_URL']

ACTION_OBJECT = None
fixtures_path = 'fixtures'
fixture_name = 'action.json'
with open(os.path.join(fixtures_path, fixture_name)) as f:
    ACTION_OBJECT = json.load(f)


class WebSocketActionTest(test_utils.AioHTTPTestCase):

    def setUp(self):
        super().setUp()
        self.mongo = motor_asyncio.AsyncIOMotorClient(MONGO_URL)
        self.db = self.mongo[REPLICA_DB]
        self.client_mongo = motor_asyncio.AsyncIOMotorClient(CLIENT_MONGO_URL)
        self.db_copy = self.client_mongo[CLIENT_DB]

    async def tearDownAsync(self):
        await self.db.action.delete_many({})
        await self.db_copy.action.delete_many({})
        await super().tearDownAsync()

    async def get_application(self):
        return App()._init_app()

    @test_utils.unittest_run_loop
    async def test_ping(self):
        self.assertTrue(True)

    @test_utils.unittest_run_loop
    async def test_insert_one(self):
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertIsNotNone(client_obj)
        self.assertEqual(document['actingEntity']['contactPoint']['name']['uk_UA'],
                         client_obj['actingEntity']['contactPoint']['name']['uk_UA'])

    @test_utils.unittest_run_loop
    async def test_update(self):
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(document['actingEntity']['contactPoint']['name']['uk_UA'],
                         client_obj['actingEntity']['contactPoint']['name']['uk_UA'])

        await self.db.action.update_one(
            {"_id": result.inserted_id},
            {'$set': {'actingEntity': {'contactPoint': {'name': {'uk_UA': 'updated_value'}}}}}
        )
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'updated_value')

    @test_utils.unittest_run_loop
80
    async def test_update_with_invalid_value(self):
81
82
83
84
85
86
87
88
89
90
91
92
93
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'magna commodo officia')

        await self.db.action.update_one({"_id": result.inserted_id},
                                        {'$set': {'actingEntity.contactPoint.name.uk_UA': None}})
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
94
95
96

        # Should not be updated because the value changes to invalid
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'magna commodo officia')
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

    @test_utils.unittest_run_loop
    async def test_replace(self):
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'magna commodo officia')

        document = await self.db.action.find_one({"_id": result.inserted_id})
        document['actingEntity']['contactPoint']['name']['uk_UA'] = 'replaced_value'
        await self.db.action.replace_one({"_id": result.inserted_id}, document, upsert=True)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'replaced_value')

    @test_utils.unittest_run_loop
    async def test_update_upsert(self):
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'magna commodo officia')

        await self.db.action.update_one(
            {"_id": result.inserted_id},
            {'$set': {'actingEntity': {'contactPoint': {'name': {'uk_UA': 'updated_value_upsert'}}}}},
            upsert=True
        )
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'],
                         'updated_value_upsert')

    @test_utils.unittest_run_loop
    async def test_delete(self):
        document = deepcopy(ACTION_OBJECT)
        document = _migrate(document, 'action')
        document = load_action_object(document).to_native()
        document.pop('_id', None)
        result = await self.db.action.insert_one(document)
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertEqual(client_obj['actingEntity']['contactPoint']['name']['uk_UA'], 'magna commodo officia')

        await self.db.action.delete_one({"_id": result.inserted_id})
        await asyncio.sleep(2)
        client_obj = await self.db_copy.action.find_one({"_id": result.inserted_id})
        self.assertIsNone(client_obj)