示例#1
0
    def insert_service_repository(self,
                                  service_name: str,
                                  repository_id: int,
                                  start_date: str = None,
                                  end_date: str = None,
                                  initial_loc: int = None):
        """

        Parameters
        ----------
        service_name: str
        repository_id: int
        start_date: str
            the date that this repo started being used for the specified service
        end_date: str
            the date that this repo stop being used for the specified service
        initial_loc: int

        Returns
        -------

        """
        service_conn = ServiceConn(path_to_db=self.db_path)
        service = service_conn.get_service(name=service_name)
        repo_mgr = RepositoryMgr(path_to_db=self.db_path)
        repo = repo_mgr.get_repository(repository_id=repository_id,
                                       service_id=service.service_id)
        service_repo_conn = ServiceRepositoryConn(self.db_path)
        # check if start_date is valid
        try:
            if start_date is not None:
                start_date_dt = datetime.strptime(start_date,
                                                  '%Y-%m-%d').date()
                assert service.start_date <= start_date_dt, \
                    "Error! service={} repo={} {} > {}".format(service.name, repo.name,
                                                               service.start_date, start_date_dt)
        except ValueError as e:
            logging.critical('Error! Invalid start_date: {} msg={}'.format(
                start_date, e))
            raise e

        # check if end_date is valid
        try:
            if end_date is not None:
                end_date_dt = datetime.strptime(end_date, '%Y-%m-%d')
                assert service.end_date >= end_date_dt

        except ValueError as e:
            logging.critical('Error! Invalid end_date: {} msg={}'.format(
                end_date, e))
            raise e
        service_repo_conn.insert_service_repo(service_id=service.service_id,
                                              repository_id=repo.repository_id,
                                              start_date=start_date,
                                              end_date=end_date,
                                              initial_loc=initial_loc)
示例#2
0
    def get_service(self, service_id=None, service_name=None) -> Service:
        """

        Parameters
        ----------
        service_id: int
        service_name: str

        Returns
        -------
        Service
        """
        repo_mgr = RepositoryMgr(path_to_db=self.db_path)
        service_conn = ServiceConn(path_to_db=self.db_path)
        service = service_conn.get_service(service_id=service_id,
                                           name=service_name)  # type: Service
        service_repo_conn = ServiceRepositoryConn(path_to_db=self.db_path)
        if service is not None:
            service_repo_list = service_repo_conn.get_service_repository(
                service_name=service.name)
            for sr in service_repo_list:

                start_date = sr.get('start_date')
                try:
                    st = datetime.strptime(start_date, '%Y-%m-%d').date()
                except (TypeError, ValueError) as e:
                    st = datetime(year=1000, month=1, day=1).date()

                end_date = sr.get('end_date')
                try:
                    ed = datetime.strptime(end_date, '%Y-%m-%d').date()
                except (TypeError, ValueError) as e:
                    ed = datetime(year=9999, month=12, day=30).date()

                repository_id = sr.get('repository_id')
                initial_loc = sr.get('initial_loc')
                repo = repo_mgr.get_repository(repository_id=repository_id,
                                               start_date=st.isoformat(),
                                               end_date=ed.isoformat(),
                                               service_id=service.service_id)
                assert repo is not None, "Error: Repository is None: repository_id={} service_id={}" \
                    .format(repository_id, service.service_id)
                service.add_repository(repository=repo,
                                       start_date=start_date,
                                       end_date=end_date,
                                       initial_loc=initial_loc)
        return service
示例#3
0
class TestServiceConn(TestCase):
    def setUp(self) -> None:
        self.db_path = os.getenv('DB_PATH')
        self.conn = ServiceConn(path_to_db=self.db_path)

    def test_get_service(self):
        cursor = self.conn.conn.cursor()
        sql = 'select name, ID from {};'.format(ServiceConn.TABLE_NAME)
        rows = cursor.execute(sql)
        for row in rows:
            name = row[0]
            sid = row[1]
            s = self.conn.get_service(name=name)
            self.assertIsInstance(s, Service)
            self.assertEqual(s.service_id, sid)
            self.assertEqual(s.name, name)

    def test_get_service_invalid_parameters(self):
        name = 'fdsfdsfd'
        s = self.conn.get_service(name=name)
        self.assertIsNone(s)

    def test_insert(self):
        start_date = date(year=2010, month=1, day=1)
        sid = self.conn.insert_service(name='test_service_name',
                                       start_date=start_date)
        self.assertIsInstance(sid, int)
        self.assertGreater(sid, 0)
        self.conn.delete_service(sid)

    def test_insert_existing_service(self):

        service_names = self.conn.list_all_service_names()
        name = random.choice(service_names)
        s = self.conn.get_service(name=name)
        self.assertIsInstance(s, Service)

        sid = self.conn.insert_service(name=s.name, start_date=s.start_date)
        self.assertEqual(s.service_id, sid)

    def test_insert_invalid_parameters(self):
        start_date = date(year=2010, month=1, day=1)
        with pytest.raises(Exception) as e_info:
            self.conn.insert_service(name=None, start_date=start_date)
        self.assertIn('AssertionError', e_info.typename)

        with pytest.raises(Exception) as e_info:
            self.conn.insert_service(name='test', start_date=None)
        self.assertIn('AssertionError', e_info.typename)

    def test_delete(self):
        test_name = 'test_service_name'
        start_date = date(year=2010, month=1, day=1)
        s = self.conn.get_service(test_name)
        self.assertIsNone(s)
        sid = self.conn.insert_service(name=test_name, start_date=start_date)
        self.assertIsInstance(sid, int)
        self.assertGreater(sid, 0)
        s = self.conn.get_service(test_name)
        self.conn.delete_service(s.service_id)
        s = self.conn.get_service(test_name)
        self.assertIsNone(s)

    def test_update_service(self):
        service_names = self.conn.list_all_service_names()
        name = random.choice(service_names)
        s = self.conn.get_service(name=name)
        self.assertIsNotNone(s)
        self.assertIsInstance(s, Service)
        resp = self.conn.update_service(start_date=s.start_date,
                                        service_id=s.service_id,
                                        end_date=s.end_date)
        self.assertTrue(resp)