def test_restore_dump_user(self, mock_dump_cmd, mock_restore_cmd):
     connector = PgDumpBinaryConnector()
     dump = connector.create_dump()
     # Without
     connector.settings.pop('USER', None)
     connector.restore_dump(dump)
     self.assertNotIn(' --user='******'USER'] = '******'
     connector.restore_dump(dump)
     self.assertIn(' --user=foo', mock_restore_cmd.call_args[0][0])
 def test_restore_dump_port(self, mock_dump_cmd, mock_restore_cmd):
     connector = PgDumpBinaryConnector()
     dump = connector.create_dump()
     # Without
     connector.settings.pop('PORT', None)
     connector.restore_dump(dump)
     self.assertNotIn(' --port=', mock_restore_cmd.call_args[0][0])
     # With
     connector.settings['PORT'] = 42
     connector.restore_dump(dump)
     self.assertIn(' --port=42', mock_restore_cmd.call_args[0][0])
class PgDumpBinaryConnectorTest(TestCase):
    def setUp(self):
        self.connector = PgDumpBinaryConnector()
        self.connector.settings['HOST'] = 'hostname'
        self.connector.settings['ENGINE'] = 'django.db.backends.postgresql'
        self.connector.settings['NAME'] = 'dbname'

    def test_create_dump(self, mock_dump_cmd):
        dump = self.connector.create_dump()
        # Test dump
        dump_content = dump.read()
        self.assertTrue(dump_content)
        self.assertEqual(dump_content, b'foo')
        # Test cmd
        self.assertTrue(mock_dump_cmd.called)
        self.assertIn('--format=custom', mock_dump_cmd.call_args[0][0])

    def test_create_dump_exclude(self, mock_dump_cmd):
        # Without
        self.connector.create_dump()
        self.assertNotIn(' --exclude-table-data=',
                         mock_dump_cmd.call_args[0][0])
        # With
        self.connector.exclude = ('foo', )
        self.connector.create_dump()
        self.assertIn(' --exclude-table-data=foo',
                      mock_dump_cmd.call_args[0][0])
        # With serveral
        self.connector.exclude = ('foo', 'bar')
        self.connector.create_dump()
        self.assertIn(' --exclude-table-data=foo',
                      mock_dump_cmd.call_args[0][0])
        self.assertIn(' --exclude-table-data=bar',
                      mock_dump_cmd.call_args[0][0])

    def test_create_dump_drop(self, mock_dump_cmd):
        # Without
        self.connector.drop = False
        self.connector.create_dump()
        self.assertNotIn(' --clean', mock_dump_cmd.call_args[0][0])
        # Binary drop at restore level
        self.connector.drop = True
        self.connector.create_dump()
        self.assertNotIn(' --clean', mock_dump_cmd.call_args[0][0])

    @patch('dbbackup.db.postgresql.PgDumpBinaryConnector.run_command',
           return_value=(BytesIO(), BytesIO()))
    def test_restore_dump(self, mock_dump_cmd, mock_restore_cmd):
        dump = self.connector.create_dump()
        self.connector.restore_dump(dump)
        # Test cmd
        self.assertTrue(mock_restore_cmd.called)
 def test_restore_dump(self, mock_dump_cmd, mock_restore_cmd):
     connector = PgDumpBinaryConnector()
     dump = connector.create_dump()
     connector.restore_dump(dump)
     # Test cmd
     self.assertTrue(mock_restore_cmd.called)