Skip to content

Commit

Permalink
Testes com banco de Dados + Django Admin
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuspdf committed Sep 23, 2023
1 parent a605107 commit 46b2dc3
Show file tree
Hide file tree
Showing 12 changed files with 317 additions and 244 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,4 @@ cython_debug/

# postgres files for local development

.pgdata
.pgdata/
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ sentry-sdk = "*"
flake8 = "*"
pytest-cov = "*"
codecov = "*"
model-mommy = "*"

[requires]
python_version = "3.10"
378 changes: 204 additions & 174 deletions Pipfile.lock

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions pypro/aperitivos/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django.contrib.admin import ModelAdmin, register

from pypro.aperitivos.models import Video


@register(Video)
class VideoAdmin(ModelAdmin):
list_display = ('titulo', 'slug', 'vimeo_id')
ordering = ('creation',)
prepopulated_fields = {'slug': ('titulo',)}
24 changes: 24 additions & 0 deletions pypro/aperitivos/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 4.2.5 on 2023-09-23 17:18

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Video',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.CharField(max_length=32)),
('titulo', models.CharField(max_length=32)),
('vimeo_id', models.CharField(max_length=32)),
('creation', models.DateTimeField(auto_now_add=True)),
],
),
]
6 changes: 5 additions & 1 deletion pypro/aperitivos/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@


class Video(models.Model):
slug = models.CharField(max_length=32)
titulo = models.CharField(max_length=32)
slug = models.SlugField(max_length=32)
vimeo_id = models.CharField(max_length=32)
creation = models.DateTimeField(auto_now_add=True)

def get_absolute_url(self):
return reverse('aperitivos:video', args=(self.slug,))

def __str__(self):
return f'Vídeo: {self.titulo}'
2 changes: 1 addition & 1 deletion pypro/aperitivos/templates/aperitivos/video.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="row">
<div class="col-md-12">
<h1 class="mt-4 mb-3">{{ video.titulo | default:"Título não encontrado" }}</h1>
<iframe src="https://player.vimeo.com/video/{{ video.vimeo_id }}?badge=0&amp;autopause=0&amp;player_id=0&amp;app_id=58479"
<iframe src="https://player.vimeo.com/video/{{ video.vimeo_id }}"
width="640" height="480" frameborder="0" allow="autoplay; fullscreen; picture-in-picture"
title="SnapInsta.io-2 Luciano Ramalho OO em Python sem sotaque-(480p)">
</iframe>
Expand Down
31 changes: 31 additions & 0 deletions pypro/aperitivos/tests/test_indice.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import pytest
from django.urls import reverse
from model_mommy import mommy

from pypro.aperitivos.models import Video
from pypro.django_assertions import assert_contains


@pytest.fixture
def videos(db):
return mommy.make(Video, 3)


@pytest.fixture
def resp(client, videos):
return client.get(reverse('aperitivos:indice'))


def test_status_code(resp):
assert resp.status_code == 200


def test_titulo_video(resp, videos):
for video in videos:
assert_contains(resp, video.titulo)


def teste_link_video(resp, videos):
for video in videos:
video_link = reverse('aperitivos:video', args=(video.slug,))
assert_contains(resp, f'href="{video_link}"')
37 changes: 37 additions & 0 deletions pypro/aperitivos/tests/test_video.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pytest
from django.urls import reverse
from model_mommy import mommy

from pypro.aperitivos.models import Video
from pypro.django_assertions import assert_contains


@pytest.fixture
def video(db):
return mommy.make(Video)


@pytest.fixture
def resp(client, video):
return client.get(reverse('aperitivos:video', args=(video.slug,)))


@pytest.fixture
def resp_video_nao_encontrado(client, video):
return client.get(reverse('aperitivos:video', args=(video.slug+'video_nao_existente',)))


def test_status_code_video_nao_encontrado(resp_video_nao_encontrado):
assert resp_video_nao_encontrado.status_code == 404


def test_status_code(resp):
assert resp.status_code == 200


def test_titulo_video(resp, video):
assert_contains(resp, video.titulo)


def test_conteudo_video(resp, video):
assert_contains(resp, f'<iframe src="https://player.vimeo.com/video/{video.vimeo_id}"')
36 changes: 0 additions & 36 deletions pypro/aperitivos/tests/tests_indice.py

This file was deleted.

21 changes: 0 additions & 21 deletions pypro/aperitivos/tests/tests_video.py

This file was deleted.

13 changes: 3 additions & 10 deletions pypro/aperitivos/views.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
from pypro.aperitivos.models import Video
from django.shortcuts import render

videos = [
Video(slug='oo', titulo='Vídeo Aperitivo: OO em Python sem sotaque', vimeo_id='856570782'),
Video(slug='iot', titulo='IOT com Luciano Ramalho', vimeo_id='861071461'),
]


videos_dct = {v.slug: v for v in videos}
from django.shortcuts import render, get_object_or_404


def indice(request):
videos = Video.objects.order_by('creation').all()
return render(request, 'aperitivos/indice.html', context={'videos': videos})


def video(request, slug):
video = Video.objects.get(slug=slug)
video = get_object_or_404(Video, slug=slug)
return render(request, 'aperitivos/video.html', context={'video': video})

0 comments on commit 46b2dc3

Please sign in to comment.