def test_import_from_txt_retrieve_desired_data(self, mocked_create_table): mocked_create_table.return_value = 42 # import using filename rows.import_from_txt(self.filename) call_args = mocked_create_table.call_args_list[0] self.assert_create_table_data(call_args) # import using fobj with open(self.filename, mode="rb") as fobj: rows.import_from_txt(fobj) call_args = mocked_create_table.call_args_list[1] self.assert_create_table_data(call_args)
def test_import_from_txt_retrieve_desired_data(self, mocked_create_table): mocked_create_table.return_value = 42 # import using filename rows.import_from_txt(self.filename) call_args = mocked_create_table.call_args_list[0] self.assert_create_table_data(call_args) # import using fobj with open(self.filename, mode='rb') as fobj: rows.import_from_txt(fobj) call_args = mocked_create_table.call_args_list[1] self.assert_create_table_data(call_args)
def test_import_from_txt_uses_create_table(self, mocked_create_table): mocked_create_table.return_value = 42 kwargs = { 'some_key': 123, 'other': 456, } result = rows.import_from_txt(self.filename, encoding=self.encoding, **kwargs) self.assertTrue(mocked_create_table.called) self.assertEqual(mocked_create_table.call_count, 1) self.assertEqual(result, 42) call = mocked_create_table.call_args called_data = call[1] meta = called_data.pop("meta") self.assertEqual(call[1], kwargs) expected_meta = { 'imported_from': 'txt', 'filename': self.filename, 'encoding': self.encoding, } for key in expected_meta: self.assertEqual(expected_meta[key], meta[key]) # test won't break if frame_style default changes in the future self.assertIn('frame_style', meta)
def test_import_from_txt_uses_create_table(self, mocked_create_table): mocked_create_table.return_value = 42 kwargs = {"some_key": 123, "other": 456} result = rows.import_from_txt(self.filename, encoding=self.encoding, **kwargs) self.assertTrue(mocked_create_table.called) self.assertEqual(mocked_create_table.call_count, 1) self.assertEqual(result, 42) call = mocked_create_table.call_args called_data = call[1] meta = called_data.pop("meta") self.assertEqual(call[1], kwargs) expected_meta = { "imported_from": "txt", "filename": self.filename, "encoding": self.encoding, } for key in expected_meta: self.assertEqual(expected_meta[key], meta[key]) # test won't break if frame_style default changes in the future self.assertIn("frame_style", meta)
def _test_import_from_txt_works_with_custom_frame(self, frame_style): temp = tempfile.NamedTemporaryFile(delete=False) # self.files_to_delete.append(temp.name) print("*" * 100, temp.name) original_data = rows.import_from_txt(self.filename) rows.export_to_txt(utils.table, temp.file, encoding='utf-8', frame_style=frame_style) new_data = rows.import_from_txt(temp.name) self.assertEqual( list(new_data), list(original_data), msg='failed to read information with frame_style == "{0}"'.format( frame_style))
def test_export_to_txt_fobj(self): # TODO: may test with codecs.open passing an encoding # TODO: may test file contents temp = tempfile.NamedTemporaryFile(delete=False) self.files_to_delete.append(temp.name) rows.export_to_txt(utils.table, temp.file, encoding='utf-8') table = rows.import_from_txt(temp.name, encoding='utf-8') self.assert_table_equal(table, utils.table)
def _test_import_from_txt_works_with_custom_frame(self, frame_style): temp = tempfile.NamedTemporaryFile(delete=False) original_data = rows.import_from_txt(self.filename) rows.export_to_txt( utils.table, temp.file, encoding="utf-8", frame_style=frame_style ) self._reset_txt_plugin() new_data = rows.import_from_txt(temp.name) self.assertEqual( list(new_data), list(original_data), msg='failed to read information with frame_style == "{0}"'.format( frame_style ), )
def test_export_to_txt_fobj(self): # TODO: may test with codecs.open passing an encoding # TODO: may test file contents temp = tempfile.NamedTemporaryFile(delete=False) self.files_to_delete.append(temp.name) rows.export_to_txt(utils.table, temp.file, encoding="utf-8") table = rows.import_from_txt(temp.name, encoding="utf-8") self.assert_table_equal(table, utils.table)
def test_issue_168(self): temp = tempfile.NamedTemporaryFile(delete=False) filename = "{}.{}".format(temp.name, self.file_extension) self.files_to_delete.append(filename) table = rows.Table(fields=OrderedDict([("jsoncolumn", rows.fields.JSONField)])) table.append({"jsoncolumn": '{"python": 42}'}) rows.export_to_txt(table, filename, encoding="utf-8") table2 = rows.import_from_txt(filename, encoding="utf-8") self.assert_table_equal(table, table2)
def test_import_from_txt_uses_create_table(self, mocked_create_table): mocked_create_table.return_value = 42 kwargs = {'encoding': self.encoding, 'some_key': 123, 'other': 456, } result = rows.import_from_txt(self.filename, **kwargs) self.assertTrue(mocked_create_table.called) self.assertEqual(mocked_create_table.call_count, 1) self.assertEqual(result, 42) call = mocked_create_table.call_args kwargs['meta'] = {'imported_from': 'txt', 'filename': self.filename, } self.assertEqual(call[1], kwargs)
def test_issue_168(self): temp = tempfile.NamedTemporaryFile(delete=False) filename = '{}.{}'.format(temp.name, self.file_extension) self.files_to_delete.append(filename) table = rows.Table(fields=OrderedDict([('jsoncolumn', rows.fields.JSONField)])) table.append({'jsoncolumn': '{"python": 42}'}) rows.export_to_txt(table, filename, encoding='utf-8') table2 = rows.import_from_txt(filename, encoding='utf-8') self.assert_table_equal(table, table2)
def test_export_to_txt_filename(self): # TODO: may test file contents temp = tempfile.NamedTemporaryFile(delete=False) self.files_to_delete.append(temp.name) rows.export_to_txt(utils.table, temp.name, encoding='utf-8') table = rows.import_from_txt(temp.name, encoding='utf-8') self.assert_table_equal(table, utils.table) with open(temp.name, mode='rb') as fobj: content = fobj.read() self.assertEqual(content[-10:].count(b'\n'), 1)
def test_export_to_txt_filename(self): # TODO: may test file contents temp = tempfile.NamedTemporaryFile(delete=False) self.files_to_delete.append(temp.name) rows.export_to_txt(utils.table, temp.name, encoding="utf-8") table = rows.import_from_txt(temp.name, encoding="utf-8") self.assert_table_equal(table, utils.table) with open(temp.name, mode="rb") as fobj: content = fobj.read() self.assertEqual(content[-10:].count(b"\n"), 1)
def test_import_from_txt_uses_create_table(self, mocked_create_table): mocked_create_table.return_value = 42 kwargs = {'some_key': 123, 'other': 456, } result = rows.import_from_txt(self.filename, encoding=self.encoding, **kwargs) self.assertTrue(mocked_create_table.called) self.assertEqual(mocked_create_table.call_count, 1) self.assertEqual(result, 42) call = mocked_create_table.call_args kwargs['meta'] = {'imported_from': 'txt', 'filename': self.filename, 'encoding': self.encoding,} self.assertEqual(call[1], kwargs)