2016-05-19 14:53:58 +00:00
|
|
|
# testing imports
|
|
|
|
import pytest
|
2016-05-20 18:19:19 +00:00
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import requests_mock
|
2016-05-19 14:53:58 +00:00
|
|
|
|
|
|
|
# package imports
|
|
|
|
import celigo
|
|
|
|
|
|
|
|
|
2016-05-20 18:19:19 +00:00
|
|
|
class CeligoTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.FAKE_API_KEY = 'fake'
|
2016-05-24 18:18:20 +00:00
|
|
|
os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY
|
2016-05-20 18:19:19 +00:00
|
|
|
|
|
|
|
def test_needs_all_params(self):
|
|
|
|
"""
|
2016-05-24 18:18:20 +00:00
|
|
|
Test that we need to pass in a data directory.
|
2016-05-20 18:19:19 +00:00
|
|
|
"""
|
2016-05-24 18:18:20 +00:00
|
|
|
with self.assertRaises(TypeError):
|
2016-05-20 18:19:19 +00:00
|
|
|
celigo.BackupCeligo()
|
|
|
|
data_dir = "fakedir"
|
2016-05-24 18:18:20 +00:00
|
|
|
celigo.BackupCeligo(data_dir)
|
|
|
|
|
2016-05-24 19:07:36 +00:00
|
|
|
def test_needs_api_key(self):
|
2016-05-24 18:18:20 +00:00
|
|
|
"""
|
2016-05-24 19:07:36 +00:00
|
|
|
Test that we need to either pass in the api_key,
|
|
|
|
or set the CELIGO_API_KEY environment variable.
|
2016-05-24 18:18:20 +00:00
|
|
|
"""
|
|
|
|
data_dir = "fakedir"
|
|
|
|
os.environ['CELIGO_API_KEY'] = ''
|
2016-05-24 19:07:36 +00:00
|
|
|
with self.assertRaisesRegexp(Exception,
|
|
|
|
'Please pass in api_key.*'):
|
2016-05-24 18:18:20 +00:00
|
|
|
celigo.BackupCeligo(data_dir)
|
|
|
|
os.environ['CELIGO_API_KEY'] = self.FAKE_API_KEY
|
2016-05-24 19:07:36 +00:00
|
|
|
bc = celigo.BackupCeligo(data_dir)
|
|
|
|
# Make sure it uses the env var one
|
|
|
|
self.assertEqual(bc.api_key, self.FAKE_API_KEY)
|
|
|
|
|
|
|
|
# Unset env var and make sure it uses the passed in one
|
|
|
|
os.environ['CELIGO_API_KEY'] = ''
|
|
|
|
bc = celigo.BackupCeligo(data_dir, api_key='fake_passed_in')
|
|
|
|
self.assertEqual(bc.api_key, 'fake_passed_in')
|
|
|
|
|
2016-05-20 18:19:19 +00:00
|
|
|
|
|
|
|
def test_ensure_directories(self):
|
|
|
|
"""
|
|
|
|
Test that the ensure_directories_exist works properly.
|
|
|
|
"""
|
|
|
|
tempdir = tempfile.mkdtemp('celigo_testing')
|
2016-05-24 18:18:20 +00:00
|
|
|
bc = celigo.BackupCeligo(tempdir)
|
2016-05-20 18:19:19 +00:00
|
|
|
imports_dir = os.path.join(tempdir, "imports")
|
|
|
|
connections_dir = os.path.join(tempdir, "connections")
|
|
|
|
# Check that the directories don't exist already.
|
|
|
|
self.assertFalse(
|
|
|
|
os.path.exists(imports_dir),
|
|
|
|
"imports dir exists")
|
|
|
|
self.assertFalse(
|
|
|
|
os.path.exists(connections_dir),
|
|
|
|
"connections dir exists")
|
|
|
|
# Make the directories.
|
|
|
|
bc.ensure_directories_exist()
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(imports_dir),
|
|
|
|
"Did not create proper directory"
|
|
|
|
)
|
|
|
|
self.assertTrue(
|
|
|
|
os.path.exists(connections_dir),
|
|
|
|
"Did not create proper directory"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure nothing errors if the directories exist already.
|
|
|
|
bc.ensure_directories_exist()
|
|
|
|
|
|
|
|
@requests_mock.Mocker()
|
|
|
|
def test_fake_requests(self, rqm):
|
|
|
|
rqm.get(celigo.DEFAULT_BASE_URL, text='resp')
|
|
|
|
tempdir = tempfile.mkdtemp('celigo_testing')
|
2016-05-24 18:18:20 +00:00
|
|
|
bc = celigo.BackupCeligo(tempdir)
|