Пример #1
0
def test_connection__should_pass_alias__if_provided(app, config_extension):
    """Make sure a simple connection pass ALIAS setting variable."""
    db = MongoEngine()
    app.config.update(config_extension)

    # Verify no extension for Mongoengine yet created for app
    assert app.extensions == {}
    assert current_mongoengine_instance() is None

    # Create db connection. Should return None.
    assert db.init_app(app) is None

    # Verify db added to Flask extensions.
    assert current_mongoengine_instance() == db

    # Verify db settings passed to pymongo driver.
    # ALIAS is used to find correct connection.
    # As we do not use default alias, default call to mongoengine.get_connection
    # should raise.
    with pytest.raises(ConnectionFailure):
        mongoengine.get_connection()

    connection = mongoengine.get_connection("simple_conn")
    mongo_engine_db = mongoengine.get_db("simple_conn")
    assert isinstance(mongo_engine_db, Database)
    assert isinstance(connection, MongoClient)
    assert mongo_engine_db.name == "flask_mongoengine_test_db"
    assert connection.HOST == "localhost"
    assert connection.PORT == 27017
Пример #2
0
 def check_connect(self):
     try:
         mongo.get_connection()
     except mongo.connection.MongoEngineConnectionError:
         self.conn = mongo.connect(
             host=
             f"mongodb+srv://{os.environ['un']}:{os.environ['pw']}@cluster0-hlox9.mongodb.net/test?retryWrites=true&w=majority"
         )
         self.user = False
    def test_create_location_list(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        BikeProcessedData(name='bike_info_location3').save()

        bike_stand_3 = BikeProcessedData.objects(
            name='bike_info_location3').first()

        bike_stand_3.data.append(
            BikeAvailabilityProcessedData(
                total_stands=40,
                in_use=10,
                day='2021-03-30 00:00:00')
        )
        bike_stand_3.save()
        # Call to orginal function[create_location_list]
        result = store_bike_data_processed.create_location_list()
        expected_result = [
            {
                'name': 'bike_info_location3',
                'in_use': [],
                'total': 40
            }
        ]
        assert expected_result == result
    def test_store_footfall_data_datebased(self):
        store_fn_datebased = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        mocked_input = {
            'Place_1': {
                '2021-03-20': 2,
                '2021-03-21': 7
            },
            'Place_2': {
                '2021-03-20': 3,
                '2021-03-21': 8
            },
            'Place_3': {
                '2021-03-20': 4,
                '2021-03-21': 9
            }
        }
        store_fn_datebased.calculate_average_footfall_date_based = MagicMock(
            return_value=mocked_input)
        self.test_store_footfall_locations()
        store_fn_datebased.store_footfall_data_datebased()
        fetch_footfall_date = FootfallDateBased.objects(
            location="Place_1").first()
        assert str(fetch_footfall_date.footfall_data[0].data_date
                   ) == '2021-03-20 00:00:00'
        assert fetch_footfall_date.footfall_data[0].count == 2
    def test_calculate_average_footfall_date_based(self):
        calculate_fn = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        data = [['20-03-2021 15:15:15', 1, 2, 3],
                ['20-03-2021 15:15:15', 4, 5, 6],
                ['21-03-2021 15:15:15', 7, 8, 9],
                ['21-03-2021 15:15:15', 7, 8, 9]]
        mocked_input1 = pd.DataFrame(
            data, columns=['Time', 'Place1', 'Place2', 'Place3'])
        calculate_fn.read_footfall = MagicMock(return_value=mocked_input1)

        result_date = calculate_fn.calculate_average_footfall_date_based()
        expected_result = {
            'Place1': {
                '2021-03-20': 2,
                '2021-03-21': 7
            },
            'Place2': {
                '2021-03-20': 3,
                '2021-03-21': 8
            },
            'Place3': {
                '2021-03-20': 4,
                '2021-03-21': 9
            }
        }
        assert result_date == expected_result
 def test_fetch_data_from_db_for_day(self):
     fetch_fn_datebased = StoreFootfallData()
     start = '2021-03-20'
     end = '2021-03-20'
     conn = get_connection()
     self.test_store_footfall_data_datebased()
     self.assertTrue(isinstance(conn, mm.MongoClient))
     fetch_fn_datebased.fetch_data_from_db_for_day(start, end)
     result_1 = expected_result = [{
         'location':
         'Place_1',
         'footfall_data': [{
             'data_date': FakeDatetime(2021, 3, 20, 0, 0),
             'count': 2
         }]
     }, {
         'location':
         'Place_2',
         'footfall_data': [{
             'data_date': FakeDatetime(2021, 3, 20, 0, 0),
             'count': 3
         }]
     }, {
         'location':
         'Place_3',
         'footfall_data': [{
             'data_date': FakeDatetime(2021, 3, 20, 0, 0),
             'count': 4
         }]
     }]
     assert expected_result == result_1
    def test_store_bikedata(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()
        # MongoDb test connection.
        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        mocked_result = [
            {
                "name": "info_1",
                "historical": [
                    {
                        "time": "2021-03-31T15:15:15Z",
                        "bike_stands": 26,
                        "available_bike_stands": 10
                    },
                    {
                        "time": "2021-03-31T15:15:15Z",
                        "bike_stands": 26,
                        "available_bike_stands": 30
                    }
                ]
            }
        ]
        store_bike_data_processed.fetch_bike_data = MagicMock(
            return_value=mocked_result)
        store_bike_data_processed.store_bikedata(1)
        fetch_bike_stand_2 = BikeProcessedData.objects(
            name="info_1").first()
        assert fetch_bike_stand_2 is not None
        assert len(fetch_bike_stand_2.data) == 1
        assert fetch_bike_stand_2.data[0].in_use == 20
        assert fetch_bike_stand_2.data[0].total_stands == 26
        assert str(
            fetch_bike_stand_2.data[0].day == "2021-03-31")
Пример #8
0
def connection(config):
    try:
        conn = mongoengine.get_connection()
    except mongoengine.connection.MongoEngineConnectionError:
        conn = mongoengine.connect(**config)
    yield conn
    conn_db = mongoengine.get_db()
    conn.drop_database(conn_db.name)
    def test_get_last_date(self):
        date_fn_overall = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        self.test_store_footfall_data_datebased()
        date = date_fn_overall.get_last_date('Place_1')
        print(date)
        assert str(date) == '2021-03-21'
    def test_fetch_footfall_overall(self):
        fetch_fn_overall = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        self.test_store_footfall_overall()
        result_all = fetch_fn_overall.fetch_footfall_overall()
        assert result_all[0]['location'] == 'Place_1'
        assert result_all[0]['count'] == 4
Пример #11
0
    def test_fetch_data_from_db_for_day(self):
        fetch_Parkings_from_database_day = StoreParkingsData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        self.test_save_parkings_data()
        date = "2021-03-14"
        startdate = datetime.strptime(date, "%Y-%m-%d")
        result = fetch_Parkings_from_database_day.fetch_data_from_db_for_day(
            startdate)
        assert str(result[0]["updateTimestamp"]) == "2021-03-14 08:34:13"
Пример #12
0
 def test_daterange(self):
     fetch_Parkings_date = StoreParkingsData()
     conn = get_connection()
     self.assertTrue(isinstance(conn, mm.MongoClient))
     startdate = "2021-03-14"
     startdate = datetime.strptime(startdate, "%Y-%m-%d")
     enddate = "2021-03-15"
     enddate = datetime.strptime(enddate, "%Y-%m-%d")
     result = fetch_Parkings_date.daterange(startdate, enddate)
     for items in result:
         if (str(items) != "2021-03-14 00:00:00"):
             assert str(items) == "2021-03-15 00:00:00"
    def test_parkings_availability(self):
        fetch_parkings_availability = FetchParkingsApi()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        ParkingsAvailability(updateTimestamp='2021-03-14 09:34:13').save()
        ParkingsAvailability(updateTimestamp='2021-03-13 08:34:13').save()

        Parkings_1 = ParkingsAvailability.objects(
            updateTimestamp='2021-03-14 09:34:13').first()
        Parkings_2 = ParkingsAvailability.objects(
            updateTimestamp='2021-03-13 08:34:13').first()

        Parkings_1.parkings.append(
            ParkingAvailability(area="NORTHWEST",
                                name="PARNELL",
                                availableSpaces=50))
        Parkings_1.save()

        Parkings_2.parkings.append(
            ParkingAvailability(area="NORTHWEST",
                                name="PARNELL",
                                availableSpaces=60))
        Parkings_2.save()
        startdate = "2021-03-12"
        enddate = "2021-03-15"
        mocked_result = [{
            '_id': {
                '$oid': None
            },
            'updateTimestamp': {
                '$date': FakeDatetime(2021, 3, 13, 0, 0)
            },
            'parkings': {
                'PARNELL': 60
            }
        }, {
            '_id': {
                '$oid': None
            },
            'updateTimestamp': {
                '$date': FakeDatetime(2021, 3, 14, 0, 0)
            },
            'parkings': {
                'PARNELL': 50
            }
        }]

        result = fetch_parkings_availability.parkings_availability(
            startdate, enddate)

        assert result == mocked_result
    def test_store_footfall_locations(self):
        store_fn_location = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        mocked_input = {'Place_1': 4, 'Place_2': 5, 'Place_3': 6}
        store_fn_location.calculate_average_footfall_overall = MagicMock(
            return_value=mocked_input)

        store_fn_location.store_footfall_locations()
        fetch_footfall_location = FootfallDateBased.objects().first()
        assert fetch_footfall_location["location"] == 'Place_1'
def connect_to_mongodb():
    db_username = '******'
    db_password = '******'
    db_presets = 'mongodb+srv://'
    db_host = '@cluster0-hlb5v.mongodb.net/'
    db_name = 'test'#armbims'
    db_config = '?retryWrites=true&w=majority'
    db_conn_string = db_presets + db_username + ':' + db_password + db_host + db_name + db_config
    try:
        me.connect(host=db_conn_string)
        db_client = me.get_connection()
        return db_client
    except me.MongoEngineConnectionError:
        print("The connection to mondoBD raised an exception")
    def test_calculate_average_footfall_overall(self):
        calculate_fn = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        mocked_input = pd.DataFrame(
            np.array([[0, 1, 2, 3], [0, 4, 5, 6], [0, 7, 8, 9]]),
            columns=['Time', 'Place_1', 'Place_2', 'Place_3'])
        calculate_fn.read_footfall = MagicMock(return_value=mocked_input)

        result = calculate_fn.calculate_average_footfall_overall()

        expected_result = {'Place_1': 4, 'Place_2': 5, 'Place_3': 6}
        assert result == expected_result
Пример #17
0
def test_connection__should_accept_host_as_list(app):
    """Make sure MONGODB_HOST can be a list hosts."""
    db = MongoEngine()
    app.config["MONGODB_SETTINGS"] = {
        "ALIAS": "host_list",
        "HOST": ["localhost:27017"],
        "DB": "flask_mongoengine_list_test_db",
    }
    db.init_app(app)

    connection = mongoengine.get_connection("host_list")
    mongo_engine_db = mongoengine.get_db("host_list")
    assert isinstance(mongo_engine_db, Database)
    assert isinstance(connection, MongoClient)
    assert mongo_engine_db.name == "flask_mongoengine_list_test_db"
    assert connection.HOST == "localhost"
    assert connection.PORT == 27017
Пример #18
0
def test_ingnored_mongodb_prefix_config(app):
    """Config starting by MONGODB_ but not used by flask-mongoengine
    should be ignored.
    """
    db = MongoEngine()
    app.config[
        "MONGODB_HOST"
    ] = "mongodb://localhost:27017/flask_mongoengine_test_db_prod"
    # Invalid host, should trigger exception if used
    app.config["MONGODB_TEST_HOST"] = "dummy://localhost:27017/test"
    db.init_app(app)

    connection = mongoengine.get_connection()
    mongo_engine_db = mongoengine.get_db()
    assert isinstance(mongo_engine_db, Database)
    assert isinstance(connection, MongoClient)
    assert mongo_engine_db.name == "flask_mongoengine_test_db_prod"
    assert connection.HOST == "localhost"
    assert connection.PORT == 27017
    def test_fetch_processed_data(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        # Call to orginal function [fetch_processed_data]
        result = store_bike_data_processed.fetch_processed_data(2)
        expected_result = [
            {
                'data': [
                    {
                        'total_stands': 40,
                        'in_use': 10,
                        'day': FakeDatetime(2021, 3, 30, 0, 0)
                    }
                ],
                'name': 'bike_info_location3'
            }
        ]
        assert expected_result == result
    def test_fetch_data_from_db_with_prediction(self):
        predict_fn_overall = StoreFootfallData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        self.test_store_footfall_data_datebased()
        days_interval = 2
        reqd_location = 'Place_1'
        result, date = predict_fn_overall.fetch_data_from_db_with_prediction(
            days_interval, reqd_location)
        assert result == [{
            'location':
            'Place_1',
            'footfall_data': [{
                'data_date': FakeDatetime(2021, 3, 20, 0, 0),
                'count': 2
            }, {
                'data_date': FakeDatetime(2021, 3, 21, 0, 0),
                'count': 7
            }]
        }]
        assert str(date) == '2021-03-21 00:00:00'
    def test_store_bikedata_all_locations(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))

        BikeProcessedData(name='bike_info_location1').save()
        BikeProcessedData(name='bike_info_location2').save()

        bike_stand_1 = BikeProcessedData.objects(
            name='bike_info_location1').first()
        bike_stand_2 = BikeProcessedData.objects(
            name='bike_info_location2').first()

        bike_stand_1.data.append(
            BikeAvailabilityProcessedData(
                total_stands=40,
                in_use=10,
                day='2021-03-31 13:15:15')
        )
        bike_stand_1.save()

        bike_stand_2.data.append(
            BikeAvailabilityProcessedData(
                total_stands=32,
                in_use=30,
                day='2021-03-15 15:15:15')
        )
        bike_stand_2.save()
        # Call to orginal function[store_bikedata_all_locations]
        store_bike_data_processed.store_bikedata_all_locations(1)
        fetch_bike_stand_2 = BikeProcessedData.objects(
            name="ALL_LOCATIONS").first()
        assert fetch_bike_stand_2 is not None
        assert len(fetch_bike_stand_2.data) == 1
        assert fetch_bike_stand_2.data[0].in_use == 30
        assert fetch_bike_stand_2.data[0].total_stands == 66
        assert str(
            fetch_bike_stand_2.data[0].day == "2021-03-31")
Пример #22
0
    def test_save_parkings_data(self):
        store_Parkings_to_database = StoreParkingsData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        Mocked_result = """<?xml version="1.0" encoding="ISO-8859-1"?>
        <carparkData>
            <Northwest>
            <carpark name="PARNELL" spaces="302"> </carpark>
            </Northwest>
        <Timestamp>08:34:13 on Wednesday 14/03/2021</Timestamp>
        </carparkData>"""
        Mocked_result = ET.fromstring(Mocked_result)
        store_Parkings_to_database.get_parkings_spaces_availability_live = MagicMock(
            return_value=Mocked_result)
        store_Parkings_to_database.store_parking_spaces_availability_live()

        fetch_parkings_data = ParkingsAvailability.objects(
            updateTimestamp="08:34:13 on Wednesday 14/03/2021").first()
        assert fetch_parkings_data.parkings[0]["area"] == "NORTHWEST"
        assert fetch_parkings_data.parkings[0]["name"] == "PARNELL"
        assert fetch_parkings_data.parkings[0]["availableSpaces"] == 302
Пример #23
0
def test_connection__should_use_defaults__if_no_settings_provided(app):
    """Make sure a simple connection to a standalone MongoDB works."""
    db = MongoEngine()

    # Verify no extension for Mongoengine yet created for app
    assert app.extensions == {}
    assert current_mongoengine_instance() is None

    # Create db connection. Should return None.
    assert db.init_app(app) is None

    # Verify db added to Flask extensions.
    assert current_mongoengine_instance() == db

    # Verify db settings passed to pymongo driver.
    # Default mongoengine db is 'default', default Flask-Mongoengine db is 'test'.
    connection = mongoengine.get_connection()
    mongo_engine_db = mongoengine.get_db()
    assert isinstance(mongo_engine_db, Database)
    assert isinstance(connection, MongoClient)
    assert mongo_engine_db.name == "test"
    assert connection.HOST == "localhost"
    assert connection.PORT == 27017
    def test_fetch_predicted_data(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        BikePredictedData(name='bike_predict1').save()
        day_ahead = "2021-04-1 15:15:15"

        bike_predict_1 = BikePredictedData.objects(
            name='bike_predict1').first()

        bike_predict_1.data.append(
            BikeAvailabilityPredictedData(
                total_stands=40,
                in_use=10,
                day=FakeDatetime(2021, 4, 1, 0, 0))
        )
        bike_predict_1.save()
        # call to orginal function.[fetch predicted_data]
        result = store_bike_data_processed.fetch_predicted_data(day_ahead)
        expected_result = {'total_stands': 40, 'in_use': 10,
                           'day': FakeDatetime(2021, 4, 1, 0, 0)}
        assert result[0]['data'] == expected_result
Пример #25
0
    def test_fetch_data_from_db_historical(self):
        fetch_Parkings_from_database_hist = StoreParkingsData()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        self.test_save_parkings_data()
        ParkingsAvailability(updateTimestamp='2021-03-14 09:34:13').save()
        ParkingsAvailability(updateTimestamp='2021-03-13 08:34:13').save()

        Parkings_1 = ParkingsAvailability.objects(
            updateTimestamp='2021-03-14 09:34:13').first()
        Parkings_2 = ParkingsAvailability.objects(
            updateTimestamp='2021-03-13 08:34:13').first()

        Parkings_1.parkings.append(
            ParkingAvailability(
                area="NORTHWEST",
                name="PARNELL",
                availableSpaces=50)
        )
        Parkings_1.save()

        Parkings_2.parkings.append(
            ParkingAvailability(
                area="NORTHWEST",
                name="PARNELL",
                availableSpaces=60)
        )
        Parkings_2.save()
        startdate = "2021-03-12"
        startdate = datetime.strptime(startdate, "%Y-%m-%d")
        enddate = "2021-03-15"
        enddate = datetime.strptime(enddate, "%Y-%m-%d")
        result = fetch_Parkings_from_database_hist.fetch_data_from_db_historical(
            startdate, enddate)
        assert result[0]["parkings"]["PARNELL"] == 60
        assert result[1]["parkings"]["PARNELL"] == 176
Пример #26
0
def test_connection__should_parse_host_uri__if_host_formatted_as_uri(
    app, config_extension
):
    """Make sure a simple connection pass ALIAS setting variable."""
    db = MongoEngine()
    app.config.update(config_extension)

    # Verify no extension for Mongoengine yet created for app
    assert app.extensions == {}
    assert current_mongoengine_instance() is None

    # Create db connection. Should return None.
    assert db.init_app(app) is None

    # Verify db added to Flask extensions.
    assert current_mongoengine_instance() == db

    connection = mongoengine.get_connection()
    mongo_engine_db = mongoengine.get_db()
    assert isinstance(mongo_engine_db, Database)
    assert isinstance(connection, MongoClient)
    assert mongo_engine_db.name == "flask_mongoengine_test_db"
    assert connection.HOST == "localhost"
    assert connection.PORT == 27017
    def test_store_predict_data_in_db(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        mocked_result = [
            {
                'name': 'bike_info_location3',
                'in_use': [10, 20, 30, 15],
                'total': 40
            }
        ]
        store_bike_data_processed.get_in_use_arr = MagicMock(
            return_value=mocked_result)
        # Call to orginal function [store_predict_data_in_db()]
        store_bike_data_processed.store_predict_data_in_db(1)
        fetch_bike_stand_2 = BikePredictedData.objects(
            name="bike_info_location3").first()
        assert fetch_bike_stand_2 is not None
        assert len(fetch_bike_stand_2.data) == 1
        assert fetch_bike_stand_2.data[0].in_use == 23
        assert fetch_bike_stand_2.data[0].total_stands == 40
        assert str(
            fetch_bike_stand_2.data[0].day == "2021-04-01")
    def test_get_in_use_arr(self):
        store_bike_data_processed = StoreProcessedBikeDataToDB()

        conn = get_connection()
        self.assertTrue(isinstance(conn, mm.MongoClient))
        mocked_result = [
            {
                'name': 'bike_info_location3',
                'in_use': [],
                'total': 40
            }
        ]
        store_bike_data_processed.create_location_list = MagicMock(
            return_value=mocked_result)
        # Call to orginal function[get_in_use_arr]
        result = store_bike_data_processed.get_in_use_arr(2)
        expected_result = [
            {
                'name': 'bike_info_location3',
                'in_use': [10],
                'total': 40
            }
        ]
        assert expected_result == result
Пример #29
0
 def setUp(self):
     connect(alias='testdb')
     conn = get_connection()
 def tearDown(self):
     conn = get_connection()
     self.assertTrue(isinstance(conn, mm.MongoClient))
     FootfallDateBased.objects().delete()
     FootfallOverall.objects().delete()