Repository: HBNetwork/django-ultratenant (language: python)

Issue:
strategie multidb with sqlite3



Discussion / hints:
Código: https://github.com/HBN3tw0rk/django-ultratenant/blob/c1c3e7e505fe3c04f5b044f548552703fbee8871/tests/test_mapper.py#L13
- [ ] Mover [DatabaseMapper](https://github.com/HBN3tw0rk/django-ultratenant/blob/c1c3e7e505fe3c04f5b044f548552703fbee8871/tests/test_mapper.py#L13) para https://github.com/HBN3tw0rk/django-ultratenant/blob/main/ultratenant/multidb.py

Fix patch:
diff --git a/ultratenant/multidb.py b/ultratenant/multidb.py
index 14e50f8..ae11d9e 100644
--- a/ultratenant/multidb.py
+++ b/ultratenant/multidb.py
@@ -1,3 +1,5 @@
+from dj_database_url import parse as dburl
+from django.apps import apps
 from django.db import models
 
 from ultratenant.threadlocal import TENANTLOCAL
@@ -14,29 +16,26 @@ def __str__(self):
         return self.key
 
 
-class SQLiteMapper(dict):
-    ENGINE = "django.db.backends.sqlite3"
+class DatabaseMapper(dict):
+    def __init__(self, default, **others):
+        self.cache = {"default": dburl(default)}
+        self.cache.update({k: dburl(v) for k, v in others.items()})
 
-    def __init__(self, path, inmemory=False, **default):
-        self.path = path
-        self.inmemory = inmemory
-        self.cache = {"default": default}
+    def __getitem__(self, key):
+        if key not in self.cache:
+            self.cache[key] = dburl(self.load_dburl(key))
 
-    def name(self, tenant_key):
-        return str(self.path / f"{tenant_key}.db.sqlite3")
+        return self.cache[key]
 
-    def __getitem__(self, tenant):
-        if tenant not in self.cache:
-            self.cache[tenant] = {
-                **self.cache["default"],
-                "ENGINE": self.ENGINE,
-                "NAME": self.name(tenant),
-            }
-        return self.cache[tenant]
-
-    def __contains__(self, tenant):
+    def __contains__(self, key):
         return True
 
+    @staticmethod
+    def load_dburl(key):
+        Tenant = apps.get_model("multidb", "Tenant")
+        t = Tenant.objects.get(key=key)
+        return t.dburl
+
 
 class TenantRouter:
     def db_for_read(self, *args, **hints):


Test patch:
diff --git a/tests/test_mapper.py b/tests/test_mapper.py
index 9472572..3755b84 100644
--- a/tests/test_mapper.py
+++ b/tests/test_mapper.py
@@ -1,36 +1,14 @@
 from pathlib import Path
 
-from dj_database_url import parse as dburl
 from django.apps import apps
 from django.test import TestCase
 
-from ultratenant.multidb import SQLiteMapper, TenantRouter
+from ultratenant.multidb import DatabaseMapper
 from ultratenant.threadlocal import TENANTLOCAL
 
 PATH = Path(__file__).parent
 
 
-class DatabaseMapper(dict):
-    def __init__(self, default, **others):
-        self.cache = {"default": dburl(default)}
-        self.cache.update({k: dburl(v) for k, v in others.items()})
-
-    def __getitem__(self, key):
-        if key not in self.cache:
-            self.cache[key] = dburl(self.load_dburl(key))
-
-        return self.cache[key]
-
-    def __contains__(self, key):
-        return True
-
-    @staticmethod
-    def load_dburl(key):
-        Tenant = apps.get_model("multidb", "Tenant")
-        t = Tenant.objects.get(key=key)
-        return t.dburl
-
-
 class TestDatabaseMapper(TestCase):
     def test_load_tenant_dburl_from_default_database(self):
         dbfilename = str(PATH / "t3.sqlite3")
@@ -42,36 +20,3 @@ def test_load_tenant_dburl_from_default_database(self):
         map = DatabaseMapper(default="sqlite://:memory:")
 
         assert map["t3"]["NAME"] == dbfilename
-
-
-class TestSQLiteMapper:
-    def test_getitem(self):
-        mapper = SQLiteMapper(path=Path("/"), test=True)
-
-        expected = {
-            "test": True,
-            "ENGINE": "django.db.backends.sqlite3",
-            "NAME": "/tenant.db.sqlite3",
-        }
-
-        assert mapper["tenant"] == expected
-
-    def test_contains(self):
-        mapper = SQLiteMapper(Path("/"))
-
-        assert "tenant" in mapper
-        assert "whatever" in mapper
-
-    def test_router_and_mapper(self):
-        TENANTLOCAL.tenant = "t1"
-
-        router, mapper = TenantRouter(), SQLiteMapper(Path("/"))
-
-        expected = {
-            "ENGINE": "django.db.backends.sqlite3",
-            "NAME": "/t1.db.sqlite3",
-        }
-        assert TENANTLOCAL.tenant == "t1"
-        assert router.db_for_read() == "t1"
-        assert mapper[router.db_for_read()] == expected
-        assert mapper[router.db_for_write()] == expected