The Mocks API provides endpoints for generating fake data to populate your development database. This is useful for testing, demos, and development without manually creating users and pets.Base Path:/api/mocksSource Files:
Routes: src/routes/mocks.router.js
Mock Generators: src/mocks/mocks.js
Mock data is generated using the Faker library and follows the same schema as real users and pets.
router.post('/generateData', async (req, res) => { try { let { user, pet } = req.query; user = parseInt(user) || 1; pet = parseInt(pet) || 1; if (user < 0 || pet < 0) { return res.status(400).json({ error: "Cantidad enviada debe de ser positiva" }); } //Genero usuarios según el largo recibido por parámetro let users = []; for (let i = 0; i < user; i++) { users.push(await generaUser()); } //Inserto los datos del array en la BD const usersInsertados = await userModel.insertMany(users); let pets = []; //Genero mascotas según el largo recibido por parámetro for (let i = 0; i < pet; i++) { let generaData = generaPet().pet; if (generaData.adopted) { const randomUser = usersInsertados[Math.floor(Math.random() * usersInsertados.length)]; generaData.owner = randomUser._id; } pets.push(generaData); } //Inserto los datos del array en la BD const petsInsertados = await petModel.insertMany(pets); return res.status(201).json({ message: 'Datos generados exitosamente', users: usersInsertados, pets: petsInsertados }); } catch (error) { console.error("Error generando datos:", error); return res.status(500).json({ error: "Error interno del servidor" }); }});
Some pets will have adopted: true and an owner field
The owner is randomly selected from the users that were just created
However, the user’s pets array is not updated (unlike in the real adoption flow)
Mock adopted pets do not update the user’s pets array. This creates inconsistent data compared to real adoptions. This is acceptable for testing but be aware of the discrepancy.
Database Impact: The /generateData endpoint directly inserts data into your MongoDB database. Use with caution in production environments.
Bulk Operations: The endpoint uses insertMany() which is efficient for bulk inserts but doesn’t trigger Mongoose middleware or validation hooks.
Data Consistency: Mock adoptions don’t update the user’s pets array, creating inconsistent data. Real adoptions use the Adoptions API which properly updates both user and pet records.
Password Hashing: Mock user passwords may or may not be hashed depending on the generaUser() implementation. Check src/mocks/mocks.js for details.