-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
101 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from typing import Dict, Type | ||
|
||
from osbot_utils.decorators.methods.cache_on_self import cache_on_self | ||
from osbot_utils.helpers.sqlite.Sqlite__Database import Sqlite__Database | ||
from osbot_utils.helpers.sqlite.tables.Sqlite__Table__Config import Sqlite__Table__Config, SQLITE__TABLE_NAME__CONFIG, \ | ||
Schema__Table__Config | ||
|
||
|
||
class Sqlite__DB(Sqlite__Database): | ||
|
||
# methods to override | ||
def tables_to_add(self): # use this to define the tables that should be automatically created on setup | ||
return {} | ||
|
||
def db_not_configured(self): | ||
return self.tables_names() == [] | ||
|
||
@cache_on_self | ||
def table_config(self): | ||
return Sqlite__Table__Config(database=self) | ||
|
||
|
||
|
||
def tables_names(self, include_sqlite_master=False, include_indexes=False): | ||
return super().tables_names(include_sqlite_master=include_sqlite_master, include_indexes=include_indexes) | ||
|
||
def setup(self): | ||
if self.db_not_configured(): | ||
self.table_config().setup() | ||
for table_name, row_schema in self.tables_to_add().items(): | ||
table = self.table(table_name) | ||
table.row_schema = row_schema | ||
table.create() | ||
return self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from unittest import TestCase | ||
|
||
from osbot_utils.helpers.sqlite.Sqlite__Database import Sqlite__Database | ||
from osbot_utils.helpers.sqlite.domains.Sqlite__DB import Sqlite__DB | ||
from osbot_utils.utils.Dev import pprint | ||
|
||
|
||
class test_Sqlite__DB(TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls.sqlite_db = Sqlite__DB().setup() | ||
|
||
def test__init__(self): | ||
with self.sqlite_db as _: | ||
default_vars = Sqlite__Database().__attr_names__() | ||
expected_vars = default_vars | ||
|
||
assert _.__attr_names__() == sorted(expected_vars) | ||
assert _.tables_names(include_indexes=False) == ['config'] | ||
|
||
def test_table_config(self): | ||
with self.sqlite_db.table_config() as _: | ||
assert _.exists() is True | ||
assert _.table_name == 'config' | ||
assert _.data() == {} | ||
|
||
def test_setup(self): | ||
with self.sqlite_db as _: | ||
assert _.exists() is True | ||
assert _.in_memory is True | ||
assert _.db_path is None | ||
assert _.tables_names() == ['config'] |