forked from Open-Cap-Stack/opencap
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Open-Cap-Stack#65 from anushac14/feature/document
Document Green Tests (Updated Data Model)
- Loading branch information
Showing
9 changed files
with
309 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,55 @@ | ||
const DocumentEmbedding = require('../models/DocumentEmbeddingModel'); | ||
const Document = require('../models/documentModel'); | ||
|
||
exports.createDocumentEmbedding = async (req, res) => { | ||
// Create a new document | ||
exports.createDocument = async (req, res) => { | ||
try { | ||
const newEmbedding = new DocumentEmbedding(req.body); | ||
const savedEmbedding = await newEmbedding.save(); | ||
res.status(201).json(savedEmbedding); | ||
const document = new Document(req.body); | ||
const savedDocument = await document.save(); | ||
res.status(201).json(savedDocument); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
exports.getDocumentEmbeddings = async (req, res) => { | ||
// Get all documents | ||
exports.getDocuments = async (req, res) => { | ||
try { | ||
const embeddings = await DocumentEmbedding.find(); | ||
res.status(200).json(embeddings); | ||
const documents = await Document.find(); | ||
res.status(200).json(documents); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
exports.getDocumentEmbeddingById = async (req, res) => { | ||
// Get a document by ID | ||
exports.getDocumentById = async (req, res) => { | ||
try { | ||
const embedding = await DocumentEmbedding.findById(req.params.id); | ||
if (!embedding) { | ||
return res.status(404).json({ message: 'Document embedding not found' }); | ||
} | ||
res.status(200).json(embedding); | ||
const document = await Document.findById(req.params.id); | ||
if (!document) return res.status(404).json({ message: 'Document not found' }); | ||
res.status(200).json(document); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
exports.updateDocumentEmbedding = async (req, res) => { | ||
// Update a document by ID | ||
exports.updateDocumentById = async (req, res) => { | ||
try { | ||
const embedding = await DocumentEmbedding.findByIdAndUpdate(req.params.id, req.body, { new: true }); | ||
if (!embedding) { | ||
return res.status(404).json({ message: 'Document embedding not found' }); | ||
} | ||
res.status(200).json(embedding); | ||
const document = await Document.findByIdAndUpdate(req.params.id, req.body, { new: true }); | ||
if (!document) return res.status(404).json({ message: 'Document not found' }); | ||
res.status(200).json(document); | ||
} catch (error) { | ||
res.status(400).json({ error: error.message }); | ||
res.status(400).json({ message: error.message }); | ||
} | ||
}; | ||
|
||
exports.deleteDocumentEmbedding = async (req, res) => { | ||
// Delete a document by ID | ||
exports.deleteDocumentById = async (req, res) => { | ||
try { | ||
const embedding = await DocumentEmbedding.findByIdAndDelete(req.params.id); | ||
if (!embedding) { | ||
return res.status(404).json({ message: 'Document embedding not found' }); | ||
} | ||
res.status(200).json({ message: 'Document embedding deleted' }); | ||
const document = await Document.findByIdAndDelete(req.params.id); | ||
if (!document) return res.status(404).json({ message: 'Document not found' }); | ||
res.status(200).json({ message: 'Document deleted' }); | ||
} catch (error) { | ||
res.status(500).json({ error: error.message }); | ||
res.status(500).json({ message: error.message }); | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// models/documentModel.js | ||
const mongoose = require('mongoose'); | ||
|
||
const documentSchema = new mongoose.Schema({ | ||
documentId: { type: String, unique: true, required: true }, | ||
name: { type: String, required: true }, | ||
metadata: { type: Object }, | ||
uploadedBy: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true }, | ||
path: { type: String, required: true }, | ||
title: { type: String, required: true }, | ||
content: { type: String, required: true }, | ||
DocumentType: { type: String, enum: ['Legal', 'Financial', 'Other'], required: true }, | ||
FileType: { type: String, enum: ['PDF', 'DOCX', 'TXT'], required: true }, | ||
Versioning: { type: String }, | ||
AccessControl: { type: Object }, | ||
LegalSignificance: { type: String } | ||
}, { timestamps: true }); | ||
|
||
module.exports = mongoose.model('Document', documentSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
const express = require('express'); | ||
const router = express.Router(); | ||
const { createDocument, getAllDocuments, getDocumentById, updateDocumentById, deleteDocumentById } = require('../controllers/documentController'); | ||
const documentController = require('../controllers/documentController'); | ||
|
||
// Existing routes | ||
router.get('/', getAllDocuments); | ||
router.post('/', createDocument); | ||
|
||
// Add these routes for update and delete | ||
router.get('/:id', getDocumentById); // Retrieve a document by ID | ||
router.put('/:id', updateDocumentById); // Update a document by ID | ||
router.delete('/:id', deleteDocumentById); // Delete a document by ID | ||
router.post('/documents', documentController.createDocument); | ||
router.get('/documents', documentController.getDocuments); | ||
router.get('/documents/:id', documentController.getDocumentById); | ||
router.put('/documents/:id', documentController.updateDocumentById); | ||
router.delete('/documents/:id', documentController.deleteDocumentById); | ||
|
||
module.exports = router; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const request = require('supertest'); | ||
const express = require('express'); | ||
const mongoose = require('mongoose'); | ||
const Document = require('../models/documentModel'); | ||
const { connectDB, disconnectDB } = require('../db'); | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
app.use('/api', require('../routes/documentRoutes')); | ||
|
||
beforeAll(async () => { | ||
await connectDB(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
describe('Document Controller', () => { | ||
it('should create a new document', async () => { | ||
const response = await request(app) | ||
.post('/api/documents') | ||
.send({ | ||
documentId: 'doc123', | ||
name: 'Sample Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/path/to/document', | ||
title: 'Sample Title', | ||
content: 'Sample Content', | ||
DocumentType: 'Legal', | ||
FileType: 'PDF', | ||
Versioning: '1.0' | ||
}); | ||
expect(response.statusCode).toBe(201); | ||
expect(response.body.name).toBe('Sample Document'); | ||
}); | ||
|
||
it('should get all documents', async () => { | ||
const response = await request(app).get('/api/documents'); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body).toBeInstanceOf(Array); | ||
}); | ||
|
||
it('should get a document by ID', async () => { | ||
const newDocument = new Document({ | ||
documentId: 'doc124', | ||
name: 'Another Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/path/to/another/document', | ||
title: 'Another Title', | ||
content: 'Another Content', | ||
DocumentType: 'Financial', | ||
FileType: 'DOCX' | ||
}); | ||
const savedDocument = await newDocument.save(); | ||
|
||
const response = await request(app).get(`/api/documents/${savedDocument._id}`); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body.name).toBe('Another Document'); | ||
}); | ||
|
||
it('should update a document by ID', async () => { | ||
const newDocument = new Document({ | ||
documentId: 'doc125', | ||
name: 'Update Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/path/to/update/document', | ||
title: 'Update Title', | ||
content: 'Update Content', | ||
DocumentType: 'Legal', | ||
FileType: 'PDF' | ||
}); | ||
const savedDocument = await newDocument.save(); | ||
|
||
const response = await request(app) | ||
.put(`/api/documents/${savedDocument._id}`) | ||
.send({ name: 'Updated Document' }); | ||
|
||
expect(response.statusCode).toBe(200); | ||
expect(response.body.name).toBe('Updated Document'); | ||
}); | ||
|
||
it('should delete a document by ID', async () => { | ||
const newDocument = new Document({ | ||
documentId: 'doc126', | ||
name: 'Delete Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/path/to/delete/document', | ||
title: 'Delete Title', | ||
content: 'Delete Content', | ||
DocumentType: 'Other', | ||
FileType: 'TXT' | ||
}); | ||
const savedDocument = await newDocument.save(); | ||
|
||
const response = await request(app).delete(`/api/documents/${savedDocument._id}`); | ||
expect(response.statusCode).toBe(200); | ||
expect(response.body.message).toBe('Document deleted'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
const mongoose = require('mongoose'); | ||
const Document = require('../models/documentModel'); | ||
const { connectDB, disconnectDB } = require('../db'); | ||
|
||
beforeAll(async () => { | ||
await connectDB(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await mongoose.connection.db.dropDatabase(); | ||
await mongoose.connection.close(); | ||
}); | ||
|
||
describe('Document Model', () => { | ||
it('should create a new document', async () => { | ||
const document = new Document({ | ||
documentId: 'doc001', | ||
name: 'Test Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/test/path', | ||
title: 'Test Title', | ||
content: 'Test Content', | ||
DocumentType: 'Legal', | ||
FileType: 'PDF', | ||
Versioning: '1.0' | ||
}); | ||
|
||
const savedDocument = await document.save(); | ||
expect(savedDocument._id).toBeDefined(); | ||
expect(savedDocument.name).toBe('Test Document'); | ||
}); | ||
|
||
it('should retrieve a document by ID', async () => { | ||
const document = new Document({ | ||
documentId: 'doc002', | ||
name: 'Retrieve Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/test/retrieve', | ||
title: 'Retrieve Title', | ||
content: 'Retrieve Content', | ||
DocumentType: 'Financial', | ||
FileType: 'DOCX' | ||
}); | ||
|
||
const savedDocument = await document.save(); | ||
const foundDocument = await Document.findById(savedDocument._id); | ||
|
||
expect(foundDocument).toBeDefined(); | ||
expect(foundDocument.name).toBe('Retrieve Document'); | ||
}); | ||
|
||
it('should update a document by ID', async () => { | ||
const document = new Document({ | ||
documentId: 'doc003', | ||
name: 'Update Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/test/update', | ||
title: 'Update Title', | ||
content: 'Update Content', | ||
DocumentType: 'Legal', | ||
FileType: 'PDF' | ||
}); | ||
|
||
const savedDocument = await document.save(); | ||
savedDocument.name = 'Updated Document'; | ||
const updatedDocument = await savedDocument.save(); | ||
|
||
expect(updatedDocument.name).toBe('Updated Document'); | ||
}); | ||
|
||
it('should delete a document by ID', async () => { | ||
const document = new Document({ | ||
documentId: 'doc004', | ||
name: 'Delete Document', | ||
uploadedBy: new mongoose.Types.ObjectId(), // Use valid ObjectId | ||
path: '/test/delete', | ||
title: 'Delete Title', | ||
content: 'Delete Content', | ||
DocumentType: 'Other', | ||
FileType: 'TXT' | ||
}); | ||
|
||
const savedDocument = await document.save(); | ||
await Document.findByIdAndDelete(savedDocument._id); | ||
|
||
const deletedDocument = await Document.findById(savedDocument._id); | ||
expect(deletedDocument).toBeNull(); | ||
}); | ||
}); |
Oops, something went wrong.