Ejemplo n.º 1
0
    def test_write_file(self):
        """
        Test BaseIO.save() when starting from an empty IO instance
        """
        for ext, cls in zip(self.types, self.classes):
            filename = self.get_filename("output", ext, True)

            # Create an empty instance of the class
            instance = cls(
                filename=filename,
                field_names=['one', 'two', 'three'],

                # These only apply to XmlFileIO, will be ignored by the others
                root_tag="root",
                item_tag="item"
            )

            # Add rows to the instance using list-style BaseIO.append()
            for row in self.data:
                instance.append(instance.create(**row))

            # Save the instance, which should write to output.[ext]
            instance.save()

            # The contents of the saved file should match the original data
            self.check_instance(load_file(filename))
 def load_io(self, **options):
     from wq.io import load_file
     from django.conf import settings
     filename = "%s/%s" % (settings.MEDIA_ROOT, self.file.name)
     if not options:
         options = self.load_file_options()
     return load_file(filename, options=options)
Ejemplo n.º 3
0
    def test_write_file(self):
        """
        Test BaseIO.save() when starting from an empty IO instance
        """
        for ext, cls in zip(self.types, self.classes):
            filename = self.get_filename("output", ext, True)

            # Create an empty instance of the class
            instance = cls(
                filename=filename,
                field_names=['one', 'two', 'three'],

                # These only apply to XmlFileIO, will be ignored by the others
                root_tag="root",
                item_tag="item"
            )

            # Add rows to the instance using list-style BaseIO.append()
            for row in self.data:
                instance.append(instance.create(**row))

            # Save the instance, which should write to output.[ext]
            instance.save()

            # The contents of the saved file should match the original data
            self.check_instance(load_file(filename))
Ejemplo n.º 4
0
    def duplicate(self, mode, xform):
        """
        Test BaseIO.copy/sync() (and implicit save()) between combinations of
        the default IO classes.
        """
        for source_ext, source_cls in zip(self.types, self.classes):
            for dest_ext, dest_cls in zip(self.types, self.classes):
                source_file = self.get_filename("test", source_ext)
                dest_file = self.get_filename(mode, dest_ext, True)

                # Sync requires key_field to be set on both classes
                source_cls = xform(source_cls)
                dest_cls = xform(dest_cls)

                # Load source data into IO instance
                source_instance = source_cls(filename=source_file)

                # Create empty instance of the destination IO class
                dest_instance = dest_cls(
                    filename=dest_file,
                    field_names=['one', 'two', 'three'],
                    root_tag="root",
                    item_tag="item",
                )

                # The Sync
                getattr(source_instance, mode)(dest_instance)

                # Load the destination file again and check contents
                self.check_instance(load_file(dest_file))
Ejemplo n.º 5
0
    def duplicate(self, mode, xform):
        """
        Test BaseIO.copy/sync() (and implicit save()) between combinations of
        the default IO classes.
        """
        for source_ext, source_cls in zip(self.types, self.classes):
            for dest_ext, dest_cls in zip(self.types, self.classes):
                source_file = self.get_filename("test", source_ext)
                dest_file = self.get_filename(mode, dest_ext, True)

                # Sync requires key_field to be set on both classes
                source_cls = xform(source_cls)
                dest_cls = xform(dest_cls)

                # Load source data into IO instance
                source_instance = source_cls(filename=source_file)

                # Create empty instance of the destination IO class
                dest_instance = dest_cls(
                    filename=dest_file,
                    field_names=['one', 'two', 'three'],
                    root_tag="root",
                    item_tag="item",
                )

                # The Sync
                getattr(source_instance, mode)(dest_instance)

                # Load the destination file again and check contents
                self.check_instance(load_file(dest_file))
Ejemplo n.º 6
0
 def test_auto_pickle(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         # Run through the io once to ensure auto-generated data is present
         self.check_instance(instance)
         instance = pickle.loads(pickle.dumps(instance))
         self.check_instance(instance)
Ejemplo n.º 7
0
 def test_auto_pickle(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         # Run through the io once to ensure auto-generated data is present
         self.check_instance(instance)
         instance = pickle.loads(pickle.dumps(instance))
         self.check_instance(instance)
Ejemplo n.º 8
0
    def test_load_csv_unicode(self):
        filename = self.get_filename("test3", "csv")
        instance = load_file(filename)
        self.check_instance(instance)
        self.assertTrue(hasattr(instance[0], "μ"))

        # Use getattr to avoid Python 2 compile error
        self.assertEqual(getattr(instance[0], "μ"), "test")
Ejemplo n.º 9
0
 def handle_noargs(self, **kwargs):
     types = load_file('../assets/pointtypes.csv')
     for row in types:
         ptype = PointType.objects.find(row.code)
         ptype.name = row.name
         ptype.value = row.value
         ptype.layout = row.layout
         ptype.path = row.path
         ptype.save()
Ejemplo n.º 10
0
    def test_xlsx(self):
        response = self.client.get("/timeseries.xlsx")
        xlfile = open('tests/output.xlsx', 'wb')
        xlfile.write(response.content)
        xlfile.close()

        data = load_file("tests/output.xlsx")
        self.assertEqual(len(data), 5)
        self.assertEqual(data[0].date.year, 2014)
        self.assertEqual(data[0].value, 0.5)
Ejemplo n.º 11
0
    def test_xlsx(self):
        response = self.client.get("/timeseries.xlsx")
        self.assertEqual(
            'attachment; filename="Time Series.xlsx"',
            response['content-disposition'],
        )
        xlfile = open('tests/output.xlsx', 'wb')
        xlfile.write(response.content)
        xlfile.close()

        data = load_file("tests/output.xlsx")
        self.assertEqual(len(data), 5)
        self.assertEqual(data[0].date.year, 2014)
        self.assertEqual(data[0].value, 0.5)
Ejemplo n.º 12
0
    def setUp(self):
        if environ.get('CLIMATA_DIRECT', None):
            return
        httpretty.enable()
        for row in load_file(join('tests', 'file_list.csv')):
            if row.module != self.module:
                continue
            f = file(join('tests', 'files', row.module + '_' + row.filename))
            data = f.read()
            f.close()
            method = getattr(httpretty, row.method.upper())
            url = row.url
            for char in '.?+':
                url = url.replace(char, '\\' + char)

            httpretty.register_uri(
                method,
                re.compile(url),
                body=data,
                match_querystring=True,
            )
Ejemplo n.º 13
0
    def setUp(self):
        if environ.get('CLIMATA_DIRECT', None):
            return
        httpretty.enable()
        for row in load_file(join('tests', 'file_list.csv')):
            if row.module != self.module:
                continue
            f = file(join('tests', 'files', row.module + '_' + row.filename))
            data = f.read()
            f.close()
            method = getattr(httpretty, row.method.upper())
            url = row.url
            for char in '.?+':
                url = url.replace(char, '\\' + char)

            httpretty.register_uri(
                method,
                re.compile(url),
                body=data,
                match_querystring=True,
            )
Ejemplo n.º 14
0
 def test_load_nodata(self):
     filename = self.get_filename("nodata", "csv")
     instance = load_file(filename)
     with self.assertRaises(NoData) as cm:
         instance[0]
     self.assertEqual(str(cm.exception), "No data returned!")
Ejemplo n.º 15
0
 def test_load_csv_prelude(self):
     filename = self.get_filename("test2", "csv")
     instance = load_file(filename)
     self.check_instance(instance)
Ejemplo n.º 16
0
 def test_load_file(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         self.check_instance(instance)
Ejemplo n.º 17
0
 def load_io(self):
     from wq.io import load_file
     from django.conf import settings
     filename = "%s/%s" % (settings.MEDIA_ROOT, self.file.name)
     options = self.load_file_options(self.run)
     return load_file(filename, options=options)
Ejemplo n.º 18
0
 def load_io(self):
     from wq.io import load_file
     options = self.load_io_options()
     return load_file(self.file.path, options=options)
Ejemplo n.º 19
0
 def test_load_nodata(self):
     filename = self.get_filename("nodata", "csv")
     instance = load_file(filename)
     with self.assertRaises(NoData) as cm:
         instance[0]
     self.assertEqual(str(cm.exception), "No data returned!")
Ejemplo n.º 20
0
 def test_load_file(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         self.check_instance(instance)
Ejemplo n.º 21
0
 def test_pickle(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         instance = pickle.loads(pickle.dumps(instance))
         self.check_instance(instance)
Ejemplo n.º 22
0
 def test_pickle(self):
     for ext in self.types:
         filename = self.get_filename("test", ext)
         instance = load_file(filename)
         instance = pickle.loads(pickle.dumps(instance))
         self.check_instance(instance)
Ejemplo n.º 23
0
 def load_io(self):
     from wq.io import load_file
     options = self.load_io_options()
     return load_file(self.file.path, options=options)
Ejemplo n.º 24
0
 def test_load_csv_prelude(self):
     filename = self.get_filename("test2", "csv")
     instance = load_file(filename)
     self.check_instance(instance)