Exemple #1
0
    def create(self, author_id, title=None, content=""):
        """create is used to add new images into database. Handles preparing memo
        for saving.

        Args:
            author_id: memo's author's id as ObjectId
            title: memo title as string. Defaults to None. If None,
                   memo is created with default title.
            content: memo content as string. Defaults to "".

        Returns:
            Union(Memo, None): if saving were successful, Memo is returned. If no
                               authors with given id or problems while saving,
                               returning None.
        """
        author = self.user_repository.get('id', author_id)
        if not author:
            return None

        memo = {
            "author": author,
            "title": title if title else "Memo " + str(get_time()),
            "content": content,
            "date": get_time(),
        }
        result = self.memo_repository.new(memo)

        author.memos.append(result.id)
        self.user_repository.update(author)

        return result
    def test_import_from_file_test_data2_no_duplicate_image_and_uses_only_own_images(
            self):
        imgsrc = path.join(path.dirname(__file__), '..', './testdata',
                           'testimage1.png')
        imgsrc = path.normpath(imgsrc)
        self.image_service.create(self.second_author.id,
                                  'test2.md/testimage1.png', imgsrc, 600)
        imgsrc = path.join(path.dirname(__file__), '..', './testdata',
                           'testimage2.jpg')
        imgsrc = path.normpath(imgsrc)
        self.image_service.create(self.second_author.id,
                                  'test2.md/testimage1.png', imgsrc, 600)
        filesrc = path.join(path.dirname(__file__), '..', './testdata',
                            'test2.md')
        filesrc = path.normpath(filesrc)
        self.memo_service.import_from_file(self.author.id, filesrc)
        second_memo = self.memo_service.import_from_file(
            self.author.id, filesrc)

        image = self.image_service.get('all')
        image = image[2]

        content = "| First Column | Second column |\n| ------------ | ------------- |\n| first        | row           |\n| second       | row           |\n\n![](" + \
            str(image.id)+")\n\n\ntest\n"
        self.assertIsNotNone(second_memo)
        self.assertIsNotNone(second_memo.id)
        self.assertEqual(second_memo.title, "Imported from test2.md")
        self.assertEqual(second_memo.content, content)
        self.assertEqual(second_memo.author, self.author)
        self.assertEqual(second_memo.date, get_time())
        self.assertEqual(self.image_service.count(), 3)
Exemple #3
0
 async def before_number_day_update(self) -> None:
     """
     Wait until midnight UTC before doing the number day update.
     """
     current_time = get_time()
     time_remaining = (timedelta(hours=24) -
                       (current_time - current_time.replace(
                           hour=0, minute=0, second=0, microsecond=0))
                       ).total_seconds() % (24 * 3600)
     await sleep(time_remaining)
     await self.bot.wait_until_ready()
 def test_import_from_url_returns_valid(self):
     memo = self.memo_service.import_from_url(
         self.author.id,
         'https://github.com/FinThunderstorm/ohte/blob/master/src/tests/testdata/test3.md'
     )
     content = "Lorem ipsum dolor sit\n\nLorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce diam mi, pulvinar vitae diam et, faucibus luctus ipsum. Nulla ullamcorper viverra sapien at facilisis. Nullam bibendum cursus metus ac posuere. Nulla faucibus convallis sapien sit amet dapibus. Integer sodales dignissim turpis, id vestibulum tellus euismod ut. Duis vitae velit molestie, interdum purus nec, aliquam sapien. Praesent purus metus, egestas convallis leo nec, faucibus iaculis augue. Nam et mi in nulla tincidunt faucibus.\n\nDonec mauris sapien, hendrerit in mi et, lobortis sodales nulla. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Donec ullamcorper commodo dignissim. Donec aliquet, erat et varius egestas, massa sem fermentum leo, feugiat semper ipsum tellus eget massa. Nulla interdum eu nunc id eleifend. Nam semper mi lorem, ac tristique lectus euismod a. Quisque sagittis sem interdum, varius dolor eget, volutpat arcu. Vestibulum iaculis nec ex vel tristique. Morbi aliquam vulputate vulputate. Vestibulum quis sapien massa. Nulla nec pellentesque ex. Vestibulum cursus purus at justo posuere, sodales maximus tortor auctor.\n\nNunc feugiat vestibulum condimentum. Sed euismod ex vel faucibus bibendum. Pellentesque non orci eget orci bibendum venenatis at id urna. Praesent in felis ante. Pellentesque a euismod diam, at ullamcorper ante. Pellentesque porttitor sagittis nisi vitae tincidunt. Nulla fringilla velit eu sagittis convallis. Duis porttitor quam sed ante faucibus, at imperdiet libero tristique. Nam quis molestie neque.\n\nVestibulum eget urna molestie, ultrices dolor ac, ornare eros. Sed commodo tristique nunc, at vehicula mauris fermentum ac. Morbi sagittis turpis ut consectetur lacinia. In leo lacus, tristique egestas massa nec, ornare molestie massa. Praesent eu velit at velit elementum commodo. Nunc fermentum vehicula mauris a dignissim. Suspendisse fermentum, metus eu egestas ultrices, erat nisi facilisis orci, in lobortis ante dolor mollis libero. Duis tincidunt metus libero, sed ultricies ipsum lacinia a. Nulla nec dictum velit, ut varius mi. In fermentum accumsan sollicitudin. Mauris libero magna, vehicula ac velit eu, laoreet dignissim nulla.\n\nAliquam finibus, leo quis ornare ullamcorper, lectus nibh iaculis nisi, sed rhoncus erat leo sed metus. Praesent quam leo, accumsan at lobortis et, accumsan sit amet augue. Praesent vitae blandit orci, quis scelerisque dolor. Maecenas sed luctus arcu. In a fringilla metus. Etiam a velit ut tortor lacinia vestibulum vel non nisi. Quisque gravida ornare odio. Nunc erat ligula, tristique nec nisl vel, rutrum dignissim elit. Proin eu fermentum lectus. Vestibulum ut imperdiet enim. Nunc semper quam ut nulla auctor, at pharetra massa semper. Aliquam ac mollis libero. Mauris semper, lacus sed tristique accumsan, dolor mi mollis purus, nec gravida enim ex non arcu."
     self.assertEqual(memo.title, 'Imported from github.com')
     self.assertEqual(memo.content, content)
     self.assertEqual(memo.author, self.author)
     self.assertEqual(memo.date, get_time())
     self.assertIsNotNone(memo.id)
 def test_import_from_file_test_data1_works(self):
     filesrc = path.join(path.dirname(__file__), '..', './testdata',
                         'test1.md')
     filesrc = path.normpath(filesrc)
     content = "# Testing\n\n- first\n- second\n- third\n\n**bold**\n"
     memo = self.memo_service.import_from_file(self.author.id, filesrc)
     self.assertIsNotNone(memo)
     self.assertIsNotNone(memo.id)
     self.assertEqual(memo.title, "Imported from test1.md")
     self.assertEqual(memo.content, content)
     self.assertEqual(memo.author, self.author)
     self.assertEqual(memo.date, get_time())
    def new(self, memo):
        """new is used for creating and saving new memo into database.

        Args:
            memo: dictionary with all memo's values: author as User, title as str,
                  content as str.

        Returns:
            Memo: returns created memo with id assigned by the database
        """
        new_memo = Memo(
            author=memo["author"],
            title=memo["title"],
            content=memo["content"],
            date=get_time(),
        )
        saved_memo = new_memo.save()
        return saved_memo
    def test_import_from_file_test_data2_works(self):
        filesrc = path.join(path.dirname(__file__), '..', './testdata',
                            'test2.md')
        filesrc = path.normpath(filesrc)

        memo = self.memo_service.import_from_file(self.author.id, filesrc)

        image = self.image_service.get('all')[0]

        content = "| First Column | Second column |\n| ------------ | ------------- |\n| first        | row           |\n| second       | row           |\n\n![](" + \
            str(image.id)+")\n\n\ntest\n"
        self.assertIsNotNone(memo)
        self.assertIsNotNone(memo.id)
        self.assertEqual(memo.title, "Imported from test2.md")
        print(repr(memo.content))
        self.assertEqual(memo.content, content)
        self.assertEqual(memo.author, self.author)
        self.assertEqual(memo.date, get_time())
        self.assertEqual(self.image_service.count(), 1)
Exemple #8
0
from os import path
import unittest
from freezegun import freeze_time
from utils.database_handler import connect_test_database, disconnect_database
from utils.helpers import get_time, get_time_timestamp, get_test_memo, get_id, get_test_memo_user, get_test_user_obj
from services.image_service import image_service
from repositories.user_repository import user_repository


@freeze_time(get_time())
class TestImageService(unittest.TestCase):
    def setUp(self):
        connect_test_database()
        self.userrepo = user_repository
        self.image_service = image_service
        self.author = self.userrepo.update(
            get_test_memo_user('6072d33e3a3c627a49901cd7', "memouser2"))
        self.second_author = self.userrepo.update(
            get_test_memo_user('608bd901507fc317f17ab301', "memouser3"))
        self.before = self.image_service.count()

    def tearDown(self):
        disconnect_database()

    def test_create_with_valid_works(self):
        imgsrc = path.join(path.dirname(__file__), '..', './testdata',
                           'testimage1.png')
        imgsrc = path.normpath(imgsrc)
        image = self.image_service.create(self.author.id, 'testimage1', imgsrc,
                                          600)
        self.assertEqual(image.name, "testimage1")
 def test_create_returns_memo_with_default_title(self):
     new_memo = self.memo_service.create(self.author.id)
     self.assertEqual(new_memo.author.id, self.author.id)
     self.assertEqual(new_memo.title, "Memo " + str(get_time()))
     self.assertEqual(new_memo.content, "")