<commit_before>from wallace import networks, nodes, db, models
import random
from nose.tools import assert_raises


class TestNetworks(object):

    def setup(self):
        self.db = db.init_db(drop_all=True)

    def teardown(self):
        self.db.rollback()
        self.db.close()

    def test_create_network(self):
        net = models.Network()
        assert isinstance(net, models.Network)

    def test_node_failure(self):
        net = networks.Network()
        for _ in range(5):
            agent = nodes.Agent()
            self.db.add(agent)
            net.add(agent)
        agent = None
        source = nodes.Source()
        self.db.add(source)
        net.add(source)
        self.db.commit()

        assert len(net.nodes(type=nodes.Agent)) == 5

        random.choice(net.nodes(type=nodes.Agent)).fail()

        assert len(net.nodes(type=nodes.Agent)) == 4
        assert len(net.nodes(type=nodes.Agent, status="all")) == 5
        assert len(net.nodes()) == 5
        assert len(net.nodes(status="all")) == 6
        assert len(net.nodes(status="failed")) == 1

    def test_network_agents(self):
        net = networks.Network()
        assert len(net.nodes(type=nodes.Agent)) == 0

        agent = nodes.Agent()
        self.db.add(agent)

        net.add(agent)

        self.db.commit()

        assert net.nodes(type=nodes.Agent) == [agent]
        assert isinstance(net, models.Network)

    def test_network_sources(self):
        net = networks.Network()

        assert len(net.nodes(type=nodes.Source)) == 0

        source = nodes.Source()
        net.add(source)
        self.db.add(source)
        self.db.commit()

        assert net.nodes(type=nodes.Source) == [source]

    def test_network_nodes(self):
        net = models.Network()

        node1 = models.Node()
        node2 = models.Node()
        agent1 = nodes.Agent()
        agent2 = nodes.Agent()
        agent3 = nodes.Agent()

        net.add([node1, node2, agent1, agent2, agent3])
        self.db.add_all([node1, node2, agent1, agent2, agent3])
        self.db.commit()

        assert net.nodes() == [node1, node2, agent1, agent2, agent3]
        assert net.nodes(type=nodes.Agent) == [agent1, agent2, agent3]

        node1.die()
        agent1.fail()

        assert net.nodes() == [node2, agent2, agent3]
        assert net.nodes(status="all") == [node1, node2, agent1, agent2, agent3]
        assert net.nodes(status="dead") == [node1]
        assert net.nodes(type=nodes.Agent, status="all") == [agent1, agent2, agent3]

    def test_network_vectors(self):
        net = networks.Network()

        assert len(net.vectors()) == 0

        agent1 = nodes.Agent()
        agent2 = nodes.Agent()
        self.db.add_all([agent1, agent2])

        net.add(agent1)
        net.add(agent2)
        self.db.commit()

        agent1.connect_to(agent2)
        self.db.commit()<commit_msg>Try to fix tests<commit_after>from wallace import networks, nodes, db, models
import random
from nose.tools import assert_raises


class TestNetworks(object):

    def setup(self):
        self.db = db.init_db(drop_all=True)

    def teardown(self):
        self.db.rollback()
        self.db.close()

    def test_create_network(self):
        net = models.Network()
        assert isinstance(net, models.Network)

    def test_node_failure(self):
        net = networks.Network()
        for _ in range(5):
            agent = nodes.Agent()
            self.db.add(agent)
            net.add(agent)
        agent = None
        source = nodes.Source()
        self.db.add(source)
        net.add(source)
        self.db.commit()

        assert len(net.nodes(type=nodes.Agent)) == 5

        random.choice(net.nodes(type=nodes.Agent)).fail()

        assert len(net.nodes(type=nodes.Agent)) == 4
        assert len(net.nodes(type=nodes.Agent, status="all")) == 5
        assert len(net.nodes()) == 5
        assert len(net.nodes(status="all")) == 6
        assert len(net.nodes(status="failed")) == 1

    def test_network_agents(self):
        net = networks.Network()
        assert len(net.nodes(type=nodes.Agent)) == 0

        agent = nodes.Agent()
        self.db.add(agent)

        net.add(agent)

        self.db.commit()

        assert net.nodes(type=nodes.Agent) == [agent]
        assert isinstance(net, models.Network)

    def test_network_sources(self):
        net = networks.Network()

        assert len(net.nodes(type=nodes.Source)) == 0

        source = nodes.Source()
        net.add(source)
        self.db.add(source)
        self.db.commit()

        assert net.nodes(type=nodes.Source) == [source]

    def test_network_nodes(self):
        net = models.Network()

        node1 = models.Node()
        node2 = models.Node()
        agent1 = nodes.Agent()
        agent2 = nodes.Agent()
        agent3 = nodes.Agent()

        net.add([node1, node2, agent1, agent2, agent3])
        self.db.add_all([node1, node2, agent1, agent2, agent3])
        self.db.commit()

        assert net.nodes() == [node1, node2, agent1, agent2, agent3]
        assert net.nodes(type=nodes.Agent) == [agent1, agent2, agent3]

        node1.die()
        agent1.fail()

        self.db.add_all([node1, node2, agent1, agent2, agent3])
        self.db.commit()

        assert net.nodes() == [node2, agent2, agent3]
        assert net.nodes(status="all") == [node1, node2, agent1, agent2, agent3]
        assert net.nodes(status="dead") == [node1]
        assert net.nodes(type=nodes.Agent, status="all") == [agent1, agent2, agent3]

    def test_network_vectors(self):
        net = networks.Network()

        assert len(net.vectors()) == 0

        agent1 = nodes.Agent()
        agent2 = nodes.Agent()
        self.db.add_all([agent1, agent2])

        net.add(agent1)
        net.add(agent2)
        self.db.commit()

        agent1.connect_to(agent2)
        self.db.commit()
{'fraction_non_alphanumeric': 0.13943206937595648, 'fraction_numerical': 0.01955449753443292, 'mean_word_length': 2.247929320817228, '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': '775369', 'n_tokens_mistral': 1906, 'n_tokens_neox': 1824, 'n_words': 366}