Skip to content

Commit

Permalink
Python3 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rsiera committed Dec 5, 2016
1 parent 1afd06c commit e72a354
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 19 deletions.
6 changes: 5 additions & 1 deletion pg_view/models/collector_partition.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import os
import time
from multiprocessing import Process

import os
import psutil
import sys

from pg_view.consts import RD, TICK_LENGTH, SECTOR_SIZE
from pg_view.formatters import StatusFormatter, FnFormatter
from pg_view.models.collector_base import BaseStatCollector, logger
from pg_view.models.displayers import COLALIGN

if sys.hexversion >= 0x03000000:
long = int


class PartitionStatCollector(BaseStatCollector):
"""Collect statistics about PostgreSQL partitions """
Expand Down
4 changes: 2 additions & 2 deletions pg_view/models/collector_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

def dbversion_as_float(server_version):
version_num = server_version
version_num /= 100
return float('{0}.{1}'.format(version_num / 100, version_num % 100))
version_num //= 100
return float('{0}.{1}'.format(version_num // 100, version_num % 100))


def process_sort_key(process):
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
def read_module(path):
data = {}
with open(path, 'r') as fd:
exec (fd.read(), data)
exec(fd.read(), data)
return data


Expand Down
5 changes: 4 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from StringIO import StringIO
try:
from io import StringIO
except ImportError:
from StringIO import StringIO

import os

Expand Down
17 changes: 8 additions & 9 deletions tests/test_db_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DbClientUtilsTest(TestCase):
@mock.patch('pg_view.models.clients.logger')
@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_none_when_error(self, mocked_open, mocked_logger):
mocked_open.side_effect = Exception
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand All @@ -20,7 +20,7 @@ def test_read_postmaster_pid_should_return_none_when_error(self, mocked_open, mo
expected_msg.format(name='default', wd='/var/lib/postgresql/9.3/main'))

@mock.patch('pg_view.models.clients.logger')
@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_none_when_error_strip(self, mocked_open, mocked_logger):
mocked_open.return_value.readline.return_value = []
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand All @@ -29,7 +29,7 @@ def test_read_postmaster_pid_should_return_none_when_error_strip(self, mocked_op
mocked_logger.error.assert_called_with(
expected_msg.format(name='default', wd='/var/lib/postgresql/9.3/main'))

@mock.patch('__builtin__.open')
@mock.patch('pg_view.models.clients.open', create=True)
def test_read_postmaster_pid_should_return_pid_when_read_file(self, mocked_open):
mocked_open.return_value.readline.return_value = '123 '
data = read_postmaster_pid('/var/lib/postgresql/9.3/main', 'default')
Expand Down Expand Up @@ -217,11 +217,9 @@ def test_establish_user_defined_connection_should_raise_error_when_cant_connect(
with self.assertRaises(NotConnectedError):
client.establish_user_defined_connection('instance', [])

mocked_logger.error.assert_has_calls([
mock.call(
"failed to establish connection to instance via {'host': 'localhost', 'database': 'db', 'port': '5432', 'user': 'user'}"),
mock.call('PostgreSQL exception: ')]
)
expected_msg = "failed to establish connection to instance via {0}".format(
client.connection_builder.build_connection())
mocked_logger.error.assert_has_calls([mock.call(expected_msg), mock.call('PostgreSQL exception: ')])

@mock.patch('pg_view.models.clients.logger')
@mock.patch('pg_view.models.clients.psycopg2')
Expand All @@ -238,7 +236,8 @@ def test_establish_user_defined_connection_should_raise_error_when_not_pid_postm
with self.assertRaises(NoPidConnectionError):
client.establish_user_defined_connection('default', [])

expected_msg = "failed to read pid of the postmaster on {'host': 'localhost', 'database': 'db', 'port': '5432', 'user': 'user'}"
expected_msg = "failed to read pid of the postmaster on {0}".format(
client.connection_builder.build_connection())
mocked_logger.error.assert_called_once_with(expected_msg)

@mock.patch('pg_view.models.clients.logger')
Expand Down
10 changes: 5 additions & 5 deletions tests/test_models_pg.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def test__read_proc_should_return_data_when_process_ok(self, mocked_psutil_proce

'guest_time': 0.0,
'starttime': datetime.datetime.fromtimestamp(1480777289.0),
'delayacct_blkio_ticks': 0L,
'delayacct_blkio_ticks': 0,
'cmdline': 'backend'
}
self.assertEqual(expected_proc_stats, proc_stats)
Expand Down Expand Up @@ -285,8 +285,8 @@ def test_refresh_should_return_results_when_ok(self, mocked__do_refresh, mocked_
'priority': 10,
'vsize': 252428288,
'guest_time': 0.0,
'starttime': 911L,
'delayacct_blkio_ticks': 1L,
'starttime': 911,
'delayacct_blkio_ticks': 1,
'cmdline': 'backend'
}

Expand All @@ -310,14 +310,14 @@ def test_refresh_should_return_results_when_ok(self, mocked__do_refresh, mocked_
'status': 'status',
'write_bytes': 1,
'vsize': 252428288,
'delayacct_blkio_ticks': 1L,
'delayacct_blkio_ticks': 1,
'pid': 1049,
'priority': 10,
'cmdline': 'backend',
'read_bytes': 655,
'uss': 10,
'stime': 0.0001,
'starttime': 911L,
'starttime': 911,
'utime': 0.0002,
'type': 'unknown',
'guest_time': 0.0,
Expand Down

0 comments on commit e72a354

Please sign in to comment.