From e1a3aca7198d6c73b513019a5be255470584bb47 Mon Sep 17 00:00:00 2001 From: Marnanel Thurman Date: Sun, 19 Aug 2018 21:47:54 +0100 Subject: [PATCH] intermediate --- django_kepi/models.py | 22 +++- tests/tests_unit.py | 108 ++++++++++++++++-- .../migrations/0002_thingarticle.py | 20 ++++ things_for_testing/models.py | 20 ++++ 4 files changed, 160 insertions(+), 10 deletions(-) create mode 100644 things_for_testing/migrations/0002_thingarticle.py diff --git a/django_kepi/models.py b/django_kepi/models.py index e96b0da..01578be 100644 --- a/django_kepi/models.py +++ b/django_kepi/models.py @@ -3,6 +3,7 @@ from django_kepi import object_type_registry from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.conf import settings +import json import datetime import warnings @@ -12,7 +13,7 @@ import warnings RESOLVE_FAILSAFE = 10 SERIALIZE = 'serialize' URL_IDENTIFIER = 'url_identifier' - + class Cobject(models.Model): class Meta: @@ -40,8 +41,6 @@ class Cobject(models.Model): result = { 'id': self.url_identifier(), 'type': self.__class__.__name__, - 'published': self.published, # XXX format - 'updated': self.updated, # XXX format } for (field, field_name) in [ @@ -88,6 +87,23 @@ class Cobject(models.Model): return result + def serialize_as_str(self): + + def json_default(obj): + + if isinstance(obj, datetime.datetime): + return obj.isoformat()+'Z' + else: + raise TypeError("{} is not serializable".format( + type(obj))) + + return json.dumps( + self.serialize(), + sort_keys=True, + indent=2, # no reason not to be pretty + default=json_default, + ) + class Activity_with_actor_and_fobject(Cobject): class Meta: diff --git a/tests/tests_unit.py b/tests/tests_unit.py index 4ccfafe..f810012 100644 --- a/tests/tests_unit.py +++ b/tests/tests_unit.py @@ -1,21 +1,115 @@ from django.test import TestCase, Client -from django_kepi.models import Create -from things_for_testing.models import ThingUser -import json +from django_kepi.models import Create, Like +from things_for_testing.models import ThingUser, ThingArticle class UserTests(TestCase): - def test_basic_objects(self): + def test_create(self): actor = ThingUser( - name='Fred', + name='Dijkstra', ) actor.save() + article = ThingArticle( + title='Go To statement considered harmful', + ) + article.save() + activity = Create( actor=actor, - fobject=actor, + fobject=article, ) activity.save() - raise ValueError(str(activity.serialize())) + #raise ValueError(str(activity.serialize())) + + def test_update(self): + + actor = ThingUser( + name='Dijkstra', + ) + actor.save() + + article = ThingArticle( + title='Go To statement considered harmful', + ) + article.save() + + activity = Create( + actor=actor, + fobject=article, + ) + activity.save() + + article2 = ThingArticle( + title='Actually I rather like spaghetti code', + ) + article2.save() + + activity = Update( + actor=actor, + fobject=article2, + ) + activity.save() + + #raise ValueError(str(activity.serialize())) + + def test_delete(self): + + actor = ThingUser( + name='Dijkstra', + ) + actor.save() + + article = ThingArticle( + title='Go To statement considered harmful', + ) + article.save() + + activity = Create( + actor=actor, + fobject=article, + ) + activity.save() + + delete = Update( + actor=actor, + fobject=article, + ) + delete.save() + + # fetch by object ID (we can't do this atm) will get Tombstone + + #raise ValueError(str(activity.serialize())) + + def test_like(self): + + liker = ThingUser( + name='Uncle Bulgaria', + ) + liker.save() + + author = ThingUser( + name='Dijkstra', + ) + author.save() + + article = ThingArticle( + title='Go To statement considered harmful', + ) + article.save() + + create = Create( + actor=author, + fobject=article, + ) + create.save() + + like = Like( + actor=liker, + fobject=article, + ) + like.save() + + raise ValueError(like.serialize_as_str()) diff --git a/things_for_testing/migrations/0002_thingarticle.py b/things_for_testing/migrations/0002_thingarticle.py new file mode 100644 index 0000000..1c80183 --- /dev/null +++ b/things_for_testing/migrations/0002_thingarticle.py @@ -0,0 +1,20 @@ +# Generated by Django 2.0.6 on 2018-08-19 15:09 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('things_for_testing', '0001_initial'), + ] + + operations = [ + migrations.CreateModel( + name='ThingArticle', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=256)), + ], + ), + ] diff --git a/things_for_testing/models.py b/things_for_testing/models.py index f4485b4..5d522ea 100644 --- a/things_for_testing/models.py +++ b/things_for_testing/models.py @@ -11,6 +11,8 @@ class ThingUser(models.Model): def serialize(self): return { + 'id': self.url_identifier(), + 'type': 'Person', 'name': self.name, } @@ -18,3 +20,21 @@ class ThingUser(models.Model): return 'https://example.com/user/{}'.format( self.name, ) + +class ThingArticle(models.Model): + + title = models.CharField(max_length=256) + + def serialize(self): + return { + 'id': self.url_identifier(), + 'type': 'Article', + 'title': self.title, + } + + def url_identifier(self): + return 'https://articles.example.com/{}'.format( + self.title, + ) + +