102 lines
3.6 KiB
JavaScript
102 lines
3.6 KiB
JavaScript
'use strict';
|
|
|
|
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const mongoose = require('mongoose');
|
|
const Professor = require('./professorModel');
|
|
const { formatProfessorDoc } = require('./professorService');
|
|
|
|
test('Professor Model & User Merge Tests', async (t) => {
|
|
await t.test('Professor schema has user reference and virtual getters', () => {
|
|
const userId = new mongoose.Types.ObjectId();
|
|
const prof = new Professor({
|
|
user: userId,
|
|
bio: 'استاد ارشد هوش مصنوعی',
|
|
expertise: ['Machine Learning', 'Python'],
|
|
isActive: true
|
|
});
|
|
|
|
assert.equal(String(prof.user), String(userId));
|
|
assert.equal(prof.bio, 'استاد ارشد هوش مصنوعی');
|
|
assert.deepEqual(prof.expertise, ['Machine Learning', 'Python']);
|
|
assert.equal(prof.isActive, true);
|
|
});
|
|
|
|
await t.test('formatProfessorDoc correctly extracts and formats linked user fields', () => {
|
|
const userId = new mongoose.Types.ObjectId();
|
|
const profId = new mongoose.Types.ObjectId();
|
|
const mockDoc = {
|
|
_id: profId,
|
|
user: {
|
|
_id: userId,
|
|
name: 'علی حسینی',
|
|
nationalIdCode: '0012345678',
|
|
phoneNumber: '09123456789',
|
|
email: 'ali@example.com',
|
|
cardNumber: '6037997112345678',
|
|
shabaNumber: 'IR120000000000000000000000',
|
|
isActive: true
|
|
},
|
|
bio: 'استاد برنامهنویسی',
|
|
expertise: ['Web', 'Vue'],
|
|
courses: [],
|
|
isActive: true
|
|
};
|
|
|
|
const formatted = formatProfessorDoc(mockDoc);
|
|
assert.equal(String(formatted._id), String(profId));
|
|
assert.equal(String(formatted.user), String(userId));
|
|
assert.equal(formatted.name, 'علی');
|
|
assert.equal(formatted.surname, 'حسینی');
|
|
assert.equal(formatted.fullName, 'علی حسینی');
|
|
assert.equal(formatted.nationalIdCode, '0012345678');
|
|
assert.equal(formatted.phoneNumber, '09123456789');
|
|
assert.equal(formatted.email, 'ali@example.com');
|
|
assert.equal(formatted.cardNumber, '6037997112345678');
|
|
assert.equal(formatted.shabaNumber, 'IR120000000000000000000000');
|
|
assert.deepEqual(formatted.expertise, ['Web', 'Vue']);
|
|
assert.equal(formatted.isActive, true);
|
|
});
|
|
|
|
await t.test('formatProfessorDoc handles legacy documents without linked user', () => {
|
|
const profId = new mongoose.Types.ObjectId();
|
|
const legacyDoc = {
|
|
_id: profId,
|
|
name: 'رضا',
|
|
surname: 'اکبری',
|
|
phoneNumber: '09351234567',
|
|
nationalIdCode: '1234567890',
|
|
email: 'reza@example.com',
|
|
cardNumber: '5022291012345678',
|
|
shabaNumber: 'IR980000000000000000000000',
|
|
bio: 'مدرس پایگاه داده',
|
|
isActive: true
|
|
};
|
|
|
|
const formatted = formatProfessorDoc(legacyDoc);
|
|
assert.equal(String(formatted._id), String(profId));
|
|
assert.equal(formatted.name, 'رضا');
|
|
assert.equal(formatted.surname, 'اکبری');
|
|
assert.equal(formatted.fullName, 'رضا اکبری');
|
|
assert.equal(formatted.phoneNumber, '09351234567');
|
|
assert.equal(formatted.nationalIdCode, '1234567890');
|
|
assert.equal(formatted.email, 'reza@example.com');
|
|
});
|
|
|
|
await t.test('Professor virtuals fallback correctly on legacy doc', () => {
|
|
const prof = new Professor();
|
|
prof._doc = {
|
|
name: 'مریم احمدی',
|
|
surname: 'احمدی',
|
|
phoneNumber: '09121112233',
|
|
nationalIdCode: '0076543210',
|
|
email: 'maryam@example.com'
|
|
};
|
|
|
|
assert.equal(prof.name, 'مریم احمدی');
|
|
assert.equal(prof.surname, 'احمدی');
|
|
assert.equal(prof.phoneNumber, '09121112233');
|
|
assert.equal(prof.nationalIdCode, '0076543210');
|
|
});
|
|
});
|