class GetSingleValidEndRecordTest(TestCase): """ Test module for getting single end record """ def setUp(self): self.end_record = EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.22) self.end_record.save() # saves the objects and creates the list to match the service response self.serialized_list = list() self.serialized_list.append({ 'id': self.end_record.id, 'timestamp': self.end_record.timestamp.strftime(TIMESTAMP_FORMAT), 'call_id': self.end_record.start_id, 'type': RecordType.END.value }) def test_get_single_valid_end_record(self): response = client.get('/callrecord/' + str(self.end_record.id) + '/') serializer = CallRecordSerializer(self.serialized_list, many=True) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEquals(response.data, serializer.data)
class GetSingleInvalidEndRecordTest(TestCase): """ Test module for getting single invalid end record """ def setUp(self): self.list_end = list() self.end_record = EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.22) # saves the objects and creates the list to match the service response self.serialized_list = list() self.serialized_list.append({ 'id': self.end_record.id, 'timestamp': self.end_record.timestamp.strftime(TIMESTAMP_FORMAT), 'call_id': self.end_record.start_id, 'type': RecordType.END.value }) self.end_record.save() def test_get_single_invalid_end_record(self): response = client.get('/callrecord/9999/') self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def setUp(self): self.list_end = list() self.list_end.append( EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.33)) self.list_end.append( EndRecord(id=ID_2, timestamp=END_TIME_2, start_id=CALL_ID_2, cost=0.44)) for self.end_record in self.list_end: self.end_record.save()
def setUp(self): self.list_start = list() self.list_start.append( StartRecord(id=ID_1, timestamp=START_TIME_1, call_id=CALL_ID_1, source=SOURCE_NUMBER, destination=DESTINATION_NUMBER)) self.list_start.append( StartRecord(id=ID_2, timestamp=START_TIME_2, call_id=CALL_ID_2, source=SOURCE_NUMBER, destination=DESTINATION_NUMBER)) for start_record in self.list_start: start_record.save() self.list_end = list() self.list_end.append( EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.22)) self.list_end.append( EndRecord(id=ID_2, timestamp=END_TIME_2, start_id=CALL_ID_2, cost=0.22)) for self.end_record in self.list_end: self.end_record.save() self.valid_end_record = { 'id': 1, 'timestamp': END_TIME_1.strftime(TIMESTAMP_FORMAT), 'call_id': CALL_ID_1, 'type': RecordType.END.value } self.invalid_end_record = { 'id': 2, 'timestamp': END_TIME_1.strftime(TIMESTAMP_FORMAT), 'call_id': '', 'type': RecordType.END.value }
def setUp(self): self.end_record = EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.22) self.end_record.save() # saves the objects and creates the list to match the service response self.serialized_list = list() self.serialized_list.append({ 'id': self.end_record.id, 'timestamp': self.end_record.timestamp.strftime(TIMESTAMP_FORMAT), 'call_id': self.end_record.start_id, 'type': RecordType.END.value })
def setUp(self): self.list_end = list() self.list_end.append( EndRecord(id=ID_1, timestamp=END_TIME_1, start_id=CALL_ID_1, cost=0.22)) self.list_end.append( EndRecord(id=ID_2, timestamp=END_TIME_2, start_id=CALL_ID_2, cost=0.99)) self.list_end.append( EndRecord(id=ID_3, timestamp=END_TIME_3, start_id=CALL_ID_3, cost=0.26)) self.list_end.append( EndRecord(id=ID_4, timestamp=END_TIME_4, start_id=CALL_ID_4, cost=0.54)) self.list_end.append( EndRecord(id=ID_5, timestamp=END_TIME_5, start_id=CALL_ID_5, cost=1.26)) self.list_end.append( EndRecord(id=ID_6, timestamp=END_TIME_6, start_id=CALL_ID_6, cost=86.94)) self.list_end.append( EndRecord(id=ID_7, timestamp=END_TIME_7, start_id=CALL_ID_7, cost=0.72)) self.list_end.append( EndRecord(id=ID_8, timestamp=END_TIME_8, start_id=CALL_ID_8, cost=0.22)) self.list_end.reverse() self.serialized_list = list() # saves the objects and creates the list to match the service response for end_record in self.list_end: self.serialized_list.append({ 'id': end_record.id, 'timestamp': end_record.timestamp.strftime(TIMESTAMP_FORMAT), 'call_id': end_record.start_id, 'type': RecordType.END.value }) end_record.save()
def create(self, validated_data): # if the type is START, then save a start record # otherwise, it is a end record if validated_data['type'] == RecordType.START.value: # Validate if object already exists queryset_record = StartRecord.objects.filter( id=validated_data['id'] ) if len(queryset_record) > 0: start_record = queryset_record[0] else: start_record = StartRecord() start_record.id = validated_data.get('id', start_record.id) start_record.timestamp = validated_data. \ get('timestamp', start_record.timestamp). \ replace(tzinfo=timezone.utc) start_record.call_id = validated_data.get( 'call_id', start_record.call_id ) start_record.source = validated_data.get( 'source', start_record.source ) start_record.destination = validated_data.get( 'destination', start_record.destination ) start_record.save() return start_record else: # RecordType.END # Validate if object already exists queryset_record = EndRecord.objects.filter( id=validated_data['id'] ) if len(queryset_record) > 0: end_record = queryset_record[0] else: end_record = EndRecord() end_record.id = validated_data.get('id', end_record.id) end_record.timestamp = validated_data. \ get('timestamp', end_record.timestamp). \ replace(tzinfo=timezone.utc) end_record.start_id = validated_data.get( 'call_id', end_record.start_id ) start_record = StartRecord.objects.filter( call_id=end_record.start_id ) end_record.start = start_record[0] end_record.cost = calculate_call_cost( start_record[0].timestamp, end_record.timestamp ) end_record.save() return end_record