Example #1
0
    def create_message(self, name="", interface=None, **fields):
        name = self.get_name(name)
        interface = interface or self.get_interface()

        if not fields:
            fields[testdata.get_ascii()] = testdata.get_int()
            fields[testdata.get_ascii()] = testdata.get_int()

        msg = interface.create_message(name, fields=fields)
        type(self).interfaces[name].append(interface)
        return msg
Example #2
0
    def __init__(self, *body, **kwargs):
        if "cwd" in kwargs:
            self.cwd = kwargs["cwd"]
        else:
            self.cwd = testdata.create_dir()

        name = kwargs.get('name', None)
        if name is not None:
            self.name = name

        else:
            self.name = "prefix{}.pmod{}_test".format(
                testdata.get_ascii(5).lower(),
                testdata.get_ascii(5).lower()
            )

        self.module_name = ""
        self.prefix = ""
        self.name_prefix = ""
        if name:
            bits = self.name.rsplit('.', 1)
            self.module_name = bits[1] if len(bits) == 2 else bits[0]
            self.prefix = bits[0] if len(bits) == 2 else ''
            self.name_prefix = bits[1][:4] if len(bits) == 2 else bits[0][:4]

        if len(body) == 1: body = body[0]
        self.body = body
        if isinstance(self.body, dict):
            for k in self.body:
                self.body[k] = self._prepare_body(self.body[k])
            self.modules = testdata.create_modules(
                self.body,
                self.cwd,
                prefix=self.name,
            )
            self.path = self.modules.path

        else:
            if kwargs.get("package", False):
                self.module = testdata.create_package(
                    self.name,
                    self._prepare_body(self.body),
                    self.cwd
                )
            else:
                self.module = testdata.create_module(
                    self.name,
                    self._prepare_body(self.body),
                    self.cwd
                )

            self.path = self.module.path
Example #3
0
    def test_key(self):
        c = Connection()
        self.assertEqual("", c.key)
        self.assertEqual(c.key, c.key)

        key = testdata.get_ascii(100)
        c = Connection(options=dict(key=key))
        self.assertNotEqual(b"", ByteString(c.key))
        self.assertEqual(c.key, c.key)

        key_path = testdata.create_file("morp.key", testdata.get_ascii(100))
        c = Connection(options=dict(key=key_path))
        self.assertNotEqual(b"", ByteString(c.key))
        self.assertEqual(c.key, c.key)
Example #4
0
    def get_encrypted_interface(self):
        """get a connected interface"""
        config = DsnConnection(os.environ['MORP_DSN_1'])

        if testdata.random.randint(0, 1):
            key_path = testdata.create_file("/morp.key", testdata.get_ascii(100))
            config.options['keyfile'] = key_path
        else:
            config.options['key'] = testdata.get_ascii(32)

        i = self.interface_class(config)
        i.connect()
        self.assertTrue(i.connected)
        return i
Example #5
0
    def get_encrypted_interface(self, config=None):
        """get a connected interface"""
        options = {}
        if testdata.yes():
            options['key'] = testdata.create_file("/morp.key", testdata.get_ascii(100))
        else:
            options['key'] = testdata.get_ascii(testdata.get_int(10, 200))

        if config:
            for k, v in options.items():
                config.options[k] = v
        else:
            config = self.get_config(**options)

        return self.get_interface(config)
Example #6
0
    def get_encrypted_interface(self):
        """get a connected interface"""
        config = DsnConnection(os.environ['MORP_DSN_1'])

        if testdata.random.randint(0, 1):
            key_path = testdata.create_file("/morp.key",
                                            testdata.get_ascii(100))
            config.options['keyfile'] = key_path
        else:
            config.options['key'] = testdata.get_ascii(32)

        i = self.interface_class(config)
        i.connect()
        self.assertTrue(i.connected)
        return i
Example #7
0
    def test_order(self):
        em = Email("foo")

        uuid = testdata.get_ascii(16)
        body = {
            "url": "http://foo.com",
            "image": "http://foo.com/bar.jpg",
        }

        body.update({
            "title": "expensive",
            "price": 100.00,
        })
        it = Item(uuid=uuid, body=dict(body), price=body["price"])
        em.cheaper_items.append(it)

        body.update({
            "title": "cheaper",
            "price": 10.00,
        })
        it = Item(uuid=uuid, body=dict(body), price=body["price"])
        em.cheaper_items.append(it)

        body.update({
            "title": "cheapest",
            "price": 1.00,
        })
        it = Item(uuid=uuid, body=dict(body), price=body["price"])
        em.cheaper_items.append(it)

        html = em.body_html
        self.assertTrue(
            html.index("cheapest") < html.index("cheaper") < html.index(
                "expensive"))
Example #8
0
    def test_crud(self):
        n = Note()
        title = testdata.get_ascii()
        plain = testdata.get_words()
        n.title = title
        n.plain = plain
        n.save()

        guid = n.guid

        n.title = testdata.get_ascii()
        n.plain = testdata.get_words()
        n.save()
        self.assertEqual(guid, n.guid)
        self.assertNotEqual(title, n.title)
        self.assertNotEqual(plain, n.plain)
Example #9
0
    def test_with(self):
        name = testdata.get_ascii()
        with Sentinal.check(name) as execute:
            self.assertFalse(execute)

        s = Sentinal(name)
        self.assertTrue(s)
Example #10
0
    def get_site(cls, input_files):
        di = {
            'bangfile.py': [
                "from bang import event",
                "@event('config')",
                "def global_config(event_name, config):",
                "    config.host = 'example.com'",
                "    config.name = 'example site'",
                ""
            ]
        }

        # replace any project files if they are present
        for rp in di.keys():
            if rp in input_files:
                di[rp] = input_files.pop(rp)

        for basename, file_contents in input_files.items():
            if "/" in basename:
                fp = os.path.join('input', basename)
            else:
                name = testdata.get_ascii(16)
                fp = os.path.join('input', name, basename)
            di[fp] = file_contents

        project_dir, output_dir = cls.get_dirs(di)

        s = Site(project_dir, output_dir)
        return s
Example #11
0
    def test_subject_total(self):
        em = Email("foo")
        it = Item(uuid=testdata.get_ascii(16), body={}, price=1)

        it = Item(uuid=testdata.get_ascii(16), body={}, price=2)
        it.save()
        em.cheaper_items.append(Item(uuid=it.uuid, body={}, price=1))

        it = Item(uuid=testdata.get_ascii(16), body={}, price=1)
        it.save()
        em.cheaper_items.append(Item(uuid=it.uuid, body={}, price=2))

        self.assertFalse("/2" in em.subject)

        em.kwargs["item_count"] = 2
        self.assertTrue("/2" in em.subject)
Example #12
0
    def test_crud(self):
        it = WatchlistItem(uuid=testdata.get_ascii(16),
                           body={
                               "foo": 1,
                               "bar": 2
                           },
                           price=17.14)

        def assertSubDict(d1, d2):
            for k, v in d1.items():
                self.assertEqual(v, d2[k])

        self.assertEqual(1714, it.price)
        #self.assertEqual({"foo": 1, "bar": 2}, it.body)
        assertSubDict({"foo": 1, "bar": 2}, it.body)

        it.save()
        self.assertLess(0, it.pk)

        self.assertEqual(1714, it.price)
        #self.assertEqual({"foo": 1, "bar": 2}, it.body)
        assertSubDict({"foo": 1, "bar": 2}, it.body)

        it2 = WatchlistItem.query.get_pk(it.pk)
        self.assertEqual(1714, it2.price)
        #self.assertEqual({"foo": 1, "bar": 2}, it2.body)
        assertSubDict({"foo": 1, "bar": 2}, it2.body)
        self.assertEqual(it.pk, it2.pk)
Example #13
0
    def test_queue_auto_create(self):
        """SQS queues will auto-create, this just makes sure that works as intended"""
        i = self.get_interface()

        with i.connection() as connection:
            with i.queue(testdata.get_ascii(), connection) as q:
                q.delete()
Example #14
0
    def test_queue_auto_create(self):
        """SQS queues will auto-create, this just makes sure that works as intended"""
        i = self.get_interface()

        with i.connection() as connection:
            with i.queue(testdata.get_ascii(), connection) as q:
                q.delete()
Example #15
0
    def __init__(self, *body, **kwargs):
        if "cwd" in kwargs:
            self.cwd = kwargs["cwd"]
        else:
            self.cwd = testdata.create_dir()

        name = kwargs.get('name', None)
        if name is not None:
            self.name = name

        else:
            self.name = "prefix{}.pmod{}_test".format(
                testdata.get_ascii(5).lower(),
                testdata.get_ascii(5).lower())

        self.module_name = ""
        self.prefix = ""
        self.name_prefix = ""
        if self.name:
            bits = self.name.rsplit('.', 1)
            self.module_name = bits[1] if len(bits) == 2 else bits[0]
            self.prefix = bits[0] if len(bits) == 2 else ''
            self.name_prefix = bits[1][:4] if len(bits) == 2 else bits[0][:4]

        if len(body) == 1: body = body[0]
        self.body = body
        if isinstance(self.body, dict):
            for k in self.body:
                self.body[k] = self._prepare_body(self.body[k])
            self.modules = testdata.create_modules(
                self.body,
                self.cwd,
                prefix=self.name,
            )
            self.path = self.modules.path

        else:
            if kwargs.get("package", False):
                self.module = testdata.create_package(
                    self.name, self._prepare_body(self.body), self.cwd)
            else:
                self.module = testdata.create_module(
                    self.name, self._prepare_body(self.body), self.cwd)

            self.path = self.module.path
Example #16
0
    def get_count_posts(cls, count):
        post_files = {}
        for x in range(count):
            name = testdata.get_ascii(8)
            post_files["{}.md".format(name)] = testdata.get_words()

        s = cls.get_site(post_files)
        s.compile()
        return s.posts if len(s.posts) else s.auxs
Example #17
0
    def get_post(cls, post_file, post_files=None):
        if not post_files:
            post_files = {}

        name = "{}.md".format(testdata.get_ascii(8))
        post_files[name] = post_file

        posts = cls.get_posts(post_files)
        return posts.first_post
Example #18
0
    def test_fset(self):
        uuid = testdata.get_ascii(16)
        body = {
            "uuid": uuid,
            "price": 100.0,
        }
        it = WatchlistItem(body=body)
        self.assertEqual(10000, it.price)
        self.assertEqual(uuid, it.uuid)

        body = {
            "uuid": testdata.get_ascii(16),
            "price": 200.0,
        }
        it.body = body
        self.assertEqual(10000, it.price)
        self.assertEqual(uuid, it.uuid)
        self.assertNotEqual(uuid, it.body["uuid"])
Example #19
0
    def test_no_bangfile_host(self):
        name = testdata.get_ascii(16)
        p = get_post({
            'foo.md': "\n".join([
                "hi"
            ]),
            'bangfile.py': ""
        }, name=name)

        self.assertEqual("/{}".format(name), p.url)
Example #20
0
    def test_key(self):
        c = Connection()
        self.assertEqual("", c.key)
        self.assertEqual(c.key, c.key)

        key = testdata.get_ascii(100)
        c = Connection(options=dict(key=key))
        self.assertNotEqual(b"", b"{}".format(c.key))
        self.assertEqual(c.key, c.key)

        key_path = testdata.create_file("morp.key", testdata.get_ascii(100))
        c = Connection(options=dict(keyfile=key_path))
        self.assertNotEqual(b"", b"{}".format(c.key))
        self.assertEqual(c.key, c.key)

        c = Connection(options=dict(key=key, keyfile=key_path))
        self.assertNotEqual(b"", b"{}".format(c.key))
        c2 = Connection(options=dict(key=key, keyfile=key_path))
        self.assertEqual(c.key, c2.key)
Example #21
0
    def test_key(self):
        c = Connection()
        self.assertEqual("", c.key)
        self.assertEqual(c.key, c.key)

        key = testdata.get_ascii(100)
        c = Connection(options=dict(key=key))
        self.assertNotEqual(b"", b"{}".format(c.key))
        self.assertEqual(c.key, c.key)

        key_path = testdata.create_file("morp.key", testdata.get_ascii(100))
        c = Connection(options=dict(keyfile=key_path))
        self.assertNotEqual(b"", b"{}".format(c.key))
        self.assertEqual(c.key, c.key)

        c = Connection(options=dict(key=key, keyfile=key_path))
        self.assertNotEqual(b"", b"{}".format(c.key))
        c2 = Connection(options=dict(key=key, keyfile=key_path))
        self.assertEqual(c.key, c2.key)
Example #22
0
    def test_no_bangfile_host(self):
        name = testdata.get_ascii(16)
        ps = self.get_posts({
            '{}/foo.md'.format(name): "\n".join([
                "hi"
            ]),
            'bangfile.py': ""
        })

        self.assertRegexpMatches(ps.first_post.url, "^/{}$".format(name))
Example #23
0
    def test_multi(self):
        uuid = testdata.get_ascii(16)

        it = WatchlistItem(uuid=uuid, body={"foo": 1}, price=17.14)
        it.save()

        it2 = WatchlistItem(uuid=uuid, body={"foo": 1}, price=17.15)
        it2.save()

        self.assertEqual(it.uuid, it2.uuid)
        self.assertLess(it.pk, it2.pk)
Example #24
0
    def test_space(self):
        cmd = testdata.get_ascii()
        commands.add(cmd, "{}", plus=False)

        url = commands.find("{} foo bar che".format(cmd))
        self.assertTrue("%20" in url)
        self.assertFalse("+" in url)

        commands.add(cmd, "{}")
        url = commands.find("{} foo bar che".format(cmd))
        self.assertTrue("+" in url)
        self.assertFalse("%20" in url)
Example #25
0
    def __init__(self, *body, **kwargs):
        if len(body) == 1:
            body = body[0]

        self.body = body
        if not isinstance(body, basestring):
            self.body = "\n".join(body)

        self.cwd = testdata.create_dir()

        name = kwargs.get("name", "")
        if name:
            self.name = name
        else:
            self.name = "prefix{}.pmod{}_test".format(testdata.get_ascii(5), testdata.get_ascii(5))

        bits = self.name.rsplit(".", 1)
        self.module_name = bits[1] if len(bits) == 2 else bits[0]
        self.prefix = bits[0] if len(bits) == 2 else ""
        self.name_prefix = bits[1][:4] if len(bits) == 2 else bits[0][:4]

        self.path = testdata.create_module(self.name, self.body, self.cwd)
Example #26
0
    def test_cookies(self):
        cookies = {
            "foo": testdata.get_ascii(),
            "bar": testdata.get_ascii(),
            "che": 1,
        }

        server = testdata.create_cookieserver(cookies)

        with server:
            res = testdata.fetch(server)
            self.assertEqual(cookies["foo"], res.cookies["foo"])
            self.assertEqual(cookies["bar"], res.cookies["bar"])
            self.assertEqual(str(cookies["che"]), res.cookies["che"])
            self.assertEqual(len(cookies), res.json()["sent_count"])

            res = testdata.fetch(server, cookies=res.cookies)
            self.assertEqual(len(cookies), res.json()["read_count"])

            # test with different case
            res = testdata.fetch(server, headers={"cookie": "foo=1234"})
            self.assertEqual("1234", res.json()["unread_cookies"]["foo"]["value"])
Example #27
0
    def test_email_unicode(self):
        em = Email("foo")
        body = {
            "url": "http://foo.com",
            #"title": "foo",
            "title": "\u2713",
            "image": "http://foo.com/bar.jpg",
            "price": 12.34
        }
        it = Item(uuid=testdata.get_ascii(16), body=body, price=body["price"])

        em.cheaper_items.append(it)

        with self.assertRaises(UnicodeEncodeError):
            str(em.body_html)

        str(em.body_html.encode("utf8"))
Example #28
0
    def test_body(self):
        uuid = testdata.get_ascii(16)
        body = {
            "url": "http://example.com",
            "title": "this is the title",
            "image": "http://example.com/image.jpg",
            "price": 0.0,
        }

        new_body = dict(body)
        new_body["price"] = 1.00
        new_item = Item(uuid=uuid, body=new_body, price=1.00)

        old_body = dict(body)
        old_body["price"] = 10.00
        old_item = Item(uuid=uuid, body=body, price=10.00)
        old_item.save()

        em = Email("wishlist-name")
        em.cheaper_items.append(new_item)
Example #29
0
def get_post(post_files, name=""):

    # clear the environment
    for k, v in os.environ.items():
        if k.startswith('BANG_'):
            del os.environ[k]
    sys.modules.pop("bangfile_module", None)

    if not name:
        name = testdata.get_ascii(16)

    di = {
        'bangfile.py': "\n".join([
            "host = 'example.com'",
            "name = 'example site'",
            ""
        ])
    }

    # replace any project files if they are present
    for rp in di.keys():
        if rp in post_files:
            di[rp] = post_files.pop(rp)

    for basename, file_contents in post_files.items():
        fp = os.path.join('input', name, basename)
        di[fp] = file_contents

    project_dir, output_dir = get_dirs(di)

#     d = Directory(project_dir.input_dir, name)
#     d.ancestor_dir = project_dir.input_dir
#     tmpl = Template(project_dir.template_dir)
#     p = Post(d, output_dir, tmpl, Config(project_dir))

    s = Site(project_dir, output_dir)
    s.output()

    #pout.v(s, len(s.posts), len(s.auxs))

    return s.posts.first_post if len(s.posts) else s.auxs.first_post
Example #30
0
    def test_delete_table_strange_name(self):
        """this makes sure https://github.com/firstopinion/prom/issues/47 is fixed,
        the problem was table names weren't wrapped with single quotes and so if they
        started with a number or something like that SQLite would choke"""
        table_name = "1{}".format(testdata.get_ascii(32))
        i = self.create_interface()
        s = self.get_schema(table_name)
        self.insert(i, s, 5)
        r = i.count(s)
        self.assertEqual(5, r)

        i.delete_table(table_name)

        r = i.count(s)
        self.assertEqual(0, r)

        i._delete_table(table_name)
        self.assertFalse(i.has_table(table_name))

        i.delete_tables(disable_protection=True)
        self.assertFalse(i.has_table(table_name))

        i.delete_tables(disable_protection=True)
        self.assertFalse(i.has_table(table_name))
Example #31
0
    def test_delete_table_strange_name(self):
        """this makes sure https://github.com/firstopinion/prom/issues/47 is fixed,
        the problem was table names weren't wrapped with single quotes and so if they
        started with a number or something like that SQLite would choke"""
        table_name = "1{}".format(testdata.get_ascii(32))
        i = self.create_interface()
        s = self.get_schema(table_name)
        self.insert(i, s, 5)
        r = i.count(s)
        self.assertEqual(5, r)

        i.delete_table(table_name)

        r = i.count(s)
        self.assertEqual(0, r)

        i._delete_table(table_name)
        self.assertFalse(i.has_table(table_name))

        i.delete_tables(disable_protection=True)
        self.assertFalse(i.has_table(table_name))

        i.delete_tables(disable_protection=True)
        self.assertFalse(i.has_table(table_name))
Example #32
0
    def test_get_ascii(self):
        s = testdata.get_ascii()
        self.assertNotEqual(u"", s)

        s = testdata.get_ascii(3)
        self.assertEqual(3, len(s))
Example #33
0
    def test_exists(self):
        s = Sentinal(testdata.get_ascii())
        self.assertFalse(s.exists())

        s.create()
        self.assertTrue(s.exists())
Example #34
0
 def test_modified_within(self):
     f = Filepath.create_temp(testdata.get_ascii(32))
     self.assertTrue(f.modified_within(5))
     self.assertTrue(f.modified_within(days=1))
     time.sleep(1.1)
     self.assertFalse(f.modified_within(1))
Example #35
0
 def foo(self, *args, **kwargs):
     kwargs['foo'].append(testdata.get_ascii())
     return kwargs['foo']
Example #36
0
 def foo(self, *args, **kwargs):
     kwargs['foo'][testdata.get_ascii()] = testdata.get_ascii()
     return kwargs['foo']
Example #37
0
 def foo(self, *args, **kwargs):
     kwargs['foo'][testdata.get_ascii()] = testdata.get_ascii()
     return kwargs['foo']
Example #38
0
 def foo(self, *args, **kwargs):
     kwargs['foo'].append(testdata.get_ascii())
     return kwargs['foo']
Example #39
0
 def get_name(self, name=""):
     if not name:
         name = 'morp-test-' + testdata.get_ascii(12)
         #name = 'morp-test-sqs'
     type(self).interfaces[name].append(None)
     return name
Example #40
0
 def test_concat(self):
     cmd = testdata.get_ascii()
     commands.add(cmd, b"{}")
     # no exception being raised is a success
     url = commands.find("{} {}".format(cmd, testdata.get_unicode_words()))
Example #41
0
    def test_no_package(self):
        ps = Packages()

        with self.assertRaises(KeyError):
            ps["foo-bar-{}".format(testdata.get_ascii())]