Ejemplo n.º 1
0
    def test_csv_without_fieldname_header_write(self):
        """
        Specify fields. Without this fields are taken from first record to be added.
        """
        data_dir = tempfile.mkdtemp()
        csv_file = os.path.join(data_dir, "lemurs.csv")
        c = CsvConnector(
            engine_url="csv://" + csv_file,
            access=ayeaye.AccessMode.WRITE,
            field_names=["common_name", "main_colours"],
        )
        for lemur in [
            {
                "common_name": "Indri"
            },
            {
                "common_name": "Ring tailed",
                "main_colours": "grey, black, white"
            },
        ]:
            c.add(lemur)

        c.close_connection()

        with open(csv_file, "r", encoding=c.encoding) as f:
            csv_content = f.read()

        expected_content = ("common_name,main_colours\n"
                            "Indri,\n"
                            'Ring tailed,"grey, black, white"\n')

        self.assertEqual(expected_content, csv_content)
Ejemplo n.º 2
0
    def test_optional_args_with_write(self):
        """Read only connector arguments raise exception when called in write mode.
        """
        data_dir = tempfile.mkdtemp()
        csv_file = os.path.join(data_dir, "garden_insects.csv")

        for optional_field in [
                'required_fields', 'expected_fields', 'alias_fields'
        ]:

            c = CsvConnector(
                engine_url="csv://" + csv_file,
                access=ayeaye.AccessMode.WRITE,
                **{optional_field: 'xyz'},
            )
            with self.assertRaises(ValueError):
                c.add({"common_name": "Grasshopper"})
Ejemplo n.º 3
0
    def test_csv_write(self):
        """
        Write to a CSV without using a schema.
        """
        data_dir = tempfile.mkdtemp()
        csv_file = os.path.join(data_dir, "fish.csv")
        c = CsvConnector(engine_url="csv://" + csv_file,
                         access=ayeaye.AccessMode.WRITE)

        # two data types that can be added
        p = ayeaye.Pinnate({"common_name": "Angel fish"})
        c.add(p)

        d = {"common_name": "Grey reef shark"}
        c.add(d)
        c.close_connection()  # flush to disk

        with open(csv_file, "r", encoding=c.encoding) as f:
            csv_content = f.read()

        expected_content = "common_name\n" "Angel fish\n" "Grey reef shark\n"

        self.assertEqual(expected_content, csv_content)