Exemplo n.º 1
0
def setup_db_department():
    """Create the database for Department table testing.

    Args:
        None

    Returns:
        None

    """
    # Initialize key variables
    idx_department = 1

    # Create a dict of all the expected values
    expected = {
        'enabled': 1,
        'name': general.hashstring(general.randomstring()),
        'idx_department': idx_department,
        'code': general.hashstring(general.randomstring()),
    }

    # Drop the database and create tables
    initialize_db()

    # Insert data into database
    data = Department(code=general.encode(expected['code']),
                      name=general.encode(expected['name']))
    database = db.Database()
    database.add_all([data], 1048)

    # Return
    return expected
Exemplo n.º 2
0
def _login_rider_data(data):
    """ Check rider data against database.

    Args:
        data: Data dictionary to post

    Returns:
        Text response if Received
    """

    password = general.encode(data['password'])
    email = general.encode(data['email'])

    database = Database()
    session = database.session()

    # check if email exists
    result = session.query(Riders).filter(
        Riders.email == email)

    if result.count() == 1:
        # User exists
        return True
    elif result.count() == 0:
        # User doesnt exist
        return False
Exemplo n.º 3
0
def setup_db_deviceagent(data, initialize=True):
    """Create the database for DeviceAgent table testing.

    Args:
        None

    Returns:
        result: Tuple of (idx_device, idx_agent)

    """
    # Initialize key variables
    devicename = data['devicename']
    id_agent = data['id_agent']
    agent_name = data['agent']
    last_timestamp = data['timestamp']

    # Initialize database if requested
    if initialize is True:
        # Drop the database and create tables
        initialize_db()

    # Add record to the database
    record = Agent(id_agent=general.encode(id_agent),
                   name=general.encode(agent_name))
    database = db.Database()
    database.add(record, 1031)

    # Get idx_agent value from database
    agent_info = db_agent.GetIDAgent(id_agent)
    idx_agent = agent_info.idx_agent()

    # Add record to the database
    dev_record = Device(devicename=general.encode(devicename))
    database = db.Database()
    database.add(dev_record, 1034)

    # Get idx of newly added device
    device_info = db_device.GetDevice(devicename)
    idx_device = device_info.idx_device()

    # Update DeviceAgent table
    if hagent.device_agent_exists(idx_device, idx_agent) is False:
        # Add to DeviceAgent table
        da_record = DeviceAgent(idx_device=idx_device, idx_agent=idx_agent)
        database = db.Database()
        database.add(da_record, 1020)

    # Update DeviceAgent table with timestamp
    database = db.Database()
    session = database.session()
    record = session.query(DeviceAgent).filter(
        and_(DeviceAgent.idx_device == idx_device,
             DeviceAgent.idx_agent == idx_agent)).one()
    record.last_timestamp = last_timestamp
    database.commit(session, 1042)

    # Return
    result = (idx_device, idx_agent)
    return result
Exemplo n.º 4
0
def _register_rider_data(data):
    """ Post rider data to mdl database.

    Args:
        data: Data dictionary to post

    Returns:
        Text response if Received
    """

    firstName = general.encode(data['firstName'])
    lastName = general.encode(data['lastName'])
    password = general.encode(data['password'])
    email = general.encode(data['email'])
    phone = general.encode(data['phone'])

    database = Database()
    session = database.session()

    # create Riders object
    record = Riders(
        first_name=firstName,
        last_name=lastName,
        password=password,
        email=email,
        enabled=0
    )
    # check if email exists
    result = session.query(Riders).filter(
        Riders.email == email)

    if result.count() == 1:
        # User exists
        return False
    elif result.count() == 0:
        # User doesnt exist
        database.add(record, 1008)
        database.close()
        return True
Exemplo n.º 5
0
def setup_db_device():
    """Create the database for Device table testing.

    Args:
        None

    Returns:
        None

    """
    # Initialize key variables
    idx_device = 1

    # Create a dict of all the expected values
    expected = {
        'devicename': general.hashstring(general.randomstring()),
        'description': general.hashstring(general.randomstring()),
        'ip_address': general.hashstring('100.100.100.100'),
        'idx_device': idx_device,
        'exists': True,
        'enabled': 1
    }

    # Drop the database and create tables
    initialize_db()

    # Insert data into database
    data = Device(description=general.encode(expected['description']),
                  devicename=general.encode(expected['devicename']),
                  ip_address=general.encode(expected['ip_address']),
                  enabled=expected['enabled'])
    database = db.Database()
    database.add_all([data], 1019)

    # Return
    return expected
Exemplo n.º 6
0
def setup_db_agent():
    """Create the database for Agent table testing.

    Args:
        None

    Returns:
        None

    """
    # Initialize key variables
    idx_agent = 1

    # Get an agent ID
    id_agent = general.hashstring('_MDL_TEST_')

    # Create a dict of all the expected values
    expected = {
        'id_agent': id_agent,
        'name': general.hashstring(general.randomstring()),
        'idx_agent': idx_agent,
        'enabled': 1
    }

    # Drop the database and create tables
    initialize_db()

    # Insert data into database
    data = Agent(id_agent=general.encode(expected['id_agent']),
                 name=general.encode(expected['name']),
                 enabled=expected['enabled'])
    database = db.Database()
    database.add_all([data], 1045)

    # Return
    return (id_agent, expected)
Exemplo n.º 7
0
    def _add_records(self):
        """Add initial records to database tables.

        Args:
            None

        Returns:
            None

        """
        # Insert GeoCountries
        if db_geocountries.idx_geocountry_exists(1) is False:
            record = GeoCountries(geocountry_name=general.encode(
                self.reserved),
                                  enabled=0)
            database = db.Database()
            database.add(record, 1046)

        # Insert GeoCities
        if db_geocities.idx_geocity_exists(1) is False:
            record = GeoCities(geocity_name=general.encode(self.reserved),
                               enabled=0)
            database = db.Database()
            database.add(record, 1051)

        # Insert Addresses
        if db_addresses.idx_address_exists(1) is False:
            record = Addresses(enabled=0)
            database = db.Database()
            database.add(record, 1054)

        # Insert BillAddresses
        if db_billaddresses.idx_billaddress_exists(1) is False:
            record = BillAddresses(enabled=0)
            database = db.Database()
            database.add(record, 1053)

        # Insert DeviceModel
        if db_devicemodels.idx_devicemodel_exists(1) is False:
            record = DeviceModels(model_name=general.encode(self.reserved),
                                  enabled=0)
            database = db.Database()
            database.add(record, 1033)

        # Insert VehicleModel
        if db_vehiclemodels.idx_vehiclemodel_exists(1) is False:
            record = VehicleModels(model_name=general.encode(self.reserved),
                                   enabled=0)
            database = db.Database()
            database.add(record, 1001)

        # Insert Vehicle
        if db_vehicles.idx_vehicle_exists(1) is False:
            record = Vehicles(license_plate=general.encode(self.reserved),
                              enabled=0)
            database = db.Database()
            database.add(record, 1024)

        # Insert DriverCompany
        if db_drivercompanies.idx_drivercompany_exists(1) is False:
            record = DriverCompanies(drivercompany_name=general.encode(
                self.reserved),
                                     enabled=0)
            database = db.Database()
            database.add(record, 1004)

        # Insert Driver
        if db_drivers.idx_driver_exists(1) is False:
            record = Drivers(first_name=general.encode(self.reserved),
                             last_name=general.encode(self.reserved),
                             password=general.encode(self.reserved),
                             enabled=0)
            database = db.Database()
            database.add(record, 1037)

        # Insert RiderDevice
        if db_riderdevices.idx_riderdevice_exists(1) is False:
            record = RiderDevices(id_riderdevice=general.encode(self.reserved),
                                  serial_riderdevice=general.encode(
                                      self.reserved),
                                  enabled=0)
            database = db.Database()
            database.add(record, 1006)

        # Insert DriverDevice
        if db_driverdevices.idx_driverdevice_exists(1) is False:
            record = DriverDevices(
                id_driverdevice=general.encode(self.reserved),
                serial_driverdevice=general.encode(self.reserved),
                enabled=0)
            database = db.Database()
            database.add(record, 1008)

        # Insert VehicleCategory
        if db_vehiclecategories.idx_vehiclecategory_exists(1) is False:
            record = VehicleCategories(vehiclecategory_name=general.encode(
                self.reserved),
                                       enabled=0)
            database = db.Database()
            database.add(record, 1041)

        # Insert DriverCompanyRatings
        if db_companyratings.idx_drivercompanyrating_exists(1) is False:
            record = DriverCompanyRatings(enabled=0)
            database = db.Database()
            database.add(record, 1011)

        # Insert VehicleRatings
        if db_vehicleratings.idx_vehiclerating_exists(1) is False:
            record = VehicleRatings(enabled=0)
            database = db.Database()
            database.add(record, 1012)

        # Insert DriverRatings
        if db_driverratings.idx_driverrating_exists(1) is False:
            record = DriverRatings(enabled=0)
            database = db.Database()
            database.add(record, 1044)

        # Insert Agents
        if db_agents.idx_agent_exists(1) is False:
            record = Agents(id_agent=general.encode(self.reserved), enabled=0)
            database = db.Database()
            database.add(record, 1047)

        # Insert RiderDeviceAgents
        if db_riderdeviceagents.idx_riderdeviceagent_exists(1) is False:
            record = RiderDeviceAgents(enabled=0)
            database = db.Database()
            database.add(record, 1029)

        # Insert DriverDeviceAgents
        if db_driverdeviceagents.idx_driverdeviceagent_exists(1) is False:
            record = DriverDeviceAgents(enabled=0)
            database = db.Database()
            database.add(record, 1070)
Exemplo n.º 8
0
    def _add_records(self):
        """Add records.

        Args:
            None

        Returns:
            None

        """
        # Insert CreditCard
        if db_creditcards.idx_creditcard_exists(1) is False:
            record = CreditCards(enabled=0)
            database = db.Database()
            database.add(record, 1026)

        # Insert DeviceMake
        if db_devicemakes.idx_devicemake_exists(1) is False:
            record = DeviceMakes(make_name=general.encode(self.reserved),
                                 enabled=0)
            database = db.Database()
            database.add(record, 1027)

        # Insert Route
        if db_routes.idx_route_exists(1) is False:
            record = Routes(route_name=general.encode(self.reserved),
                            enabled=0)
            database = db.Database()
            database.add(record, 1002)

        # Insert Rider
        if db_riders.idx_rider_exists(1) is False:
            record = Riders(first_name=general.encode(self.reserved),
                            last_name=general.encode(self.reserved),
                            password=general.encode(self.reserved),
                            enabled=0)
            database = db.Database()
            database.add(record, 1003)

        # Insert VehicleMake
        if db_vehiclemakes.idx_vehiclemake_exists(1) is False:
            record = VehicleMakes(make_name=general.encode(self.reserved),
                                  enabled=0)
            database = db.Database()
            database.add(record, 1032)

        # Insert GeoRegions
        if db_georegions.idx_georegion_exists(1) is False:
            record = GeoRegions(georegion_name=general.encode(self.reserved),
                                enabled=0)
            database = db.Database()
            database.add(record, 1038)

        # Insert AgentNames
        if db_agentnames.idx_agentname_exists(1) is False:
            record = AgentNames(agent_name=general.encode(self.reserved),
                                enabled=0)
            database = db.Database()
            database.add(record, 1007)

        # Insert CompanyCategory
        if db_companycategories.idx_companycategory_exists(1) is False:
            record = CompanyCategories(companycategory_name=general.encode(
                self.reserved),
                                       enabled=0)
            database = db.Database()
            database.add(record, 1040)
Exemplo n.º 9
0
 def test_encode(self):
     """Test function test_encode."""
     # Initialize key variables
     expected = b'carrot'
     result = general.encode("carrot")
     self.assertEqual(result, expected)
Exemplo n.º 10
0
def setup_db_datapoint():
    """Create the database for Datapoint table testing.

    Args:
        None

    Returns:
        results: List of dicts of values to expect

    """
    # Initialize key variables
    idx_datapoint = 1
    results = []
    timestamp = general.normalized_timestamp()
    id_datapoint = general.hashstring(general.randomstring())
    devicename = general.hashstring(general.randomstring())
    id_agent = general.hashstring(general.randomstring())
    devicename = general.randomstring()
    agent_name = general.randomstring()

    # Drop the database and create tables
    initialize_db()

    # Initialize agent variables
    agent_data = {}
    agent_data['devicename'] = devicename
    agent_data['id_agent'] = id_agent
    agent_data['agent'] = agent_name
    agent_data['timestamp'] = timestamp
    (idx_device, idx_agent) = setup_db_deviceagent(agent_data,
                                                   initialize=False)

    # Get DeviceAgent index value
    deviceagent = hagent.GetDeviceAgent(idx_device, idx_agent)
    idx_deviceagent = deviceagent.idx_deviceagent()

    # Create dict of expected results
    expected = {
        'value': 100,
        'idx_datapoint': idx_datapoint,
        'timestamp': timestamp
    }

    # Insert Department data into database
    dept_data = Department(code=general.randomstring().encode())
    database = db.Database()
    database.add_all([dept_data], 1035)

    # Insert Billcode data into database
    bill_data = Billcode(code=general.randomstring().encode())
    database = db.Database()
    database.add_all([bill_data], 1039)

    # Insert Datapoint data into database
    new_data = Datapoint(idx_deviceagent=idx_deviceagent,
                         id_datapoint=general.encode(id_datapoint))
    database = db.Database()
    database.add_all([new_data], 1072)

    # Add value to expected
    expected['id_datapoint'] = id_datapoint
    results.append(expected)

    # Return
    return results