Skip to content

Commit

Permalink
passing tests adding update settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nilbacardit26 committed Oct 22, 2024
1 parent 624cc42 commit 049764c
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

- Adding metadata field as an object
- Adding command audit-update-mappings to update mappings
- Adding command audit-update-settings to update settings
[nilbacardit26]


Expand Down
3 changes: 2 additions & 1 deletion guillotina_audit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
}
},
"commands": {
"audit-update-mappings": "guillotina_audit.commands.mappings.UpdateMappingsCommand"
"audit-update-mappings": "guillotina_audit.commands.mappings.UpdateMappingsCommand",
"audit-update-settings": "guillotina_audit.commands.settings.UpdateSettingsCommand",
},
}

Expand Down
2 changes: 1 addition & 1 deletion guillotina_audit/commands/mappings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import logging


logger = logging.getLogger("guillotina_elasticsearch")
logger = logging.getLogger("guillotina_audit")


class UpdateMappingsCommand(Command):
Expand Down
24 changes: 24 additions & 0 deletions guillotina_audit/commands/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from guillotina.commands import Command
from guillotina.component import get_utility
from guillotina.interfaces import ICatalogUtility

import asyncio
import logging


logger = logging.getLogger("guillotina_audit")


class UpdateSettingsCommand(Command):
description = "Update Settings Command"
migrator = None
reindexer = None

def get_parser(self):
parser = super(UpdateSettingsCommand, self).get_parser()
return parser

async def run(self, arguments, settings, app):
search = get_utility(ICatalogUtility)
await asyncio.sleep(1)
await search.update_settings()
3 changes: 2 additions & 1 deletion guillotina_audit/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ def elasticsearch_fixture(es):

@pytest.fixture(scope="function")
async def guillotina_es(elasticsearch_fixture, guillotina):
audit_utility = query_utility(IAuditUtility)
await audit_utility.create_index()
response, status = await guillotina(
"POST", "/db/", data=json.dumps({"@type": "Container", "id": "guillotina"})
)
assert status == 200
yield guillotina
audit_utility = query_utility(IAuditUtility)
await audit_utility.async_es.indices.delete(index="audit")
32 changes: 1 addition & 31 deletions guillotina_audit/tests/test_audit_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@


async def test_audit_basic(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
# Let's check the index has been created
audit_utility = query_utility(IAuditUtility)
resp = await audit_utility.async_es.indices.get_alias()
assert "audit" in resp
resp = await audit_utility.async_es.indices.get_mapping(index="audit")
Expand Down Expand Up @@ -146,11 +141,6 @@ async def test_audit_basic(guillotina_es):


async def test_audit_wildcard(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)

payload = AuditDocument(action="added", type_name="Fullscreen")
Expand Down Expand Up @@ -214,11 +204,6 @@ async def test_audit_wildcard(guillotina_es):


async def test_json_dumps(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
json.dumps(
{"datetime": datetime.now(), "date": date.today()},
Expand All @@ -227,11 +212,6 @@ async def test_json_dumps(guillotina_es):


async def test_permissions_modified_without_indexing(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200

response, status = await guillotina_es(
"POST",
"/db/guillotina/",
Expand Down Expand Up @@ -291,11 +271,6 @@ async def test_permissions_modified_without_indexing(guillotina_es):
}
)
async def test_permissions_modified_with_indexing(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200

response, status = await guillotina_es(
"POST",
"/db/guillotina/",
Expand Down Expand Up @@ -332,11 +307,6 @@ async def test_permissions_modified_with_indexing(guillotina_es):


async def test_metadata_field(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
payload = AuditDocument(
action="CreatingMetadata",
Expand Down
8 changes: 1 addition & 7 deletions guillotina_audit/tests/test_mappings.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
from guillotina.component import query_utility
from guillotina_audit.interfaces import IAuditUtility

import asyncio
import json
import pytest


pytestmark = pytest.mark.asyncio


async def test_mappings(guillotina_es):
response, status = await guillotina_es(
"POST", "/db/guillotina/@addons", data=json.dumps({"id": "audit"})
)
assert status == 200
await asyncio.sleep(2)
audit_utility = query_utility(IAuditUtility)
await audit_utility.update_settings()
await audit_utility.update_mappings()
49 changes: 45 additions & 4 deletions guillotina_audit/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ def _custom_serializer(self, obj):
return obj.strftime("%Y-%m-%d")
raise TypeError("Object of type %s is not JSON serializable" % type(obj))

async def update_settings(self):
await self.async_es.indices.close(index=self.index)
try:
await self.async_es.indices.put_settings(
body=self.default_settings(), index=self.index
)
logger.info(f"Updating mappings {self.default_settings()}")
except Exception:
logger.error("Error updating settings", exc_info=True)
finally:
await self.async_es.indices.open(index=self.index)

async def update_mappings(self):
await self.async_es.indices.put_mapping(
body=self.default_mappings(), index=self.index
Expand All @@ -59,15 +71,44 @@ async def create_index(self):
except BadRequestError:
logger.error("An exception occurred when creating index", exc_info=True)

def settings(self):
return {
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase"],
},
"my_stop_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": ["lowercase", "english_stop"],
},
},
"filter": {
"english_stop": {"type": "stop", "stopwords": "_english_"}
},
}
}
}

def default_settings(self):
return {
"analysis": {
"analyzer": {"path_analyzer": {"tokenizer": "path_tokenizer"}},
"analyzer": {
"path_analyzer": { # Custom analyzer definition
"type": "custom",
"tokenizer": "path_tokenizer",
}
},
"tokenizer": {
"path_tokenizer": {"type": "path_hierarchy", "delimiter": "/"}
"path_tokenizer": { # Custom tokenizer definition
"type": "path_hierarchy",
"delimiter": "/",
}
},
"filter": {},
"char_filter": {},
}
}

Expand Down

0 comments on commit 049764c

Please sign in to comment.