-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestSchemas.js
72 lines (64 loc) · 2.25 KB
/
testSchemas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const mongoose = require('mongoose');
// Import models
const User = require('./models/userModel');
const Word = require('./models/wordModel');
const UserWord = require('./models/userWordModel');
// Connect to MongoDB
const connectDB = async () => {
try {
await mongoose.connect('mongodb://127.0.0.1:27017/aaronDB');
console.log('MongoDB connected...');
} catch (error) {
console.error('Database connection error:', error.message);
process.exit(1);
}
};
// Test function
const testSchemas = async () => {
try {
// Step 1: Create a test user
const user = new User({
firstName: 'John',
lastName: 'Doe',
email: '[email protected]',
password: 'securepassword',
proficiencyLevel: 'Intermediate',
learningLanguage: 'French',
});
await user.save();
console.log('User created:', user);
// Step 2: Add a test word
const word = new Word({
word: 'Bonjour',
meanings: [
{ definition: 'Hello', usageExample: 'Bonjour, comment ça va?' },
{ definition: 'Good day', usageExample: 'Bonjour, monsieur!' },
],
grammarNotes: 'Common greeting in French.',
});
await word.save();
console.log('Word created:', word);
// Step 3: Track user interaction with the word
const userWord = new UserWord({
userId: user._id,
wordId: word._id,
encounteredCount: 3,
checkedCount: 1,
isMarkedForReview: true,
});
await userWord.save();
console.log('UserWord interaction created:', userWord);
// Step 4: Query the UserWord and populate word details
const userWordDetails = await UserWord.findOne({ userId: user._id })
.populate('wordId'); // Populate word details
console.log('Populated UserWord details:', userWordDetails);
// Disconnect after testing
mongoose.disconnect();
console.log('Database connection closed.');
} catch (error) {
console.error('Error during testing:', error.message);
process.exit(1);
}
};
// Run the tests
connectDB().then(() => testSchemas());