<commit_before>"""Test identifiers"""
import json
import sys

from flask_webtest import SessionScope
import pytest

from portal.extensions import db
from portal.models.identifier import Identifier
from portal.models.user import User
from tests import TEST_USER_ID, TestCase

if sys.version_info.major > 2:
    pytest.skip(msg="not yet ported to python3", allow_module_level=True)
class TestIdentifier(TestCase):

    def testGET(self):
        expected = len(self.test_user.identifiers)
        self.login()
        rv = self.client.get('/api/user/{}/identifier'.format(TEST_USER_ID))
        self.assert200(rv)
        self.assertEqual(len(rv.json['identifier']), expected)

    def testPOST(self):
        """Add an existing and fresh identifier - confirm it sticks"""
        expected = len(self.test_user.identifiers) + 2
        existing = Identifier(system='http://notreal.com', value='unique')
        with SessionScope(db):
            db.session.add(existing)
            db.session.commit()
        existing = db.session.merge(existing)
        fresh = Identifier(system='http://another.com', value='unique')
        data = {'identifier': [i.as_fhir() for i in (existing, fresh)]}
        self.login()
        rv = self.client.post(
            '/api/user/{}/identifier'.format(TEST_USER_ID),
            content_type='application/json', data=json.dumps(data))
        self.assert200(rv)
        self.assertEqual(len(rv.json['identifier']), expected)
        user = User.query.get(TEST_USER_ID)
        self.assertEqual(len(user.identifiers), expected)
<commit_msg>cleaned up test_identifier.py<commit_after>"""Test identifiers"""
from __future__ import unicode_literals  # isort:skip

import json

from flask_webtest import SessionScope

from portal.extensions import db
from portal.models.identifier import Identifier
from portal.models.user import User
from tests import TEST_USER_ID, TestCase

class TestIdentifier(TestCase):

    def testGET(self):
        expected = len(self.test_user.identifiers)
        self.login()
        response = self.client.get('/api/user/{}/identifier'.format(TEST_USER_ID))
        assert response.status_code == 200
        assert len(response.json['identifier']) == expected

    def testPOST(self):
        """Add an existing and fresh identifier - confirm it sticks"""
        expected = len(self.test_user.identifiers) + 2
        existing = Identifier(system='http://notreal.com', value='unique')
        with SessionScope(db):
            db.session.add(existing)
            db.session.commit()
        existing = db.session.merge(existing)
        fresh = Identifier(system='http://another.com', value='unique')
        data = {'identifier': [i.as_fhir() for i in (existing, fresh)]}
        self.login()
        response = self.client.post(
            '/api/user/{}/identifier'.format(TEST_USER_ID),
            content_type='application/json', data=json.dumps(data))
        assert response.status_code == 200
        assert len(response.json['identifier']) == expected
        user = User.query.get(TEST_USER_ID)
        assert len(user.identifiers) == expected

{'fraction_non_alphanumeric': 0.1233974358974359, 'fraction_numerical': 0.005128205128205128, 'mean_word_length': 3.1948924731182795, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2940103', 'n_tokens_mistral': 890, 'n_tokens_neox': 867, 'n_words': 195}