Exemplo n.º 1
0
    def test_camera_analyze(self):
        # instantiate the SUT and dependencies
        reader = EndpointReader("http://domain.com/camera", 10)
        poller = RangePoller(reader)
        sut = Summarizer(poller)

        cam_detail = {
            "camera_id":
            2,
            "images": [{
                "file_size": 1565
            }, {
                "file_size": 2802
            }, {
                "file_size": 18
            }, {
                "file_size": 12
            }]
        }
        result = sut.camera_analyze(cam_detail)

        expected = {
            'camera_id': 2,
            'totalbytes': 4397,
            'imagecount': 4,
            'largestimage': 2802
        }
        self.assertEqual(expected, result)
Exemplo n.º 2
0
def poll_and_summarize():
    args = parser.parse_args()
    """ json format: {"camera_id": 10, "images": [{"file_size": 5635}, {"file_size": 8022}, {"file_size": 7632}]} """
    """ reading tested with "http://www.mocky.io/v2/5b32d8103400002e343fd4fa """
    reader = EndpointReader("http://domain.com/camera", args.timeout)
    """ by default, the real requests.get is being called """
    """ http get calls can be replaced with a fake (random json values in struct) for internal testing """
    if args.simulate:
        reader.enable_fake_get()

    poller = RangePoller(reader)
    poller.poll(args.cameras)

    summarizer = Summarizer(poller)
    summarizer.compile()
    summarizer.print_stats()
Exemplo n.º 3
0
    def test_get_valid_url_retrieves_data(self, mock_get):
        # instantiate the SUT
        sut = EndpointReader("http://domain.com/camera", 10)

        expected = {
            "camera_id":
            1,
            "images": [{
                "file_size": 5635
            }, {
                "file_size": 8022
            }, {
                "file_size": 7632
            }]
        }
        json_data = sut.get(1)
        # test that the mocked requests.get was called
        self.assertTrue(mock_get.called, "Failed to call requests.get.")
        # test that we received the expected data
        self.assertEqual(expected, json_data)
Exemplo n.º 4
0
    def test_get_camera_returns_None_upon_Timeout_in_get(self, mock_get):
        # instantiate the SUT and dependencies
        reader = EndpointReader("http://domain.com/camera", 10)
        sut = RangePoller(reader)

        # this will generate a Timeout exception in the reader
        result = sut.get_camera(992)

        # test that the mocked requests.get was called
        self.assertTrue(mock_get.called, "Failed to call requests.get.")

        # test that returned result was None
        self.assertIsNone(result)
Exemplo n.º 5
0
    def test_range_poll_retrieves_3_valid_cams(self, mock_get):
        # instantiate the SUT and dependencies
        reader = EndpointReader("http://domain.com/camera", 10)
        sut = RangePoller(reader)

        sut.poll([1, 2, 3, 991, 992])

        # test that the mocked requests.get was called
        self.assertTrue(mock_get.called, "Failed to call requests.get.")

        # test that we have retrieved json data from the 3 valid cameras
        # while the other 2 that timed out did not affect the process
        self.assertEqual(len(sut.cameras), 3)
Exemplo n.º 6
0
    def test_compile_results(self, mock_get):
        # instantiate the SUT and dependencies
        reader = EndpointReader("http://domain.com/camera", 10)
        poller = RangePoller(reader)
        sut = Summarizer(poller)

        poller.poll([1, 2, 3])
        sut.compile()

        self.assertEqual(sut.cams_by_space.data["camera_id"], 1)
        self.assertEqual(sut.cams_by_imagecount.data["camera_id"], 1)
        self.assertEqual(sut.cams_by_largestimage.data["camera_id"], 1)

        self.assertEqual(sut.cams_by_space.lowest()["camera_id"], 2)
        self.assertEqual(sut.cams_by_imagecount.lowest()["camera_id"], 1)
        self.assertEqual(sut.cams_by_largestimage.lowest()["camera_id"], 2)

        self.assertEqual(sut.cams_by_space.highest()["camera_id"], 1)
        self.assertEqual(sut.cams_by_imagecount.highest()["camera_id"], 2)
        self.assertEqual(sut.cams_by_largestimage.highest()["camera_id"], 3)
Exemplo n.º 7
0
 def test_get_timeout_is_handled_gracefully(self, mock_get):
     # instantiate the SUT
     sut = EndpointReader("http://domain.com/camera", 10)
     self.assertRaises(requests.exceptions.Timeout, sut.get, 992)
Exemplo n.º 8
0
 def test_get_connectionerror_is_handled_gracefully(self, mock_get):
     # instantiate the SUT
     sut = EndpointReader("http://domain.com/camera", 10)
     self.assertRaises(requests.exceptions.ConnectionError, sut.get, 991)