Exemple #1
0
def phone_call():
    """Endpoint to receive the telephone calls records and save it on the database."""
    data = request.json
    if not data:
        return jsonify({
            'success': False,
            'errors': constants.MESSAGE_INVALID_DATA_REQUEST
        })

    all_records = []
    for item in data:
        record = CallRecord(
            item.get('id'),
            item.get('type'),
            item.get('timestamp'),
            item.get('call_id'),
            item.get('source'),
            item.get('destination'),
        )

        errors = record.validate()
        if errors:
            return jsonify({'success': False, 'errors': errors})

        if record.save():
            all_records.append(record)

    if all_records:
        return jsonify({'success': True, 'processed': len(all_records)})

    return jsonify({
        'success': False,
        'errors': constants.MESSAGE_INVALID_DATA_REQUEST
    })
Exemple #2
0
def test_call_record_validate_end(get_by_id):
    """Test validate function from CallRecord class with valid end data."""
    get_by_id.return_value = None
    obj = CallRecord(
        VALID_CALL_RECORD_END.get('record_id'),
        VALID_CALL_RECORD_END.get('record_type'),
        VALID_CALL_RECORD_END.get('record_timestamp'),
        VALID_CALL_RECORD_END.get('call_identifier'),
    )
    obj.exists_call_id = mock.Mock(return_value=False)

    result = obj.validate()

    assert result == []
Exemple #3
0
def test_call_record_exists_call_id_invalid_parameters(get_by_id):
    """Test exists_call_id function from CallRecord class with invalid parameters."""
    get_by_id.return_value = None
    obj = CallRecord(
        VALID_CALL_RECORD_START.get('record_id'),
        VALID_CALL_RECORD_START.get('record_type'),
        VALID_CALL_RECORD_START.get('record_timestamp'),
        None,
        VALID_CALL_RECORD_START.get('origin_number'),
        VALID_CALL_RECORD_START.get('destination_number'),
    )

    result = obj.exists_call_id()

    assert not result
Exemple #4
0
def test_call_record_validate_mandatory(get_by_id):
    """Test validate function from CallRecord class with data without mandatory fields."""
    get_by_id.return_value = None
    obj = CallRecord(
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('record_id'),
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('record_type'),
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('record_timestamp'),
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('call_identifier'),
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('origin_number'),
        INVALID_CALL_RECORD_WITHOUT_MANDATORY.get('destination_number'),
    )
    obj.exists_call_id = mock.Mock(return_value=False)

    result = obj.validate()

    assert 'The field record_type is mandatory.' in result
    assert 'The field call_identifier is mandatory.' in result
Exemple #5
0
def test_call_record_validate_types(get_by_id):
    """Test validate function from CallRecord class with invalid data types."""
    get_by_id.return_value = None
    obj = CallRecord(
        INVALID_CALL_RECORD_TYPES.get('record_id'),
        INVALID_CALL_RECORD_TYPES.get('record_type'),
        INVALID_CALL_RECORD_TYPES.get('record_timestamp'),
        INVALID_CALL_RECORD_TYPES.get('call_identifier'),
        INVALID_CALL_RECORD_TYPES.get('origin_number'),
        INVALID_CALL_RECORD_TYPES.get('destination_number'),
    )
    obj.exists_call_id = mock.Mock(return_value=False)

    result = obj.validate()

    assert 'The field record_type has an invalid value.' in result
    assert 'The field record_timestamp has an invalid value.' in result
Exemple #6
0
def record_start(get_by_id):
    """Fixture to return a valid PhoneCall start object."""
    get_by_id.return_value = None
    return CallRecord(
        VALID_CALL_RECORD_START.get('record_id'),
        VALID_CALL_RECORD_START.get('record_type'),
        VALID_CALL_RECORD_START.get('record_timestamp'),
        VALID_CALL_RECORD_START.get('call_identifier'),
        VALID_CALL_RECORD_START.get('origin_number'),
        VALID_CALL_RECORD_START.get('destination_number'),
    )
Exemple #7
0
def test_call_record_exists_call_id_invalid_without_record_id(get_db):
    """Test exists_call_id function from CallRecord class without record_id."""
    obj = CallRecord(
        None,
        VALID_CALL_RECORD_START.get('record_type'),
        VALID_CALL_RECORD_START.get('record_timestamp'),
        VALID_CALL_RECORD_START.get('call_identifier'),
        VALID_CALL_RECORD_START.get('origin_number'),
        VALID_CALL_RECORD_START.get('destination_number'),
    )

    get_db.return_value.cursor.return_value.execute.return_value.fetchone.return_value = None

    result = obj.exists_call_id()

    assert not result
    get_db.return_value.cursor.return_value.execute.assert_called_once_with(
        'SELECT 1 FROM phone_call WHERE call_identifier = 1 AND record_type = ?',
        [obj.record_type]
    )
Exemple #8
0
def test_calculate_phone_bill_with_data(get_by_id, phone_bill):
    """Test calculate_phone_bill function with a list of record calls mocked."""
    end_records = [
        CallRecord(None, 'end', '2018-10-05T06:00:02', 11),
        CallRecord(None, 'end', '2018-10-09T06:01:00', 12),
        CallRecord(None, 'end', '2018-10-13T00:00:00', 13),
        CallRecord(None, 'end', '2018-10-16T05:59:34', 14),
        CallRecord(None, 'end', '2018-10-18T12:02:45', 15),
        CallRecord(None, 'end', '2018-10-20T23:59:59', 16),
        CallRecord(None, 'end', '2018-10-23T01:59:59', 17),
        CallRecord(None, 'end', '2018-10-25T22:00:00', 18),
        CallRecord(None, 'end', '2018-10-27T22:01:55', 19),
        CallRecord(None, 'end', '2018-10-30T06:30:26', 20)
    ]
    start_records = [
        CallRecord(None, 'start', '2018-10-05T06:00:00', 11, '14981226543', '14998887654'),
        CallRecord(None, 'start', '2018-10-09T06:00:04', 12, '14981226543', '14998887654'),
        CallRecord(None, 'start', '2018-10-12T23:23:23', 13, '14981226543', '1432324455'),
        CallRecord(None, 'start', '2018-10-16T04:54:21', 14, '14981226543', '1432324455'),
        CallRecord(None, 'start', '2018-10-18T12:01:45', 15, '14981226543', '1833445566'),
        CallRecord(None, 'start', '2018-10-20T23:58:59', 16, '14981226543', '1434357263'),
        CallRecord(None, 'start', '2018-10-23T01:00:00', 17, '14981226543', '1432324455'),
        CallRecord(None, 'start', '2018-10-25T19:56:23', 18, '14981226543', '1943536785'),
        CallRecord(None, 'start', '2018-10-27T20:15:55', 19, '14981226543', '1345632789'),
        CallRecord(None, 'start', '2018-10-30T05:30:04', 20, '14981226543', '1345632789')
    ]

    phone_bill.get_phone_end_records = mock.Mock(return_value=end_records)
    phone_bill.get_phone_start_records = mock.Mock(return_value=start_records)
    get_by_id.return_value = None

    phone_bill.calculate_phone_bill()

    assert phone_bill.total == 26.91

    assert phone_bill.record_calls[0].price == 0.36
    assert phone_bill.record_calls[1].price == 0.36
    assert phone_bill.record_calls[2].price == 0.36
    assert phone_bill.record_calls[3].price == 0.36
    assert phone_bill.record_calls[4].price == 0.45
    assert phone_bill.record_calls[5].price == 0.36
    assert phone_bill.record_calls[6].price == 0.36
    assert phone_bill.record_calls[7].price == 11.43
    assert phone_bill.record_calls[8].price == 9.72
    assert phone_bill.record_calls[9].price == 3.15