Ejemplo n.º 1
0
    def setUp(self):

        self.uuid = generate_uuid()  # Generate an id except for POST
        self.uuid1 = generate_uuid()  # Generate id for update

        self.data = [{'id': self.uuid,
                      'name': 'collector1',
                      'ip_address': '10.42.0.11',
                      'udp_port': 2055,
                      'col_type': 'rawforward',
                      'encapsulation_protocol': 'udp',
                      }]

        self.data1 = [{'id': self.uuid1,
                       'name': 'collector2',
                       'ip_address': '10.42.0.12',
                       'udp_port': 2056,
                       'col_type': 'netflow',
                       }]
        self.data2 = [{'id': self.uuid,
                       'name': 'collector2',
                       'ip_address': '10.42.0.12',
                       'udp_port': 2057,
                       'col_type': 'rawforward',
                       'encapsulation_protocol': 'udp',
                       }]

        self.data3 = [{}]

        serializer_data = json.loads(json.dumps(self.data))
#        for serializerdata in serializer_data:
        serializer = CollectorSerializer(data=serializer_data[0])
        if serializer.valid(self.uuid):
            serializer.create(serializer.data)
Ejemplo n.º 2
0
 def test_post_with_invalid_data(self):
     """test case to test create function with invalid data"""
     serializer_data1 = json.loads(json.dumps(self.data1))
     serializer = CollectorSerializer(data=serializer_data1[0])
     if serializer.valid(self.uuid):
         with self.assertRaises(TypeError):
             serializer.create(None)
Ejemplo n.º 3
0
 def test_put_with_invalid_Data(self):
     obj = Collector.get(self.uuid)
     serializer_data3 = json.loads(json.dumps(self.data3))
     serializer = CollectorSerializer(obj,
                                      data=serializer_data3[0],
                                      partial=True,
                                      context={'pk': self.uuid})
     with self.assertRaises(TypeError):
         serializer.update(obj, None)
Ejemplo n.º 4
0
 def test_update_id(self):
     """test case to test update function with id update"""
     obj = Collector.get(self.uuid)
     serializer_data1 = json.loads(json.dumps(self.data1))
     with self.assertRaises(IDUpdateNotPermitted):
         serializer = CollectorSerializer(obj,
                                          data=serializer_data1[0],
                                          partial=True,
                                          context={'pk': self.uuid1})
         serializer.update(obj, serializer_data1[0])
Ejemplo n.º 5
0
 def test_post_data(self):
     """test case to test create function with valid data"""
     serializer_data1 = json.loads(json.dumps(self.data1))
     serializerdata1 = serializer_data1[0]
     serializer = CollectorSerializer(data=serializerdata1)
     if serializer.valid(self.uuid1):
         serializer.create(serializer.data)
     else:
         print("serializer data is not valid")
     serializerdata1['encapsulation_protocol'] = ''
     self.assertEqual(serializerdata1, serializer.data)
Ejemplo n.º 6
0
 def test_put_Data(self):
     """test case to test update function with valid data"""
     obj = Collector.get(self.uuid)
     serializer_data2 = json.loads(json.dumps(self.data2))
     serializer = CollectorSerializer(obj,
                                      data=serializer_data2[0],
                                      partial=True,
                                      context={'pk': self.uuid})
     serializer.update(obj, serializer_data2[0])
     serializerdata2 = serializer_data2[0]
     serializerdata2['id'] = self.uuid
     self.assertEqual(serializerdata2, serializer.data)
    def test_valid_serializer_update(self):
        """test case to test serializer_update with valid record"""
        record = Collector.get(self.uuid)
        serializer = CollectorSerializer(record,
                                         data=self.data3,
                                         partial=True,
                                         context={'pk': self.uuid})
        Collector.serializer_update(serializer, record, self.data3)

        updated_data = Collector.get(self.uuid)
        updated_serializer = CollectorSerializer(updated_data)
        self.data3['id'] = self.uuid
        self.assertEqual(self.data3, updated_serializer.data)
 def test_save_valid_record(self):
     """test case to save valid record"""
     collector = Collector(**self.data1)
     collector.save()
     record = Collector.get(self.uuid1)
     serializer = CollectorSerializer(record)
     self.assertEqual(self.data1, serializer.data)
 def test_valid_get_records_by_secondary_index(self):
     """test case to test get records by secondary index with valid data"""
     collector = Collector(**self.data1)
     collector.save()
     records = collector.get_all_records_by_secondary_index('col_type',
                                                            'rawforward')
     serializer = CollectorSerializer(records)
     self.listdata.append(self.data1)
     self.assertEqual(self.listdata, serializer.data)
    def test_invalid_serializer(self):
        """test case to test serializer_valid"""
        serializer = CollectorSerializer(data=None)
        print("test case to test serializer_valid")
        return_value = Collector.serializer_valid(serializer,
                                                  None)
        self.assertEqual(return_value, False)

        return_value1 = Collector.serializer_valid(serializer,
                                                   self.uuid1)
        self.assertEqual(return_value1, False)
    def test_invalid_serializer_update(self):
        """test case to test serializer_update with invalid record"""
        collector = Collector.get(self.uuid)
        with self.assertRaises(IDUpdateNotPermitted):
            serializer = CollectorSerializer(collector)
            record = Collector.serializer_update(serializer,
                                                 collector,
                                                 self.data1)

        with self.assertRaises(TypeError):
            record = Collector.serializer_update(serializer,
                                                 collector,
                                                 None)
 def test_valid_get_record(self):
     """test case to test with valid record"""
     record = Collector.get(self.uuid)
     serializer = CollectorSerializer(record)
     self.assertEqual(self.data, serializer.data)
 def test_valid_serializer_create(self):
     """test case to test serializer_create with valid record"""
     record = Collector.serializer_create(self.data1)
     serializer = CollectorSerializer(record)
     self.assertEqual(self.data1, serializer.data)
Ejemplo n.º 14
0
 def test_update_no_record(self):
     """Test case to update a Collector with None record."""
     serializer = CollectorSerializer(data=None)
     collector = Collector()
     with self.assertRaises(TypeError):
         serializer.update(collector, None)
Ejemplo n.º 15
0
 def test_create_None_record(self):
     """Test case to create None record."""
     serializer = CollectorSerializer(data=None)
     with self.assertRaises(TypeError):
         serializer.create(None)
Ejemplo n.º 16
0
 def test_invalid_serializer_without_pk(self):
     """test case to test valid function with invalid serializer without pk
     """
     record = Collector.get(self.uuid)
     serializer = CollectorSerializer(record)
     self.assertEqual(serializer.valid(None), False)
Ejemplo n.º 17
0
 def test_valid_serializer(self):
     """test case to test valid function with valid serializer"""
     serializer_data1 = json.loads(json.dumps(self.data1))
     serializer = CollectorSerializer(data=serializer_data1[0])
     self.assertEqual(serializer.valid(self.uuid), True)
 def test_valid_get_all_record(self):
     """test case to test get_all with valid record"""
     record = Collector.get_all()
     serializer = CollectorSerializer(record)
     self.listdata.append(self.data)
     self.assertEqual(self.listdata, serializer.data)
 def test_valid_serializer(self):
     """test case to test serializer_valid with valid record"""
     serializer = CollectorSerializer(data=self.data3)
     return_value = Collector.serializer_valid(serializer,
                                               self.uuid)
     self.assertEqual(return_value, True)