class TestsBusinessCodeImporterCSVLoad(HPCStatsTestCase):

    def setUp(self):
        self.filename = 'fake'
        self.cluster = Cluster('testcluster')
        HPCStatsConf.__bases__ = (MockConfigParser, object)
        self.conf = HPCStatsConf(self.filename, self.cluster.name)
        self.conf.conf = CONFIG
        self.app = None
        self.db = None
        self.importer = BusinessCodeImporterCSV(self.app, self.db, self.conf)
        init_reqs()

    def test_init(self):
        """ProjectImporterCSV.__init__() runs w/o problem
        """
        pass

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_1(self, m_isfile):
        """BusinessCodeImporterCSV.load() works with simple data
        """

        m_isfile.return_value = True

        csv = "code1;business description 1"

        m_open = mock.mock_open(read_data=csv)
        m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
        with mock.patch("%s.open" % (module), m_open, create=True):
            self.importer.load()

        business = self.importer.businesses[0]
        self.assertEquals(business.code, 'code1')
        self.assertEquals(business.description, 'business description 1')

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_multiple_lines(self, m_isfile):
        """BusinessCodeImporterCSV.load() works with multiple lines
        """

        m_isfile.return_value = True

        csv = "code1;business description 1\n" \
              "code2;business description 2\n"

        m_open = mock.mock_open(read_data=csv)
        m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
        with mock.patch("%s.open" % (module), m_open, create=True):
            self.importer.load()

        self.assertEquals(len(self.importer.businesses), 2)

        business1 = self.importer.businesses[0]
        business2 = self.importer.businesses[1]

        self.assertEquals(business1.code, 'code1')
        self.assertEquals(business1.description, 'business description 1')
        self.assertEquals(business2.code, 'code2')
        self.assertEquals(business2.description, 'business description 2')

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_invalid_line(self, m_isfile):
        """BusinessCodeImporterCSV.load() raise exception when a business code
           line in CSV file is invalid
        """

        m_isfile.return_value = True

        csvs = [ "line without separator",
                 "line;with;3_separators"]

        for csv in csvs:
            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.assertRaisesRegexp(
                       HPCStatsSourceError,
                       "business line format in CSV is invalid",
                       self.importer.load)

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_business_empty_code(self, m_isfile):
        """BusinessCodeImporterCSV.load() raise exception when business code is
           empty
        """

        m_isfile.return_value = True

        codes = [ '', " ", "      " ]
        for code in codes:
            csv = "%s;business description 1" % code

            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.assertRaisesRegexp(
                       HPCStatsSourceError,
                       "business code in CSV is empty",
                       self.importer.load)

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_business_empty_description(self, m_isfile):
        """BusinessCodeImporterCSV.load() set description to None when empty.
        """

        m_isfile.return_value = True

        descs = [ '', " ", "      " ]
        for desc in descs:
            csv = "code1;%s" % desc

            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.importer.load()
                self.assertIsNone(self.importer.businesses[0].description)
示例#2
0
class TestsBusinessCodeImporterCSVLoad(HPCStatsTestCase):
    def setUp(self):
        self.filename = 'fake'
        self.cluster = Cluster('testcluster')
        HPCStatsConf.__bases__ = (MockConfigParser, object)
        self.conf = HPCStatsConf(self.filename, self.cluster.name)
        self.conf.conf = CONFIG
        self.app = None
        self.db = None
        self.importer = BusinessCodeImporterCSV(self.app, self.db, self.conf)
        init_reqs()

    def test_init(self):
        """ProjectImporterCSV.__init__() runs w/o problem
        """
        pass

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_1(self, m_isfile):
        """BusinessCodeImporterCSV.load() works with simple data
        """

        m_isfile.return_value = True

        csv = "code1;business description 1"

        m_open = mock.mock_open(read_data=csv)
        m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
        with mock.patch("%s.open" % (module), m_open, create=True):
            self.importer.load()

        business = self.importer.businesses[0]
        self.assertEquals(business.code, 'code1')
        self.assertEquals(business.description, 'business description 1')

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_multiple_lines(self, m_isfile):
        """BusinessCodeImporterCSV.load() works with multiple lines
        """

        m_isfile.return_value = True

        csv = "code1;business description 1\n" \
              "code2;business description 2\n"

        m_open = mock.mock_open(read_data=csv)
        m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
        with mock.patch("%s.open" % (module), m_open, create=True):
            self.importer.load()

        self.assertEquals(len(self.importer.businesses), 2)

        business1 = self.importer.businesses[0]
        business2 = self.importer.businesses[1]

        self.assertEquals(business1.code, 'code1')
        self.assertEquals(business1.description, 'business description 1')
        self.assertEquals(business2.code, 'code2')
        self.assertEquals(business2.description, 'business description 2')

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_invalid_line(self, m_isfile):
        """BusinessCodeImporterCSV.load() raise exception when a business code
           line in CSV file is invalid
        """

        m_isfile.return_value = True

        csvs = ["line without separator", "line;with;3_separators"]

        for csv in csvs:
            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.assertRaisesRegexp(
                    HPCStatsSourceError,
                    "business line format in CSV is invalid",
                    self.importer.load)

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_business_empty_code(self, m_isfile):
        """BusinessCodeImporterCSV.load() raise exception when business code is
           empty
        """

        m_isfile.return_value = True

        codes = ['', " ", "      "]
        for code in codes:
            csv = "%s;business description 1" % code

            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.assertRaisesRegexp(HPCStatsSourceError,
                                        "business code in CSV is empty",
                                        self.importer.load)

    @mock.patch("%s.os.path.isfile" % module)
    def test_load_business_empty_description(self, m_isfile):
        """BusinessCodeImporterCSV.load() set description to None when empty.
        """

        m_isfile.return_value = True

        descs = ['', " ", "      "]
        for desc in descs:
            csv = "code1;%s" % desc

            m_open = mock.mock_open(read_data=csv)
            m_open.return_value.__iter__ = lambda self: iter(self.readline, '')
            with mock.patch("%s.open" % (module), m_open, create=True):
                self.importer.load()
                self.assertIsNone(self.importer.businesses[0].description)