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 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