2019-11-25 12:18:15 +01:00
|
|
|
from django.test import TestCase
|
|
|
|
from przyrost.models import *
|
|
|
|
from django.utils import timezone
|
2020-01-12 17:55:22 +01:00
|
|
|
from django.core import serializers
|
|
|
|
|
2019-11-25 12:18:15 +01:00
|
|
|
class testbasic(TestCase):
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
User.objects.all().delete()
|
|
|
|
Category.objects.all().delete()
|
|
|
|
Post.objects.all().delete()
|
|
|
|
u=User.objects.create(name="Admin")
|
|
|
|
c=Category.objects.create(name="test")
|
|
|
|
u.save()
|
|
|
|
c.save()
|
|
|
|
p=Post.objects.create(
|
|
|
|
created_at=timezone.datetime.now(),
|
|
|
|
category=c,
|
|
|
|
text="I nawet kiedy będę sam nie zmienię się to nie móóóój świattttt"
|
|
|
|
)
|
|
|
|
p.authors.set([u])
|
|
|
|
p.save()
|
2020-01-12 17:55:22 +01:00
|
|
|
|
|
|
|
d = """[{"model": "przyrost.post","fields": {
|
|
|
|
"text": "to jest drugi post",
|
|
|
|
"category": 1, "authors": [1]}}]"""
|
|
|
|
obj = serializers.deserialize("json", d)
|
|
|
|
for l in obj:
|
|
|
|
if not l.object.created_at:
|
|
|
|
l.object.created_at = datetime.datetime.now()
|
|
|
|
l.save()
|
|
|
|
|
|
|
|
xml = """<?xml version="1.0" encoding="utf-8"?><django-objects version="1.0"><object model="przyrost.post" ><field name="text" type="TextField">Why would I use XML???</field><field name="created_at" type="DateTimeField">2012-12-01T14:02:01.589710</field><field name="category" rel="ManyToOneRel" to="przyrost.category">1</field><field name="authors" rel="ManyToManyRel" to="przyrost.user"><object pk="1"></object></field></object><object model="przyrost.post" pk="2"><field name="text" type="TextField">to jest drugi post</field><field name="created_at" type="DateTimeField">2019-12-08T13:52:18.473000</field><field name="category" rel="ManyToOneRel" to="przyrost.category">1</field><field name="authors" rel="ManyToManyRel" to="przyrost.user"><object pk="1"></object></field></object></django-objects>"""
|
|
|
|
obj = serializers.deserialize("xml", xml)
|
|
|
|
for l in obj:
|
|
|
|
if not l.object.created_at:
|
|
|
|
l.object.created_at = datetime.datetime.now()
|
|
|
|
l.save()
|
|
|
|
|
|
|
|
|
|
|
|
print("final values:")
|
|
|
|
print(Post.objects.all().values())
|
2019-11-25 12:18:15 +01:00
|
|
|
def test_getPostByDate(self):
|
2020-01-12 17:55:22 +01:00
|
|
|
|
|
|
|
print(list(Post.objects.all().values()))
|
|
|
|
self.assertEqual(len(Post.objects.all()),3)
|
|
|
|
self.assertEqual(len(Post.getTodays()), 2)
|
|
|
|
self.assertEqual(len(Post.getYesterday()), 0)
|
|
|
|
def test__serializeXML(self):
|
|
|
|
print("XML: ")
|
|
|
|
data = serializers.serialize("xml", Post.objects.all())
|
|
|
|
print(data)
|
|
|
|
def test__serializeJSON(self):
|
|
|
|
print("JSON: ")
|
|
|
|
data = serializers.serialize("json", Post.objects.all())
|
|
|
|
print(data)
|
|
|
|
|
|
|
|
def test__deserializeJSON(self):
|
|
|
|
d= """[{"model": "przyrost.post","fields": {
|
|
|
|
"text": "to jest drugi post",
|
|
|
|
"created_at": "2019-12-08T13:52:18.473", "category": 1, "authors": [1]}}]"""
|
|
|
|
obj = serializers.deserialize("json",d)
|
|
|
|
#for l in obj:
|
|
|
|
# l.save()
|
|
|
|
# from time import sleep
|
|
|
|
#sleep(1)
|
|
|
|
print("new values")
|
|
|
|
print(Post.objects.all().values())
|
|
|
|
def test__getPostsOfAuthor(self):
|
|
|
|
d = Post.getPostsOfAuthor(User.objects.filter(name="Admin")[0])
|
|
|
|
self.assertEqual(len(d), 3)
|