-
-
Notifications
You must be signed in to change notification settings - Fork 5
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
21 changed files
with
450 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
name: Ruff Linter | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
tags-ignore: | ||
- '**' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Install Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements-dev.txt | ||
pip install ruff | ||
- name: Run Ruff | ||
run: ruff check --output-format=github . |
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,37 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: | ||
push: | ||
branches: | ||
- main | ||
tags-ignore: | ||
- '**' | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Install Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
- name: Install dependencies | ||
run: | | ||
python -m pip install --upgrade pip | ||
pip install -r requirements-dev.txt | ||
pip install codecov | ||
- name: Run Tests with coverage | ||
env: | ||
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} | ||
run: | | ||
pytest -s --cov=src/dalf --cov-report=xml tests/testproject | ||
codecov -f coverage.xml | ||
- name: Upload coverage to Codecov | ||
uses: codecov/codecov-action@v4.0.1 | ||
with: | ||
token: ${{ secrets.CODECOV_TOKEN }} | ||
files: ./coverage.xml | ||
flags: unittests |
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,5 @@ | ||
Django==5.0.6 | ||
pytest==8.2.1 | ||
pytest-cov==5.0.0 | ||
pytest-django==4.8.0 | ||
pytest-factoryboy==2.7.0 |
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,23 @@ | ||
#!/usr/bin/env python | ||
# ruff: noqa: TRY003,EM101 | ||
|
||
import os | ||
import sys | ||
|
||
|
||
def main(): | ||
"""Run administrative tasks.""" | ||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testproject.settings') | ||
try: | ||
from django.core.management import execute_from_command_line | ||
except ImportError as exc: | ||
raise ImportError( | ||
"Couldn't import Django. Are you sure it's installed and " | ||
'available on your PYTHONPATH environment variable? Did you ' | ||
'forget to activate a virtual environment?' | ||
) from exc | ||
execute_from_command_line(sys.argv) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,5 @@ | ||
[pytest] | ||
DJANGO_SETTINGS_MODULE = testproject.settings | ||
python_files = tests.py test_*.py *_tests.py | ||
pythonpath = ../../src | ||
addopts = -p no:warnings --strict-markers --no-migrations --reuse-db --capture=no |
Empty file.
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 dalf.admin import ( | ||
DALFChoicesField, | ||
DALFModelAdmin, | ||
DALFRelatedField, | ||
DALFRelatedFieldAjax, | ||
DALFRelatedOnlyField, | ||
) | ||
from django.contrib import admin | ||
|
||
from .models import Category, Post, Tag | ||
|
||
|
||
@admin.register(Post) | ||
class PostAdmin(DALFModelAdmin): | ||
list_display = ('title',) | ||
list_filter = ( | ||
('author', DALFRelatedField), | ||
('audience', DALFChoicesField), | ||
('category', DALFRelatedFieldAjax), | ||
('tags', DALFRelatedOnlyField), | ||
) | ||
|
||
|
||
@admin.register(Category) | ||
class CategoryAdmin(admin.ModelAdmin): | ||
search_fields = ('name',) | ||
ordering = ('name',) | ||
|
||
|
||
@admin.register(Tag) | ||
class TagAdmin(admin.ModelAdmin): | ||
search_fields = ('name',) | ||
ordering = ('name',) |
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,6 @@ | ||
from django.apps import AppConfig | ||
|
||
|
||
class TestappConfig(AppConfig): | ||
default_auto_field = 'django.db.models.BigAutoField' | ||
name = 'testapp' |
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,12 @@ | ||
import pytest | ||
from pytest_factoryboy import register | ||
|
||
from .factories import PostFactory, TagFactory | ||
|
||
register(TagFactory) | ||
register(PostFactory) | ||
|
||
|
||
@pytest.fixture() | ||
def posts(): | ||
return PostFactory.create_batch(10) |
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,77 @@ | ||
import factory | ||
from django.contrib.auth import get_user_model | ||
from factory import fuzzy | ||
|
||
from .models import AudienceChoices, Category, Post, Tag | ||
|
||
FAKE_USERNAMES = [ | ||
'vigo', | ||
'turbo', | ||
'move', | ||
] | ||
|
||
FAKE_CATEGORIES = [ | ||
'Python', | ||
'Ruby', | ||
'Go', | ||
'Bash', | ||
'AppleScript', | ||
'C', | ||
'Perl', | ||
] | ||
|
||
FAKE_TAGS = [ | ||
'django', | ||
'django rest', | ||
'linux', | ||
'macos', | ||
'stdlib', | ||
] | ||
|
||
|
||
class UserFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = get_user_model() | ||
django_get_or_create = ('username',) | ||
|
||
username = fuzzy.FuzzyChoice(FAKE_USERNAMES) | ||
email = factory.Faker('email') | ||
password = factory.PostGenerationMethodCall('set_password', 'defaultpassword') | ||
|
||
|
||
class CategoryFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = Category | ||
django_get_or_create = ('name',) | ||
|
||
name = factory.Iterator(FAKE_CATEGORIES) | ||
|
||
|
||
class TagFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = Tag | ||
django_get_or_create = ('name',) | ||
|
||
name = fuzzy.FuzzyChoice(FAKE_TAGS) | ||
|
||
|
||
class PostFactory(factory.django.DjangoModelFactory): | ||
class Meta: | ||
model = Post | ||
django_get_or_create = ('title',) | ||
|
||
author = factory.SubFactory(UserFactory) | ||
category = factory.SubFactory(CategoryFactory) | ||
title = factory.Sequence(lambda n: f'Book about {FAKE_CATEGORIES[n % len(FAKE_CATEGORIES)]} - {n}') | ||
audience = fuzzy.FuzzyChoice(AudienceChoices.choices, getter=lambda c: c[0]) | ||
|
||
@factory.post_generation | ||
def tags(self, create, extracted, **kwargs): # noqa: ARG002 | ||
if not create: | ||
return | ||
|
||
if extracted: | ||
self.tags.add(*extracted) | ||
else: | ||
tags = TagFactory.create_batch(len(FAKE_TAGS)) | ||
self.tags.add(*tags) |
Empty file.
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,46 @@ | ||
from django.conf import settings | ||
from django.db import models | ||
|
||
|
||
class Category(models.Model): | ||
name = models.CharField(max_length=255) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class Tag(models.Model): | ||
name = models.CharField(max_length=255) | ||
|
||
def __str__(self): | ||
return self.name | ||
|
||
|
||
class AudienceChoices(models.TextChoices): | ||
BEGINNER = 'beginner', 'Beginer' | ||
INTERMEDIATE = 'intermediate', 'Intermediate' | ||
PRO = 'pro', 'Pro' | ||
|
||
|
||
class Post(models.Model): | ||
author = models.ForeignKey( | ||
to=settings.AUTH_USER_MODEL, | ||
on_delete=models.CASCADE, | ||
related_name='posts', | ||
) | ||
category = models.ForeignKey( | ||
to='Category', | ||
on_delete=models.CASCADE, | ||
related_name='posts', | ||
) | ||
tags = models.ManyToManyField(to='Tag', blank=True) | ||
audience = models.CharField( | ||
max_length=100, | ||
choices=AudienceChoices.choices, | ||
default=AudienceChoices.BEGINNER, | ||
) | ||
|
||
title = models.CharField(max_length=255) | ||
|
||
def __str__(self): | ||
return self.title |
Oops, something went wrong.