def setUp(self):
     self.components = {
         "test_component0": TestComponent0,
         "test_component1": TestComponent1,
         "test_component2": TestComponent2,
         "test_component3": TestComponent3,
         "test_component4": TestComponent4,
         "test_component5": TestComponent5,
     }
     self.component_source = ArrayComponentSource(self.components)
     self.entity1 = self.component_source.create_entity()
     self.entity2 = self.component_source.create_entity()
Ejemplo n.º 2
0
    def setUp(self):
        self.engine = base.engine('sqlite:///:memory:')
        self.Session = base.create_sessionmaker(self.engine)
        base.Base.metadata.create_all(self.engine)

        all_db_components = {}
        all_db_components.update(db_components)
        all_db_components.update(db_room_components)

        all_components = {}
        all_components.update(components)
        all_components.update(room_components)

        self.component_object = ArrayComponentSource(all_components)
        self.db_component_object = DBComponentSource(
            all_db_components, self.Session())
        self.comp_manager = ComponentManager(
            [self.db_component_object, self.component_object])
        self.node_factory = NodeFactoryDB(self.comp_manager)

        starting_room = self.node_factory.create_new_node({"container": {}})

        self.avatar_factory = AvatarFactory(self.node_factory, self.comp_manager, data={
                                            "starting_room": starting_room.id, "starting_location": "temp", "player_id": "temp"})

        self.account_utils = account.AccountUtils(self.avatar_factory)

        self.session = self.Session()
        self.db_component_object.set_session(self.session)
Ejemplo n.º 3
0
def setup_objects(all_db_components, all_components, session):
    object_db = DBComponentSource(all_db_components, session)
    object_array = ArrayComponentSource(all_components)
    component_manager = ComponentManager([object_array])
    db_component_manager = ComponentManager([object_db, object_array])
    node_factory = NodeFactoryDB(component_manager)
    db_node_factory = NodeFactoryDB(db_component_manager)
    player_factory = PlayerFactory(component_manager)
    default_room = "6cb2fa80-ddb1-47bb-b980-31b01d97add5"
    avatar_factory = AvatarFactory(db_node_factory, db_component_manager, {
        "starting_room": default_room,
        "player_id": 0
    })
    account_utils = AccountUtils(avatar_factory)

    return avatar_factory, node_factory, db_node_factory, object_db, player_factory, account_utils
Ejemplo n.º 4
0
    def setUp(self):
        self.engine = base.engine('sqlite://', echo=True)
        self.Session = base.create_sessionmaker(self.engine)
        base.Base.metadata.create_all(self.engine)

        all_db_components = {}
        all_db_components.update(db_components)
        all_db_components.update(db_room_components)

        all_components = {}
        all_components.update(components)
        all_components.update(room_components)

        self.array_component = ArrayComponentSource(all_components)
        self.component_object = DBComponentSource(all_db_components)
        self.comp_manager = ComponentManager(
            [self.component_object, self.array_component])
        self.node_factory = NodeFactoryDB(self.comp_manager)

        self.session = self.Session()
        self.component_object.set_session(self.session)

        self.chair1 = self.comp_manager.create_entity({
            "names": {"name": "chair1"},
            "type": {"type": "chair1"},
            "network_messages": None
        })
        self.chair2 = self.comp_manager.create_entity({
            "names": {"name": "chair1"},
            "type": {"type": "chair1"},
            "creating": {'new_name': 'foo', 'format': 'bar'}
        })
        self.chair3 = self.comp_manager.create_entity({
            "names": {"name": "chair1"},
            "type": {"type": "chair1"},
            "network_messages": None

        })

        room_factory = RoomFactory(self.comp_manager)
        self.test_room = room_factory.create_room("test", 0, 20, 20, 20)
        self.session.commit()
Ejemplo n.º 5
0
    def setUp(self):

        self.components = {
            "test_component0": TestComponent0,
            "test_component1": TestComponent1,
            "test_component2": TestComponent2,
            "test_component3": TestComponent3,
            "test_component4": TestComponent4,
            "test_component5": TestComponent5,
        }

        self.component_source = ArrayComponentSource(self.components)

        self.component_manager = ComponentManager([self.component_source])

        # all components
        self.test_entity1 = self.component_manager.create_entity({
            "test_component0": {
                "some_string": "E1C0"
            },
            "test_component1": {
                "some_string": "E1C1"
            },
            "test_component2": {
                "some_string": "E1C2"
            },
            "test_component3": {
                "some_string": "E1C3"
            },
            "test_component4": {
                "some_string": "E1C4"
            },
        })
        # no components
        self.test_entity2 = self.component_manager.create_entity({})

        # even components
        self.test_entity3 = self.component_manager.create_entity({
            "test_component0": {
                "some_string": "E3C0"
            },
            "test_component2": {
                "some_string": "E3C2"
            },
            "test_component4": {
                "some_string": "E3C4"
            },
        })

        # odd components
        self.test_entity4 = self.component_manager.create_entity({
            "test_component1": {
                "some_string": "E4C1"
            },
            "test_component3": {
                "some_string": "E4C3"
            },
        })

        # copy of entity1 to make sure that they stay distinct
        self.test_entity5 = self.component_manager.create_entity({
            "test_component0": {
                "some_string": "E5C0"
            },
            "test_component1": {
                "some_string": "E5C1"
            },
            "test_component2": {
                "some_string": "E5C2"
            },
            "test_component3": {
                "some_string": "E5C3"
            },
            "test_component4": {
                "some_string": "E5C4"
            },
        })

        self.node_factory = NodeFactoryDB(self.component_manager)
class TestArrayComponentSource(unittest.TestCase):
    def setUp(self):
        self.components = {
            "test_component0": TestComponent0,
            "test_component1": TestComponent1,
            "test_component2": TestComponent2,
            "test_component3": TestComponent3,
            "test_component4": TestComponent4,
            "test_component5": TestComponent5,
        }
        self.component_source = ArrayComponentSource(self.components)
        self.entity1 = self.component_source.create_entity()
        self.entity2 = self.component_source.create_entity()

    def tearDown(self):
        pass

    def testAddGetComponent(self):
        self.component_source.add_component_to_object("test_component0",
                                                      self.entity1.id,
                                                      {"some_string": "test1"})
        comp = self.component_source.get_component(self.entity1.id,
                                                   "test_component0")
        self.assertEqual(comp.some_string, "test1")

    def testGetSupportedSubset(self):
        self.assertEqual(
            sorted(["test_component0", "test_component3"]),
            sorted(
                self.component_source.get_supported_subset([
                    "test_component0", "test_component6", "test_component3",
                    "test_component7"
                ])))

    def testHas(self):
        self.assertTrue(self.component_source.has("test_component0"))
        self.assertTrue(self.component_source.has("test_component1"))
        self.assertTrue(self.component_source.has("test_component2"))
        self.assertTrue(self.component_source.has("test_component3"))
        self.assertTrue(self.component_source.has("test_component4"))
        self.assertTrue(self.component_source.has("test_component5"))
        self.assertFalse(self.component_source.has("test_component6"))

    def testGetEntitiesForComponent(self):
        entities = [self.component_source.create_entity() for _ in range(10)]
        even_entities = []
        odd_entities = []
        for i, e in enumerate(entities):
            counter = i % 2
            self.component_source.add_component_to_object(
                "test_component" + str(0 + counter), e.id, None)
            self.component_source.add_component_to_object(
                "test_component" + str(2 + counter), e.id, None)
            self.component_source.add_component_to_object(
                "test_component" + str(4 + counter), e.id, None)
            if counter == 0:
                even_entities.append(e.id)
            else:
                odd_entities.append(e.id)

        self.assertEqual(
            sorted(even_entities),
            sorted(
                self.component_source.get_entities_for_component(
                    "test_component2")))
        self.assertEqual(
            sorted(odd_entities),
            sorted(
                self.component_source.get_entities_for_component(
                    "test_component5")))

    def test_remove_component(self):
        entity1 = self.component_source.create_entity()
        self.component_source.add_component_to_object("test_component0",
                                                      entity1.id,
                                                      {"some_string": "test1"})
        comp = self.component_source.get_component(entity1.id,
                                                   "test_component0")
        self.assertEqual(comp.some_string, "test1")
        self.component_source.remove_component(entity1.id, "test_component0")
        with self.assertRaises(AttributeError):
            self.component_source.get_component(entity1.id, "test_component0")
Ejemplo n.º 7
0
    def setUp(self):

        self.components1 = {"test_component0": TestComponent0,
                            "test_component1": TestComponent1,
                            "test_component2": TestComponent2,
                            "test_component3": TestComponent3,
                            "test_component8": TestComponent8,

                            }
        self.components2 = {
            "test_component4": TestComponent4,
            "test_component5": TestComponent5,
            "test_component7": TestComponent7,
            "test_component9": TestComponent9,
        }

        self.component_source1 = ArrayComponentSource(self.components1)
        self.component_source2 = ArrayComponentSource(self.components2)

        self.component_manager = ComponentManager(
            [self.component_source1, self.component_source2])

        # all components
        self.test_entity1 = self.component_manager.create_entity({"test_component0": {"some_string": "E1C0"},
                                                                  "test_component1": {"some_string": "E1C1"},
                                                                  "test_component2": {"some_string": "E1C2"},
                                                                  "test_component3": {"some_string": "E1C3"},
                                                                  "test_component4": {"some_string": "E1C4"},
                                                                  })
        # no components
        self.test_entity2 = self.component_manager.create_entity({})

        # even components
        self.test_entity3 = self.component_manager.create_entity({
            "test_component0": {"some_string": "E3C0"},

            "test_component2": {"some_string": "E3C2"},

            "test_component4": {"some_string": "E3C4"},

        })

        # odd components
        self.test_entity4 = self.component_manager.create_entity({"test_component1": {"some_string": "E4C1"},

                                                                  "test_component3": {"some_string": "E4C3"},


                                                                  })

        # copy of entity1 to make sure that they stay distinct
        self.test_entity5 = self.component_manager.create_entity({"test_component0": {"some_string": "E5C0"},
                                                                  "test_component1": {"some_string": "E5C1"},
                                                                  "test_component2": {"some_string": "E5C2"},
                                                                  "test_component3": {"some_string": "E5C3"},
                                                                  "test_component4": {"some_string": "E5C4"},
                                                                  })

        # Component only in second source
        self.test_entity6 = self.component_manager.create_entity(
            {'test_component7': {"some_string": "E6C7"}})

        self.test_entity7 = self.component_manager.create_entity(
            {'test_component3': {"some_string": "E7C7"}, 'test_component7': {'some_string': "E7C3"}})
Ejemplo n.º 8
0
class Test(unittest.TestCase):

    def setUp(self):

        self.components1 = {"test_component0": TestComponent0,
                            "test_component1": TestComponent1,
                            "test_component2": TestComponent2,
                            "test_component3": TestComponent3,
                            "test_component8": TestComponent8,

                            }
        self.components2 = {
            "test_component4": TestComponent4,
            "test_component5": TestComponent5,
            "test_component7": TestComponent7,
            "test_component9": TestComponent9,
        }

        self.component_source1 = ArrayComponentSource(self.components1)
        self.component_source2 = ArrayComponentSource(self.components2)

        self.component_manager = ComponentManager(
            [self.component_source1, self.component_source2])

        # all components
        self.test_entity1 = self.component_manager.create_entity({"test_component0": {"some_string": "E1C0"},
                                                                  "test_component1": {"some_string": "E1C1"},
                                                                  "test_component2": {"some_string": "E1C2"},
                                                                  "test_component3": {"some_string": "E1C3"},
                                                                  "test_component4": {"some_string": "E1C4"},
                                                                  })
        # no components
        self.test_entity2 = self.component_manager.create_entity({})

        # even components
        self.test_entity3 = self.component_manager.create_entity({
            "test_component0": {"some_string": "E3C0"},

            "test_component2": {"some_string": "E3C2"},

            "test_component4": {"some_string": "E3C4"},

        })

        # odd components
        self.test_entity4 = self.component_manager.create_entity({"test_component1": {"some_string": "E4C1"},

                                                                  "test_component3": {"some_string": "E4C3"},


                                                                  })

        # copy of entity1 to make sure that they stay distinct
        self.test_entity5 = self.component_manager.create_entity({"test_component0": {"some_string": "E5C0"},
                                                                  "test_component1": {"some_string": "E5C1"},
                                                                  "test_component2": {"some_string": "E5C2"},
                                                                  "test_component3": {"some_string": "E5C3"},
                                                                  "test_component4": {"some_string": "E5C4"},
                                                                  })

        # Component only in second source
        self.test_entity6 = self.component_manager.create_entity(
            {'test_component7': {"some_string": "E6C7"}})

        self.test_entity7 = self.component_manager.create_entity(
            {'test_component3': {"some_string": "E7C7"}, 'test_component7': {'some_string': "E7C3"}})

    def tearDown(self):
        pass

    def testGetComponentWithNoEntitiesReturnsNone(self):
        es = self.component_manager.get_entities_with_components(
            ["test_component8"])
        self.assertEqual(len(es), 0)

        es = self.component_manager.get_entities_with_components(
            ["test_component9"])
        self.assertEqual(len(es), 0)

        es = self.component_manager.get_entities_with_components(
            ["test_component8", 'test_component7'])
        self.assertEqual(len(es), 0)

        es = self.component_manager.get_entities_with_components(
            ["test_component7", 'test_component8'])
        self.assertEqual(len(es), 0)

        es = self.component_manager.get_entities_with_components(
            ["test_component3", 'test_component9'])
        self.assertEqual(len(es), 0)

        es = self.component_manager.get_entities_with_components(
            ["test_component9", 'test_component3'])
        self.assertEqual(len(es), 0)

    def testGetEntityFromOneComponent(self):
        es = self.component_manager.get_entities_with_components(
            ["test_component7"])
        self.assertEqual(len(es), 2)

    def testGetEntityFromDifferentSources(self):
        es = self.component_manager.get_entities_with_components(
            ['test_component7', 'test_component3'])
        self.assertEqual(len(es), 1)

        es2 = self.component_manager.get_entities_with_components(
            ['test_component3', 'test_component7'])
        self.assertEqual(len(es2), 1)

    def testAddGetComponent(self):
        self.component_manager.add_component_to_object(
            "test_component0", self.test_entity2.id, {"some_string": "test1"})
        comp = self.component_manager.get_component(
            self.test_entity2.id, "test_component0")
        self.assertEqual(comp.some_string, "test1")

    def testGetSupportedSubset(self):
        self.assertEqual(sorted(["test_component0", "test_component3"]), sorted(self.component_source1.get_supported_subset(
            ["test_component0", "test_component6", "test_component3", "test_component7"])))

    def testHas(self):
        self.assertTrue(
            self.component_manager.has_component("test_component0"))
        self.assertTrue(
            self.component_manager.has_component("test_component1"))
        self.assertTrue(
            self.component_manager.has_component("test_component2"))
        self.assertTrue(
            self.component_manager.has_component("test_component3"))
        self.assertTrue(
            self.component_manager.has_component("test_component4"))
        self.assertTrue(
            self.component_manager.has_component("test_component5"))
        self.assertFalse(
            self.component_manager.has_component("test_component6"))

    def testGetEntitiesForComponent(self):
        entities = [self.component_manager.create_entity(
            {}) for _ in range(10)]
        even_entities = []
        odd_entities = []
        for i, e in enumerate(entities):
            counter = i % 2
            self.component_manager.add_component_to_object(
                "test_component" + str(0 + counter), e.id, None)
            self.component_manager.add_component_to_object(
                "test_component" + str(2 + counter), e.id, None)
            self.component_manager.add_component_to_object(
                "test_component" + str(4 + counter), e.id, None)
            if counter == 0:
                even_entities.append(e.id)
            else:
                odd_entities.append(e.id)

        print(entities)
        print(sorted(self.component_manager.get_entities_for_component("test_component2")))
        print(even_entities)
        self.assertTrue(set(even_entities).issubset(
            set(self.component_manager.get_entities_for_component("test_component2"))))
        self.assertTrue(set(odd_entities).issubset(
            set(self.component_manager.get_entities_for_component("test_component5"))))

    def test_remove_component(self):
        entity1 = self.component_manager.create_entity({})
        self.component_manager.add_component_to_object(
            "test_component0", entity1.id, {"some_string": "test1"})
        comp = self.component_manager.get_component(
            entity1.id, "test_component0")
        self.assertEqual(comp.some_string, "test1")
        self.component_manager.remove_component("test_component0", entity1.id)
        with self.assertRaises(AttributeError):
            self.component_manager.get_component(entity1.id, "test_component0")