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()
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)
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)
def test_insert(self): with self.db as cur: cur.execute(insert('person', { 'person_id': 'mosky', 'name' : 'Mosky Liu' })) self.db._conn.rollback()
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)
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)
def test_insert(self): with self.db as cur: cur.execute( insert('person', { 'person_id': 'mosky', 'name': 'Mosky Liu' })) self.db._conn.rollback()
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)
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)
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()
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)
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()
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
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
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
def test_text_input(): exp = u'INSERT INTO "message" ("always") VALUES (\'😘モスã‚ー\')' eq_(insert('message', {'always': u'😘モスã‚ー'}), exp)
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()
def test_query_output(): result = insert('message', {'always': u'Mosky \u2665'}) assert_true(isinstance(result, text_type)) # We always output Unicode.
def test_insert(self): with self.db as cur: cur.execute(insert("person", {"person_id": "mosky", "name": "Mosky Liu"})) self.db._conn.rollback()
#!/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()
#!/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!'
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)