def setup(self):
        self.tmp_file = NamedTemporaryFile()
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=self.tmp_file.name, ), ), )
        self.graph = create_object_graph("example",
                                         testing=True,
                                         loader=loader)
        self.builder = self.graph.sqlite_builder

        self.dog_store = DogStore()
        self.person_store = PersonStore()

        Example.create_all(self.graph)

        self.people = csv(
            dedent("""
            id,first,last
             1,Stephen,Curry
             2,Klay,Thompson
        """))

        self.dogs = csv(
            dedent("""
            id,name,owner_id
             1,Rocco,2
             2,Reza,1
             3,Rookie,1
        """))
    def setup(self):
        self.tmp_file = NamedTemporaryFile()
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=self.tmp_file.name, ), ), )
        self.graph = create_object_graph("example",
                                         testing=True,
                                         loader=loader)
        self.dumper = self.graph.sqlite_dumper

        self.person_store = PersonStore()

        Example.create_all(self.graph)

        self.outfile = StringIO()
        with Example.new_context(self.graph):
            self.person_store.create(
                Person(
                    id="1",
                    first="Stephen",
                    last="Curry",
                ))
            self.person_store.create(
                Person(
                    id="2",
                    first="Klay",
                    last="Thompson",
                ))
            self.person_store.session.commit()
        self.context = Person.new_context(self.graph).open()
示例#3
0
    def setup(self):
        self.graph = create_object_graph("example", testing=True)

        self.gc = Person(id=1, first="George", last="Clinton")
        self.gw = Person(id=2, first="George", last="Washington")
        self.rf = Person(id=3, first="Rosalind", last="Franklin")
        self.store = PersonStore()

        Person.recreate_all(self.graph)
        self.context = Person.new_context(self.graph).open()
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))
示例#5
0
class TestStore:
    def setup(self):
        self.graph = create_object_graph("example", testing=True)

        self.gc = Person(id=1, first="George", last="Clinton")
        self.gw = Person(id=2, first="George", last="Washington")
        self.rf = Person(id=3, first="Rosalind", last="Franklin")
        self.store = PersonStore()

        Person.recreate_all(self.graph)
        self.context = Person.new_context(self.graph).open()

    def populate(self):
        self.store.create(self.gw)
        self.store.create(self.rf)
        self.store.create(self.gc)
        self.context.commit()

    def teardown(self):
        self.context.close()
        Person.dispose(self.graph)

    def test_create_integrity_error(self):
        assert_that(
            calling(self.store.create).with_args(
                Person(id=3, first=None, last=None)),
            raises(ModelIntegrityError),
        )

    def test_create_after_integrity_error(self):
        assert_that(
            calling(self.store.create).with_args(
                Person(id=3, first=None, last=None)),
            raises(ModelIntegrityError),
        )

        # create works after IntegrityError
        self.store.create(self.gw)
        assert_that(self.store.search(), contains(self.gw))

    def test_create_duplicate_error(self):
        self.store.create(Person(id=3, first="First", last="Last"))
        self.context.commit()

        assert_that(
            calling(self.store.create).with_args(
                Person(id=3, first="First", last="Last")),
            raises(DuplicateModelError),
        )

    def test_delete(self):
        self.populate()

        assert_that(
            self.store.delete(first=self.gw.first, last=self.gw.last),
            is_(equal_to(True)),
        )
        assert_that(
            calling(self.store.delete).with_args(first=self.gw.first,
                                                 last=self.gw.last),
            raises(ModelNotFoundError),
        )

        assert_that(self.store.count(), is_(equal_to(2)))

        self.store.delete()

        assert_that(self.store.count(), is_(equal_to(0)))

    def test_count(self):
        assert_that(self.store.count(), is_(equal_to(0)))

        self.populate()

        assert_that(self.store.count(), is_(equal_to(3)))
        # pagination arguments are not supported for count and should be ignored
        # if passed in
        assert_that(self.store.count(offset=1), is_(equal_to(3)))
        assert_that(self.store.count(limit=1), is_(equal_to(3)))
        assert_that(self.store.count(offset=1, limit=1), is_(equal_to(3)))

    def test_first(self):
        assert_that(self.store.first(), is_(none()))

        self.populate()

        assert_that(self.store.first(), is_(equal_to(self.gc)))
        assert_that(self.store.first(offset=1), is_(equal_to(self.gw)))
        assert_that(self.store.first(limit=1), is_(equal_to(self.gc)))
        assert_that(self.store.first(offset=1, limit=1),
                    is_(equal_to(self.gw)))

    def test_one(self):
        assert_that(
            calling(self.store.one),
            raises(ModelNotFoundError),
        )

        self.populate()

        assert_that(
            calling(self.store.one),
            raises(MultipleModelsFoundError),
        )

        assert_that(
            calling(self.store.one).with_args(first="George"),
            raises(MultipleModelsFoundError),
        )

        assert_that(self.store.one(first="Rosalind"), is_(equal_to(self.rf)))
        assert_that(self.store.one(limit=1), is_(equal_to(self.gc)))
        assert_that(self.store.one(first="George", offset=1),
                    is_(equal_to(self.gw)))
        assert_that(self.store.one(offset=1, limit=1), is_(equal_to(self.gw)))

    def test_search(self):
        assert_that(self.store.search(), is_(empty()))

        self.populate()

        assert_that(self.store.search(), contains(self.gc, self.gw, self.rf))
        assert_that(self.store.search(offset=1), contains(self.gw, self.rf))
        assert_that(self.store.search(limit=2), contains(self.gc, self.gw))
        assert_that(self.store.search(offset=1, limit=1), contains(self.gw))
class TestCSVBuilders:
    def setup(self):
        self.tmp_file = NamedTemporaryFile()
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=self.tmp_file.name, ), ), )
        self.graph = create_object_graph("example",
                                         testing=True,
                                         loader=loader)
        self.builder = self.graph.sqlite_builder

        self.dog_store = DogStore()
        self.person_store = PersonStore()

        Example.create_all(self.graph)

        self.people = csv(
            dedent("""
            id,first,last
             1,Stephen,Curry
             2,Klay,Thompson
        """))

        self.dogs = csv(
            dedent("""
            id,name,owner_id
             1,Rocco,2
             2,Reza,1
             3,Rookie,1
        """))

    def teardown(self):
        self.tmp_file.close()

    def test_build_with_csv_builder(self):
        self.builder.csv(Person).build(self.people)
        self.builder.csv(Dog).build(self.dogs)

        with Example.new_context(self.graph):
            dogs = self.dog_store.search()
            people = self.person_store.search()

            assert_that(
                dogs,
                contains(
                    has_properties(name="Reza", ),
                    has_properties(name="Rocco", ),
                    has_properties(name="Rookie", ),
                ),
            )
            assert_that(
                people,
                contains(
                    has_properties(first="Klay", ),
                    has_properties(first="Stephen", ),
                ),
            )

            for dog in dogs:
                assert_that(dog.is_a_good_boy, is_(equal_to(True)))

            assert_that(dogs[0].owner, is_(equal_to(people[1])))

    def test_build_with_bulk_csv_builder(self):
        bulk_input = [
            (Person, self.people),
            (Dog, self.dogs),
        ]

        self.builder.csv(Example).bulk().build(bulk_input)

        with Example.new_context(self.graph):
            dogs = self.dog_store.search()
            people = self.person_store.search()

            assert_that(
                dogs,
                contains(
                    has_properties(name="Reza", ),
                    has_properties(name="Rocco", ),
                    has_properties(name="Rookie", ),
                ),
            )

            assert_that(
                people,
                contains(
                    has_properties(first="Klay", ),
                    has_properties(first="Stephen", ),
                ),
            )

    def test_bulk_builder_when_violating_foreign_keys(self):
        dogs = csv(
            dedent("""
            id,name,owner_id
             1,Rocco,2
             2,Reza,1
             3,Rookie,1
             4,Stretch,3
        """))

        more_people = csv(
            dedent("""
            id,first,last
             3,John,Doe
        """))

        bulk_input = [
            (Person, self.people),
            (Dog, dogs),
            (Person, more_people),
        ]

        self.builder.csv(Example).bulk().build(bulk_input)

        with Example.new_context(self.graph):
            dogs = self.dog_store.search()
            people = self.person_store.search()

            assert_that(
                dogs,
                contains(
                    has_properties(name="Reza", ),
                    has_properties(name="Rocco", ),
                    has_properties(name="Rookie", ),
                    has_properties(name="Stretch", ),
                ),
            )

            assert_that(
                people,
                contains(
                    has_properties(first="John", ),
                    has_properties(first="Klay", ),
                    has_properties(first="Stephen", ),
                ),
            )

    def test_build_with_csv_builder_default(self):
        """
        Known values can be defaulted (for entire CSVs)

        """
        people = csv(
            dedent("""
            id,first
             1,Stephen
             2,Dell
        """))
        self.builder.csv(Person).default(last="Curry").build(people)
class TestCSVDumpers:
    def setup(self):
        self.tmp_file = NamedTemporaryFile()
        loader = load_from_dict(sqlite=dict(paths=dict(
            example=self.tmp_file.name, ), ), )
        self.graph = create_object_graph("example",
                                         testing=True,
                                         loader=loader)
        self.dumper = self.graph.sqlite_dumper

        self.person_store = PersonStore()

        Example.create_all(self.graph)

        self.outfile = StringIO()
        with Example.new_context(self.graph):
            self.person_store.create(
                Person(
                    id="1",
                    first="Stephen",
                    last="Curry",
                ))
            self.person_store.create(
                Person(
                    id="2",
                    first="Klay",
                    last="Thompson",
                ))
            self.person_store.session.commit()
        self.context = Person.new_context(self.graph).open()

    def teardown(self):
        self.tmp_file.close()

    def test_dump_with_csv_dump(self):
        self.dumper.csv(self.person_store).dump(
            self.outfile,
            field_names=["id", "first", "last"],
        )
        assert_that(
            self.outfile.getvalue(),
            equal_to(
                "id,first,last\r\n1,Stephen,Curry\r\n2,Klay,Thompson\r\n"))

    def test_dump_with_csv_dump_custom_header(self):
        self.dumper.csv(self.person_store).dump(
            self.outfile,
            field_names=["id", "first", "last"],
            custom_header=dict(
                id="ID",
                first="First Name",
                last="Last Name",
            ),
        )
        assert_that(
            self.outfile.getvalue(),
            equal_to(
                "ID,First Name,Last Name\r\n1,Stephen,Curry\r\n2,Klay,Thompson\r\n"
            ))

    def test_dump_selected_portion(self):
        self.dumper.csv(self.person_store).dump(
            self.outfile,
            items=self.person_store.search(first="Klay"),
            field_names=["id", "first", "last"],
        )
        assert_that(self.outfile.getvalue(),
                    equal_to("id,first,last\r\n2,Klay,Thompson\r\n"))