<commit_before>import unittest
import sys
import json
import logging
from api_request import *
from configuration_load import *

class dw_tests(unittest.TestCase):

    def test_extension(self):
        offices = get_extension_offices(extensions_url, access_token)

        # check that we have extension locations
        self.assertGreater(len(offices["data"]), 10)

        for office in offices["data"]:
            self.assertIsNotNone(office["id"])
            self.assertEqual(office["type"], "locations")
            self.assertIsNotNone(office["attributes"]["name"])
            self.assertEqual(office["attributes"]["type"], "building")
            self.assertIsNotNone(office["attributes"]["county"])
            self.assertIsNotNone(office["attributes"]["zip"])
            self.assertIsNotNone(office["attributes"]["fax"])
            self.assertIsNotNone(office["attributes"]["website"])
            self.assertIsNotNone(office["attributes"]["website"])

            # Refactor into a separate method?
            self.assertTrue(self.check_lat_lon(office))

    def test_dining(self):
        invalid_dining_count = 0

        # check that we have extension locations
        self.assertGreater(len(restaurants["data"]), 10)

        for diner in restaurants["data"]:
            if not self.is_valid_dining(diner) or not self.has_valid_open_hours(diner):
                invalid_dining_count += 1

        logger.debug("invalid dining locations: " + str(invalid_dining_count))
        self.assertLessEqual(invalid_dining_count, max_invalid_dining)
        return

    def is_valid_dining(self, location):
        return location["attributes"]["name"] is not None and \
                location["attributes"]["summary"] is not None and \
                self.check_lat_lon(location)

    def check_lat_lon(self, location):
        return location["attributes"]["geoLocation"] is not None and \
                location["attributes"]["geoLocation"]["lat"] is not None and \
                location["attributes"]["geoLocation"]["lon"] is not None and \
                (type(location["attributes"]["geoLocation"]["lon"]) is float) and \
                (type(location["attributes"]["geoLocation"]["lat"]) is float)

    def has_valid_open_hours(self, location):
        valid_day_range = range(1,7)
        invalid_days = 0

        if "openHours" not in location["attributes"]:
            logger.debug("doesn't have hours " + str(location["attributes"]["name"]))
            return False

        for day, open_hour_list in location["attributes"]["openHours"].iteritems():
            valid_day_index = int(day) in valid_day_range
            invalid_time_slot_count = len([i for i in open_hour_list if not self.is_valid_open_slot(i)])
            logger.debug("day in range: " + str(valid_day_index))
            if invalid_time_slot_count > 1 or not valid_day_index:
                invalid_days += 1

        logger.debug("invalid days: " + str(invalid_days) + " check against: " + str(min_valid_days))
        return (7 - invalid_days) >= min_valid_days

    def is_valid_open_slot(self, open_slot):
        return open_slot is not None and open_slot["start"] is not None and open_slot["end"] is not None

    def testOpenHoursUTC(self):
        test_slot = None
        test_diners = [diner for diner in restaurants["data"] if self.has_valid_open_hours(diner)]
        open_hours = test_diners[0]["attributes"]["openHours"] 

        logger.debug("test utc with diner: " + test_diners[0]["attributes"]["name"])

        for day, open_hour_list in open_hours.iteritems():
            valid_slots = [i for i in open_hour_list if self.is_valid_open_slot(i)]
            if len(valid_slots) > 0:
                test_slot = valid_slots[0]
                logger.debug("test day slot day: " + str(day) + ", slot: " + str(test_slot))
                break

        regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}[T][0-9]{2}:[0-9]{2}:[0-9]{2}[Z]"
        self.assertRegexpMatches(test_slot["start"], regex)
        self.assertRegexpMatches(test_slot["end"], regex)

    # Tests that different verbs return expected responses
    def test_verbs(self):
        query_params = {}

        self.assertEqual(query_request(extensions_url, access_token, "get", query_params).status_code, 200)

        self.assertEqual(query_request(extensions_url, access_token, "post", query_params).status_code, 405)
        self.assertEqual(query_request(extensions_url, access_token, "put", query_params).status_code, 405)
        self.assertEqual(query_request(extensions_url, access_token, "delete", query_params).status_code, 405)

    # Tests that a request with auth header returns a 401
    def test_unauth(self):
        self.assertEqual(unauth_request(extensions_url), 401)<commit_msg>Wrap failing assertions w/ a NoneType check<commit_after>import unittest
import sys
import json
import logging
from api_request import *
from configuration_load import *

class dw_tests(unittest.TestCase):

    def test_extension(self):
        offices = get_extension_offices(extensions_url, access_token)

        # check that we have extension locations
        self.assertGreater(len(offices["data"]), 10)

        for office in offices["data"]:
            self.assertIsNotNone(office["id"])
            self.assertEqual(office["type"], "locations")
            self.assertIsNotNone(office["attributes"]["name"])
            self.assertEqual(office["attributes"]["type"], "building")
            self.assertIsNotNone(office["attributes"]["county"])
            self.assertIsNotNone(office["attributes"]["zip"])
            self.assertIsNotNone(office["attributes"]["fax"])
            self.assertIsNotNone(office["attributes"]["website"])
            self.assertIsNotNone(office["attributes"]["website"])

            # Refactor into a separate method?
            self.assertTrue(self.check_lat_lon(office))

    def test_dining(self):
        invalid_dining_count = 0

        # check that we have extension locations
        self.assertGreater(len(restaurants["data"]), 10)

        for diner in restaurants["data"]:
            if not self.is_valid_dining(diner) or not self.has_valid_open_hours(diner):
                invalid_dining_count += 1

        logger.debug("invalid dining locations: " + str(invalid_dining_count))
        self.assertLessEqual(invalid_dining_count, max_invalid_dining)
        return

    def is_valid_dining(self, location):
        return location["attributes"]["name"] is not None and \
                location["attributes"]["summary"] is not None and \
                self.check_lat_lon(location)

    def check_lat_lon(self, location):
        return location["attributes"]["geoLocation"] is not None and \
                location["attributes"]["geoLocation"]["lat"] is not None and \
                location["attributes"]["geoLocation"]["lon"] is not None and \
                (type(location["attributes"]["geoLocation"]["lon"]) is float) and \
                (type(location["attributes"]["geoLocation"]["lat"]) is float)

    def has_valid_open_hours(self, location):
        valid_day_range = range(1,7)
        invalid_days = 0

        if "openHours" not in location["attributes"]:
            logger.debug("doesn't have hours " + str(location["attributes"]["name"]))
            return False

        for day, open_hour_list in location["attributes"]["openHours"].iteritems():
            valid_day_index = int(day) in valid_day_range
            invalid_time_slot_count = len([i for i in open_hour_list if not self.is_valid_open_slot(i)])
            logger.debug("day in range: " + str(valid_day_index))
            if invalid_time_slot_count > 1 or not valid_day_index:
                invalid_days += 1

        logger.debug("invalid days: " + str(invalid_days) + " check against: " + str(min_valid_days))
        return (7 - invalid_days) >= min_valid_days

    def is_valid_open_slot(self, open_slot):
        return open_slot is not None and open_slot["start"] is not None and open_slot["end"] is not None

    def testOpenHoursUTC(self):
        test_slot = None
        test_diners = [diner for diner in restaurants["data"] if self.has_valid_open_hours(diner)]
        open_hours = test_diners[0]["attributes"]["openHours"] 

        logger.debug("test utc with diner: " + test_diners[0]["attributes"]["name"])

        for day, open_hour_list in open_hours.iteritems():
            valid_slots = [i for i in open_hour_list if self.is_valid_open_slot(i)]
            if len(valid_slots) > 0:
                test_slot = valid_slots[0]
                logger.debug("test day slot day: " + str(day) + ", slot: " + str(test_slot))
                break

        regex = "[0-9]{4}-[0-9]{2}-[0-9]{2}[T][0-9]{2}:[0-9]{2}:[0-9]{2}[Z]"
        if test_slot is not None:
            self.assertRegexpMatches(test_slot["start"], regex)
            self.assertRegexpMatches(test_slot["end"], regex)

    # Tests that different verbs return expected responses
    def test_verbs(self):
        query_params = {}

        self.assertEqual(query_request(extensions_url, access_token, "get", query_params).status_code, 200)

        self.assertEqual(query_request(extensions_url, access_token, "post", query_params).status_code, 405)
        self.assertEqual(query_request(extensions_url, access_token, "put", query_params).status_code, 405)
        self.assertEqual(query_request(extensions_url, access_token, "delete", query_params).status_code, 405)

    # Tests that a request with auth header returns a 401
    def test_unauth(self):
        self.assertEqual(unauth_request(extensions_url), 401)
{'fraction_non_alphanumeric': 0.12032418952618454, 'fraction_numerical': 0.010806317539484621, 'mean_word_length': 3.107981220657277, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1446071', 'n_tokens_mistral': 2870, 'n_tokens_neox': 2781, 'n_words': 596}