def test_fqn(): assert fqn(DemoModel) == 'demoproject.demoapp.models.DemoModel' assert fqn('demoproject.demoapp.models.DemoModel' ) == 'demoproject.demoapp.models.DemoModel' assert fqn(fqn) == 'strategy_field.utils.fqn' with pytest.raises(ValueError): assert fqn(2)
def pytest_generate_tests(metafunc): func_name = metafunc.function.__name__ values = ids = [] if 'target' in metafunc.fixturenames: if func_name.endswith('_lookup_in'): values = [lambda o: [fqn(o.sender[0])], lambda o: o.sender, lambda o: [fqn(Sender1), fqn(Sender2)], lambda o: [Sender1, Sender2]] ids = ['fqn(target.sender)', 'target.sender', 'fqn(Sender1)', 'Sender1'] else: values = [lambda o: [fqn(Sender1)], lambda o: [Sender1]] ids = ['fqn(Sender1)', 'Sender1'] if 'demo_multiple_model' in metafunc.fixturenames: values.extend([lambda o: [fqn(o.sender[0])], lambda o: o.sender]) ids.extend(['fqn(target.sender)', 'target.sender']) metafunc.parametrize("target", values, ids=ids)
def test_registry_contains(): r = Registry('demoproject.demoapp.models.AbstractSender') r.register(Sender1) r.register(fqn(Sender2)) assert Sender1 in r assert fqn(Sender1) in r assert not 'a.b.c' in r
def test_post_single(webapp): url = reverse('single') res = webapp.post(url, {'sender': fqn(Sender1)}) assert res.json['sender'] == fqn(Sender1) assert DemoModelNone.objects.get(pk=res.json['id']).sender == Sender1 res = webapp.post(url, {'sender': ''}) assert res.json['sender'] is None assert DemoModelNone.objects.get(pk=res.json['id']).sender is None
def test_post_multiple(webapp): url = reverse('multiple') res = webapp.post(url, {'sender': fqn(Sender1)}) assert res.json['sender'] == fqn(Sender1) assert DemoMultipleModel.objects.get(pk=res.json['id']).sender == [Sender1] res = webapp.post(url, {'sender': stringify([Sender1, Sender2])}) assert res.json['sender'] == stringify([Sender1, Sender2]) assert DemoMultipleModel.objects.get(pk=res.json['id']).sender == [Sender1, Sender2]
def pytest_generate_tests(metafunc): func_name = metafunc.function.__name__ values = ids = [] if 'target_factory' in metafunc.fixturenames: if func_name.endswith('_lookup_in'): values = [ lambda o: [fqn(o.sender)], lambda o: [o.sender], lambda o: [fqn(Strategy), fqn(Strategy1)], lambda o: [Strategy, Strategy1] ] ids = [ fqn(Strategy), '<context.sender instance>', str([fqn(Strategy)]), str([Strategy, Strategy1]) ] elif func_name == ('test_model_save'): values = [ lambda o: fqn(o.sender), lambda o: o.sender, lambda o: Strategy(o, 'b') ] ids = [ fqn(Strategy), '<context.sender instance>', 'Strategy1(context)' ] else: values = [lambda o: fqn(Strategy)] ids = ['fqn(Strategy)'] if 'democustommodel' in metafunc.fixturenames: values.extend([lambda o: fqn(o.sender), lambda o: o.sender]) ids.extend( ['fqn(target_factory.sender)', 'target_factory.sender']) metafunc.parametrize("target_factory", values, ids=ids)
def pytest_generate_tests(metafunc): func_name = metafunc.function.__name__ values = ids = [] if 'target_factory' in metafunc.fixturenames: if func_name.endswith('_lookup_in'): values = [lambda o: [fqn(o.sender)], lambda o: [o.sender], lambda o: [fqn(Strategy), fqn(Strategy1)], lambda o: [Strategy, Strategy1]] ids = [fqn(Strategy), '<context.sender instance>', str([fqn(Strategy)]), str([Strategy, Strategy1])] elif func_name == ('test_model_save'): values = [lambda o: fqn(o.sender), lambda o: o.sender, lambda o: Strategy(o, 'b')] ids = [fqn(Strategy), '<context.sender instance>', 'Strategy1(context)'] else: values = [lambda o: fqn(Strategy)] ids = ['fqn(Strategy)'] if 'democustommodel' in metafunc.fixturenames: values.extend([lambda o: fqn(o.sender), lambda o: o.sender]) ids.extend(['fqn(target_factory.sender)', 'target_factory.sender']) metafunc.parametrize("target_factory", values, ids=ids)
def get_prep_lookup(self): value = super(StrategyFieldLookupMixin, self).get_prep_lookup() if value is None: return None if isinstance(value, six.string_types): pass elif isinstance(value, (list, tuple)): value = stringify(value) elif isinstance(value, self.lhs.output_field.registry.klass): value = fqn(value) elif isclass(value) or isinstance(value, object): value = fqn(value) return value
def test_post_single(webapp): url = reverse('single') res = webapp.post(url, params={'sender': fqn(Sender1)}) assert res.json['sender'] == fqn(Sender1) assert DemoModelNone.objects.get(pk=res.json['id']).sender == Sender1 res = webapp.post(url, params={'sender': ''}) assert res.json['sender'] is None assert DemoModelNone.objects.get(pk=res.json['id']).sender is None res = webapp.post(url, expect_errors=True, params={'sender': fqn(DemoModelNone)}) assert res.status_code == 400 assert res.json['sender'] == ['Invalid entry `%s`' % fqn(DemoModelNone)]
def test_form_not_valid(demomodel): form_class = modelform_factory(DemoModel, exclude=[]) form = form_class({'sender': fqn(DemoModel)}, instance=demomodel) assert not form.is_valid() assert form.errors['sender'] == ['Select a valid choice. ' 'demoproject.demoapp.models.DemoModel ' 'is not one of the available choices.']
def test_form_save(democustommodel): form_class = modelform_factory(DemoCustomModel, exclude=[]) form = form_class({'sender': fqn(democustommodel.sender)}, instance=democustommodel) form.is_valid() instance = form.save() assert instance.sender == democustommodel.sender
def test_registry_as_choices(): r = Registry('demoproject.demoapp.models.AbstractSender') r.register(Sender1) r.register(fqn(Sender1)) assert r.as_choices() == [('demoproject.demoapp.models.Sender1', 'demoproject.demoapp.models.Sender1')]
def test_registry(): r = Registry(AbstractSender) r.register(Sender1) assert Sender1 in r assert fqn(Sender1) in r assert Sender2 not in r
def check_or_create( self, prefix, viewset, basename, url_name, ): name = fqn(viewset) source_model = ContentType.objects.get_for_model( viewset().get_queryset().model) service, isnew = self.model.objects.get_or_create( viewset=viewset, defaults={ 'name': name, 'cache_ttl': '1y', 'access': getattr(viewset, 'default_access', conf.DEFAULT_ACCESS), 'description': getattr(viewset, '__doc__', ""), 'source_model': source_model }) with disable_concurrency(service): service.url_name = url_name service.basename = basename service.suffix = prefix service.source_model = source_model service.save() service.viewset.get_service.cache_clear() return service, isnew
def dispatch(self, request, *args, **kwargs): request._view = self if hasattr(request, 'api_info'): request.api_info["view"] = fqn(self) request.api_info["service"] = self.get_service() return super().dispatch(request, *args, **kwargs)
def test_registry_string(): r = Registry('demoproject.demoapp.models.AbstractSender') r.register(Sender1) assert Sender1 in r assert fqn(Sender1) in r assert Sender2 not in r
def test_form_not_valid(demo_multiple_model): form_class = modelform_factory(DemoMultipleModel, exclude=[]) form = form_class({'sender': [fqn(DemoMultipleModel)]}, instance=demo_multiple_model) assert not form.is_valid() assert form.errors['sender'] == ['Select a valid choice. ' 'demoproject.demoapp.models.DemoMultipleModel ' 'is not one of the available choices.']
def test_form_save(demo_multiplecustom_model): form_class = modelform_factory(DemoMultipleCustomModel, exclude=[]) form = form_class({'sender': [fqn(demo_multiplecustom_model.sender[0])]}, instance=demo_multiplecustom_model) form.is_valid() instance = form.save() assert instance.sender == demo_multiplecustom_model.sender
def process_cache_response(self, view_instance, view_method, request, args, kwargs): cache = caches[self.cache_name] if config.CACHE_ENABLED: key = self.calculate_key(view_instance=view_instance, view_method=view_method, request=request, args=args, kwargs=kwargs) response = cache.get(key) else: response = None key = '--' if not response: view_instance.request._request.api_info['cache-hit'] = False response = view_method(view_instance, request, *args, **kwargs) response = view_instance.finalize_response(request, response, *args, **kwargs) response.render( ) # should be rendered, before picklining while storing to cache if config.CACHE_ENABLED and response.status_code == 200: # pragma: no cover expire = parse_ttl(view_instance.get_service().cache_ttl or '1y') cache.set(key, response, expire) else: view_instance.request._request.api_info['cache-hit'] = True view_instance.store('cache-ttl', view_instance.get_service().cache_ttl) view_instance.store('service', view_instance.get_service()) view_instance.store('view', fqn(view_instance)) if not hasattr(response, '_closable_objects'): # pragma: no cover response._closable_objects = [] return response
def test_log(enable_stats, django_app, system_user, reset_stats, settings): settings.ENABLE_LIVE_STATS = True url = InterventionViewSet.get_service().endpoint # url = reverse("api:intervention-list", args=['v1']) url = f"{url}?country_name=bolivia,chad,lebanon" res = django_app.get(url) assert res.status_code == 200 log = APIRequestLog.objects.first() assert log assert log.method == 'GET' assert log.content_type == 'application/json' assert log.remote_addr == '192.168.66.66' assert log.host == 'testserver' assert log.user == system_user assert log.viewset == InterventionViewSet assert log.service == fqn(InterventionViewSet) assert not log.cached daily = DailyCounter.objects.get(day=log.requested_at) assert daily.total == 1 assert daily.response_max == log.response_ms assert daily.response_min == log.response_ms assert daily.response_average == log.response_ms res = django_app.get(url) assert res.status_code == 200 daily = MonthlyCounter.objects.get(day__month=log.requested_at.month, user=system_user) assert daily.total == 2
def test_get_single(webapp): x = G(DemoModelNone) res = webapp.get('/api/s/' + str(x.id) + '/') assert res.json['sender'] == fqn(x.sender) x = G(DemoModelNone, sender=None) res = webapp.get('/api/s/' + str(x.id) + '/') assert res.json['sender'] is None
def get_prep_value(self, value): if value is None: return None # if isinstance(value, six.string_types): # return value # if isclass(value) or isinstance(value, object): # return fqn(value) return fqn(value)
def test_get_class(): assert get_class(None) == None assert get_class('') == '' assert get_class(fqn(DemoModel)) == DemoModel assert get_class(DemoModel) == DemoModel assert get_class(DemoModel()) == DemoModel with pytest.raises(ValueError): assert get_class('x') assert get_class(2) == int
def get_stash_url(obj, label=None, **kwargs): if not obj: return '' qn = fqn(obj) url = "{}{}.py".format(config.SOURCE_REPOSITORY, os.path.dirname(qn.replace('.', '/'))) attrs = " ".join(['{}="{}"'.format(k, v) for k, v in kwargs.items()]) return mark_safe('<a class="code" {} href="{}?c={}">{}</a>'.format( attrs, url, qn, label or qn.split('.')[-1]))
def test_queue(django_app, admin_user, modeladmin, monkeypatch): opts = modeladmin.model._meta target = fqn(modeladmin.model) monkeypatch.setattr(f'{target}.loader.task.delay', MagicMock()) url = reverse(f"admin:{opts.app_label}_{opts.model_name}_changelist") res = django_app.get(url, user=admin_user) res = res.click("Queue").follow() assert res.status_code == 200
def authenticate(self, request): view = request._request._view service = view.get_service() if service.access == acl.ACL_ACCESS_OPEN: User = get_user_model() user = User.objects.get_or_create(username='******')[0] request.user = user login(request, user, fqn(ModelBackend)) return (user, None)
class SystemFilter(models.Model): """ Store 'hardcoded' filters per user @see AutoFilterRule """ application = models.ForeignKey(Application, models.CASCADE, blank=True, null=True) user = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, blank=True, null=True) group = models.ForeignKey(Group, models.CASCADE, blank=True, null=True) service = models.ForeignKey(Service, models.CASCADE) description = models.TextField(blank=True) handler = models.CharField(max_length=500, default=fqn(SystemFilterHandler)) objects = SystemFilterManager() class Meta: unique_together = (('service', 'user'), ('service', 'group')) def __str__(self): return f"{self.user}/{self.service}" def set_rule(self, **kwargs): for field, value in kwargs.items(): self.test(**{field: value}) r, __ = self.rules.get_or_create(field=field) r.value = value r.save() def test(self, **kwargs): try: self.service.viewset().get_queryset().filter(**kwargs) except (FieldError, TypeError) as e: raise InvalidField(e) @lru_cache() def get_filters(self): f = {} for r in self.rules.all(): f[r.field] = r.value return f @lru_cache() def get_querystring(self): f = [] for field, value in self.get_filters().items(): f.append("{0}={1}".format(field, value)) return "&".join(f) def filter_queryset(self, queryset): return queryset.filter(**self.get_filters())
def test_registry_is_valid(): r = Registry('demoproject.demoapp.models.AbstractSender') assert r.is_valid(Sender1) assert r.is_valid(fqn(Sender1)) assert not r.is_valid(DemoModel) r = Registry(None) assert r.is_valid(Sender1) assert r.is_valid(DemoModel) assert not r.is_valid('demoproject.demoapp.models.Wrong')
def deserialize(self, value, obj=None): ret = [] if isinstance(value, six.string_types): value = value.split(',') for v in value: if isinstance(v, six.string_types): v = import_by_name(v) if not issubclass(v, self.klass): raise ValueError(fqn(v)) ret.append(v(obj)) return ret
def test_load_requiredismissing(loader1): with mock.patch( 'etools_datamart.apps.mart.data.models.Intervention.loader.need_refresh', lambda *a: True): # update_context() is mocked only to prevent not needed long running test # because update_context() is invoked only if RequiredIsMissing is not raised # we do not want wait the full load only to detect the error with mock.patch( '%s.update_context' % fqn(loader1), side_effect=Exception('missing to raise RequiredIsMissing')): with pytest.raises(RequiredIsMissing): loader1.load(max_records=2, force_requirements=False)
def test_display_attribute(demomodel, registry, monkeypatch): monkeypatch.setattr(SenderNotRegistered, 'label', classmethod(lambda s: fqn(s).split('.')[-1]), raising=False) DemoModel._meta.get_field('sender').display_attribute = 'label' DemoModel._meta.get_field('sender').registry = registry registry.register(SenderNotRegistered) form_class = modelform_factory(DemoModel, exclude=[]) form = form_class(instance=demomodel) assert form.fields['sender'].choices[1][1] == 'SenderNotRegistered'
def test_refresh(django_app, admin_user, modeladmin, monkeypatch): target = fqn(modeladmin.model) monkeypatch.setattr(f'{target}.loader.load', MagicMock()) opts = modeladmin.model._meta url = reverse(f"admin:{opts.app_label}_{opts.model_name}_changelist") res = django_app.get(url, user=admin_user) assert res.status_code == 200 res = res.click("Refresh$").follow() assert res.status_code == 200 storage = res.context['messages'] assert [messages.DEFAULT_TAGS[m.level] for m in storage] == ['success'], [m.message for m in storage]
def test_post_multiple(webapp): url = reverse('multiple') res = webapp.post(url, params={'sender': [fqn(Sender1), fqn(Sender2)]}) assert res.json['sender'] == [fqn(Sender1), fqn(Sender2)] assert DemoMultipleModel.objects.get(pk=res.json['id']).sender == [Sender1, Sender2] res = webapp.post(url, expect_errors=True, params={'sender': [fqn(Sender1),fqn(DemoModelNone)]}) assert res.status_code == 400 assert res.json['sender'] == ['Invalid entry `%s`' % fqn(DemoModelNone)]
def to_representation(self, obj): return [fqn(i) for i in obj]
def test_fqn(): assert fqn(DemoModel) == 'demoproject.demoapp.models.DemoModel' assert fqn('demoproject.demoapp.models.DemoModel') == 'demoproject.demoapp.models.DemoModel' assert fqn(fqn) == 'strategy_field.utils.function' with pytest.raises(ValueError): assert fqn(2)
def test_formfield_invalid_classname(registry): f = StrategyFormField(registry=registry, choices=registry.as_choices()) with pytest.raises(ValidationError): assert f.clean(fqn(User))
def test_form_save(demomodel): form_class = modelform_factory(DemoModel, exclude=[]) form = form_class({'sender': fqn(demomodel.sender)}, instance=demomodel) assert form.is_valid(), form.errors instance = form.save() assert instance.sender == demomodel.sender
def prepare_value(self, value): if isinstance(value, six.string_types): return value if value: return fqn(value)
def bound_data(self, data, initial): if isinstance(data, six.string_types): return data return fqn(data)
def democustommodel(): from strategy_field.utils import fqn from demoproject.demoapp.models import (DemoCustomModel, Strategy, ) return DemoCustomModel.objects.get_or_create(sender=fqn(Strategy))[0]
def demo_multiplecustom_model(): from strategy_field.utils import fqn from demoproject.demoapp.models import (DemoMultipleCustomModel, Strategy, ) return DemoMultipleCustomModel.objects.get_or_create(sender=[fqn(Strategy)])[0]
def get_choices(self, include_blank=True, blank_choice=BLANK_CHOICE_DASH, limit_choices_to=None): first_choice = blank_choice if include_blank else [] return first_choice + [(fqn(klass), l) for klass, l in self.choices]
def pre_save(self, model_instance, add): value = getattr(model_instance, self.attname) if value: return fqn(value)