예제 #1
0
class MessageRenameTestCase(unittest.TestCase):
    '''Test mapping when message name does not match
    django type name
    '''
    global proto
     
    def setUp(self):
        self.module_name = "simple_test"
        self.pb = ProtocolBuffer(self.module_name)
        self.simple = genMsg('SimpleRename', models.Simple)
        self.pb.addMessage(self.simple)
        mod_name = pbandj.genMod(self.pb)
        global proto
        proto = __import__(mod_name)
        self.con = Converter()
        
    def test_mappingExists(self):
        self.assert_(not self.pb.messages.get('SimpleRename') is None,
                     'No mapping by message name to message object')
        self.assert_(not self.pb.messages.get(models.Simple) is None,
                     'No mapping by django type to message object')
        self.assertEqual(self.pb.messages['SimpleRename'],
                         self.pb.messages[models.Simple],
                         'mapping from string not equal to mapping from django type')
    
    def test_invertible(self):
        msg = proto.SimpleRename()
        msg.val = 22
        django = self.con.toDjangoObj(self.pb, msg)
        msg = self.con.toProtoMsg(self.pb, django, proto)
예제 #2
0
class EnumTestCase(unittest.TestCase):
    proto = None
    def setUp(self):
        self.module_name = "enum_test"
        self.pb = ProtocolBuffer(self.module_name)
        self.emsg= genMsg('EnumTest',
                                        models.EnumTest)
        self.pb.addMessage(self.emsg)
        self.django_class_fields = [field for field in 
                               models.EnumTest._meta.local_fields]
        self.pb_field_names = [f.name for f in self.emsg.mapped_fields]
        mod_name = pbandj.genMod(self.pb)
        global proto
        proto = __import__(mod_name)
        emsg = proto.EnumTest()
        emsg.enum_test = proto.EnumTest.ENUM_TEST_VAL1
        self.emsg = emsg
        self.converter = Converter()
        
        enum_db = models.EnumTest(enum_test='Val1')
        enum_db.save()
        self.enum_db = enum_db
        
    def test_toDjangoObj(self):
        dj_obj = self.converter.toDjangoObj(self.pb, self.emsg)
        self.assertEqual(dj_obj.enum_test, self.pb.messages['EnumTest'].enums[0].dj2val[proto.EnumTest.ENUM_TEST_VAL1])
    
    def test_conversion_invertible(self):
        '''Make sure conversion is invertible'''
        dj_obj = self.converter.toDjangoObj(self.pb, self.emsg)
        pb_obj = self.converter.toProtoMsg(self.pb, dj_obj, proto)
        self.assertEqual(self.emsg, pb_obj)
    
    def test_save_invertible(self):
        '''Make sure conversion is invertible going through database'''
        dj_obj = self.converter.toDjangoObj(self.pb, self.emsg)
        dj_obj.save()
        pb_from_db = self.converter.toProtoMsg(self.pb,
                                          type(dj_obj).objects.get(pk=dj_obj.id),
                                          proto)
        self.emsg.id = dj_obj.id
        self.assertEqual(self.emsg, pb_from_db)
예제 #3
0
class ForeignKeyTestCase(unittest.TestCase):
    proto = None
    
    def setUp(self):
        self.module_name = "fk_test"
        self.pb = ProtocolBuffer(self.module_name)
        self.fkt = genMsg('ForeignKeyTest',
                                        models.ForeignKeyTest)
        self.pb.addMessage(self.fkt)
        
        self.django_class_fields = [field for field in 
                               models.ForeignKeyTest._meta.local_fields]
        self.pb_fields_by_name = dict([(f.name, f) for f in self.fkt.mapped_fields])
        mod_name = pbandj.genMod(self.pb)
        global proto
        proto = __import__(mod_name)
        self.simple = models.Simple()
        self.simple.val = 1234
        self.simple.save()
        self.con = Converter()
    
    def test_genMsg(self):
        ''' Ensure that there is a type map definition for each Django field
            type
        '''
        for field in self.django_class_fields:
            self.assert_(DJ2PB.get(type(field)) != None,
                         "No protocol buffer type mapped to %s Django type" % 
                         type(field).__name__)
        
    def test_genMsg_fields_exist(self):
        ''' Ensure that all fields from the django type exist in the generated
            protocol buffer type and their type matches the type map
        '''
        for field in self.django_class_fields:
            self.assert_(self.pb_fields_by_name.has_key(field.name),
                         "Django field %s does not exist in protocol buffer message" % field.name)
            pb_field = self.pb_fields_by_name[field.name]
            dj_type = type(field)
            expected_type = [DJ2PB[dj_type]]
            if (dj_type == django_models.ForeignKey or dj_type == django_models.ManyToManyField):
                expected_type.append(ProtocolBuffer.Message)
            self.assert_(type(pb_field.pb_type) in expected_type,
                         "Protocol Buffer field type %s does not match type %s specified in type map for Django type %s" %
                         (type(pb_field.pb_type), DJ2PB[dj_type],
                          dj_type.__name__))
            
    def test_invertible(self):
        test = proto.ForeignKeyTest()
        test.fkey_test_id = self.simple.pk
        dj_test = self.con.toDjangoObj(self.pb, test)
        pb_test = self.con.toProtoMsg(self.pb, dj_test, proto)
        self.assertEqual(test.fkey_test_id, pb_test.fkey_test_id,
                         "Foreign key test objects not equal after conversion")
예제 #4
0
def onAddContact(service_impl, controller, request, done):
    """ Handler method for the add_contact RPC described below """
    print "In add contact"
    from generated_pb2 import Response
    resp = Response()
    print "Converting to django"
    converter = Converter()
    try:
        contact = converter.toDjangoObj(pbuf, request)
    except Exception, e:
        print e
예제 #5
0
class ManyToManyTestCase(unittest.TestCase):
    
    def setUp(self):
        self.module_name = "m2m_test"
        self.pb = ProtocolBuffer(self.module_name)
        self.m2m = genMsg('ManyToManyTest',
                                        models.ManyToManyTest)
        self.pb.addMessage(self.m2m)
        
        self.django_class_fields = [field for field in 
                               models.ManyToManyTest._meta.local_fields +
                               models.ManyToManyTest._meta.many_to_many]
        self.pb_fields_by_name = dict([(f.name, f) for f in self.m2m.mapped_fields])
        mod_name = pbandj.genMod(self.pb)
        global proto
        proto = __import__(mod_name)
        self.simple1 = models.Simple()
        self.simple1.val = 1234
        self.simple1.save()
        self.simple2 = models.Simple()
        self.simple2.val = 5678
        self.simple2.save()
        self.con = Converter()
        
    def test_genMsg(self):
        ''' Ensure that there is a type map definition for each Django field
            type
        '''
        for field in self.django_class_fields:
            self.assert_(DJ2PB.get(type(field)) != None,
                         "No protocol buffer type mapped to %s Django type" % 
                         type(field).__name__)
    
    def test_genMsg_fields_exist(self):
        ''' Ensure that all fields from the django type exist in the generated
            protocol buffer type and their type matches the type map
        '''
        for field in self.django_class_fields:
            self.assert_(self.pb_fields_by_name.has_key(field.name),
                         "Django field %s does not exist in protocol buffer message" % field.name)
            pb_field = self.pb_fields_by_name[field.name]
            dj_type = type(field)
            expected_type = [DJ2PB[dj_type]]
            if (dj_type == django_models.ForeignKey or dj_type == django_models.ManyToManyField):
                expected_type.append(ProtocolBuffer.Message)
            self.assert_(type(pb_field.pb_type) in expected_type,
                         "Protocol Buffer field type %s does not match type %s specified in type map for Django type %s" %
                         (type(pb_field.pb_type), DJ2PB[dj_type],
                          dj_type.__name__))
    
    def test_toDjangoObj(self):
        pb_test = proto.ManyToManyTest()
        pb_test.test_val = 1
        pb_test.m2m_test_id.append(2)
        dj_test = self.con.toDjangoObj(self.pb, pb_test)
        self.assertEqual(pb_test.test_val, dj_test.test_val,
                         'Django object does not match source Protocol Buffer Message')
    
    def test_toProtoMsg(self):
        dj_test = models.ManyToManyTest()
        dj_test.test_val = 1
        dj_test.save()
        dj_test.m2m_test.add(self.simple1) 
        dj_test.m2m_test.add(self.simple2)       
#        dj_test2 = models.ManyToManyTest()
#        dj_test2.test_val = 2
#        dj_test2.save()
#        dj_test2.m2m_test.add(self.simple) 
        pb_test = self.con.toProtoMsg(self.pb, dj_test, proto)
        self.assert_(len(pb_test.m2m_test_id) == 2,
                     "Added 2 objects to the relation but found %d" % len(pb_test.m2m_test_id))
        self.assertEqual(pb_test.m2m_test_id[0], self.simple1.pk,
                         "1st assoc object doesn't match the original")
        self.assertEqual(pb_test.m2m_test_id[1], self.simple2.pk,
                         "2nd assoc object doesn't match the original")
예제 #6
0
class OneOfEverythingUsageTestCase(unittest.TestCase):
    
    proto = None
    
    def setUp(self):
        self.module_name = "ooe_test"
        self.pb = ProtocolBuffer(self.module_name)
        self.ooe = genMsg('OneOfEverything',
                                        models.OneOfEverything)
        self.pb.addMessage(self.ooe)
        
        self.django_class_fields = [field for field in 
                               models.OneOfEverything._meta.local_fields]
        self.pb_field_names = [f.name for f in self.ooe.mapped_fields]
        mod_name = pbandj.genMod(self.pb)
        global proto
        proto = __import__(mod_name)
        
        ooe = proto.OneOfEverything()
        ooe.bool_test = True
        ooe.char_test = "ABC123"
        ooe.comma_test = "A,B,C"
        ooe.date_test.year = 1980
        ooe.date_test.month = 6
        ooe.date_test.day = 10
        ooe.date_time_test.year = 1989
        ooe.date_time_test.month = 9
        ooe.date_time_test.day = 19
        ooe.date_time_test.time.hour = 1
        ooe.date_time_test.time.minute = 2
        ooe.date_time_test.time.second = 3
        ooe.decimal_test = 12.34
        ooe.float_test = 56.789
        ooe.email_test = '*****@*****.**'
        ooe.file_test = "path/to/file"
        ooe.image_test = "/path/to/image"
        ooe.int_test = -1
        ooe.ip_test = '123.456.789.123'
        ooe.null_bool_test = False
        ooe.pos_int_test = 1
        ooe.pos_sm_int_test = 1
        ooe.slug_test = 'This is a slug'
        ooe.sm_int_test = -1
        ooe.text_test = 'This is some text'
        ooe.time_test.hour = 12
        ooe.time_test.minute = 13
        ooe.time_test.second = 14
        ooe.url_test = 'http://www.lcogt.net'
        ooe.xml_test = '<abc>123</abc>'
        self.ooe = ooe
        self.converter = Converter()
        
        
    def test_conversion_invertible(self):
        '''Make sure conversion is invertible'''
        dj_obj = self.converter.toDjangoObj(self.pb, self.ooe)
        pb_obj = self.converter.toProtoMsg(self.pb, dj_obj, proto)
        self.assertEqual(self.ooe, pb_obj)
        
    def test_save_invertible(self):
        '''Make sure conversion is invertible going through database'''
        dj_obj = self.converter.toDjangoObj(self.pb, self.ooe)
        dj_obj.save()
        pb_from_db = self.converter.toProtoMsg(self.pb,
                                          type(dj_obj).objects.get(pk=dj_obj.id),
                                          proto)
        self.ooe.id = dj_obj.id
        self.assertEqual(self.ooe, pb_from_db)
        
    def test_char_field_truncation(self):
        ''' Test truncation of char field '''
        self.ooe.char_test = "ABC123456789"
        dj_obj = self.converter.toDjangoObj(self.pb, self.ooe)
        dj_obj.save()
        pb_from_db = self.converter.toProtoMsg(self.pb,
                                          type(dj_obj).objects.get(pk=dj_obj.id),
                                          proto)
        self.ooe.id = dj_obj.id
        self.ooe.char_test = self.ooe.char_test[0:10]
        self.assertEqual(self.ooe, pb_from_db)