def test_behaviour(self): p = models.Publication(id=None, title='The Python Journal') a = models.Article(id=None, headline='Django lets you build Web apps easily') # Associate the Article with a Publication. self.assertRaises(ValueError, lambda a, p: a.publications.add(p), a, p) p.save() a.save() article_id = a.id self.addCleanup(p.delete) self.addCleanup(a.delete) article_alias = adapter.DjangoClassAlias(models.Article, None) x = models.Article() article_alias.applyAttributes(x, { 'headline': 'Foo bar!', 'id': article_id, 'publications': [p] }) self.assertEqual(x.headline, 'Foo bar!') self.assertEqual(x.id, article_id) self.assertEqual(list(x.publications.all()), [p])
def test_many_to_many(self): # install some test data - taken from # http://www.djangoproject.com/documentation/models/many_to_many/ p1 = models.Publication(id=None, title='The Python Journal') p1.save() p2 = models.Publication(id=None, title='Science News') p2.save() p3 = models.Publication(id=None, title='Science Weekly') p3.save() self.addCleanup(p1.delete) self.addCleanup(p2.delete) self.addCleanup(p3.delete) # Create an Article. a1 = models.Article(id=None, headline='Django lets you build Web apps easily') a1.save() self.addCleanup(a1.delete) self.assertEqual(a1.id, 1) # Associate the Article with a Publication. a1.publications.add(p1) pub_alias = adapter.DjangoClassAlias(models.Publication, None) art_alias = adapter.DjangoClassAlias(models.Article, None) test_publication = models.Publication.objects.filter(pk=1)[0] test_article = models.Article.objects.filter(pk=1)[0] attrs = pub_alias.getEncodableAttributes(test_publication) self.assertEqual(attrs, {'id': 1, 'title': 'The Python Journal'}) attrs = art_alias.getEncodableAttributes(test_article) self.assertEqual( attrs, { 'headline': 'Django lets you build Web apps easily', 'id': 1, 'publications': [p1] }) x = models.Article() art_alias.applyAttributes(x, { 'headline': 'Test', 'id': 1, 'publications': [p1] }) self.assertEqual(x.headline, 'Test') self.assertEqual(x.id, 1) self.assertEqual(list(x.publications.all()), [p1]) y = models.Article() attrs = art_alias.getDecodableAttributes( y, { 'headline': 'Django lets you build Web apps easily', 'id': 0, 'publications': [] }) self.assertEqual(attrs, {'headline': 'Django lets you build Web apps easily'})
def test_one_to_many(self): # initialise the db .. r = models.Reporter(first_name='John', last_name='Smith', email='*****@*****.**') r.save() self.addCleanup(r.delete) r2 = models.Reporter(first_name='Paul', last_name='Jones', email='*****@*****.**') r2.save() self.addCleanup(r2.delete) a = models.Article(headline="This is a test", reporter=r) a.save() self.addCleanup(a.delete) article_id = a.id del a a = models.Article.objects.filter(pk=article_id)[0] self.assertFalse('_reporter_cache' in a.__dict__) a.reporter self.assertTrue('_reporter_cache' in a.__dict__) del a a = models.Article.objects.filter(pk=article_id)[0] alias = adapter.DjangoClassAlias(models.Article, defer=True) self.assertFalse(hasattr(alias, 'fields')) attrs = alias.getEncodableAttributes(a) # note that the reporter attribute does not exist. self.assertEqual(attrs, { 'headline': 'This is a test', 'id': article_id, 'publications': [] }) self.assertFalse('_reporter_cache' in a.__dict__) self.assertEqual( pyamf.encode(a, encoding=pyamf.AMF3).getvalue(), '\n\x0b\x01\x11headline\x06\x1dThis is a test\x05id\x04%s' '\x19publications\t\x01\x01\x01' % (chr(article_id), )) del a # now with select_related to pull in the reporter object a = (models.Article.objects.select_related('reporter').filter( pk=article_id)[0]) alias = adapter.DjangoClassAlias(models.Article, defer=True) self.assertFalse(hasattr(alias, 'fields')) self.assertEqual( alias.getEncodableAttributes(a), { 'headline': 'This is a test', 'id': article_id, 'reporter': r, 'publications': [] }) self.assertTrue('_reporter_cache' in a.__dict__) self.assertEqual( pyamf.encode(a, encoding=pyamf.AMF3).getvalue(), '\n\x0b\x01\x11reporter\n\x0b\x01\x15first_name\x06\tJohn\x13' 'last_name\x06\x0bSmith\x05id\x04\x01\x0bemail\x06!john' '@example.com\x01\x11headline\x06\x1dThis is a test\x19' 'publications\t\x01\x01\n\x04%s\x01' % (chr(article_id), ))