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 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 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_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])))
示例#5
0
    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",
                    ),
                ),
            )