<commit_before># -*- coding: utf-8 -*-
import unittest
import os
import random
from datetime import timedelta

from openprocurement.api.models import get_now
from openprocurement.api.utils import get_revision_changes
from openprocurement.api.tests.base import test_organization
from openprocurement.concord.tests.base import BaseTenderWebTest
from openprocurement.concord.daemon import conflicts_resolve as resolve
from jsonpatch import make_patch, apply_patch as _apply_patch
#from json_tools import diff, patch as _patch
from iso8601 import parse_date

IGNORE = ['_attachments', '_revisions', 'revisions', 'dateModified', "_id", "_rev", "doc_type"]


def conflicts_resolve(db):
    """ Branch apply algorithm """
    for c in db.view('conflicts/all', include_docs=True, conflicts=True):
        resolve(db, c)


class TenderConflictsTest(BaseTenderWebTest):

    def patch_tender(self, i, j, app):
        for i in range(i, j):
            a = app(i)
            c = random.choice(['USD', 'UAH', 'RUB'])
            response = a.patch_json('/tenders/{}'.format(self.tender_id), {'data': {
                'title': "title changed #{}".format(i),
                'description': "description changed #{}".format(i),
                'value': {
                    'amount': i*1000 + 500,
                    'currency': c
                },
                'minimalStep': {
                    'currency': c
                }
            }})
            self.assertEqual(response.status, '200 OK')

    def test_conflict_tenderID(self):
        self.db2.save({'_id': 'tenderID'})<commit_msg>test_conflict_draft<commit_after># -*- coding: utf-8 -*-
import unittest
import os
import random
from datetime import timedelta

from openprocurement.api.models import get_now
from openprocurement.api.utils import get_revision_changes
from openprocurement.api.tests.base import test_organization
from openprocurement.concord.tests.base import BaseTenderWebTest
from openprocurement.concord.daemon import conflicts_resolve as resolve
from jsonpatch import make_patch, apply_patch as _apply_patch
#from json_tools import diff, patch as _patch
from iso8601 import parse_date

IGNORE = ['_attachments', '_revisions', 'revisions', 'dateModified', "_id", "_rev", "doc_type"]


def conflicts_resolve(db):
    """ Branch apply algorithm """
    for c in db.view('conflicts/all', include_docs=True, conflicts=True):
        resolve(db, c)


class TenderConflictsTest(BaseTenderWebTest):

    def patch_tender(self, i, j, app):
        for i in range(i, j):
            a = app(i)
            c = random.choice(['USD', 'UAH', 'RUB'])
            response = a.patch_json('/tenders/{}'.format(self.tender_id), {'data': {
                'title': "title changed #{}".format(i),
                'description': "description changed #{}".format(i),
                'value': {
                    'amount': i*1000 + 500,
                    'currency': c
                },
                'minimalStep': {
                    'currency': c
                }
            }})
            self.assertEqual(response.status, '200 OK')

    def test_conflict_draft(self):
        data = self.initial_data.copy()
        data['status'] = 'draft'
        response = self.app.post_json('/tenders', {'data': data})
        tender = response.json['data']
        self.assertEqual(tender['status'], 'draft')
        tender_id = tender['id']
        self.couchdb_server.replicate(self.db.name, self.db2.name)
        self.couchdb_server.replicate(self.db2.name, self.db.name)
        response = self.app.patch_json('/tenders/{}'.format(tender_id), {'data': {'status': 'active.enquiries'}})
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.json['data']['status'], 'active.enquiries')
        response = self.app2.patch_json('/tenders/{}'.format(tender_id), {'data': {'status': 'active.enquiries'}})
        self.assertEqual(response.status, '200 OK')
        self.assertEqual(response.json['data']['status'], 'active.enquiries')
        self.couchdb_server.replicate(self.db.name, self.db2.name)
        self.couchdb_server.replicate(self.db2.name, self.db.name)
        self.assertGreater(len(self.db.view('conflicts/all')), 0)
        conflicts_resolve(self.db)
        self.assertEqual(len(self.db.view('conflicts/all')), 0)
        self.couchdb_server.replicate(self.db.name, self.db2.name)
        self.couchdb_server.replicate(self.db2.name, self.db.name)
        self.assertEqual(len(self.db.view('conflicts/all')), 0)

    def test_conflict_tenderID(self):
        self.db2.save({'_id': 'tenderID'})
{'fraction_non_alphanumeric': 0.13773625896154681, 'fraction_numerical': 0.010427981751031936, 'mean_word_length': 2.9083191850594226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2261692', 'n_tokens_mistral': 1462, 'n_tokens_neox': 1389, 'n_words': 292}