def test_threading():
    with NamedTemporaryFile() as tmp_file:
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=tmp_file.name, ), ), )
        graph = create_object_graph("example", testing=True, loader=loader)
        store = PersonStore()

        Person.recreate_all(graph)

        with SessionContext(graph, Example) as context:
            gw = store.create(Person(id=1, first="George",
                                     last="Washington"), )
            tj = store.create(Person(id=2, first="Thomas", last="Jefferson"), )
            context.commit()

        pool = ThreadPool(2)

        store.get_session = GetOrCreateSession(graph)
        people = pool.map(lambda index: store.search()[index], range(2))

        assert_that(people, contains(gw, tj))
示例#2
0
def test_except():
    with NamedTemporaryFile() as tmp_file:
        # NB: not using :memory: sqlite database
        # as it maintains single database connection
        loader = load_from_dict(
            sqlite=dict(
                paths=dict(
                    example=tmp_file.name,
                ),
            ),
        )
        graph = create_object_graph("example", testing=True, loader=loader)
        store = PersonExclusionStore()

        Person.recreate_all(graph)

        person1 = Person(
            id=1,
            first="John",
            last="Krakow",
        )

        person2 = Person(
            id=2,
            first="Susan",
            last="Krakow",
        )

        with SessionContext(graph, Example):
            store.create(person1)
            store.create(person2)

            assert_that(
                store.search(
                    last="Krakow",
                    exclude_first="John",
                    limit=5,
                ),
                contains(person2),
            )
def test_foreign_keys():
    with NamedTemporaryFile() as tmp_file:
        # NB: not using :memory: sqlite database
        # as it maintains single database connection
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=tmp_file.name, ), ), )
        graph = create_object_graph("example", testing=True, loader=loader)
        store = DogStore()

        Dog.recreate_all(graph)

        dog = Dog(
            id=1,
            name="Hooch",
            owner_id=999,
        )

        with SessionContext(graph, Example):
            assert_that(
                calling(store.create).with_args(dog),
                raises(ModelIntegrityError),
            )
    def new_context(cls, graph, **kwargs):
        """
        Create a new session context.

        """
        return SessionContext(graph=graph, data_set=cls.resolve(), **kwargs)