File: README.rst

package info (click to toggle)
python-testing.postgresql 1.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 196 kB
  • ctags: 118
  • sloc: python: 355; makefile: 8
file content (206 lines) | stat: -rw-r--r-- 5,669 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
About
=====
``testing.postgresql`` automatically setups a postgresql instance in a temporary directory, and destroys it after testing.

.. image:: https://travis-ci.org/tk0miya/testing.postgresql.svg?branch=master
   :target: https://travis-ci.org/tk0miya/testing.postgresql

.. image:: https://coveralls.io/repos/tk0miya/testing.postgresql/badge.png?branch=master
   :target: https://coveralls.io/r/tk0miya/testing.postgresql?branch=master

.. image:: https://codeclimate.com/github/tk0miya/testing.postgresql/badges/gpa.svg
   :target: https://codeclimate.com/github/tk0miya/testing.postgresql


Documentation
  https://github.com/tk0miya/testing.postgresql
Issues
  https://github.com/tk0miya/testing.postgresql/issues
Download
  https://pypi.python.org/pypi/testing.postgresql

Install
=======
Use pip::

   $ pip install testing.postgresql

And ``testing.postgresql`` requires PostgreSQL server in your PATH.


Usage
=====
Create PostgreSQL instance using ``testing.postgresql.Postgresql``::

  import testing.postgresql
  from sqlalchemy import create_engine

  # Lanuch new PostgreSQL server
  with testing.postgresql.Postgresql() as postgresql:
      # connect to PostgreSQL
      engine = create_engine(postgresql.url())

      # if you use postgresql or other drivers:
      #   import psycopg2
      #   db = psycopg2.connect(**postgresql.dsn())

      #
      # do any tests using PostgreSQL...
      #

  # PostgreSQL server is terminated here


``testing.postgresql.Postgresql`` executes ``initdb`` and ``postgres`` on instantiation.
On deleting Postgresql object, it terminates PostgreSQL instance and removes temporary directory.

If you want a database including tables and any fixtures for your apps,
use ``copy_data_from`` keyword::

  # uses a copy of specified data directory of PostgreSQL.
  postgresql = testing.postgresql.Postgresql(copy_data_from='/path/to/your/database')


For example, you can setup new PostgreSQL server for each testcases on setUp() method::

  import unittest
  import testing.postgresql

  class MyTestCase(unittest.TestCase):
      def setUp(self):
          self.postgresql = testing.postgresql.Postgresql()

      def tearDown(self):
          self.postgresql.stop()


To make your tests faster
-------------------------

``testing.postgresql.Postgresql`` invokes ``initdb`` command on every instantiation.
That is very simple. But, in many cases, it is very waste that generating brandnew database for each testcase.

To optimize the behavior, use ``testing.postgresql.PostgresqlFactory``.
The factory class is able to cache the generated database beyond the testcases,
and it reduces the number of invocation of ``initdb`` command::

  import unittest
  import testing.postgresql

  # Generate Postgresql class which shares the generated database
  Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True)


  def tearDownModule(self):
      # clear cached database at end of tests
      Postgresql.clear_cache()


  class MyTestCase(unittest.TestCase):
      def setUp(self):
          # Use the generated Postgresql class instead of testing.postgresql.Postgresql
          self.postgresql = Postgresql()

      def tearDown(self):
          self.postgresql.stop()

If you want to insert fixtures to the cached database, use ``initdb_handler`` option::

  # create initial data on create as fixtures into the database
  def handler(postgresql):
      conn = psycopg2.connect(**postgresql.dsn())
      cursor = conn.cursor()
      cursor.execute("CREATE TABLE hello(id int, value varchar(256))")
      cursor.execute("INSERT INTO hello values(1, 'hello'), (2, 'ciao')")
      cursor.close()
      conn.commit()
      conn.close()

  # Use `handler()` on initialize database
  Postgresql = testing.postgresql.PostgresqlFactory(cache_initialized_db=True,
                                                    on_initialized=handler)


Requirements
============
* Python 2.6, 2.7, 3.2, 3.3, 3.4, 3.5
* pg8000 1.10

License
=======
Apache License 2.0


History
=======

1.3.0 (2016-02-03)
-------------------
* Add testing.postgresql.PostgresqlFactory
* Depend on ``testing.common.database`` package

1.2.1 (2015-08-22)
-------------------
* Fix bug:

  - Close #3 Fix AttributeError on end of tests

1.2.0 (2015-05-17)
-------------------
* Use `pg8000` for connector to create test database
* Connect to `postgres` to create test database (instead of `template1`)

1.1.2 (2015-04-06)
-------------------
* Fix bugs:

  - Do not call os.getpid() on destructor (if not needed)
  - Raise detailed RuntimeError if initdb exits non-zero

1.1.1 (2015-01-18)
-------------------
* Disable logging_collector feature (For Fedora)
* Fix bugs:

  - MacPorts default path is /opt/local/lib/postgresql*, no dash

1.1.0 (2014-12-20)
-------------------
* Invoke 'postgres' command instead of 'postmaster'

1.0.6 (2014-07-19)
-------------------
* Fix #1 Dirty postmaster shut down

1.0.5 (2014-07-19)
-------------------
* Fix path for PostgreSQL
* Use absolute path for which command

1.0.4 (2014-06-19)
-------------------
* Fix timeout on terminating postgresql
* Support PostgreSQL on /usr/local/bin (cf. FreeBSD ports)
* Fix bugs

1.0.3 (2014-06-11)
-------------------
* Fix ImportError if caught SIGINT on py3

1.0.2 (2013-12-06)
-------------------
* Change behavior: Postgresql#stop() cleans workdir
* Fix caught AttributeError on object deletion

1.0.1 (2013-12-05)
-------------------
* Add @skipIfNotInstalled decorator (alias of skipIfNotFound)
* Suport python 2.6 and 3.2

1.0.0 (2013-12-04)
-------------------
* Add @skipIfNotFound decorator

0.1.0 (2013-11-26)
-------------------
* First release