Skip to content

Commit

Permalink
Merge pull request #17 from navyad/navyad/patch-2
Browse files Browse the repository at this point in the history
Attachment cannot be instantiated
  • Loading branch information
geeknam committed Jan 12, 2017
2 parents 9426e92 + 751aeef commit 56c8715
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
15 changes: 14 additions & 1 deletion messengerbot/attachments.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import abc

class Attachment(object):

class Attachment:

__metaclass__ = abc.ABCMeta

@abc.abstractmethod
def to_dict(self):
return {
'type': self.attachment_type,
Expand All @@ -21,6 +26,10 @@ def payload(self):
'url': self._url
}

def to_dict(self):
_super_obj = super(ImageAttachment, self)
return _super_obj.to_dict()


class TemplateAttachment(Attachment):

Expand All @@ -32,3 +41,7 @@ def __init__(self, template):
@property
def payload(self):
return self.template.to_dict()

def to_dict(self):
_super_obj = super(TemplateAttachment, self)
return _super_obj.to_dict()
41 changes: 41 additions & 0 deletions tests/test_attachments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from unittest import TestCase
from messengerbot import (templates, attachments, elements)


class AttachementTestCase(TestCase):

def test_attachment(self):
"""
Attachment cannot be instantiated
"""
message = "Can't instantiate abstract class Attachment with abstract methods to_dict"
with self.assertRaises(TypeError) as exp:
attachments.Attachment()
self.assertEqual(exp.exception.message, message)

def test_image_attachment(self):
"""
to_dict test for ImageAttachment
"""
url = "http://www.webreality.co.uk/media/1079/i_love_open_source.jpg"
image_attachement = attachments.ImageAttachment(url)
_dic = image_attachement.to_dict()
self.assertEqual(_dic['payload']['url'], url)
self.assertEqual(_dic['type'], image_attachement.attachment_type)

def test_template_attachment(self):
"""
TemplateAttachment
"""
template_text = 'What do you want to do next?',
postback_button = elements.PostbackButton(
title='Start chatting', payload='USER_DEFINED_PAYLOAD')
template = templates.ButtonTemplate(
text=template_text, buttons=[postback_button])

template_attachment = attachments.TemplateAttachment(template=template)
_dic = template_attachment.to_dict()
payload = _dic['payload']
self.assertEqual(_dic['type'], template_attachment.attachment_type)
self.assertEqual(payload['text'], template_text)
self.assertEqual(payload['template_type'], 'button')

0 comments on commit 56c8715

Please sign in to comment.