33
44from rrmngmnt import Host , User
55from rrmngmnt .db import Database
6- from .common import FakeExecutorFactory
76
7+ from .common import FakeExecutorFactory
88
99host_executor_factory = Host .executor_factory
1010
@@ -19,40 +19,38 @@ def fake_cmd_data(cmd_to_data, files=None):
1919
2020class TestDb (object ):
2121 data = {
22- 'which systemctl' : (0 , "" , "" ),
23- 'systemctl list-unit-files | grep -o ^[^.][^.]*.service | '
24- 'cut -d. -f1 | sort | uniq' : (0 , "postgresql\n " , "" ,),
25- 'systemctl restart postgresql.service' : (0 , '' , '' ),
26- 'export PGPASSWORD=db_pass; psql -d db_name -U db_user '
27- '-h localhost -R __RECORD_SEPARATOR__ -t -A -c '
22+ "which systemctl" : (0 , "" , "" ),
23+ "systemctl list-unit-files | grep -o ^[^.][^.]*.service | cut -d. -f1 | sort | uniq" : (
24+ 0 ,
25+ "postgresql\n " ,
26+ "" ,
27+ ),
28+ "systemctl restart postgresql.service" : (0 , "" , "" ),
29+ "export PGPASSWORD=db_pass; psql -d db_name -U db_user "
30+ "-h localhost -R __RECORD_SEPARATOR__ -t -A -c "
2831 '"SELECT key, value FROM dist"' : (
2932 0 ,
3033 "key1|value1__RECORD_SEPARATOR__key2|value2" ,
3134 "" ,
3235 ),
33- 'export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost '
34- '-R __RECORD_SEPARATOR__ -t -A -c "SELECT * FROM table ERROR"' : (
35- 1 , "" , "Syntax Error"
36- ),
37- 'export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost '
38- '-c \\ \\ dt' : (
36+ "export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost "
37+ '-R __RECORD_SEPARATOR__ -t -A -c "SELECT * FROM table ERROR"' : (1 , "" , "Syntax Error" ),
38+ "export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost -c \\ \\ dt" : (
3939 0 ,
4040 (
4141 "List of relations\n "
4242 " Schema | Name | Type | Owner\n "
4343 "--------+----------------------+-------+---------\n "
4444 " public | test_table | table | postgres\n "
4545 ),
46- ""
47- ),
48- 'export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost '
49- '-c \\ \\ dv' : (
50- 0 , "" , "Did not find any relations."
46+ "" ,
5147 ),
52- 'export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost '
53- '-c \\ \\ gg' : (
54- 1 , "" , "invalid command \\ gg"
48+ "export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost -c \\ \\ dv" : (
49+ 0 ,
50+ "" ,
51+ "Did not find any relations." ,
5552 ),
53+ "export PGPASSWORD=db_pass; psql -d db_name -U db_user -h localhost -c \\ \\ gg" : (1 , "" , "invalid command \\ gg" ),
5654 }
5755 files = {}
5856
@@ -64,37 +62,35 @@ class TestDb(object):
6462 def setup_class (cls ):
6563 fake_cmd_data (cls .data , cls .files )
6664
67- def get_db (self , ip = '1.1.1.1' ):
68- h = Host (ip )
69- h .add_user (User ('root' , '34546' ))
65+ @pytest .fixture (scope = "class" )
66+ def db (self ):
67+ h = Host ("1.1.1.1" )
68+ h .add_user (User ("root" , "34546" ))
7069 return Database (
71- h , self .db_name , User (self .db_user , self .db_pass ),
70+ h ,
71+ self .db_name ,
72+ User (self .db_user , self .db_pass ),
7273 )
7374
74- def test_restart (self ):
75- db = self .get_db ()
75+ def test_restart (self , db ):
7676 db .restart ()
7777
78- def test_psql (self ):
79- db = self .get_db ()
78+ def test_psql (self , db ):
8079 res = db .psql ("SELECT %s, %s FROM %s" , "key" , "value" , "dist" )
81- assert res == [[' key1' , ' value1' ], [' key2' , ' value2' ]]
80+ assert res == [[" key1" , " value1" ], [" key2" , " value2" ]]
8281
83- def test_negative (self ):
84- db = self .get_db ()
82+ def test_negative (self , db ):
8583 with pytest .raises (Exception ) as ex_info :
8684 db .psql ("SELECT * FROM table ERROR" )
8785 assert "Syntax Error" in str (ex_info .value )
8886
89- def test_psql_cmd (self ):
90- db = self .get_db ()
91- res = db .psql_cmd ('\\ \\ dt' )
92- assert 'List of relations' in res
93- res = db .psql_cmd ('\\ \\ dv' )
94- assert res == 'Did not find any relations.'
87+ def test_psql_cmd (self , db ):
88+ res = db .psql_cmd ("\\ \\ dt" )
89+ assert "List of relations" in res
90+ res = db .psql_cmd ("\\ \\ dv" )
91+ assert res == "Did not find any relations."
9592
96- def test_negative_cmd (self ):
97- db = self .get_db ()
93+ def test_negative_cmd (self , db ):
9894 with pytest .raises (Exception ) as ex_info :
99- db .psql_cmd (' \\ \\ gg' )
100- assert ' invalid command' in str (ex_info .value )
95+ db .psql_cmd (" \\ \\ gg" )
96+ assert " invalid command" in str (ex_info .value )
0 commit comments