<commit_before>from annotator.model import Annotation, Range, Consumer
from annotator.model import create_all, drop_all, session

from nose.tools import assert_raises

class TestAnnotation():
    def setup(self):
        create_all()

    def teardown(self):
        session.remove()
        drop_all()

    def test_id(self):
        ann = Annotation()
        session.commit()
        assert isinstance(ann.id, int), "annotation id wasn't an integer"

    def test_text(self):
        ann = Annotation(text="Hello there")
        assert ann.text == "Hello there", "annotation text wasn't set"

    def test_user(self):
        ann = Annotation(user="Alice")
        assert ann.user == "Alice", "annotation user wasn't set"

    def test_ranges(self):
        ann = Annotation()
        ann.ranges.append(Range())
        ann.ranges.append(Range())
        assert len(ann.ranges) == 2, "ranges weren't added to annotation"

    def test_to_dict(self):
        ann = Annotation(text="Foo")
        data = {'ranges': [], 'text': 'Foo', 'id': None, 'user': None}
        assert ann.to_dict() == data, "annotation wasn't converted to dict correctly"

    def test_to_dict_with_range(self):
        ann = Annotation(text="Bar")
        ann.ranges.append(Range())
        assert len(ann.to_dict()['ranges']) == 1, "annotation ranges weren't in dict"

    def test_from_dict(self):
        ann = Annotation()
        ann.from_dict({'text': 'Baz'})
        assert ann.text == "Baz", "annotation wasn't updated from dict"

    def test_from_dict_with_range(self):
        ann = Annotation()
        ann.from_dict({'ranges': [{}]})
        assert len(ann.ranges) == 1, "annotation ranges weren't updated from dict"

    def test_extras_in(self):
        ann = Annotation()
        ann.from_dict({'foo':1, 'bar':2})
        print ann.extras
        assert ann.extras == '{"foo": 1, "bar": 2}', "extras weren't serialized properly"

    def test_extras_out(self):
        ann = Annotation(extras='{"bar": 3, "baz": 4}')
        data = ann.to_dict()
        assert data['bar'] == 3, "extras weren't deserialized properly"
        assert data['baz'] == 4, "extras weren't deserialized properly"

    def test_authorise_read_nouser(self):
        ann = Annotation()<commit_msg>[tests/model][xs]: correct overly fragile test (depended on dict key ordering).<commit_after>from annotator.model import Annotation, Range, Consumer
from annotator.model import create_all, drop_all, session

from nose.tools import assert_raises

class TestAnnotation():
    def setup(self):
        create_all()

    def teardown(self):
        session.remove()
        drop_all()

    def test_id(self):
        ann = Annotation()
        session.commit()
        assert isinstance(ann.id, int), "annotation id wasn't an integer"

    def test_text(self):
        ann = Annotation(text="Hello there")
        assert ann.text == "Hello there", "annotation text wasn't set"

    def test_user(self):
        ann = Annotation(user="Alice")
        assert ann.user == "Alice", "annotation user wasn't set"

    def test_ranges(self):
        ann = Annotation()
        ann.ranges.append(Range())
        ann.ranges.append(Range())
        assert len(ann.ranges) == 2, "ranges weren't added to annotation"

    def test_to_dict(self):
        ann = Annotation(text="Foo")
        data = {'ranges': [], 'text': 'Foo', 'id': None, 'user': None}
        outdict = ann.to_dict()
        for k,v in data.items():
            assert outdict[k] == v, "annotation wasn't converted to dict correctly"

    def test_to_dict_with_range(self):
        ann = Annotation(text="Bar")
        ann.ranges.append(Range())
        assert len(ann.to_dict()['ranges']) == 1, "annotation ranges weren't in dict"

    def test_from_dict(self):
        ann = Annotation()
        ann.from_dict({'text': 'Baz'})
        assert ann.text == "Baz", "annotation wasn't updated from dict"

    def test_from_dict_with_range(self):
        ann = Annotation()
        ann.from_dict({'ranges': [{}]})
        assert len(ann.ranges) == 1, "annotation ranges weren't updated from dict"

    def test_extras_in(self):
        ann = Annotation()
        ann.from_dict({'foo':1, 'bar':2})
        print ann.extras
        assert ann.extras == '{"foo": 1, "bar": 2}', "extras weren't serialized properly"

    def test_extras_out(self):
        ann = Annotation(extras='{"bar": 3, "baz": 4}')
        data = ann.to_dict()
        assert data['bar'] == 3, "extras weren't deserialized properly"
        assert data['baz'] == 4, "extras weren't deserialized properly"

    def test_authorise_read_nouser(self):
        ann = Annotation()
{'fraction_non_alphanumeric': 0.1381310418904404, 'fraction_numerical': 0.00472610096670247, 'mean_word_length': 2.8929765886287626, 'pattern_counts': {'":': 8, '<': 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': '2860322', 'n_tokens_mistral': 1498, 'n_tokens_neox': 1414, 'n_words': 380}