def test_by_date(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() # create entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() now = datetime.now() date = now.date() # result should only contain the entry for the date result = EntryItem.permitted.by_date(date) self.failUnlessEqual(result.count(), 1)
def test_comment_count(self): comment_model = comments.get_model() # Return 0 if no comments exist. obj = ModelBase() obj.save() self.failUnless(obj.comment_count == 0) # Return the number of comments if comments exist. Here it should be 1 since we've created 1 comment. comment_obj = comment_model(content_object=obj, site_id=1) comment_obj.save() self.failUnless(obj.comment_count == 1) # Return 0 if no comments exist. dummy_obj = DummyModel() dummy_obj.save() self.failUnless(dummy_obj.comment_count == 0) # Return the number of comments if comments exist on the ModelBase object. # Here it should be 1 since we've created 1 comment on the ModelBase object. comment_obj = comment_model(content_object=dummy_obj.modelbase_obj, site_id=1) comment_obj.save() self.failUnless(dummy_obj.modelbase_obj.comment_count == 1) # If a comment was made on the ModelBase object it should still count for leaf class objects. self.failUnless(dummy_obj.comment_count == 1) # Add another comment on dummy object and make sure the count is 2 for both the dummy object and its modelbase object. comment_obj = comment_model(content_object=dummy_obj, site_id=1) comment_obj.save() self.failUnless(dummy_obj.comment_count == 2) self.failUnless(dummy_obj.modelbase_obj.comment_count == 2) # There should now only be 3 comment objects. self.failUnless(comment_model.objects.all().count() == 3)
def test_now(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() # create entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() # should return currently active entry items, ordered by start queryset = EntryItem.permitted.now() self.failUnless(queryset.count()) for entry_item in queryset: self.failUnless(entry_item.start < datetime.now()) self.failUnless(entry_item.end > datetime.now())
def setUp(self): content = ModelBase() content.save() self.content = content calendar = models.Calendar() calendar.save() self.calendar = calendar
def test_save_model(self): # setup mock objects admin_obj = ModelBaseAdmin(ModelBase, 1) request = RequestFactory() request.user = self.user # after admin save the object's owner should be the current user obj = ModelBase() admin_obj.save_model(request, obj, admin_obj.form, 1) self.failUnless(obj.owner == self.user) obj.save()
def test_vote_total(self): # create object with some votes obj = ModelBase(title='title') obj.save() obj.add_vote("token1", 1) obj.add_vote("token2", -1) obj.add_vote("token3", 1) # vote_total should return an integer result = obj.vote_total self.failUnlessEqual(result.__class__, int) # vote total is calculated as total_upvotes - total_downvotes self.failUnlessEqual(result, 1)
def test_save(self): before_save = datetime.now() # created field should be set on save obj = ModelBase(title='title') obj.save() # created field should be set to current datetime on save after_save = datetime.now() self.failIf(obj.created > after_save or obj.created < before_save) # if a user supplies a created date use that instead of the current datetime test_datetime = datetime(2008, 10, 10, 12, 12) obj = ModelBase(title='title', created=test_datetime) obj.save() self.failIf(obj.created != test_datetime) # modified should be set to current datetime on each save before_save = datetime.now() obj = ModelBase(title='title') obj.save() after_save = datetime.now() self.failIf(obj.modified > after_save or obj.modified < before_save) # leaf class content type should be set on save obj = DummyModel(title='title') obj.save() self.failUnless(obj.content_type == ContentType.objects.get_for_model(DummyModel)) # leaf class class name should be set on save self.failUnless(obj.class_name == DummyModel.__name__) # correct leaf class content type should be retained over base class' content type base = obj.modelbase_ptr base.save() self.failUnless(base.content_type == ContentType.objects.get_for_model(DummyModel)) # correct leaf class class name should be retained over base class' class name self.failUnless(base.class_name == DummyModel.__name__)
def test_generate_slug(self): # on save a slug should be set obj = ModelBase(title='utils test case title') obj.save() self.failIf(obj.slug=='') # slug should become sluggified version of title obj = ModelBase(title='utils test case title 1') obj.save() self.failUnless(obj.slug==slugify(obj.title)) # no two items should have the same slug obj = ModelBase(title='utils test case title 1') obj.save() # in case an object title is updated, the slug should also be updated obj.title = "updated title" obj.save() self.failUnless(obj.slug==slugify(obj.title)) # in case an object is updated, without the title being changed, the slug should remain unchanged orig_slug = obj.slug obj.save() self.failUnless(obj.slug==orig_slug) # make sure the slug is actually saved obj = ModelBase.objects.get(id=obj.id) self.failIf(obj.slug=='') # Empty slugs might trip up regex query. obj = ModelBase() obj.save() obj = ModelBase() obj.save() obj = ModelBase() obj.save()
def test_get_query_set(self): # create unpublished item unpublished_obj = ModelBase(title='title', state='unpublished') unpublished_obj.save() unpublished_obj.sites.add(self.web_site) unpublished_obj.save() # create published item published_obj = ModelBase(title='title', state='published') published_obj.save() published_obj.sites.add(self.web_site) published_obj.save() # create staging item staging_obj = ModelBase(title='title', state='staging') staging_obj.save() staging_obj.sites.add(self.web_site) staging_obj.save() # unpublished objects should not be available in queryset queryset = ModelBase.permitted.all() self.failIf(unpublished_obj in queryset) # published objects should always be available in queryset self.failUnless(published_obj in queryset) # staging objects should only be available on instances that define settings.STAGING = True settings.STAGING = False queryset = ModelBase.permitted.all() self.failIf(staging_obj in queryset) settings.STAGING = True queryset = ModelBase.permitted.all() self.failUnless(staging_obj in queryset) # queryset should only contain items for the current site published_obj_web = ModelBase(state='published') published_obj_web.save() published_obj_web.sites.add(self.web_site) published_obj_web.save() queryset = ModelBase.permitted.all() self.failUnless(published_obj_web in queryset) # queryset should not contain items for other sites mobile_site = Site(domain="mobi.address.com") mobile_site.save() published_obj_mobile = ModelBase(state='published') published_obj_mobile.save() published_obj_mobile.sites.add(mobile_site) published_obj_mobile.save() queryset = ModelBase.permitted.all() self.failIf(published_obj_mobile in queryset)
def test_can_comment(self): # create dummy request object request = type('Request', (object,), {}) class User(): def is_authenticated(self): return False request.user = User() request.secretballot_token = 'test_token' # return false when commenting is closed obj = ModelBase(comments_enabled=True, comments_closed=True, anonymous_comments=True) obj.save() self.failIf(obj.can_comment(request)) # return false when commenting is disabled obj = ModelBase(comments_enabled=False, comments_closed=False, anonymous_comments=True) obj.save() self.failIf(obj.can_comment(request)) # return false if anonymous and anonymous commenting is disabled obj = ModelBase(comments_enabled=True, comments_closed=False, anonymous_comments=False) obj.save() self.failIf(obj.can_comment(request)) # return true if anonymous and anonymous commenting is enabled obj = ModelBase(comments_enabled=True, comments_closed=False, anonymous_comments=True) obj.save() self.failUnless(obj.can_comment(request))
def test_can_vote(self): # create dummy request object request = type('Request', (object,), {}) class User(): def is_authenticated(self): return False request.user = User() request.secretballot_token = 'test_token' # return false when liking is closed obj = ModelBase(likes_enabled=True, likes_closed=True, anonymous_likes=True) obj.save() self.failIf(obj.can_vote(request)[0]) # return false when liking is disabled obj = ModelBase(likes_enabled=False, likes_closed=False, anonymous_likes=True) obj.save() self.failIf(obj.can_vote(request)[0]) # return false if anonymous and anonymous liking is disabled obj = ModelBase(likes_enabled=True, likes_closed=False, anonymous_likes=False) obj.save() self.failIf(obj.can_vote(request)[0]) # return true if anonymous and anonymous liking is enabled obj = ModelBase(likes_enabled=True, likes_closed=False, anonymous_likes=True) obj.save() self.failUnless(obj.can_vote(request)) # return false if vote already exist content_type = ContentType.objects.get(app_label="panya", model="modelbase") Vote.objects.create(object_id=obj.id, token='test_token', content_type=content_type, vote=1) self.failIf(obj.can_vote(request)[0])
def test_is_permitted(self): # create website site item and set as current site web_site = Site(domain="web.address.com") web_site.save() settings.SITE_ID = web_site.id # create unpublished item unpublished_obj = ModelBase(title='title', state='unpublished') unpublished_obj.save() unpublished_obj.sites.add(web_site) unpublished_obj.save() # create published item published_obj = ModelBase(title='title', state='published') published_obj.save() published_obj.sites.add(web_site) published_obj.save() # create staging item staging_obj = ModelBase(title='title', state='staging') staging_obj.save() staging_obj.sites.add(web_site) staging_obj.save() # is_permitted should be False for unpublished objects self.failIf(unpublished_obj.is_permitted) # is_permitted should be True for published objects self.failUnless(published_obj.is_permitted) # is_permitted should be True for otherwise published objects in the staging state for instances that define settings.STAGING = True settings.STAGING = False self.failIf(staging_obj.is_permitted) settings.STAGING = True self.failUnless(staging_obj.is_permitted) # is_permitted should be True only if the object is published for the current site published_obj_web = ModelBase(state='published') published_obj_web.save() published_obj_web.sites.add(web_site) published_obj_web.save() self.failUnless(published_obj_web.is_permitted) # is_permitted should be False if the object is not published for the current site mobile_site = Site(domain="mobi.address.com") mobile_site.save() published_obj_mobile = ModelBase(state='published') published_obj_mobile.save() published_obj_mobile.sites.add(mobile_site) published_obj_mobile.save() self.failIf(published_obj_mobile.is_permitted)
def test_by_range(self): # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create published content content = ModelBase(title='title', state='published') content.save() content.sites.add(self.web_site) content.save() start = datetime.now() end = start + timedelta(days=2) # create entryitem that spans the range spanning_entryitem = EntryItem(entry_id=1, start=start - timedelta(days=1), end=end + timedelta(days=1), content=content) spanning_entryitem.save() spanning_entryitem.calendars.add(published_cal) # create entryitem that precedes the range preceding_entryitem = EntryItem(entry_id=1, start=start - timedelta(days=1), end=start, content=content) preceding_entryitem.save() preceding_entryitem.calendars.add(published_cal) # create entryitem that procedes the range proceding_entryitem = EntryItem(entry_id=1, start=end, end=end + timedelta(days=1), content=content) proceding_entryitem.save() proceding_entryitem.calendars.add(published_cal) # create entryitem that is contained in the range contained_entryitem = EntryItem(entry_id=1, start=start, end=end, content=content) contained_entryitem.save() contained_entryitem.calendars.add(published_cal) # create entryitem that starts before range but ends within range end_contained_entryitem = EntryItem(entry_id=1, start=start-timedelta(days=1), end=end, content=content) end_contained_entryitem.save() end_contained_entryitem.calendars.add(published_cal) # create entryitem that starts in range but ends after range start_contained_entryitem = EntryItem(entry_id=1, start=start, end=end+timedelta(days=1), content=content) start_contained_entryitem.save() start_contained_entryitem.calendars.add(published_cal) result = EntryItem.permitted.by_range(start, end) # spanning entry should be in result self.failUnless(spanning_entryitem in result) # preceding entry should not be in result self.failIf(preceding_entryitem in result) # proceding entry should not be in result self.failIf(proceding_entryitem in result) # contained entryitem should be in result self.failUnless(contained_entryitem in result) # entry starting before range but ending withing range should be in result self.failUnless(end_contained_entryitem in result) # entry starting in range but ending after range should be in result self.failUnless(start_contained_entryitem in result)
def test_get_query_set(self): # create unpublished calendar unpublished_cal = Calendar(title='title', state='unpublished') unpublished_cal.save() unpublished_cal.sites.add(self.web_site) unpublished_cal.save() # create staging calendar staging_cal = Calendar(title='title', state='staging') staging_cal.save() staging_cal.sites.add(self.web_site) staging_cal.save() # create published calendar published_cal = Calendar(title='title', state='published') published_cal.save() published_cal.sites.add(self.web_site) published_cal.save() # create unpublished content unpublished_content = ModelBase(title='title', state='unpublished') unpublished_content.save() unpublished_content.sites.add(self.web_site) unpublished_content.save() # create staging content staging_content = ModelBase(title='title', state='staging') staging_content.save() staging_content.sites.add(self.web_site) staging_content.save() # create published content published_content = ModelBase(title='title', state='published') published_content.save() published_content.sites.add(self.web_site) published_content.save() # entries with unpublished calendars and content should not be available in queryset # create unpublished cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=unpublished_content) entry_obj.save() entry_obj.calendars.add(unpublished_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with unpublished calendars should not be available in queryset, regardless of content state # create unpublished cal, published content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content) entry_obj.save() entry_obj.calendars.add(unpublished_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with unpublished content should not be available in queryset, regardless of cal state # create unpublished content, published cal entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=unpublished_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count()) Entry.objects.all().delete() # entries with staging calendars and content should be available in queryset but only if settings.STAGING = True # create staging cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=staging_content) entry_obj.save() entry_obj.calendars.add(staging_cal) entry_obj.save() settings.STAGING = False queryset = EntryItem.permitted.all() self.failIf(queryset.count()) settings.STAGING = True queryset = EntryItem.permitted.all() self.failUnless(queryset.count()) Entry.objects.all().delete() # entries with published cal and content should be available in queryset # create published cal and content entries entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content) entry_obj.save() entry_obj.calendars.add(published_cal) entry_obj.save() queryset = EntryItem.permitted.all() self.failUnless(queryset.count()) Entry.objects.all().delete() # queryset should not contain items for other sites mobile_site = Site(domain="mobi.address.com") mobile_site.save() # create published calendar for mobile site published_cal_mobile = Calendar(title='title', state='published') published_cal_mobile.save() published_cal_mobile.sites.add(mobile_site) published_cal_mobile.save() # create published calendar for mobile site published_content_mobile = ModelBase(title='title', state='published') published_content_mobile.save() published_content_mobile.sites.add(mobile_site) published_content_mobile.save() entry_obj = Entry(start=datetime.now(), end=datetime.now() + timedelta(days=1), repeat="daily", repeat_until=(datetime.now() + timedelta(days=30)).date(), content=published_content_mobile) entry_obj.save() entry_obj.calendars.add(published_cal_mobile) entry_obj.save() queryset = EntryItem.permitted.all() self.failIf(queryset.count())