Skip to content

Commit

Permalink
Added Test
Browse files Browse the repository at this point in the history
  • Loading branch information
yubarajshrestha committed Jun 7, 2022
1 parent e23eb8c commit 0623fe4
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion config/database.py
Expand Up @@ -9,7 +9,7 @@
They can be named whatever you want.
"""
DATABASES = {
"default": env("DB_CONNECTION", "postgres"),
"default": env("DB_CONNECTION", "sqlite"),
"sqlite": {
"driver": "sqlite",
"database": env("SQLITE_DB_DATABASE", "masonite.sqlite3"),
Expand Down
Binary file added masonite.sqlite3
Binary file not shown.
8 changes: 4 additions & 4 deletions src/masonite_audit/observer/audit_observer.py
Expand Up @@ -13,10 +13,10 @@ def _parse_model(self, model, action):
old_value = model.__original_attributes__

for key in new_value:
if isinstance(new_value[key], datetime):
new_value[key] = new_value[key].to_datetime_string()
if isinstance(old_value[key], datetime):
old_value[key] = old_value[key].to_datetime_string()
if isinstance(new_value.get(key), datetime):
new_value[key] = new_value.get(key).to_datetime_string()
if isinstance(old_value.get(key), datetime):
old_value[key] = old_value.get(key).to_datetime_string()

data = {
"action": action,
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/test_audit.py
@@ -0,0 +1,40 @@
from masonite.tests import TestCase
from masoniteorm.query import QueryBuilder
from tests.integrations.app.models.User import User
from masonite.facades import Hash


class TestAudit(TestCase):
@classmethod
def setUpClass(cls):
QueryBuilder().table("audit_logs").truncate(True)
QueryBuilder().table("users").truncate(True)

def tearDown(self):
super().tearDown()
QueryBuilder().table("audit_logs").truncate(True)
QueryBuilder().table("users").truncate(True)

def test_audit_created(self):
user = User.create({
"name": "Yubaraj",
"email": "user@example.com",
"password": Hash.make("secret"),
"phone": "+123456789",
})

self.assertDatabaseHas(
"users",
{
"email": "user@example.com",
},
)

self.assertDatabaseHas(
"audit_logs",
{
"model_id": user.id,
"model_name": "users",
"action": "CREATED",
}
)

0 comments on commit 0623fe4

Please sign in to comment.