<commit_before>from pytest import raises

from xii import entity, error

import factories


def test_init():
    e = factories.EntityWithStore(name="init")
    assert(e.entity() == "init")

def test_has_parent():
    p = factories.EntityWithStore()
    c = factories.Entity(parent=p)

    assert(c.has_parent() == True)

def test_entity_path():
    p = factories.EntityWithStore()
    c = factories.Entity(parent=p)

    assert(c.entity_path() == [p.entity(), c.entity()])

def test_entity():
    e = factories.EntityWithStore(name="test")
    assert(e.entity() == "test")

def test_store():
    s = factories.StoreWithNode()
    e = factories.Entity(store=s)

    assert(e.store() == s)

def test_config():
    st = factories.StoreWithNode()
    pa = factories.EntityWithStore(store=st)
    ch = factories.Entity(parent=pa)
    c2 = factories.Entity(parent=ch)

    assert(pa.config("image") == st.get("image"))
    assert(ch.config("image") == st.get("image"))
    assert(c2.config("image") == st.get("image"))

def test_add_child():
    pa  = factories.EntityWithStore(name="pa")
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)

    pa.add_child(a)
    pa.add_child(b)
    pa.add_child(c)

    assert(pa._childs == [a, b, c])

def test_validate():

    pa  = factories.EntityWithStore()
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)

    pa.add_child(a)
    pa.add_child(b)
    pa.add_child(c)

    pa.validate(["a", "b", "c"])

    with raises(error.NotFound):
        pa.validate(["a", "b", "d"])


def test_share():
    pa  = factories.EntityWithStore()

    def mock_creator():
        return "sample"

    def mock_finalizer(value):
        assert(value == "sample")

    result = pa.share("foo", mock_creator, mock_finalizer)
    assert(result == "sample")
    assert(pa.share("foo", mock_creator, mock_finalizer) == "sample")

def test_finalize(capsys):
    pa  = factories.EntityWithStore()

    def mock_creator():
        return "sample"

    def mock_finalizer(value):
        print("test_finalizer_called")
        assert(value == "sample")

    pa.share("foo", mock_creator, mock_finalizer)
    pa.finalize()
    out, _ = capsys.readouterr()
    assert("test_finalizer_called" in out)


def test_finalize_childs(capsys):

    def add_test_share(f):
        def _cr():
            return f.entity()

        def _fin(value):
            print("finalize-{}".format(value))
            assert(value == f.entity())
        return f.share("test-{}".format(f.entity()), _cr, _fin)

    pa  = factories.EntityWithStore(name="pa")
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)

    pa.add_child(a)
    pa.add_child(b)
    pa.add_child(c)

    add_test_share(pa)
    add_test_share(c)

    pa.finalize()
    out, _ = capsys.readouterr()
    assert("finalize-pa" in out)
    assert("finalize-c" in out)


def test_run(capsys):
    class Test(entity.Entity):
        def test(self):
            print("test-{}".format(self.entity()))

    t = Test(name="t")
    t.run("test")
    out, _ = capsys.readouterr()
    assert("test-t" in out)

def test_get_child():
    pa  = factories.EntityWithStore(name="pa")
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)<commit_msg>Remove child finalize test since childs are not finalized anymore<commit_after>from pytest import raises

from xii import entity, error

import factories


def test_init():
    e = factories.EntityWithStore(name="init")
    assert(e.entity() == "init")

def test_has_parent():
    p = factories.EntityWithStore()
    c = factories.Entity(parent=p)

    assert(c.has_parent() == True)

def test_entity_path():
    p = factories.EntityWithStore()
    c = factories.Entity(parent=p)

    assert(c.entity_path() == [p.entity(), c.entity()])

def test_entity():
    e = factories.EntityWithStore(name="test")
    assert(e.entity() == "test")

def test_store():
    s = factories.StoreWithNode()
    e = factories.Entity(store=s)

    assert(e.store() == s)

def test_config():
    st = factories.StoreWithNode()
    pa = factories.EntityWithStore(store=st)
    ch = factories.Entity(parent=pa)
    c2 = factories.Entity(parent=ch)

    assert(pa.config("image") == st.get("image"))
    assert(ch.config("image") == st.get("image"))
    assert(c2.config("image") == st.get("image"))

def test_add_child():
    pa  = factories.EntityWithStore(name="pa")
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)

    pa.add_child(a)
    pa.add_child(b)
    pa.add_child(c)

    assert(pa._childs == [a, b, c])

def test_validate():

    pa  = factories.EntityWithStore()
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)

    pa.add_child(a)
    pa.add_child(b)
    pa.add_child(c)

    pa.validate(["a", "b", "c"])

    with raises(error.NotFound):
        pa.validate(["a", "b", "d"])


def test_share():
    pa  = factories.EntityWithStore()

    def mock_creator():
        return "sample"

    def mock_finalizer(value):
        assert(value == "sample")

    result = pa.share("foo", mock_creator, mock_finalizer)
    assert(result == "sample")
    assert(pa.share("foo", mock_creator, mock_finalizer) == "sample")

def test_finalize(capsys):
    pa  = factories.EntityWithStore()

    def mock_creator():
        return "sample"

    def mock_finalizer(value):
        print("test_finalizer_called")
        assert(value == "sample")

    pa.share("foo", mock_creator, mock_finalizer)
    pa.finalize()
    out, _ = capsys.readouterr()
    assert("test_finalizer_called" in out)

def test_run(capsys):
    class Test(entity.Entity):
        def test(self):
            print("test-{}".format(self.entity()))

    t = Test(name="t")
    t.run("test")
    out, _ = capsys.readouterr()
    assert("test-t" in out)

def test_get_child():
    pa  = factories.EntityWithStore(name="pa")
    a  = factories.Entity(name="a", parent=pa)
    b  = factories.Entity(name="b", parent=pa)
    c  = factories.Entity(name="c", parent=pa)
{'fraction_non_alphanumeric': 0.16438994101210805, 'fraction_numerical': 0.0006209251785159888, 'mean_word_length': 3.3563218390804597, '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': '3484216', 'n_tokens_mistral': 2317, 'n_tokens_neox': 2213, 'n_words': 410}