Ejemplo n.º 1
0
class TestMultipleModels:
    def setUp(self):
        self.content = OrderedDict()
        self.content.update({"Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
        self.content.update({"Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
        self.result1 = [{'Y': 4, 'X': 1, 'Z': 7}, {'Y': 5, 'X': 2, 'Z': 8}, {'Y': 6, 'X': 3, 'Z': 9}]
        self.result2 = [{'B': 4, 'A': 1, 'C': 7}, {'B': 5, 'A': 2, 'C': 8}, {'B': 6, 'A': 3, 'C': 9}]

    def test_save_to_more_models(self):
        model1=FakeDjangoModel()
        model2=FakeDjangoModel()
        to_store = {
            "Sheet1": self.content['Sheet1'][1:],
            "Sheet2": self.content['Sheet2'][1:]
        }
        writer = DjangoBookWriter(DB_DJANGO, {
            "Sheet1": [model1, self.content['Sheet1'][0], None, None],
            "Sheet2": [model2, self.content['Sheet2'][0], None, None]
        })
        writer.write(to_store)
        writer.close()
        assert model1.objects.objs == self.result1
        assert model2.objects.objs == self.result2

    def test_reading_from_more_models(self):
        model1=FakeDjangoModel()
        model2=FakeDjangoModel()
        to_store = {
            "Sheet1": self.content['Sheet1'][1:],
            "Sheet2": self.content['Sheet2'][1:]
        }
        save_data(DB_DJANGO, to_store, models={
            "Sheet1": [model1, self.content['Sheet1'][0], None, None],
            "Sheet2": [model2, self.content['Sheet2'][0], None, None]
        })
        assert model1.objects.objs == self.result1
        assert model2.objects.objs == self.result2
        model1._meta.model_name = "Sheet1"
        model2._meta.model_name = "Sheet2"
        model1._meta.update(["X", "Y", "Z"])
        model2._meta.update(["A", "B", "C"])
        reader = DjangoBookReader([model1, model2])
        data = reader.sheets()
        assert data == self.content

    def test_special_case_where_only_one_model_used(self):
        model1=FakeDjangoModel()
        to_store = {
            "Sheet1": self.content['Sheet1'][1:],
            "Sheet2": self.content['Sheet2'][1:]
        }
        save_data(DB_DJANGO, to_store, models={
            "Sheet1": [model1, self.content['Sheet1'][0], None, None]
        })
        assert model1.objects.objs == self.result1
        model1._meta.model_name = "Sheet1"
        model1._meta.update(["X", "Y", "Z"])
        reader = DjangoBookReader([model1])
        data = reader.sheets()
        assert data['Sheet1'] == self.content['Sheet1']
Ejemplo n.º 2
0
 def test_pyexcel_xls_issue_2(self):
     data = OrderedDict()
     array = []
     for i in range(4100):
         array.append([datetime.datetime.now()])
     data.update({"test": array})
     s = xls.save_data("test.xls", data)
     os.unlink("test.xls")
Ejemplo n.º 3
0
 def setUp(self):
     self.content = OrderedDict()
     self.content.update({
         'Sheet 1': 
             [
                 [1.0, 2.0, 3.0], 
                 [4.0, 5.0, 6.0], 
                 [7.0, 8.0, 9.0]
             ]
     })
     self.content.update({
         'Sheet 2': 
             [
                 ['X', 'Y', 'Z'], 
                 [1.0, 2.0, 3.0], 
                 [4.0, 5.0, 6.0]
             ]
     })
     self.content.update({
         'Sheet 3': 
             [
                 ['O', 'P', 'Q'], 
                 [3.0, 2.0, 1.0], 
                 [4.0, 3.0, 2.0]
             ] 
     })
     self.file="mybook.csvz"
     save_data(self.file, self.content)
Ejemplo n.º 4
0
 def setUp(self):
     self.file_type = "csv"
     self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type
     self.data1 = [
         ["1", "2", "3"],
         ["4", "5", "6"],
         ["7", "8", "9"]
     ]
     self.data2 = [
         ["1", "2", "3"],
         ["4", "5", "6"],
         ["7", "8", "1000"]
     ]
     self.data3 = [
         ["1", "2", "3"],
         ["4", "5", "6888"],
         ["7", "8", "9"]
     ]
     self.sheets = OrderedDict()
     self.sheets.update({"sheet1": self.data1})
     self.sheets.update({"sheet2": self.data2})
     self.sheets.update({"sheet3": self.data3})
     self.result_dict = OrderedDict()        
     self.result1 = dedent("""
        1,2,3
        4,5,6
        7,8,9
     """).strip('\n')
     self.result2 = dedent("""
        1,2,3
        4,5,6
        7,8,1000
     """).strip('\n')
     self.result3 = dedent("""
        1,2,3
        4,5,6888
        7,8,9
     """).strip('\n')
     self.result_dict.update({"sheet1": self.result1})
     self.result_dict.update({"sheet2": self.result2})
     self.result_dict.update({"sheet3": self.result3})
Ejemplo n.º 5
0
class TestMultipleSheet:
    def setUp(self):
        self.content = OrderedDict()
        self.content.update({
            'Sheet 1': 
                [
                    [1.0, 2.0, 3.0], 
                    [4.0, 5.0, 6.0], 
                    [7.0, 8.0, 9.0]
                ]
        })
        self.content.update({
            'Sheet 2': 
                [
                    ['X', 'Y', 'Z'], 
                    [1.0, 2.0, 3.0], 
                    [4.0, 5.0, 6.0]
                ]
        })
        self.content.update({
            'Sheet 3': 
                [
                    ['O', 'P', 'Q'], 
                    [3.0, 2.0, 1.0], 
                    [4.0, 3.0, 2.0]
                ] 
        })
        self.file="mybook.csvz"
        save_data(self.file, self.content)

    def test_read_one_from_many_by_name(self):
        b = CSVZipBook(self.file, load_sheet_with_name="Sheet 1")
        sheets = b.sheets()
        assert sheets['Sheet 1'] == [
            [u'1.0', u'2.0', u'3.0'],
            [u'4.0', u'5.0', u'6.0'],
            [u'7.0', u'8.0', u'9.0']
        ]
        
    @raises(ValueError)
    def test_read_one_from_many_by_unknown_name(self):
        CSVZipBook(self.file, load_sheet_with_name="Sheet X")

    def test_read_one_from_many_by_index(self):
        b = CSVZipBook(self.file, load_sheet_at_index=0)
        sheets = b.sheets()
        assert sheets['Sheet 1'] == [
            [u'1.0', u'2.0', u'3.0'],
            [u'4.0', u'5.0', u'6.0'],
            [u'7.0', u'8.0', u'9.0']
        ]

    @raises(IndexError)
    def test_read_one_from_many_by_unknown_index(self):
        CSVZipBook(self.file, load_sheet_at_index=999)

    def tearDown(self):
        os.unlink(self.file)
Ejemplo n.º 6
0
 def setUp(self):
     Base.metadata.drop_all(engine)
     Base.metadata.create_all(engine)
     self.session = Session()
     data = {
         "Category":[
             ["id", "name"],
             [1, "News"],
             [2, "Sports"]
         ],
         "Post":[
             ["id", "title", "body", "pub_date", "category"],
             [1, "Title A", "formal", datetime.datetime(2015,1,20,23,28,29), "News"],
             [2, "Title B", "informal", datetime.datetime(2015,1,20,23,28,30), "Sports"]
         ]
      }
     def category_init_func(row):
         c = Category(row['name'])
         c.id = row['id']
         return c
     
     def post_init_func(row):
         c = self.session.query(Category).filter_by(name=row['category']).first()
         p = Post(row['title'], row['body'], c, row['pub_date'])
         return p
     tables = {
         "Category": [Category, data['Category'][0], None, category_init_func],
         "Post": [Post, data['Post'][0], None, post_init_func]
     }
     to_store = OrderedDict()
     to_store.update({"Category": data['Category'][1:]})
     to_store.update({"Post": data['Post'][1:]})
     writer = SQLBookWriter(DB_SQL,
                            session=self.session,
                            tables=tables)
     writer.write(to_store)
     writer.close()
Ejemplo n.º 7
0
 def setUp(self):
     self.file_type = "csv"
     self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type
     self.data = [
         ["1", "2", "3"],
         ["4", "5", "6"],
         ["7", "8", "9"]
     ]
     self.sheets = OrderedDict()
     self.sheets.update({"sheet1": self.data})
     self.sheets.update({"sheet2": self.data})
     self.sheets.update({"sheet3": self.data})
     index = 0
     for key, value in self.sheets.items():
         file_name = self.test_file_formatter % (key, index)
         with open(file_name, 'w') as f:
             for row in value:
                 f.write(",".join(row) + "\n")
         index = index + 1
Ejemplo n.º 8
0
class TestReadMultipleSheets:
    def setUp(self):
        self.file_type = "csv"
        self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type
        self.data = [
            ["1", "2", "3"],
            ["4", "5", "6"],
            ["7", "8", "9"]
        ]
        self.sheets = OrderedDict()
        self.sheets.update({"sheet1": self.data})
        self.sheets.update({"sheet2": self.data})
        self.sheets.update({"sheet3": self.data})
        index = 0
        for key, value in self.sheets.items():
            file_name = self.test_file_formatter % (key, index)
            with open(file_name, 'w') as f:
                for row in value:
                    f.write(",".join(row) + "\n")
            index = index + 1

    def test_multiple_sheet(self):
        b = CSVBook("csv_multiple.csv")
        sheets = b.sheets()
        assert sheets == self.sheets

    def test_read_one_from_many_by_name(self):
        b = CSVBook("csv_multiple.csv", load_sheet_with_name="sheet1")
        sheets = b.sheets()
        assert sheets["sheet1"] == self.sheets["sheet1"]

    @raises(ValueError)
    def test_read_one_from_many_by_non_existent_name(self):
        CSVBook("csv_multiple.csv", load_sheet_with_name="notknown")

    def test_read_one_from_many_by_index(self):
        b = CSVBook("csv_multiple.csv", load_sheet_at_index=1)
        sheets = b.sheets()
        assert sheets["sheet2"] == self.sheets["sheet2"]

    @raises(IndexError)
    def test_read_one_from_many_by_wrong_index(self):
        CSVBook("csv_multiple.csv", load_sheet_at_index=90)
        
    def tearDown(self):
        index = 0
        for key, value in self.sheets.items():
            file_name = self.test_file_formatter % (key, index)
            os.unlink(file_name)
            index = index + 1
Ejemplo n.º 9
0
class TestWriteMultipleSheets:
    def setUp(self):
        self.file_type = "csv"
        self.test_file_formatter = "csv_multiple__%s__%s." + self.file_type
        self.data1 = [
            ["1", "2", "3"],
            ["4", "5", "6"],
            ["7", "8", "9"]
        ]
        self.data2 = [
            ["1", "2", "3"],
            ["4", "5", "6"],
            ["7", "8", "1000"]
        ]
        self.data3 = [
            ["1", "2", "3"],
            ["4", "5", "6888"],
            ["7", "8", "9"]
        ]
        self.sheets = OrderedDict()
        self.sheets.update({"sheet1": self.data1})
        self.sheets.update({"sheet2": self.data2})
        self.sheets.update({"sheet3": self.data3})
        self.result_dict = OrderedDict()        
        self.result1 = dedent("""
           1,2,3
           4,5,6
           7,8,9
        """).strip('\n')
        self.result2 = dedent("""
           1,2,3
           4,5,6
           7,8,1000
        """).strip('\n')
        self.result3 = dedent("""
           1,2,3
           4,5,6888
           7,8,9
        """).strip('\n')
        self.result_dict.update({"sheet1": self.result1})
        self.result_dict.update({"sheet2": self.result2})
        self.result_dict.update({"sheet3": self.result3})

    def test_multiple_sheet(self):
        """Write csv book into multiple file"""
        b = CSVWriter("csv_multiple.csv")
        for key, value in self.sheets.items():
            w = b.create_sheet(key)
            w.write_array(value)
            w.close()
        b.close()
        index = 0
        for key, value in self.sheets.items():
            file_name = self.test_file_formatter % (key, index)
            with open(file_name, 'r') as f:
                content = f.read().replace('\r', '')
                assert content.strip('\n') == self.result_dict[key]
            index = index + 1

    #def test_multiple_sheet_into_memory(self):
    #    """Write csv book into a single stream"""
    #    io = get_io(self.file_type)
    #    b = CSVWriter(io)
    #    for key, value in self.sheets.items():
    #        w = b.create_sheet(key)
    #        w.write_array(value)
    #        w.close()
    #    b.close()
    #    content = io.getvalue()    
    #    index = 0
    #    for key, value in self.content.split('---pyexcel---\r\n'):
    #        file_name = self.test_file_formatter % (key, index)
    #        with open(file_name, 'r') as f:
    #            content = f.read().replace('\r', '')
    #            assert content.strip('\n') == self.result_dict[key]
    #        index = index + 1

    def tearDown(self):
        index = 0
        for key, value in self.sheets.items():
            file_name = self.test_file_formatter % (key, index)
            os.unlink(file_name)
            index = index + 1
Ejemplo n.º 10
0
 def setUp(self):
     self.content = OrderedDict()
     self.content.update({"Sheet1": [[u'X', u'Y', u'Z'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
     self.content.update({"Sheet2": [[u'A', u'B', u'C'], [1, 4, 7], [2, 5, 8], [3, 6, 9]]})
     self.result1 = [{'Y': 4, 'X': 1, 'Z': 7}, {'Y': 5, 'X': 2, 'Z': 8}, {'Y': 6, 'X': 3, 'Z': 9}]
     self.result2 = [{'B': 4, 'A': 1, 'C': 7}, {'B': 5, 'A': 2, 'C': 8}, {'B': 6, 'A': 3, 'C': 9}]