Write some test for the Posts model
This commit is contained in:
parent
0945dc5b6b
commit
da7075a904
5 changed files with 58 additions and 1 deletions
32
flangr/posts/migrations/0001_initial.py
Normal file
32
flangr/posts/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
# Generated by Django 4.0.5 on 2022-07-03 21:35
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = []
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="Post",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.BigAutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("img", models.ImageField(upload_to="posts/%Y/%m")),
|
||||||
|
("posted", models.DateTimeField(auto_now_add=True)),
|
||||||
|
("public", models.BooleanField(default=False)),
|
||||||
|
("title", models.CharField(max_length=255)),
|
||||||
|
("body", models.TextField()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -5,7 +5,7 @@ from django.db import models
|
||||||
|
|
||||||
class Post(models.Model):
|
class Post(models.Model):
|
||||||
img = models.ImageField(upload_to="posts/%Y/%m")
|
img = models.ImageField(upload_to="posts/%Y/%m")
|
||||||
posted = models.DateTiemField(auto_now_add=True)
|
posted = models.DateTimeField(auto_now_add=True)
|
||||||
public = models.BooleanField(default=False)
|
public = models.BooleanField(default=False)
|
||||||
|
|
||||||
title = models.CharField(max_length=255)
|
title = models.CharField(max_length=255)
|
||||||
|
|
|
||||||
BIN
flangr/posts/test_data/test_img.png
Normal file
BIN
flangr/posts/test_data/test_img.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 320 B |
|
|
@ -1,3 +1,26 @@
|
||||||
|
import os
|
||||||
|
|
||||||
from django.test import TestCase
|
from django.test import TestCase
|
||||||
|
from django.core.files import File
|
||||||
|
|
||||||
|
from .models import Post
|
||||||
|
|
||||||
# Create your tests here.
|
# Create your tests here.
|
||||||
|
|
||||||
|
|
||||||
|
class TestPostModelTests(TestCase):
|
||||||
|
def test_can_create_model(self):
|
||||||
|
with open(
|
||||||
|
os.path.join(os.path.dirname(__file__), "test_data", "test_img.png"),
|
||||||
|
mode="rb",
|
||||||
|
) as f:
|
||||||
|
try:
|
||||||
|
p = Post.objects.create(
|
||||||
|
img=File(f, "somefile.png"),
|
||||||
|
title="Foobar",
|
||||||
|
body="Some file",
|
||||||
|
)
|
||||||
|
|
||||||
|
self.assertIn("Foobar", str(p))
|
||||||
|
finally:
|
||||||
|
p.img.delete()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
pillow
|
||||||
|
django
|
||||||
Loading…
Add table
Reference in a new issue