Skip to content

Commit

Permalink
Modified black linter to use master branch rather than main
Browse files Browse the repository at this point in the history
Ran black formatter

Added tests github action

Updated poetry lock

Add django as dependency of tests

Added venv

Changed to use poetry to run tests

Changed to use poetry to run tests

Changed to use poetry to run tests

Added typing to dependencies
  • Loading branch information
TreyWW committed Mar 22, 2024
1 parent c33da51 commit 5e33612
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 390 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_black_linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: Black Linter (PY)
on:
push:
branches:
- main
- master
pull_request:

jobs:
Expand Down
56 changes: 56 additions & 0 deletions .github/workflows/run_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Backend Tests
on:
push:
branches:
- master
pull_request:

jobs:
build:
strategy:
fail-fast: false
matrix:
os: [ ubuntu-latest ]
python-version: [ "3.10", "3.11", "3.12" ]
django-version: [ 4, 5 ]

runs-on: ${{ matrix.os }}

steps:
#----------------------------------------------
# check-out repo and set-up python
#----------------------------------------------
- name: Checkout code
uses: actions/checkout@v4

- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: snok/install-poetry@v1
with:
virtualenvs-create: true
virtualenvs-in-project: true
#----------------------------------------------
# load cached venv if cache exists
#----------------------------------------------
- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}
#----------------------------------------------
# install dependencies if cache does not exist
#----------------------------------------------
- name: Install dependencies
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: |
poetry install --no-interaction --no-root
poetry add django@${{ matrix.django-version }} --no-interaction
- name: Run Tests
run: |
poetry run python -m manage test
2 changes: 1 addition & 1 deletion django_umami/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.1"
__version__ = "0.0.1"
1 change: 1 addition & 0 deletions django_umami/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import requests


@dataclass
class UmamiResponse:
success: bool
Expand Down
2 changes: 2 additions & 0 deletions django_umami/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ def decorator(func):
def wrapper(*args, **kwargs):
umami.track(event_name, event_data)
return func(*args, **kwargs)

return wrapper

return decorator
2 changes: 1 addition & 1 deletion django_umami/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,4 @@


UMAMI_PAGE_URL = "https://test.strelix.org"
UMAMI_WEBSITE_ID = "xxxxxxxxxxxxxxxx"
UMAMI_WEBSITE_ID = "xxxxxxxxxxxxxxxx"
30 changes: 13 additions & 17 deletions django_umami/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class UmamiTestCase(TestCase):
@classmethod
def setUpClass(cls):
# Mock settings attributes
settings.UMAMI_PAGE_URL = 'https://example.com'
settings.UMAMI_WEBSITE_ID = '123456'
settings.UMAMI_PAGE_URL = "https://example.com"
settings.UMAMI_WEBSITE_ID = "123456"

@classmethod
def tearDownClass(cls):
Expand All @@ -23,48 +23,44 @@ def setUp(self):
self.umami = Umami(options=self.umami_config)

def test_umami_config(self):
self.assertEqual(self.umami_config.host_url, 'https://example.com')
self.assertEqual(self.umami_config.website_id, '123456')
self.assertEqual(self.umami_config.host_url, "https://example.com")
self.assertEqual(self.umami_config.website_id, "123456")

def test_umami_send(self):
with patch('requests.post') as mocked_post:
with patch("requests.post") as mocked_post:
mocked_post.return_value.status_code = 200

payload = UmamiPayload(website='123456', data={'name': 'test_event'})
payload = UmamiPayload(website="123456", data={"name": "test_event"})
response = self.umami.send(payload)

mocked_post.assert_called_once()
self.assertEqual(response.status_code, 200)

def test_umami_track_string_event(self):
with patch.object(self.umami, 'send') as mocked_send:
with patch.object(self.umami, "send") as mocked_send:
mocked_send.return_value.status_code = 200

self.umami.track("test_event")

mocked_send.assert_called_once_with(
payload=UmamiPayload(website='123456', data={'name': 'test_event'})
)
mocked_send.assert_called_once_with(payload=UmamiPayload(website="123456", data={"name": "test_event"}))

def test_umami_track_dict_event(self):
with patch.object(self.umami, 'send') as mocked_send:
with patch.object(self.umami, "send") as mocked_send:
mocked_send.return_value.status_code = 200

event_data = UmamiEventData(name='test_event', url='/test-url')
event_data = UmamiEventData(name="test_event", url="/test-url")
self.umami.track(event_data)

mocked_send.assert_called_once_with(
payload=UmamiPayload(website='123456', data=event_data)
)
mocked_send.assert_called_once_with(payload=UmamiPayload(website="123456", data=event_data))

def test_umami_track_invalid_event(self):
with patch.object(self.umami, 'send') as mocked_send:
with patch.object(self.umami, "send") as mocked_send:
mocked_send.return_value.status_code = 200

self.umami.track(123)

mocked_send.assert_not_called()


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion django_umami/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@


def get_setting(name: str, default=None):
return getattr(settings, name, default)
return getattr(settings, name, default)
20 changes: 8 additions & 12 deletions examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,22 @@


@django_umami.decorators.track("someone went to django view!")
def django_view(request):
...
def django_view(request): ...


def my_function():
django_umami.core.umami.track("someone went to my function!")

data = django_umami.core.UmamiEventData(
hostname="example.com",
language = "en-GB",
referrer = "",
screen = "1920x1080",
title = "abc",
url = "/my_event",
name = "My Custom Event"
language="en-GB",
referrer="",
screen="1920x1080",
title="abc",
url="/my_event",
name="My Custom Event",
)

django_umami.core.umami.track(data)

django_umami.core.umami.track({
"name": "My Custom Event",
"url": "/my_page/123"
})
django_umami.core.umami.track({"name": "My Custom Event", "url": "/my_page/123"})
Loading

0 comments on commit 5e33612

Please sign in to comment.