Example #1
0
    def test_delete_site(self):
        # Make note of the starting site count
        starting_sites = Site.all()

        # Create a Site manually first so that we isolate this test to only the Update API
        raw_site = Site()
        raw_site.name = 'tc_lambda_sites_test_delete_site'
        raw_site.address = '123 tc_lambda_sites_test_delete_site street'
        raw_site.availability_status = 'Green'
        raw_site.hours = '12-12'
        raw_site.is_open = True
        raw_site.save()
        self.assertEqual("123 tc_lambda_sites_test_delete_site street",
                         raw_site.address)
        self.assertEqual('tc_lambda_sites_test_delete_site', raw_site.name)
        self.assertEqual('Green', raw_site.availability_status)
        self.assertEqual(True, raw_site.is_open)
        self.assertEqual('12-12', raw_site.hours)

        event = {
            'httpMethod': 'DELETE',
            'path': '/site/tc_lambda_sites_test_delete_site',
            'resource': '/site/{sitename}',
            'pathParameters': {
                'sitename': 'tc_lambda_sites_test_delete_site'
            },
            'body': None,
        }

        response = LambdaApiHandler.site_apis(event)
        self.assertEqual('', response['body'])
        self.assertEqual('200', response['statusCode'])
        self.assertEqual(
            len(starting_sites), len(Site.all()),
            'The Delete Site API failed to destroy the record it created.')

        # Fetch the site separately and validate the content
        site = Site.find('tc_lambda_sites_test_delete_site')
        self.assertIsNone(site)

        # Cleanup.
        if raw_site != None:
            raw_site.delete()
        # Cleanup validation
        self.assertEqual(len(starting_sites), len(Site.all()),
                         "Failed to clean up the site we created!")
Example #2
0
    def test_create_new(self):
        site = Site()
        site.name = 'test_site_2'
        site.address = '234 Fake Street'
        site.availability_status = 'Green'
        site.hours = '1-2'
        site.is_open = True

        result = site.save()
        self.assertTrue(result, 'Failed to save the new site')
        # Validate that the record was indeed written out to the DB
        self.assertEqual(2, len(Site.all()))

        # Now fetch the site data back out
        site2 = Site.find('test_site_2')
        self.assertEqual("234 Fake Street", site.address)
        self.assertEqual('test_site_2', site.name)
        self.assertEqual('Green', site.availability_status)
        self.assertEqual(True, site.is_open)
        self.assertEqual('1-2', site.hours)

        # Delete the newly created site
        site2.delete()
        self.assertEqual(1, len(Site.all()))
Example #3
0
    def test_update_site(self):
        # Make note of the starting site count
        starting_sites = Site.all()

        # Create a Site manually first so that we isolate this test to only the Update API
        raw_site = Site()
        raw_site.name = 'tc_lambda_sites_test_update_site'
        raw_site.address = '123 tc_lambda_sites_test_update_site street'
        raw_site.availability_status = 'Green'
        raw_site.hours = '12-12'
        raw_site.is_open = True

        raw_site.save()
        self.assertEqual("123 tc_lambda_sites_test_update_site street",
                         raw_site.address)
        self.assertEqual('tc_lambda_sites_test_update_site', raw_site.name)
        self.assertEqual('Green', raw_site.availability_status)
        self.assertEqual(True, raw_site.is_open)
        self.assertEqual('12-12', raw_site.hours)

        event = {
            'httpMethod':
            'POST',
            'path':
            '/site/tc_lambda_sites_test_update_site',
            'resource':
            '/site/{sitename}',
            'pathParameters': {
                'sitename': 'tc_lambda_sites_test_update_site'
            },
            'body':
            json.dumps({
                "Site": {
                    "name": raw_site.name,
                    "address": "123 tc_lambda_sites_test_update_site parkway",
                    "hours": raw_site.hours,
                    "is_open": raw_site.is_open,
                    "availability_status": raw_site.availability_status
                }
            }),
        }

        response = LambdaApiHandler.site_apis(event)
        self.assertEqual('', response['body'])
        self.assertEqual('200', response['statusCode'])
        self.assertEqual(
            len(starting_sites) + 1, len(Site.all()),
            'Somehow the Update Site API created a new entry')

        # Fetch the site separately and validate the content
        site = Site.find('tc_lambda_sites_test_update_site')
        self.assertIsNotNone(site)
        self.assertEqual('tc_lambda_sites_test_update_site', site.name)
        self.assertEqual("123 tc_lambda_sites_test_update_site parkway",
                         site.address)
        self.assertEqual('Green', site.availability_status)
        self.assertEqual(True, site.is_open)
        self.assertEqual('12-12', site.hours)

        # Cleanup.
        if raw_site != None:
            raw_site.delete()
        # Cleanup validation
        self.assertEqual(len(starting_sites), len(Site.all()),
                         "Failed to clean up the site we created!")