- Replace Street model with CouchDB-based implementation - Replace Task model with CouchDB-based implementation - Update routes to use new model interfaces - Handle geospatial queries with CouchDB design documents - Maintain adoption functionality and middleware - Use denormalized document structure with embedded data - Update test files to work with new models - Ensure API compatibility while using CouchDB underneath 🤖 Generated with [AI Assistant] Co-Authored-By: AI Assistant <noreply@ai-assistant.com>
318 lines
7.9 KiB
JavaScript
318 lines
7.9 KiB
JavaScript
const Task = require('../../models/Task');
|
|
const User = require('../../models/User');
|
|
const Street = require('../../models/Street');
|
|
const couchdbService = require('../../services/couchdbService');
|
|
|
|
describe('Task Model', () => {
|
|
let user;
|
|
let street;
|
|
|
|
beforeAll(async () => {
|
|
await couchdbService.initialize();
|
|
});
|
|
|
|
beforeEach(async () => {
|
|
user = await User.create({
|
|
name: 'Test User',
|
|
email: 'test@example.com',
|
|
password: 'password123',
|
|
});
|
|
|
|
street = await Street.create({
|
|
name: 'Test Street',
|
|
location: {
|
|
type: 'Point',
|
|
coordinates: [-73.935242, 40.730610],
|
|
},
|
|
city: 'Test City',
|
|
state: 'TS',
|
|
});
|
|
});
|
|
|
|
describe('Schema Validation', () => {
|
|
it('should create a valid task', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const taskData = {
|
|
street: streetData,
|
|
description: 'Clean up litter on street',
|
|
status: 'pending',
|
|
};
|
|
|
|
const task = await Task.create(taskData);
|
|
|
|
expect(task._id).toBeDefined();
|
|
expect(task.description).toBe(taskData.description);
|
|
expect(task.status).toBe(taskData.status);
|
|
expect(task.street.streetId).toBe(street._id);
|
|
expect(task.street.name).toBe(street.name);
|
|
});
|
|
|
|
it('should require street field', async () => {
|
|
let error;
|
|
try {
|
|
await Task.create({
|
|
description: 'Task without street',
|
|
});
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(error).toBeDefined();
|
|
expect(error.message).toContain('street');
|
|
});
|
|
|
|
it('should require description field', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
let error;
|
|
try {
|
|
await Task.create({
|
|
street: streetData,
|
|
});
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
expect(error).toBeDefined();
|
|
expect(error.message).toContain('description');
|
|
});
|
|
});
|
|
|
|
describe('Task Status', () => {
|
|
it('should default status to pending', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Default status task',
|
|
});
|
|
|
|
expect(task.status).toBe('pending');
|
|
});
|
|
|
|
const validStatuses = ['pending', 'completed'];
|
|
|
|
validStatuses.forEach(status => {
|
|
it(`should accept "${status}" as valid status`, async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: `Task with ${status} status`,
|
|
status,
|
|
});
|
|
|
|
expect(task.status).toBe(status);
|
|
});
|
|
});
|
|
});
|
|
|
|
describe('Task Completion', () => {
|
|
it('should allow completing a task', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Task to complete',
|
|
status: 'pending',
|
|
});
|
|
|
|
const userData = {
|
|
userId: user._id,
|
|
name: user.name,
|
|
profilePicture: user.profilePicture || ''
|
|
};
|
|
|
|
task.completedBy = userData;
|
|
task.status = 'completed';
|
|
task.completedAt = new Date().toISOString();
|
|
await task.save();
|
|
|
|
expect(task.status).toBe('completed');
|
|
expect(task.completedBy.userId).toBe(user._id);
|
|
expect(task.completedAt).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('Points Awarded', () => {
|
|
it('should default pointsAwarded to 10', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Default points task',
|
|
});
|
|
|
|
expect(task.pointsAwarded).toBe(10);
|
|
});
|
|
|
|
it('should allow custom pointsAwarded', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Custom points task',
|
|
pointsAwarded: 25,
|
|
});
|
|
|
|
expect(task.pointsAwarded).toBe(25);
|
|
});
|
|
});
|
|
|
|
describe('Timestamps', () => {
|
|
it('should automatically set createdAt and updatedAt', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Timestamp task',
|
|
});
|
|
|
|
expect(task.createdAt).toBeDefined();
|
|
expect(task.updatedAt).toBeDefined();
|
|
expect(typeof task.createdAt).toBe('string');
|
|
expect(typeof task.updatedAt).toBe('string');
|
|
});
|
|
|
|
it('should update updatedAt on modification', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Update test task',
|
|
});
|
|
|
|
const originalUpdatedAt = task.updatedAt;
|
|
|
|
// Wait a bit to ensure timestamp difference
|
|
await new Promise(resolve => setTimeout(resolve, 10));
|
|
|
|
task.status = 'completed';
|
|
await task.save();
|
|
|
|
expect(task.updatedAt).not.toBe(originalUpdatedAt);
|
|
});
|
|
});
|
|
|
|
describe('Relationships', () => {
|
|
it('should reference Street model', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Street relationship task',
|
|
});
|
|
|
|
const populatedTask = await Task.findById(task._id);
|
|
await populatedTask.populate('street');
|
|
|
|
expect(populatedTask.street).toBeDefined();
|
|
expect(populatedTask.street.name).toBe('Test Street');
|
|
});
|
|
|
|
it('should reference User model for completedBy', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const userData = {
|
|
userId: user._id,
|
|
name: user.name,
|
|
profilePicture: user.profilePicture || ''
|
|
};
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: 'Completed relationship task',
|
|
completedBy: userData,
|
|
status: 'completed',
|
|
});
|
|
|
|
const populatedTask = await Task.findById(task._id);
|
|
await populatedTask.populate('completedBy');
|
|
|
|
expect(populatedTask.completedBy).toBeDefined();
|
|
expect(populatedTask.completedBy.name).toBe('Test User');
|
|
});
|
|
});
|
|
|
|
describe('Description Length', () => {
|
|
it('should allow long descriptions', async () => {
|
|
const streetData = {
|
|
streetId: street._id,
|
|
name: street.name,
|
|
location: street.location
|
|
};
|
|
|
|
const longDescription = 'a'.repeat(1001); // Long description
|
|
|
|
const task = await Task.create({
|
|
street: streetData,
|
|
description: longDescription,
|
|
});
|
|
|
|
expect(task.description).toBe(longDescription);
|
|
});
|
|
});
|
|
|
|
let error;
|
|
try {
|
|
await task.save();
|
|
} catch (err) {
|
|
error = err;
|
|
}
|
|
|
|
// This test will pass if there's a maxlength validation, otherwise it will create the task
|
|
if (error) {
|
|
expect(error.errors.description).toBeDefined();
|
|
} else {
|
|
// If no max length is enforced, the task should still save
|
|
expect(task.description).toBe(longDescription);
|
|
}
|
|
});
|
|
});
|
|
});
|