Exemplo n.º 1
0
    def test_fetch_all_data(self):
        with self.db as cur:
            cur.execute(insert("person", {"person_id": "mosky", "name": "Mosky Liu"}))

            cur.execute(select("person"))
            results = all_to_dicts(cur)
            self.db._conn.rollback()
Exemplo n.º 2
0
def test_simple_insert():
    mosky = OrderedDict([
        ('person_id', 'mosky'),
        ('name', 'Mosky Liu'),
    ])
    exp = 'INSERT INTO "person" ("person_id", "name") VALUES (\'mosky\', \'Mosky Liu\')'
    eq_(insert('person', mosky), exp)
Exemplo n.º 3
0
def test_insert_dict():
    gen = insert('person', OrderedDict([
        ('person_id', 'mosky'), ('name', 'Mosky Liu')
    ]))
    exp = ('INSERT INTO "person" ("person_id", "name") '
           'VALUES (\'mosky\', \'Mosky Liu\')')
    eq_(gen, exp)
Exemplo n.º 4
0
 def test_insert(self):
     with self.db as cur:
         cur.execute(insert('person', {
             'person_id': 'mosky',
             'name'     : 'Mosky Liu'
         }))
         self.db._conn.rollback()
Exemplo n.º 5
0
def test_insert_returing():
    gen = insert('person', OrderedDict([
        ('person_id', 'mosky'), ('name', 'Mosky Liu'),
    ]), returning=raw('*'))
    exp = ('INSERT INTO "person" ("person_id", "name") '
           'VALUES (\'mosky\', \'Mosky Liu\') RETURNING *')
    eq_(gen, exp)
Exemplo n.º 6
0
    def test_str_input():
        """Test str literals on Python 3.

        This should yield identical results to test_text_input because the u
        prefix has not effect on Python 3.
        """
        exp = 'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
        eq_(insert('message', {'always': '😘モスキー'}), exp)
Exemplo n.º 7
0
 def test_insert(self):
     with self.db as cur:
         cur.execute(
             insert('person', {
                 'person_id': 'mosky',
                 'name': 'Mosky Liu'
             }))
         self.db._conn.rollback()
Exemplo n.º 8
0
    def test_str_input():
        """Test str literals on Python 3.

        This should yield identical results to test_text_input because the u
        prefix has not effect on Python 3.
        """
        exp = 'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
        eq_(insert('message', {'always': '😘モスキー'}), exp)
Exemplo n.º 9
0
 def test_binary_input():
     """Binary input should always be decoded with UTF-8 on Python 2.
     """
     gen = insert('message', {
         'always': b'\xf0\x9f\x98\x98\xe3\x83\xa2\xe3\x82\xb9\xe3\x82\xad\xe3\x83\xbc',
     })
     exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
     eq_(gen, exp)
Exemplo n.º 10
0
    def test_str_input():
        """Test str literals on Python 2.

        On Python 2 this ensures MoSQL can handle binary str literal input. The
        input should always be decoded with UTF-8, not platform encoding, so
        this works on all OSs.
        """
        exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
        eq_(insert('message', {'always': '😘モスキー'}), exp)
Exemplo n.º 11
0
    def test_str_input():
        """Test str literals on Python 2.

        On Python 2 this ensures MoSQL can handle binary str literal input. The
        input should always be decoded with UTF-8, not platform encoding, so
        this works on all OSs.
        """
        exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
        eq_(insert('message', {'always': '😘モスキー'}), exp)
Exemplo n.º 12
0
    def test_fetch_all_data(self):
        with self.db as cur:
            cur.execute(insert('person', {
                'person_id': 'mosky',
                'name'     : 'Mosky Liu'
            }))

            cur.execute(select('person'))
            results = all_to_dicts(cur)
            self.db._conn.rollback()
Exemplo n.º 13
0
 def test_binary_input():
     """Binary input should always be decoded with UTF-8 on Python 2.
     """
     gen = insert(
         'message', {
             'always':
             b'\xf0\x9f\x98\x98\xe3\x83\xa2\xe3\x82\xb9\xe3\x82\xad\xe3\x83\xbc',
         })
     exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
     eq_(gen, exp)
Exemplo n.º 14
0
def test_insert_returing():
    gen = insert('person',
                 OrderedDict([
                     ('person_id', 'mosky'),
                     ('name', 'Mosky Liu'),
                 ]),
                 returning=raw('*'))
    exp = ('INSERT INTO "person" ("person_id", "name") '
           'VALUES (\'mosky\', \'Mosky Liu\') RETURNING *')
    eq_(gen, exp)
Exemplo n.º 15
0
    def test_fetch_all_data(self):
        with self.db as cur:
            cur.execute(
                insert('person', {
                    'person_id': 'mosky',
                    'name': 'Mosky Liu'
                }))

            cur.execute(select('person'))
            results = all_to_dicts(cur)
            self.db._conn.rollback()
Exemplo n.º 16
0
    def test_escape(self):

        # NOTE: \0 will cause an OperationalError of MoSQL
        strange_name = "\n\r\\'\"\x1A\b\t"

        with self.db as cur:

            cur.execute(insert("person", {"person_id": "mosql", "name": strange_name}))

            cur.execute("select name from person where person_id = ?", ("mosql",))
            name, = cur.fetchone()

            self.db._conn.rollback()

        assert strange_name == name
Exemplo n.º 17
0
    def test_escape(self):

        # NOTE: \0 will cause an OperationalError of MoSQL
        strange_name =  '\n\r\\\'\"\x1A\b\t'

        with self.db as cur:

            cur.execute(insert('person', {
                'person_id': 'mosql',
                'name'     : strange_name
            }))

            cur.execute('select name from person where person_id = ?', ('mosql',))
            name, = cur.fetchone()

            self.db._conn.rollback()

        assert strange_name == name
Exemplo n.º 18
0
    def test_escape(self):

        # NOTE: \0 will cause an OperationalError of MoSQL
        strange_name = '\n\r\\\'\"\x1A\b\t'

        with self.db as cur:

            cur.execute(
                insert('person', {
                    'person_id': 'mosql',
                    'name': strange_name
                }))

            cur.execute('select name from person where person_id = ?',
                        ('mosql', ))
            name, = cur.fetchone()

            self.db._conn.rollback()

        assert strange_name == name
Exemplo n.º 19
0
def test_text_input():
    exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
    eq_(insert('message', {'always': u'😘モスキー'}), exp)
Exemplo n.º 20
0
import sys
from mosql.query import insert
import sqlite3

if __name__ == '__main__':
    q = input('Question: ')
    a = input('Answer: ')

    data = {
        'question': q,
        'answer': a
    }

    query = insert('pycon2017', data)

    s = sqlite3.connect('sqlite3.db')
    c = s.cursor()

    c.execute(query)
    s.commit()
    s.close()
Exemplo n.º 21
0
def test_query_output():
    result = insert('message', {'always': u'Mosky \u2665'})
    assert_true(isinstance(result, text_type))  # We always output Unicode.
Exemplo n.º 22
0
 def test_insert(self):
     with self.db as cur:
         cur.execute(insert("person", {"person_id": "mosky", "name": "Mosky Liu"}))
         self.db._conn.rollback()
Exemplo n.º 23
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import psycopg2
from mosql.util import star
from mosql.query import insert

conn = psycopg2.connect(host='127.0.0.1')
cur = conn.cursor()

dave = {
    'person_id': 'dave',
    'name'     : 'Dave',
}

# MoSQL is here! :)
cur.execute(insert('person', dave, returning=star))

person_id, name = cur.fetchone()
print person_id
print name

cur.close()
#conn.commit() # Actually we don't want to commit here.
conn.close()
Exemplo n.º 24
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import psycopg2
from mosql.util import star
from mosql.query import insert
from mosql.db import Database, one_to_dict

dave = {
    'person_id': 'dave',
    'name': 'Dave',
}

db = Database(psycopg2, host='127.0.0.1')

with db as cur:

    cur.execute(insert('person', dave, returning=star))
    print one_to_dict(cur)
    print

    assert 0, 'Rollback!'
Exemplo n.º 25
0
def test_insert_dict():
    gen = insert('person',
                 OrderedDict([('person_id', 'mosky'), ('name', 'Mosky Liu')]))
    exp = ('INSERT INTO "person" ("person_id", "name") '
           'VALUES (\'mosky\', \'Mosky Liu\')')
    eq_(gen, exp)
Exemplo n.º 26
0
def test_text_input():
    exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスキー\')'
    eq_(insert('message', {'always': u'😘モスキー'}), exp)