Пример #1
0
 def test_get_devices_with_data(self, testclient, fakedevice, fake_ts_data):
     device = model_to_dict(fakedevice)
     # Add latest sensor data to expected results
     sensor_data = model_to_dict(fake_ts_data[0])
     # 'id' and 'sensor' is not returned in serializer
     del sensor_data["id"]
     del sensor_data["sensor"]
     device.update({"sensors_current": {"power_sensor": sensor_data}})
     expected = {
         "status_code": 200,
         "body": paginated_body([device]),
     }
     testclient.get_request_test_helper(expected,
                                        expect_identical_values=False)
    def test_put_requires_all_fields(self, testclient, fakelocation):
        """This test a bit superfluous, just make sure a 'put' requires all fields"""
        path_params = {
            "location_id": fakelocation.id
        }

        # As expected
        expected = {
            "status_code": 200,
            "body": model_to_dict(fakelocation)
        }
        testclient.get_request_test_helper(expected, path_params=path_params)

        # Change country
        after_update_location = model_to_dict(fakelocation)
        after_update_location["country"] = "Bolivia"
        after_update_location.update({"updated_at": None})

        post_body = {
            "country": "Bolivia",
        }
        expected = {
            "status_code": 400,
            "body": {
                "region": [
                    "This field is required."
                ],
                "postalcode": [
                    "This field is required."
                ]

            }
        }
        testclient.put_request_test_helper(post_body, expected, path_params=path_params)

        # Post with all should work
        post_body = after_update_location
        expected = {
            "status_code": 200,
            "body": after_update_location
        }
        testclient.put_request_test_helper(post_body, expected, path_params=path_params)

        # Expect it to change
        expected = {
            "status_code": 200,
            "body": after_update_location
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
    def test_get_multiple_locations(self, testclient, fakelocation):
        """Get all locations

        This should work just because the second location was added after
        fakelocation and there's no ordering. If ordering is applied in future
        this just needs changing so the order is correct.
        """
        other_location = LocationFactory(locality="Secret lab")
        expected = {
            "status_code": 200,
            "body": paginated_body([
                model_to_dict(fakelocation),
                model_to_dict(other_location),
            ]),
        }
        testclient.get_request_test_helper(expected)
Пример #4
0
    def test_get_time_series_data(self, testclient, fakedevice, fake_ts_data):
        now = datetime.datetime.now()
        path_params = {
            "device_id": fakedevice.id,
        }
        resolution = 120
        num_readings = 3
        query_params = {
            "start": str(int((now - datetime.timedelta(seconds=resolution*num_readings))
                             .timestamp()*1000)),
            "end": str(int(now.timestamp()*1000)),
            "resolution":str(resolution),
        }

        power_sensor_data = [model_to_dict(fake_ts_data[i]) for i in range(num_readings)]
        for i, _ in enumerate(power_sensor_data):
            for key in ('sensor', "id"):
                del power_sensor_data[i][key]

        expected = {
            "status_code": 200,
            "body": {
                "power_sensor": power_sensor_data,
            }
        }

        testclient.get_request_test_helper(expected,
                                           path_params=path_params,
                                           query_params=query_params)
    def test_create_multiple(self, testclient, adminuser, joeseed):
        """Can't create multiple fields at once

        This might change if the serializer is set to accept it...?
        """
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict["timezone"] = ""

        post_body = location_as_dict.copy()

        post_body = [
            post_body,
            post_body,
        ]

        expected = {
            "status_code": 400,
            "body": {
                "non_field_errors": [
                    "Invalid data. Expected a dictionary, but got list."
                ]
            }
        }
        testclient.post_request_test_helper(post_body, expected)
 def test_get_locations(self, testclient, fakelocation):
     """Get expected location"""
     expected = {
         "status_code": 200,
         "body": paginated_body([model_to_dict(fakelocation)]),
     }
     testclient.get_request_test_helper(expected)
Пример #7
0
def membership_to_dict(membership, user):
    as_dict = model_to_dict(membership)

    del as_dict["modified"]
    del as_dict["organization"]

    as_dict["user"] = {
        "id": user.id,
        "email": user.email,
    }

    return as_dict
    def test_get_specific_location(self, testclient, fakelocation):
        other_location = LocationFactory(locality="Secret lab")

        # First one, shouldn't return the other one
        expected = {
            "status_code": 200,
            "body": model_to_dict(fakelocation),
        }
        path_params = {
            "location_id": fakelocation.id
        }
        testclient.get_request_test_helper(expected, path_params=path_params)

        # And the other one
        expected = {
            "status_code": 200,
            "body": model_to_dict(other_location),
        }
        path_params = {
            "location_id": other_location.id
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
Пример #9
0
    def test_get_org_no_logo(self, testclient, fake_org):
        """Get a single organization without a logo"""

        dumped = model_to_dict(fake_org)
        dumped["logo"] = None
        del dumped["created"]
        del dumped["modified"]
        expected = {
            "status_code": 200,
            "body": dumped
        }
        params = {"org_id": fake_org.id}
        testclient.get_request_test_helper(expected, path_params=params)
Пример #10
0
    def test_get_one(self, testclient, fake_org):
        """Get a single organization in a paginated body"""

        dumped = model_to_dict(fake_org)
        dumped["logo"] = None
        del dumped["created"]
        del dumped["modified"]
        expected = {
            "status_code": 200,
            "body": paginated_body([dumped])
        }

        testclient.get_request_test_helper(expected)
    def test_create_new_location(self, testclient, adminuser, joeseed):
        """Create a new location and return it - timezone should be
        automatically updated if an empty one is passed"""
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict.update({"created_at": None, "updated_at": None})

        post_body = location_as_dict.copy()
        post_body["timezone"] = ""

        expected = {
            "status_code": 201,
            "body": location_as_dict,
        }
        testclient.post_request_test_helper(post_body, expected)
    def test_unauthenticated_post_failure(self, testclient, joeseed):
        """attempt to create new location without logging in"""
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict["timezone"] = ""

        post_body = location_as_dict.copy()

        expected = {
            "status_code": 401,
            "body": {
                "detail": "Authentication credentials were not provided."
            }
        }
        testclient.post_request_test_helper(post_body, expected)
    def test_create_invalid_latlong(self, testclient, joeseed, latitude, as_str):
        """Invalid lat/long fails"""
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict["timezone"] = ""

        post_body = location_as_dict.copy()
        post_body["latitude"] = latitude

        expected = {
            "status_code": 400,
            "body": {
                "detail": "JSON parse error - Out of range float values are not JSON compliant: '{}'".format(as_str)
            }
        }
        testclient.post_request_test_helper(post_body, expected)
    def test_create_missing_fields(self, testclient, joeseed):
        """Missing field -> error"""
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict["timezone"] = ""

        post_body = location_as_dict.copy()
        del post_body["country"]

        expected = {
            "status_code": 400,
            "body": {
                "country": [
                    "This field is required."
                ]
            }
        }
        testclient.post_request_test_helper(post_body, expected)
Пример #15
0
    def test_get_device(self, testclient, fakedevice, fake_ts_data, joeseed):
        device = device_to_dict(fakedevice)
        # Add latest sensor data to expected results
        sensor_data = model_to_dict(fake_ts_data[0])
        del sensor_data["id"]
        del sensor_data["sensor"]

        device.update({
            "sensors_current": {
                "power_sensor": sensor_data
            },
        })
        expected = {
            "status_code": 200,
            "body": device,
        }

        path_params = {
            "device_id": fakedevice.id,
        }
        testclient.get_request_test_helper(expected, path_params=path_params)
    def test_create_bad_latlong(self, testclient, joeseed, latitude, longitude, which_bad):
        """Invalid lat/long fails"""
        new_location = LocationFactory()
        new_location.delete()

        location_as_dict = model_to_dict(new_location)
        location_as_dict["timezone"] = ""

        post_body = location_as_dict.copy()
        post_body["latitude"] = latitude
        post_body["longitude"] = longitude

        expected = {
            "status_code": 400,
            "body": {
                which_bad: None,
                # Ideally would do this, but would result in lots of copied code
                # "latitude": [
                #     "Ensure this value is less than or equal to 90.0."
                # ]
            }
        }
        testclient.post_request_test_helper(post_body, expected)
Пример #17
0
def expect_model(status, model, serializer=None):
    return {
        "status_code": status,
        "body": serializer(model).data if serializer else model_to_dict(model)
    }