Exemplo n.º 1
0
 def test_error_on_get_top_submissions_db_error(self, mock_filter):
     with self.assertRaises(api.SubmissionInternalError):
         mock_filter.side_effect = DatabaseError("Bad things happened")
         student_item = copy.deepcopy(STUDENT_ITEM)
         api.get_top_submissions(student_item["course_id"],
                                 student_item["item_id"],
                                 "Peer_Submission",
                                 1,
                                 read_replica=False)
Exemplo n.º 2
0
 def test_force_database_error(self, mock_get):
     """
     Tests to see if an database error will be handled
     """
     mock_get.side_effect = DatabaseError("DatabaseError")
     with self.assertRaises(api.ValInternalError):
         api.get_video_info(
             constants.VIDEO_DICT_FISH.get("edx_video_id")
         )
Exemplo n.º 3
0
 def delete_customer(self, id):
     if id is None:
         raise ValueError('Customer id is required')
     try:
         self.model.objects.get(id=id).delete()
     except DatabaseError as e:
         raise DatabaseError(
             'Technical problem occurred while deleting customer')
     return True
Exemplo n.º 4
0
def test_readiness_with_db_error(mock_manager, db, client):
    mock_manager.filter.side_effect = DatabaseError('fubar')
    response = client.get(reverse('health.readiness'))
    assert response.status_code == 503
    assert 'fubar' in response.reason_phrase
    assert 'max-age=0' in response['Cache-Control']
    assert 'no-cache' in response['Cache-Control']
    assert 'no-store' in response['Cache-Control']
    assert 'must-revalidate' in response['Cache-Control']
Exemplo n.º 5
0
 def update_product(self, id, data):
     old_product = self.get(id=id)
     old_product.product_name = data['product_name']
     old_product.product_description = data['product_description']
     try:
         old_product.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError("Database technical problem updating product")
     return old_product
Exemplo n.º 6
0
    def create_invest(self, form_data):
        try:
            new_invest = self.model(**form_data)
            new_invest.save(using=self.db)
        except DatabaseError as e:
            raise DatabaseError(
                "Technical problem occurred while adding new invest")

        return new_invest
Exemplo n.º 7
0
    def test_close_database__database_error__reraise(self, mocker):
        conn = MagicMock()
        conn.close.side_effect = DatabaseError("reraise me")
        all_ = mocker.patch("django.db.connections.all")
        all_.return_value = [conn]

        worker = get_worker()
        with pytest.raises(DatabaseError):
            worker.close_database()
Exemplo n.º 8
0
def test_status_test_acccounts_no_database(client, mock_status_externals):
    """The status JSON shows accounts unavailable if no database."""
    mock_status_externals['test_users'].side_effect = DatabaseError('wat')
    url = reverse('health.status')
    response = client.get(url)
    data = json.loads(response.content)
    assert data['services']['test_accounts'] == {
        'available': False,
    }
Exemplo n.º 9
0
 def delete_variant(self, id):
     if id is None:
         raise ValueError("Id is required")
     try:
         from product.models import OrderedItem
         variant = self.model.objects.get(id=id)
         from product.models import Order
         orders = Order.objects.prefetch_related('ordered_items').filter(
             ordered_items__product=variant)
         for order in orders:
             try:
                 order.delete()
             except DatabaseError as e:
                 raise DatabaseError("Technical problem to delete variant")
         variant.delete()
     except DatabaseError as e:
         raise DatabaseError("Technical problem to delete variant")
     return True
Exemplo n.º 10
0
 def type_id(self):
     """
     Returns the :class:`~django.contrib.contenttypes.models.ContentType` id of the model.
     """
     if self._type_id is None:
         try:
             self._type_id = ContentType.objects.get_for_model(self.model).id
         except DatabaseError as e:
             raise DatabaseError("Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {0})".format(str(e)))
     return self._type_id
Exemplo n.º 11
0
 def test_that_verification_result_querying_locked_row_should_reschedule_task(
         self):
     with mock.patch('core.tasks.Subtask.objects.select_for_update',
                     side_effect=DatabaseError()):
         # Exception is raised because task is executed directly as a function.
         with self.assertRaises(Retry):
             verification_result(  # pylint: disable=no-value-for-parameter
                 self.subtask.subtask_id,
                 VerificationResult.MATCH.name,
             )
Exemplo n.º 12
0
 def test_create_submission_for_team_error_creating_individual_submission(self, mocked_log_submission):
     """
     Test for when there is an error creating one individual submission.
     the team submission and all other individual submissions should not be created.
     """
     mocked_log_submission.side_effect = [None, None, None, DatabaseError()]
     with self.assertRaises(SubmissionInternalError):
         self._call_create_submission_for_team_with_default_args()
     self.assertEqual(TeamSubmission.objects.count(), 0)
     self.assertEqual(Submission.objects.count(), 0)
Exemplo n.º 13
0
Arquivo: models.py Projeto: hodduc/otl
def fetch_assignments(student_id):
    """
    Moodle DB에 접속하여 해당 학번의 학생이 듣고 있는 과목들에 대한 과제 정보를 가져온다.
    """
    if not isinstance(student_id, int):
        raise TypeError('student_id must be an integer.')

    # TODO: cache!!
    taking_courses = fetch_taking_courses(student_id)
    assignments = []
    try:
        import MySQLdb
        moodle_db = MySQLdb.connect(host=settings.MOODLEDB_HOST,
                                    user=settings.MOODLEDB_USER,
                                    passwd=settings.MOODLEDB_PASSWORD,
                                    db=settings.MOODLEDB_NAME,
                                    use_unicode=True,
                                    charset='utf8')
    except ImportError:
        raise DatabaseError('MySQLdb module is not installed!')
    except:
        raise DatabaseError('Cannot access the moodle database!')
    cursor = moodle_db.cursor()
    for lecture in taking_courses:
        cursor.execute(
            """SELECT c.shortname, a.name, a.description, a.format, a.assignmenttype, a.timedue, a.timeavailable, a.grade, a.timemodified
        FROM mdl_assignment a, mdl_course c WHERE c.id = a.course AND c.shortname LIKE '%s%%'"""
            % lecture.old_code)
        # TODO: moodle 조교와 협의하여 shortname에 과목 코드뿐만 아니라 분반, 개설년도/개설학기 정보도 포함시켜 정확한 과목 matching이 이루어지게 한다.
        #       현재 같은 과목이라도 분반에 따라 moodle을 이용하기도 하고 이용하지 않기도 하는 경우가 있어 과목코드만으로 가져오면 실제로 자기하고는
        #       상관 없는 과제 정보를 얻어오는 경우가 있다.
        rows = cursor.fetchall()
        for row in rows:
            # Convert time
            assignment = [item for item in row]
            assignment[5] = datetime.fromtimestamp(assignment[5])
            assignment[6] = datetime.fromtimestamp(assignment[6])
            assignment[8] = datetime.fromtimestamp(assignment[8])
            assignments.append(assignment)
    cursor.close()
    moodle_db.close()

    return assignments
Exemplo n.º 14
0
    def test_close_database__database_error__no_reraise(self, mocker):
        conn = MagicMock()
        conn.close.side_effect = DatabaseError("closed not connected don't reraise me")
        all_ = mocker.patch("django.db.connections.all")
        all_.return_value = [conn]

        worker = get_worker()
        worker.close_database()

        assert conn.close.called
Exemplo n.º 15
0
def test_status_failed_database(client, mock_status_externals):
    """The status JSON shows if the database is unavailable."""
    mock_status_externals["document"].side_effect = DatabaseError("fubar")
    url = reverse("health.status")
    response = client.get(url)
    data = json.loads(response.content)
    assert data["services"]["database"] == {
        "available": False,
        "populated": False,
    }
Exemplo n.º 16
0
 def test_assess_training_example_database_error(self):
     training_api.get_training_example(self.submission_uuid, RUBRIC,
                                       EXAMPLES)
     with patch(
             'openassessment.assessment.models.student_training.StudentTrainingWorkflow.objects.get'
     ) as mock_db:
         mock_db.side_effect = DatabaseError("Kaboom!")
         with self.assertRaises(StudentTrainingInternalError):
             training_api.assess_training_example(
                 self.submission_uuid, EXAMPLES[0]['options_selected'])
Exemplo n.º 17
0
    def create_product(self, **fields):
        if 'new_product' in fields:
            product = self.model(**fields['new_product'])
            try:
                product.save(using=self._db)
            except DatabaseError as e:
                raise DatabaseError(e)

            fields['product'] = product
            del fields['new_product']

        if 'new_color' in fields:
            from product.models import Color
            color = Color.objects.create(color=fields['new_color'])
            fields['color'] = color
            del fields['new_color']

        if 'new_size' in fields:
            from product.models import Size
            size = Size.objects.create(size=fields['new_size'])
            fields['size'] = size
            del fields['new_size']

        if 'new_category' in fields:
            from product.models import Category
            category = Category.objects.create(category=fields['new_category'])
            fields['category'] = category
            del fields['new_category']

        if 'product' in fields:
            keys = [
                'product', 'size', 'category', 'marketing_cost', 'vat',
                'profit', 'stock_total', 'transport_cost', 'bag_purchase_price'
            ]
        if 'supplier' not in fields or not fields['supplier']:
            raise ValueError("Supplier field can't be empty.")

        if all(key in fields and fields[key] for key in keys):
            from product.models import ProductVariant
            supplier = fields['supplier']
            del fields['supplier']
            product = ProductVariant(**fields)
            product.save(using=self.db)
            from product.models import SupplierTransaction
            supplier = SupplierTransaction(
                supplier=supplier,
                product=product,
                total_supplied=fields['stock_total'],
                per_product_purchase_price=fields['bag_purchase_price'])
            supplier.save(using=self.db)
            return product
        else:
            raise ValueError(
                'Product name, size, category, marketing cost, vat, bag purchase price, transport cost, '
                'stock total, profit can\t be empty')
Exemplo n.º 18
0
Arquivo: models.py Projeto: hodduc/otl
def fetch_taking_courses(student_id):
    """
    학사 DB에 접속하여 해당 학번의 학생이 듣고 있는 과목 정보를 가져온다.
    """
    if not isinstance(student_id, int):
        raise TypeError('student_id must be an integer.')

    # TODO: cache!!
    taking_courses = []
    try:
        import Sybase
        scholar_db = Sybase.connect(settings.SCHOLARDB_HOST,
                                    settings.SCHOLARDB_USER,
                                    settings.SCHOLARDB_PASSWORD,
                                    settings.SCHOLARDB_NAME)
    except ImportError:
        raise DatabaseError('Sybase module is not installed!')
    except:
        raise DatabaseError('Cannot access the scholar database!')
    cursor = scholar_db.cursor()
    cursor.execute(
        """SELECT l.subject_no, l.old_no, l.dept_id, l.lecture_class FROM view_OTL_attend a, view_OTL_lecture l
    WHERE a.student_no = %d AND a.lecture_year = %d AND a.lecture_term = %d
    AND a.lecture_year = l.lecture_year AND a.lecture_term = l.lecture_term AND a.subject_no = l.subject_no AND a.lecture_class = l.lecture_class AND a.dept_id = l.dept_id"""
        % (student_id, settings.CURRENT_YEAR, settings.CURRENT_SEMESTER))
    rows = cursor.fetchall()
    try:
        for row in rows:
            taking_courses.append(
                Lecture.objects.get(year=settings.CURRENT_YEAR,
                                    semester=settings.CURRENT_SEMESTER,
                                    code=row[0],
                                    department__id=row[2],
                                    class_no=row[3].strip()))
    except Lecture.DoesNotExist:
        raise DatabaseError(
            'Matching lecture(%s) which a student(%d) is taking was not found! Check scholar-db synchronization.'
            % (row[0], student_id))
    cursor.close()
    scholar_db.close()

    return taking_courses
    def __performTransaction(self, myMap, myExtras):

        errorMessage = ''

        noErrors, errorMessage = self.dataCategoryHandler(
            myMap.get('DC_description'))

        if noErrors is False:
            DBFunctions.dropTable(self.tableName)
            raise DatabaseError(errorMessage)

        cleanAttributeFormat = Helper.seperateByName(myExtras, 4, False, False,
                                                     self.dcID)

        noErrors, errorMessage = DBFunctions.insertToAttribute(
            cleanAttributeFormat, self.dcID)

        if noErrors is False:
            DBFunctions.dropTable(self.tableName)
            raise DatabaseError(errorMessage)
Exemplo n.º 20
0
 def test_readiness_with_db_error(self):
     """Test the readiness endpoint when there is a database issue."""
     url = reverse("health.readiness")
     article_model_mgr = "developerportal.apps.health.views.Article.objects"
     with mock.patch(article_model_mgr) as mocked:
         mocked.filter.side_effect = DatabaseError("fubar")
         for method in ("get", "head"):
             with self.subTest(method=method):
                 response = getattr(self.client, method)(url)
                 self.assertEqual(response.status_code, 503)
                 self.assertTrue("fubar" in response.reason_phrase)
Exemplo n.º 21
0
def test_database_exists():
    from django.db import connection, DatabaseError

    try:
        if settings.DATABASE_ENGINE == 'sqlite3':
            if not os.path.exists(settings.DATABASE_NAME):
                raise DatabaseError()
        connection.cursor()
        return True
    except DatabaseError, err:
        return False
Exemplo n.º 22
0
 def db_for_write(self, model, **hints):
     """
     Raises an error for write attempts to Sierra DB, and routes
     all others to default. Note that base.models have their save
     and delete methods disabled, so attempting to write base.models
     should never call this code in the first place.
     """
     if model._meta.app_label == 'base':
         raise DatabaseError('Attempted to write to Sierra database.')
     else:
         return 'default'
Exemplo n.º 23
0
def test_status_failed_database(client, mock_status_externals):
    """The status JSON shows if the database is unavailable."""
    mock_status_externals['document'].side_effect = DatabaseError('fubar')
    url = reverse('health.status')
    response = client.get(url)
    data = json.loads(response.content)
    assert data['services']['database'] == {
        'available': False,
        'populated': False,
        'document_count': 0,
    }
Exemplo n.º 24
0
 def update_customer_details(self, id, data):
     customer_info = self.get(id=id)
     customer_info.customer_name = data['customer_name']
     customer_info.customer_phone = data['customer_phone']
     customer_info.customer_address = data['customer_address']
     try:
         customer_info.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError(
             "Technical problem occurred during updating customer")
     return True
Exemplo n.º 25
0
    def update_variant(self, id, form_data):
        old_variant = self.get(id=id)
        for key, value in form_data.items():
            setattr(old_variant, key, value)

        try:
            old_variant.save(using=self.db)
        except DatabaseError as e:
            raise DatabaseError(
                "In updating variant there is database technical problem.")
        return old_variant
Exemplo n.º 26
0
 def add_new_stock(self, id, form_data):
     old_variant = self.model.objects.get(id=id)
     old_variant.stock_total = old_variant.stock_total + form_data[
         'new_stock']
     from product.models import SupplierTransaction
     try:
         old_variant.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError("Adding new stock problem in database")
     try:
         supplier = SupplierTransaction(
             supplier=form_data['supplier'],
             product=old_variant,
             total_supplied=form_data['new_stock'],
             per_product_purchase_price=form_data[
                 'per_product_purchase_price'])
         supplier.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError("Adding supplier info problem in database")
     return supplier
Exemplo n.º 27
0
 def type_id(self):
     """
     Shortcut to retrieving the ContentType id of the model.
     """
     try:
         return ContentType.objects.get_for_model(
             self.model, for_concrete_model=False).id
     except DatabaseError as e:
         raise DatabaseError(
             "Unable to fetch ContentType object, is a plugin being registered before the initial syncdb? (original error: {0})"
             .format(str(e)))
Exemplo n.º 28
0
 def update_investor(self, id, form_data):
     try:
         old_investor = self.model.objects.get(id=id)
         old_investor.name = form_data['name']
         old_investor.phone_no = form_data['phone_no']
         if 'address' in form_data:
             old_investor.address = form_data['address']
         old_investor.save(using=self.db)
     except DatabaseError as e:
         raise DatabaseError('Technical problem while updating shareholder')
     return True
Exemplo n.º 29
0
 def _execute_raise_user_already_exists(self,
                                        cursor,
                                        statements,
                                        parameters,
                                        verbosity,
                                        allow_quiet_fail=False):
     # Raise "user already exists" only in test user creation
     if statements and statements[0].startswith("CREATE USER"):
         raise DatabaseError(
             "ORA-01920: user name 'string' conflicts with another user or role name"
         )
Exemplo n.º 30
0
def coerce_unicode(value):
    if isinstance(value, str):
        try:
            value = value.decode('utf-8')
        except UnicodeDecodeError:
            # This must be a Django databaseerror, because the exception happens too
            # early before Django's exception wrapping can take effect (e.g. it happens on SQL
            # construction, not on execution.
            raise DatabaseError("Bytestring is not encoded in utf-8")

    # The SDK raises BadValueError for unicode sub-classes like SafeText.
    return unicode(value)