示例#1
0
    def test_custom_type_generator(self):

        def int_generator():
            return 9

        Generator.register(int_generator, "IntegerField")
        my_obj = Generator.create(CharModel)
        self.assertEquals(my_obj.int_field, 9)
        my_obj2 = Generator.create(CharModel)
        self.assertEquals(my_obj2.int_field, 9)
示例#2
0
    def test_custom_field_generator(self):

        def int_generator():
            return 12

        Generator.register(int_generator, CharModel, "int_field",)
        my_obj = Generator.create(CharModel)
        self.assertEquals(my_obj.int_field, 12)
        my_obj = Generator.create(CharModel, int_field=18)
        self.assertEquals(my_obj.int_field, 18)
示例#3
0
    def saveGenerators(self):
        """Saves all the new/changed generators to the dabase, while trying to
		minimize the impact on DB performance."""
        logger = logging.getLogger(__name__)

        # Analyze the retrieved generators, and create list of all the generators
        # we would need to try to fetch from the database.
        newGenerators = {}
        rawGid = []
        rawAlertID = []
        for gen in self.generators:
            if self.generators[gen][0] == self.RAW:
                newGenerators[gen] = self.generators[gen][1]
                rawGid.append(self.generators[gen][1][0])
                rawAlertID.append(self.generators[gen][1][1])

        logger.debug("Found %d new generators to be checked" % len(rawGid))

        # Try to fetch the generators from the database, and loop trough them:
        generators = Generator.objects.filter(GID__in=rawGid,
                                              alertID__in=rawAlertID).all()
        for generator in generators:
            key = "%d-%d" % (generator.GID, generator.alertID)
            # If this is a generator that we want to look at (as we will also get 1-2 when we
            #   look for 1-1 and 2-2... )
            if key in newGenerators:
                status = self.SAVED
                rawGenerator = newGenerators.pop(key)

                # If the message is new, add the new message.
                if (rawGenerator[2] != generator.message):
                    status = self.CHANGED
                    generator.message = rawGenerator[2]

                # If anything needed to be changed, save the object.
                if (status == self.CHANGED):
                    generator.save()
                    status = self.SAVED
                    logger.debug("Updated %s" % str(generator))

                # Store the object in the local cache, in case it is needed later.
                self.generators[key] = [status, generator]

        # If there are any generators we could not find in the database
        if (len(newGenerators)):
            # Make a list of new Generator objects, a list of gid's and a list of alertID's.
            g = []
            gids = []
            alertIDs = []
            for gen in newGenerators:
                generator = newGenerators[gen]
                gids.append(generator[0])
                alertIDs.append(generator[1])
                g.append(
                    Generator(GID=generator[0],
                              alertID=generator[1],
                              message=generator[2]))

            # Insert the created Generator objects to the database.
            Generator.objects.bulk_create(g)
            logger.debug("Created %d new generators" % len(g))

            # Read them back out, and store them in memory. In case somebody needs them later in the
            # update.
            for generator in Generator.objects.filter(
                    GID__in=gids, alertID__in=alertIDs).all():
                key = "%d-%d" % (generator.GID, generator.alertID)
                self.generators[key] = [self.SAVED, generator]
示例#4
0
    def test_many2many_generator(self):
        my_obj = Generator.create(Many2ManyModel)
        self.assertTrue(my_obj.many2many.count()>0)

        
        
示例#5
0
 def test_value_generator(self):
     Generator.register(10, CharModel, "int_field",)
     my_obj = Generator.create(CharModel)
     self.assertEquals(my_obj.int_field, 10)
示例#6
0
 def test_partial_value(self):
     my_obj = Generator.create(CharModel, int_field=18)
     self.assertEquals(my_obj.int_field, 18)
示例#7
0
 def check_this_model(self, model):
     self.assertRaises(IntegrityError, model.objects.create)
     my_obj = Generator.create(model)
     self.assertTrue(isinstance(my_obj,model), "The object should be of the %s class" % model)