Skip to content

Commit

Permalink
finalize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gg-hsi committed Jan 25, 2025
1 parent fd932f4 commit 63b12c3
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 28 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,38 @@
</table>
</td>
</tr>
<tr>
<td>3</td>
<td>The endpoint <a href="https://www.alphavantage.co/documentation/#currency-monthly">DIGITAL_CURRENCY_MONTHLY</a> should return an error when the parameter "symbol" is missing</td>
<td>
<table>
<tr><th>Step #</th><th>Description</th></tr>
<tr><td>1</td><td>Enter the following parameters in Postman:<br>1- function:"DIGITAL_CURRENCY_MONTHLY"<br>2- market:"EUR"<br>3- apikey:"API_KEY"<br>Send the HTTP request</td></tr>
<tr><td>2</td><td>Check that response code is 200</td></tr>
<tr><td>3</td><td>Check that response contains the following JSON key:<br>
"Error Message": "Invalid API call. Please retry or visit the documentation (https://www.alphavantage.co/documentation/) for DIGITAL_CURRENCY_MONTHLY."</td></tr>
</table>
</td>
</tr>

</table>
## Automation
All the above tests were automated (check folder 'tests'):

```sh
| tests/
|--- alphavantage/
|------ api-digital-currency-monthly.spec.ts
|------ api-time-series-intraday.spec.ts
```

All data files are in the folder 'data':

```sh
| data/
|--- digital_currency.json
|--- intraday.json
```

### Getting Started

Expand Down Expand Up @@ -63,6 +92,10 @@ To run tests for this project use:
npx playwright test
```

### Github Actions

Tests can be also run in [Github Actions](https://github.com/gg-hsi/Payrails_QA_Tech_Challenge/actions)

### Modules used

- [Node.js](https://nodejs.org/en/)
Expand Down
12 changes: 1 addition & 11 deletions fixture.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
import { APIRequestContext, test as base, request } from '@playwright/test';
import { test as base } from '@playwright/test';

const fixtures = base.extend<{
apiKey: string;
cleanContext: APIRequestContext;
}>({
apiKey: async ({}, use) => {
const apiKey = process.env.API_KEY?.toString() || 'demo';
await use(apiKey);
},
cleanContext: async ({ baseURL }, use) => {
const context = await request.newContext({
baseURL,
extraHTTPHeaders: {
'User-Agent': 'request',
},
});
await use(context);
},
});

export { fixtures };
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
"lint": "eslint .",
"test": "playwright test",
"test:ci": "playwright test -j 3",
"test:Chrome": "playwright test --project='chromium'",
"test:Firefox": "playwright test --project='firefox'",
"test:Safari": "playwright test --project='webkit'"
"test:Chrome": "playwright test --project='chromium'"
},
"devDependencies": {
"@eslint/js": "^9.17.0",
Expand Down
19 changes: 13 additions & 6 deletions tests/alphavantage/api-digital-currency-monthly.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import responseExample from '../../data/digital_currency.json';
import { fixtures as test } from '../../fixture';
import { DigitalCurrencyMonthlyData } from '../../types';

test.describe('Market News', () => {
test.describe('Cryptocurrencies', () => {
const endpoint = 'DIGITAL_CURRENCY_MONTHLY';
test('Get Digital Currency', async ({ cleanContext, baseURL, apiKey }) => {

test('Get Digital Currency', async ({ request, baseURL, apiKey }) => {
// Define endpoint and query parameters
const params = {
function: endpoint,
Expand All @@ -17,11 +18,12 @@ test.describe('Market News', () => {
const queryString = new URLSearchParams(params).toString();
const url = `${baseURL}?${queryString}`;
// Make the GET request
const response = await cleanContext.get(url);
const response = await request.get(url);
expect(response.ok()).toBeTruthy();
const responseBody: DigitalCurrencyMonthlyData = await response.json();

// No Error in response
expect(responseBody).not.toHaveProperty('Error Message');
expect(responseBody, "response shouldn't contain an error").not.toHaveProperty('Error Message');

//Validate response Data structure
Object.keys(responseBody).forEach((key) => expect(responseExample).toHaveProperty(key));
Expand All @@ -37,9 +39,14 @@ test.describe('Market News', () => {
Object.keys(responseExample['Time Series (Digital Currency Monthly)']['testDate']),
).toEqual(Object.keys(responseBody['Time Series (Digital Currency Monthly)'][key]));
});

//Validate Time Series date objects keys
Object.keys(responseBody['Time Series (Digital Currency Monthly)']).forEach((date) =>
expect(Date.parse(date)).toBeTruthy(),
);
// Validate the content of the response
expect(responseBody['Meta Data']['1. Information']).toBe(
'Monthly Prices and Volumes for Digital Currency',
responseExample['Meta Data']['1. Information'],
);
expect(responseBody['Meta Data']['2. Digital Currency Code']).toBe(params.symbol);
expect(responseBody['Meta Data']['4. Market Code']).toBe(params.market);
Expand All @@ -62,6 +69,6 @@ test.describe('Market News', () => {
const responseBody = await response.json();

// Error in response
expect(responseBody).toHaveProperty('Error Message');
expect(responseBody, 'response should contain an error').toHaveProperty('Error Message');
});
});
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect } from '@playwright/test';
import responseExample from '../../data/intraday.json';
import { fixtures as test } from '../../fixture';
import { IntraDayData } from '../../types';

test.describe('Market News', () => {
const endpoint = 'TIME_SERIES_INTRADAY';
Expand All @@ -19,9 +20,9 @@ test.describe('Market News', () => {
// Make the GET request
const response = await request.get(url);
expect(response.ok()).toBeTruthy();
const responseBody = await response.json();
const responseBody: IntraDayData = await response.json();
// No Error in response
expect(responseBody).not.toHaveProperty('Error Message');
expect(responseBody, "response shouldn't contain an error").not.toHaveProperty('Error Message');

//Validate response Data structure
Object.keys(responseBody).forEach((key) => expect(responseExample).toHaveProperty(key));
Expand All @@ -30,20 +31,23 @@ test.describe('Market News', () => {
expect(Object.keys(responseExample['Meta Data'])).toEqual(
Object.keys(responseBody['Meta Data']),
);
// response should contain the most recent 100 intraday OHLCV bars by default when the outputsize parameter is not set
expect(Object.keys(responseBody['Time Series (1min)']).length).toBe(100);

//Validate Time Series Data structure
Object.keys(responseBody['Time Series (1min)']).forEach((key) => {
expect(Object.keys(responseExample['Time Series (1min)']['testTimeStamp'])).toEqual(
Object.keys(responseBody['Time Series (1min)'][key]),
);
});
// No Error in response
expect(responseBody['Error Message']).toBeFalsy();
//Validate Time Series date objects keys
Object.keys(responseBody['Time Series (1min)']).forEach((date) =>
expect(Date.parse(date)).toBeTruthy(),
);

// Validate the structure of the response
expect(responseBody['Meta Data']).toBeDefined();
// Validate the date in the response
expect(responseBody['Meta Data']['1. Information']).toBe(
'Intraday (1min) open, high, low, close prices and volume',
responseExample['Meta Data']['1. Information'],
);
expect(responseBody['Meta Data']['2. Symbol']).toBe(params.symbol);
expect(responseBody['Meta Data']['4. Interval']).toBe(params.interval);
Expand All @@ -66,6 +70,6 @@ test.describe('Market News', () => {
const responseBody = await response.json();

// Error in response
expect(responseBody).toHaveProperty('Error Message');
expect(responseBody, 'response should contain an error').toHaveProperty('Error Message');
});
});
19 changes: 19 additions & 0 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,22 @@ export type DigitalCurrencyMonthlyData = {
};
};
};
export type IntraDayData = {
'Meta Data': {
'1. Information': string;
'2. Symbol': string;
'3. Last Refreshed': string;
'4. Interval': string;
'5. Output Size': string;
'6. Time Zone': string;
};
'Time Series (Digital Currency Monthly)': {
[date: string]: {
'1. open': string;
'2. high': string;
'3. low': string;
'4. close': string;
'5. volume': string;
};
};
};

0 comments on commit 63b12c3

Please sign in to comment.