Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitAuto: [FEATURE] Implement Non Structured Specification API #349

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI

on:
pull_request:
branches: [ main ]

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install Dependencies
run: npm install
- name: Run Tests
run: npm test
16 changes: 16 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "vtex-sdk-dotnet",
"version": "1.0.0",
"description": "VTEX SDK for .NET with added Non Structured Specification API",
"main": "src/app.js",
"scripts": {
"test": "jest"
},
"dependencies": {
"express": "^4.17.1"
},
"devDependencies": {
"jest": "^27.0.0",
"supertest": "^6.1.3"
}
}
12 changes: 12 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const app = express();

const nonStructuredSpecificationsController = require('./controllers/nonStructuredSpecificationsController');

Check notice on line 4 in src/app.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/app.js#L4

Strings must use doublequote.

app.use('/non-structured-specifications', nonStructuredSpecificationsController);

const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);

Check failure on line 10 in src/app.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/app.js#L10

Unexpected console statement.
});

25 changes: 25 additions & 0 deletions src/controllers/nonStructuredSpecificationsController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const express = require('express');

Check notice on line 1 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L1

Strings must use doublequote.
const router = express.Router();
const nonStructuredSpecificationsService = require('../services/nonStructuredSpecificationsService');

// GET /non-structured-specifications
router.get('/', async (req, res) => {

Check notice on line 6 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L6

Strings must use doublequote.
try {

Check notice on line 7 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L7

Unnecessary block.
const specifications = await nonStructuredSpecificationsService.getAllSpecifications();
res.json(specifications);
} catch (error) {
res.status(500).json({ error: 'Failed to retrieve specifications' });

Check notice on line 11 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L11

Strings must use doublequote.
}
});

// DELETE /non-structured-specifications/:id
router.delete('/:id', async (req, res) => {

Check notice on line 16 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L16

Strings must use doublequote.
try {
await nonStructuredSpecificationsService.deleteSpecification(req.params.id);
res.status(204).send();
} catch (error) {
res.status(500).json({ error: 'Failed to delete specification' });

Check notice on line 21 in src/controllers/nonStructuredSpecificationsController.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/controllers/nonStructuredSpecificationsController.js#L21

Strings must use doublequote.
}
});

module.exports = router;
5 changes: 5 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Mock database module

module.exports = {
query: jest.fn().mockResolvedValue({ rows: [] }),

Check notice on line 4 in src/db.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/db.js#L4

'jest' is not defined.
};
23 changes: 23 additions & 0 deletions src/services/nonStructuredSpecificationsService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const db = require('../db'); // hypothetical database module

Check notice on line 1 in src/services/nonStructuredSpecificationsService.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/services/nonStructuredSpecificationsService.js#L1

Strings must use doublequote.

async function getAllSpecifications() {
try {
const result = await db.query('SELECT * FROM non_structured_specifications');

Check notice on line 5 in src/services/nonStructuredSpecificationsService.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/services/nonStructuredSpecificationsService.js#L5

Strings must use doublequote.
return result.rows;
} catch (error) {
throw new Error('Database query failed');
}
}

async function deleteSpecification(id) {
try {
await db.query('DELETE FROM non_structured_specifications WHERE id = $1', [id]);

Check notice on line 14 in src/services/nonStructuredSpecificationsService.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

src/services/nonStructuredSpecificationsService.js#L14

Strings must use doublequote.
} catch (error) {
throw new Error('Database query failed');
}
}

module.exports = {
getAllSpecifications,
deleteSpecification,
};
36 changes: 36 additions & 0 deletions tests/nonStructuredSpecifications.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const request = require('supertest');

Check notice on line 1 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L1

Strings must use doublequote.
const express = require('express');

Check notice on line 2 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L2

Strings must use doublequote.
const nonStructuredSpecificationsService = require('../src/services/nonStructuredSpecificationsService');

Check notice on line 3 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L3

Strings must use doublequote.

const app = express();
app.use('/non-structured-specifications', nonStructuredSpecificationsController);

Check notice on line 6 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L6

'nonStructuredSpecificationsController' is not defined.

Check notice on line 6 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L6

Strings must use doublequote.

describe('Non Structured Specifications API', () => {

Check notice on line 8 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L8

Strings must use doublequote.
it('should retrieve all non structured specifications', async () => {

Check notice on line 9 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L9

Strings must use doublequote.
const response = await request(app).get('/non-structured-specifications');

Check notice on line 10 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L10

Strings must use doublequote.
expect(response.statusCode).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});

it('should delete a non structured specification', async () => {

Check notice on line 15 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L15

Strings must use doublequote.
const idToDelete = 1; // Example ID
const response = await request(app).delete(`/non-structured-specifications/${idToDelete}`);
expect(response.statusCode).toBe(204);
});

it('should return 500 if deletion fails', async () => {

Check notice on line 21 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L21

Strings must use doublequote.
const invalidId = 'invalid-id';

Check notice on line 22 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L22

Strings must use doublequote.
const response = await request(app).delete(`/non-structured-specifications/${invalidId}`);
expect(response.statusCode).toBe(500);
expect(response.body.error).toBe('Failed to delete specification');
});

it('should return 500 if retrieval fails', async () => {
jest.spyOn(nonStructuredSpecificationsController, 'getAllSpecifications').mockImplementation(() => {

Check notice on line 29 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L29

Strings must use doublequote.
throw new Error('Database query failed');

Check notice on line 30 in tests/nonStructuredSpecifications.test.js

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/nonStructuredSpecifications.test.js#L30

Strings must use doublequote.
});
const response = await request(app).get('/non-structured-specifications');
expect(response.statusCode).toBe(500);
expect(response.body.error).toBe('Failed to retrieve specifications');
});
});
Loading