mardi 14 juillet 2020

Python Unit Test Start Directory Not Importable

I have encountered this error and tried a number of fixes posted on this site but have had no luck. I wrote a unit test and cannot call it due to the Start Directory Not Importable error. My file structure is:

Root
    └── tests
        └── fixtures
            └── customFile.json
        ├── __init__.py
        └── functionTest.py
    └── Lambdas
        └── FunctionA
            ├── __init__.py
            └── FunctionToTest.py

and my unit test looks like this:

import unittest
import json
import boto3

from Lambdas.FunctionA import FunctionToTest

s3 = boto3.client('s3', 'us-east-1')
file = s3.get_object(Bucket="BUCKET", Key="KEY")

mapper = s3.get_object(Key="KEY", Bucket="BUCKET")
mapperJSON = json.loads(mapper['Body'].read().decode('utf-8'))

path = r"./tests/fixtures/customFile.json"
with open(path) as file:
    endList = json.load(file)
    file.close()

class TestMatchModeler(unittest.TestCase):
    def test_run(self):
        test = FunctionToTest(file, mapperJSON)
        self.assertEqual(len(test), len(endList))


if __name__ == '__main__':
    unittest.main()

and the actual code it's testing is:

def FunctionToTest(file, mapperjson):
    Fields = mapperjson['mappings']

    lines1 = file['Body'].read().decode('utf-8').split('\n')
    fieldnames = lines1[0].replace('"','').split(',')
    testls = [row for row in csv.DictReader(lines1[1:], fieldnames)]
    out = json.dumps(testls)
    jlist1 = json.loads(out)

    dicts = []
    Fieldsinv = {v: k for k, v in Fields.items()}
    for i in jlist1:
        d = {}
        for k, v in i.items():
            if k in Fieldsinv:
                d[Fieldsinv[k]] = v
        d['individual_id'] = str(uuid.uuid4())
        dicts.append(d)
    return dicts

Essentially the customFile.json is a list of dictionaries that I know are correct, and I want to test that my code modifies a CSV correctly to have the same number of dicts. This question has been asked a lot and the answer is always to have the files linked via an init.py file but as you can see I've done that and it still doesn't work.

I'm sure this may get flagged as a duplicate but im not sure what else to try.

Aucun commentaire:

Enregistrer un commentaire