xxxxxxxxxx
import { Test, TestingModule } from '@nestjs/testing';
import { HttpStatus, INestApplication } from '@nestjs/common';
import * as request from 'supertest';
import { HealthcheckController } from './healthcheck.controller';
describe('HealthcheckController', () => {
let controller: HealthcheckController;
let app: INestApplication;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [HealthcheckController],
}).compile();
app = module.createNestApplication();
controller = module.get<HealthcheckController>(HealthcheckController);
await app.init();
});
afterEach(() => {
jest.clearAllMocks();
});
afterAll(async () => {
await app.close();
});
describe('healthcheck', () => {
it('should return "OK" string', async () => {
const result = controller.healthcheck();
expect(result).toBe('OK');
});
});
});
xxxxxxxxxx
import pandas as pd
s = pd.Series([1,2,3,4,5])
# adding 5 to each value
new = s.apply(lambda num : num + 5)
# printing elements of old and new series
print(s.head().values,"\n",new.head().values)
# [1 2 3 4 5]
# [6 7 8 9 10]
xxxxxxxxxx
# This function doubles the input value
def double(x):
return 2*x
# Apply this function to double every value in a specified column
df.column1 = df.column1.apply(double)
# Lambda functions can also be supplied to `apply()`
df.column2 = df.column2.apply(lambda x : 3*x)
# Applying to a row requires it to be called on the entire DataFrame
df['newColumn'] = df.apply(lambda row:
row['column1'] * 1.5 + row['column2'],
axis=1
)
Nestjs testing module not found
xxxxxxxxxx
{
"moduleNameMapper": {
"^@Shared/(.)*$": "<rootDir>/src/app/shared/$1"
}
}
If you're coming from google then this can also happen if VSCode autocompletes your imports as src/xyz/abc/service.ts instead of ../xyz/abc/service.ts.
When running tests the test code won't be able to locate the correct service(s) at run-time. It needs fully relative paths without an absolute 'src/' at the beginning.
As you are using typescript's path mapping, you need to also update your jest config with the mapped paths as well. Your jest config needs to have the following added:Assuming that your
xxxxxxxxxx
import pandas as pd
# Function to add
def add_values(row):
return row['A'] + row['B'] + row['C']
def main():
# Create a dictionary with three fields each
data = {
'A': [1, 2, 3],
'B': [4, 5, 6],
'C': [7, 8, 9]}
# Convert the dictionary into DataFrame
df = pd.DataFrame(data)
print("Original DataFrame:\n", df)
# Apply the user-defined function to every row
df['add'] = df.apply(add_values, axis=1)
print('\nAfter Applying Function: ')
# Print the new DataFrame
print(df)
if __name__ == '__main__':
main()
xxxxxxxxxx
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
# The Extract phase: Just load the data using pd.read_csv, pd.read_parquet, pd.read_json etc
# The Transform phase
def transform(sql_query, dbname='hr', user='postgres', password='postgres', port='5432', host='localhost'):
# Create a connection to the PostgreSQL database using psycopg2
conn = psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
df = pd.read_sql(sql_query, conn) # Execute the query and load the results into a DataFrame
conn.close() # Close the psycopg2 connection
return df # Return the DataFrame
# The Load phase
def load(df, table_name, dbname='hr', user='postgres', password='postgres', port='5432', host='localhost'):
# Create a connection to the PostgreSQL database using SQLAlchemy for to_sql
engine = create_engine(f'postgresql://{user}:{password}@{host}:{port}/{dbname}')
# Persist the DataFrame to the SQL table
df.to_sql(table_name, engine, if_exists='append', index=False) # if_exists='replace' for replacement
print(f"DataFrame persisted to table '{table_name}' successfully!")
# transform the data from PostgreSQL
query_result = transform('SELECT * FROM jobs')
# Load or Persist the result in the PostgreSQL database
load(query_result, 'final_table')