Beispiel #1
0
class TestSuggestion(unittest.TestCase):
    def setUp(self):

        self.filename = os.path.join(
            os.path.dirname(os.path.dirname(__file__)), 'data', 'test_db.tsv')
        self.db = Database()
        self.db.load(self.filename)
        self.suggestion = Suggestion(self.db)

    def test_get_name(self):

        response = self.suggestion.get('Abb')

        self.assertEqual(len(response['suggestions']), 1)
        self.assertEqual(response['suggestions'][0]['name'],
                         'Abbotsford, 02, CA')
        self.assertTrue(0 >= response['suggestions'][0]['score'] <= 1)

    def test_get_name_with_params(self):
        response = self.suggestion.get('Abb', -10, 10)

        self.assertEqual(len(response['suggestions']), 1)
        self.assertEqual(response['suggestions'][0]['name'],
                         'Abbotsford, 02, CA')
        self.assertTrue(0 >= response['suggestions'][0]['score'] <= 1)
class TestDatabase(unittest.TestCase):
    def setUp(self):
        self.db = Database()
        self.filename = os.path.join(
            os.path.dirname(os.path.dirname(__file__)), 'data', 'test_db.tsv')

    def test_load(self):
        self.db.load(self.filename)

        self.assertEqual(len(self.db.cities), 3)
        self.assertEqual(self.db.cities[0].name, 'Abbotsford, 02, CA')

    def test_get(self):
        self.db.load(self.filename)

        result = self.db.get('Abb')

        self.assertEqual(len(result), 1)
        self.assertEqual(result[0].name, 'Abbotsford, 02, CA')
class RestServer:
    """
    The RestServer class is in charge of the database and
    registers every resource ( endpoint )
    """
    def __init__(self, application=None):
        self.application = application
        self.api = Api(self.application)
        self.database = Database()
        self.api.add_resource(EndPoint,
                              '/suggestions',
                              resource_class_kwargs={'db': self.database})

    def load_db(self, name):
        """
        Loading the db from filename
        :param name: database file name
        :return: none
        """
        self.database.load(name)