def test_get_objects_of_tenant(db): store1 = StoreTenant.objects.create() store2 = StoreTenant.objects.create() with tenant_context(store1): Product.objects.create(name="prod1") with tenant_context(store2): Product.objects.create(name="prod2") assert Product.objects.count() == 1
def test_all_objects(db): store1 = StoreTenant.objects.create() store2 = StoreTenant.objects.create() with tenant_context(store1): Product.objects.create(name='prod1') with tenant_context(store2): Product.objects.create(name='prod2') assert Product.all_objects.count() == 2
def test_all_objects(db): store1 = StoreTenant.objects.create() store2 = StoreTenant.objects.create() with tenant_context(store1): Product.objects.create(name="prod1") Product.objects.count() == 1 with tenant_context(store2): Product.objects.create(name="prod2") Product.objects.count() == 1 with tenant_context_disabled(): assert Product.objects.count() == 2
def tenant_ctx(db): """Create a tenant a set context""" from easy_tenants import tenant_context, get_tenant_model tenant = get_tenant_model().objects.create() with tenant_context(tenant): yield tenant
def test_tenant_context(db): tenant1 = StoreTenant.objects.create() tenant2 = StoreTenant.objects.create() set_current_tenant(tenant1) with tenant_context(tenant2): assert tenant2 == get_current_tenant() assert tenant1 == get_current_tenant()
def __call__(self, request): if not request.user.is_authenticated: return self.get_response(request) self.tenant = self.get_tenant(request) if not self.tenant: return self.get_response(request) # views works with tenant with tenant_context(self.tenant): return self.get_response(request)
def test_modelfield_contents(tenant_ctx): class ProductForm(forms.Form): product = forms.ModelChoiceField(queryset=Product.objects.all()) p1 = Product.objects.create(name="prod1") p2 = Product.objects.create(name="prod2") other_tenant = StoreTenant.objects.create() with tenant_context(other_tenant): Product.objects.create(name="prod3") form = ProductForm() form_template = form.as_p() assert f'<option value="{p1.id}">{p1.name}</option>' in form_template assert f'<option value="{p2.id}">{p2.name}</option>' in form_template assert "prod3" not in form_template
def test_related_field_contents(tenant_ctx): class ProductForm(forms.Form): category = forms.ModelChoiceField(queryset=Category.objects.all()) c1 = Category.objects.create(name="cat1") c2 = Category.objects.create(name="cat2") other_tenant = StoreTenant.objects.create() with tenant_context(other_tenant): Category.objects.create(name="cat3") form = ProductForm() form_template = form.as_p() assert f'<option value="{c1.id}">{c1.name}</option>' in form_template assert f'<option value="{c2.id}">{c2.name}</option>' in form_template assert "cat3" not in form_template
def test_tenant_required_error(db): with pytest.raises(TenantError): with tenant_context(): Product.objects.create(name="prod1")