from django.test import TestCase from przyrost.models import * from django.utils import timezone from django.core import serializers 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() 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 = """Why would I use XML???2012-12-01T14:02:01.5897101to jest drugi post2019-12-08T13:52:18.4730001""" 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()) def test_getPostByDate(self): 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)