def test_honor_save(self): """ test if saving honors the only attribute :return: """ previous1, random1, random2 = ( random_string(10), random_string(10), random_string(10), ) # create instance instance = OrdinaryTest() instance.random = previous1 instance.save() ModelEvent.objects.all().delete() instance.random = random1 instance.random2 = random2 instance.save(update_fields=['random2']) events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] modifications = event.modifications.all() self.assertEqual(modifications.count(), 1) modification = modifications[0] self.assertEqual(modification.operation, int(Operation.CREATE)) self.assertEqual(modification.field.name, 'random2')
def test_models(self): from django.conf import settings from automated_logging.settings import settings as conf self.clear() settings.AUTOMATED_LOGGING['model']['exclude']['models'] = [ 'automated_logging.tests.models.OrdinaryTest' ] conf.load.cache_clear() OrdinaryTest(random=random_string()).save() self.assertEqual(ModelEvent.objects.count(), 0) settings.AUTOMATED_LOGGING['model']['exclude']['models'] = [ 'automated_logging.OrdinaryTest' ] conf.load.cache_clear() OrdinaryTest(random=random_string()).save() self.assertEqual(ModelEvent.objects.count(), 0) settings.AUTOMATED_LOGGING['model']['exclude']['models'] = [ 'OrdinaryTest' ] conf.load.cache_clear() OrdinaryTest(random=random_string()).save() self.assertEqual(ModelEvent.objects.count(), 0) OneToOneTest().save() self.assertEqual(ModelEvent.objects.count(), 1) self.assertEqual(ModelEvent.objects.all()[0].modifications.count(), 1)
def test_layering(self): Model = include_model(operations=['modify'], fields=['random2'])( include_model(operations=['create'], fields=['random2'])(OrdinaryTest)) self.clear() subject = Model() subject.save() subject.random = random_string() subject.save() self.assertEqual(ModelEvent.objects.count(), 2) delattr(AutomatedLoggingMiddleware.thread, 'dal') cached_model_exclusion.cache_clear() Model = exclude_model(operations=['modify'], fields=['random2'])( exclude_model(operations=['create'], fields=['random2'])(OrdinaryTest)) self.clear() subject = Model() subject.save() subject.random = random_string() subject.save() self.assertEqual(ModelEvent.objects.count(), 0)
def test_globals(self): from django.conf import settings from automated_logging.settings import settings as conf self.clear() settings.AUTOMATED_LOGGING['unspecified']['exclude'][ 'applications'] = [] settings.AUTOMATED_LOGGING['model']['exclude']['applications'] = [] settings.AUTOMATED_LOGGING['request']['exclude']['applications'] = [] settings.AUTOMATED_LOGGING['globals']['exclude']['applications'] = [ 'automated*' ] conf.load.cache_clear() OrdinaryTest(random=random_string()).save() self.assertEqual(ModelEvent.objects.count(), 0) self.request('GET', self.view) self.assertEqual(RequestEvent.objects.count(), 0) logger = logging.getLogger(__name__) logger.info('[TEST]') self.assertEqual(UnspecifiedEvent.objects.count(), 0)
def test_performance(self): """ test if setting the performance parameter works correctly :return: """ from django.conf import settings from automated_logging.settings import settings as conf self.bypass_request_restrictions() settings.AUTOMATED_LOGGING['model']['performance'] = True conf.load.cache_clear() ModelEvent.objects.all().delete() instance = OrdinaryTest() instance.random = random_string(10) checkpoint = datetime.datetime.now() instance.save() checkpoint = datetime.datetime.now() - checkpoint events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertIsNotNone(event.performance) self.assertLess(event.performance.total_seconds(), checkpoint.total_seconds())
def test_delete(self): """ test if deletion is working correctly and records all the changes :return: """ value = random_string(10) # create instance instance = OrdinaryTest() instance.random = value instance.save() pk = instance.pk re = repr(instance) ModelEvent.objects.all().delete() instance.delete() # DUP of save events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.operation, int(Operation.DELETE)) self.assertEqual(event.user, None) self.assertEqual(event.entry.primary_key, str(pk)) self.assertEqual(event.entry.value, re) # DELETE does currently not record deleted values modifications = event.modifications.all() self.assertEqual(modifications.count(), 0)
def test_reverse(self): m2m = M2MTest() m2m.save() subject = OrdinaryTest(random=random_string()) subject.save() ModelEvent.objects.all().delete() subject.m2mtest_set.add(m2m) subject.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.modifications.count(), 0) self.assertEqual(event.relationships.count(), 1) self.assertEqual(event.entry.mirror.name, 'M2MTest') relationship = event.relationships.all()[0] self.assertEqual(relationship.operation, int(Operation.CREATE)) self.assertEqual(relationship.field.name, 'relationship') self.assertEqual(relationship.entry.primary_key, str(subject.id))
def test_foreign(self): """ test if ForeignKey are correctly recognized. should be handled by save.py """ fk = ForeignKeyTest() fk.save() subject = OrdinaryTest(random=random_string()) subject.save() ModelEvent.objects.all().delete() fk.relationship = subject fk.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.modifications.count(), 1) self.assertEqual(event.relationships.count(), 0) modification = event.modifications.all()[0] self.assertEqual(modification.field.name, 'relationship_id') self.assertEqual(modification.current, repr(subject.pk))
def test_one2one(self): """ test if OneToOne are correctly recognized, should be handled by save.py """ o2o = OneToOneTest() o2o.save() subject = OrdinaryTest(random=random_string()) subject.save() ModelEvent.objects.all().delete() o2o.relationship = subject o2o.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.modifications.count(), 1) self.assertEqual(event.relationships.count(), 0) modification = event.modifications.all()[0] self.assertEqual(modification.field.name, 'relationship_id') self.assertEqual(modification.current, repr(subject.pk))
def view(request): value = random_string() instance = OrdinaryTest() instance.random = value instance.save() return JsonResponse({})
def test_model_fields(self): Model = include_model(operations=[], fields=['random'])(exclude_model( operations=None, fields=['random2'])(OrdinaryTest)) subject = Model() subject.save() self.clear() subject.random = random_string() subject.save() self.assertEqual(ModelEvent.objects.count(), 1) subject.random2 = random_string() subject.save() self.assertEqual(ModelEvent.objects.count(), 1)
def test_modify(self): """ test if modification results in proper delete and create field operations :return: """ previous, current = random_string(10), random_string(10) # create instance instance = OrdinaryTest() instance.random = previous instance.save() # delete all stuff related to the instance events to have a clean slate ModelEvent.objects.all().delete() instance.random = current instance.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.user, None) self.assertEqual(event.operation, int(Operation.MODIFY)) modifications = event.modifications.all() self.assertEqual(modifications.count(), 1) modification = modifications[0] self.assertEqual(modification.operation, int(Operation.MODIFY)) self.assertEqual(modification.previous, previous) self.assertEqual(modification.current, current) relationships = event.relationships.all() self.assertEqual(relationships.count(), 0)
def test_include_model(self): self.clear() subject = DecoratorOverrideExclusionTest(random=random_string()) subject.save() self.assertEqual(ModelEvent.objects.count(), 1) self.clear() # test if overriding works include_model(FullClassBasedExclusionTest, operations=['delete'])() subject = FullClassBasedExclusionTest(random=random_string()) subject.save() self.assertEqual(ModelEvent.objects.count(), 0) subject.delete() self.assertEqual(ModelEvent.objects.count(), 1) # just to make sure we clean up delattr(AutomatedLoggingMiddleware.thread, 'dal')
def test_unknown(self): from django.conf import settings from automated_logging.settings import settings as conf logger = logging.getLogger(__name__) default_factory = logging.getLogRecordFactory() def factory(*args, **kwargs): """ force setting the pathname and module wrong so that we can pretend to exclude unknowns """ record = default_factory(*args, **kwargs) record.pathname = '/example.py' record.module = 'default' return record self.clear() logging.setLogRecordFactory(factory=factory) settings.AUTOMATED_LOGGING['unspecified']['exclude']['unknown'] = True conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) settings.AUTOMATED_LOGGING['unspecified']['exclude']['unknown'] = False conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 1) logging.setLogRecordFactory(default_factory)
def test_partial(self): self.clear() subject = PartialClassBasedExclusionTest(random=random_string()) subject.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.modifications.count(), 1) self.assertEqual(event.modifications.all()[0].field.name, 'id') self.clear() subject.delete() self.assertEqual(ModelEvent.objects.count(), 0)
def test_create_simple_value(self): """ test if creation results in the correct fields :return: """ self.bypass_request_restrictions() value = random_string() instance = OrdinaryTest() instance.random = value instance.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.operation, int(Operation.CREATE)) self.assertEqual(event.user, None) self.assertEqual(event.entry.primary_key, str(instance.pk)) self.assertEqual(event.entry.value, repr(instance)) self.assertEqual(event.entry.mirror.name, 'OrdinaryTest') self.assertEqual(event.entry.mirror.application.name, 'automated_logging') modifications = event.modifications.all() # pk and random added and modified self.assertEqual(modifications.count(), 2) self.assertEqual({m.field.name for m in modifications}, {'random', 'id'}) modification = [m for m in modifications if m.field.name == 'random'][0] self.assertEqual(modification.operation, int(Operation.CREATE)) self.assertEqual(modification.previous, None) self.assertEqual(modification.current, value) self.assertEqual(modification.event, event) self.assertEqual(modification.field.name, 'random') self.assertEqual(modification.field.type, 'CharField') relationships = event.relationships.all() self.assertEqual(relationships.count(), 0)
def test_snapshot(self): from django.conf import settings from automated_logging.settings import settings as conf self.bypass_request_restrictions() settings.AUTOMATED_LOGGING['model']['snapshot'] = True conf.load.cache_clear() instance = OrdinaryTest(random=random_string()) instance.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertIsNotNone(event.snapshot) self.assertEqual(instance, event.snapshot)
def test_exclude_model(self): self.clear() subject = FullDecoratorBasedExclusionTest() subject.save() self.assertEqual(ModelEvent.objects.count(), 0) subject = PartialDecoratorBasedExclusionTest(random=random_string()) subject.save() events = ModelEvent.objects.all() self.assertEqual(events.count(), 1) event = events[0] self.assertEqual(event.modifications.count(), 1) self.clear() subject.delete() self.assertEqual(ModelEvent.objects.count(), 0)
def generate_children(samples=10): """ generate X children that are going to be used in various tests """ children = [OrdinaryTest(random=random_string()) for _ in range(samples)] [c.save() for c in children] return children
def test_files(self): from django.conf import settings from automated_logging.settings import settings as conf path = Path(__file__).absolute() project = Path(__file__).parent.parent.parent relative = path.relative_to(project) logger = logging.getLogger(__name__) self.clear() # absolute path settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = [ path.as_posix() ] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) # relative path settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = [ relative.as_posix() ] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) # file name settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = [ relative.name ] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) # single directory name settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = [ 'automated_logging' ] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) # absolute directory settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = [ path.parent.as_posix() ] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 0) # file not excluded settings.AUTOMATED_LOGGING['unspecified']['exclude']['files'] = ['dal'] conf.load.cache_clear() logger.info(random_string()) self.assertEqual(UnspecifiedEvent.objects.count(), 1)