Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new app and added tests for the models.py and admin.py; updated… #2

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ services:
- .:/code
ports:
- "8000:8000"
env_file:
- .env
depends_on:
db:
condition: service_healthy
Empty file added tasks/__init__.py
Empty file.
13 changes: 13 additions & 0 deletions tasks/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from django.contrib import admin
from . import models


class TaskAdmin(admin.ModelAdmin):
model = models.Task
list_display = ('excerpt',)

def excerpt(self, obj):
return obj.get_excerpt(8)


admin.site.register(models.Task, TaskAdmin)
6 changes: 6 additions & 0 deletions tasks/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TasksConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'tasks'
Empty file added tasks/migrations/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions tasks/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.db import models


class Task(models.Model):
title = models.CharField(max_length=100)
is_completed = models.BooleanField(default=False)

def get_excerpt(self, total_char):
return self.title[:total_char]
Empty file added tasks/tests/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions tasks/tests/test_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .. import models
from .. import admin
from django.contrib.admin.sites import AdminSite
import pytest
from mixer.backend.django import mixer


@pytest.mark.django_db
class TestTaskAdmin:
def test_excerpt(self):
site = AdminSite()
task_admin = admin.TaskAdmin(models.Task, site)

obj = mixer.blend('tasks.Task', title='Buy milk from the market')
result = task_admin.excerpt(obj)
assert result == 'Buy milk', 'Should return first few characters'
14 changes: 14 additions & 0 deletions tasks/tests/test_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pytest
from mixer.backend.django import mixer


@pytest.mark.django_db
class TestTask:
def test_model(self):
obj = mixer.blend('tasks.Task')
assert obj.pk == 1, 'Should create a task instance'

def test_excerpt(self):
obj = mixer.blend('tasks.Task', title='Buy milk from the market')
result = obj.get_excerpt(8)
assert result == 'Buy milk', 'Should return first few characters'
3 changes: 3 additions & 0 deletions tasks/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.shortcuts import render

# Create your views here.
4 changes: 3 additions & 1 deletion todos_project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -20,7 +21,7 @@
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-2+6(-(v1ckia(cvp+v%_or%(c*m6@6_wg#a_m*i=v3(!2k-$00'
SECRET_KEY = os.environ.get("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand All @@ -37,6 +38,7 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'tasks.apps.TasksConfig',
]

MIDDLEWARE = [
Expand Down