jeudi 24 août 2017

Testing file import

I am trying to test my file imports for files with a .xlsx extension. I have the creation of the imported table working but I can't get the assertions to work.

Here is my current test:

class testImport(TestCase):
    def setUp(self):
        self.client = Client()

    @classmethod
    def setUpTestData(cls):
        user = get_user_model().objects.create(username='xlsx_importer')
        table_name = "Test Import"
        version_name = "Master"
        publish = False
        cls.user = user
        cls.table_name = table_name
        cls.version_name = version_name
        cls.publish = publish

    def test_xlsx_import(self):
        tmp = tempfile.NamedTemporaryFile(suffix='.xlsx')
        workbook = xlsxwriter.Workbook(tmp)
        worksheet = workbook.add_worksheet()
        worksheet.set_column('A:A', 20)
        worksheet.write('A1', 'Hello')
        worksheet.write('A2', '1')
        worksheet.write('B1', 'World')
        worksheet.write('B2', '2')
        tmp.seek(0)
        workbook.close()

        table = s.create_table_from_file(
            file=tmp,
            table_name=self.table_name,
            version_name=self.version_name,
            publish=self.publish,
            created_by=self.user,
        )

What I want to do is go through the columns attached to that table (through the ORM) and make a data structure that maps from friendly name to id (and therefore dictionary key).

I can currently get:

<QuerySet [{'name': 'World', 'content_type_id': 49, 'id': 44, 'order': 1, 'commit_id': 14}, {'name': 'Hello', 'content_type_id': 49, 'id': 43, 'order': 0, 'commit_id': 14}]>

By using

table.commit_set.all()[0].column_set.all().values()

The Queryset I am testing against is:

<QuerySet [{'id': 1, '43': '1', '44': '2'}]>

where '43' equates to Hello and '44' equates to World from the first Queryset.

The assertion I want to make is to see if the values '1' and '2' are present, so as I mentioned before I need to use that first Queryset to map the friendly name (i.e. name) to the id.

Any help with this would be much appreciated.

Thanks for your time.

Aucun commentaire:

Enregistrer un commentaire