|
1 | 1 | import { expect } from 'chai'; |
2 | 2 | import { faker } from '@faker-js/faker/locale/en'; |
3 | | -import { generateScript } from './script-generation-utils'; |
| 3 | +import { generateScript, generateDocument } from './script-generation-utils'; |
4 | 4 | import type { FakerFieldMapping } from './types'; |
5 | 5 |
|
6 | 6 | /** |
@@ -1421,4 +1421,151 @@ describe('Script Generation', () => { |
1421 | 1421 | } |
1422 | 1422 | }); |
1423 | 1423 | }); |
| 1424 | + |
| 1425 | + describe('generateDocument', () => { |
| 1426 | + it('should generate document with simple flat fields', () => { |
| 1427 | + const schema = { |
| 1428 | + name: { |
| 1429 | + mongoType: 'String' as const, |
| 1430 | + fakerMethod: 'person.fullName', |
| 1431 | + fakerArgs: [], |
| 1432 | + probability: 1.0, |
| 1433 | + }, |
| 1434 | + age: { |
| 1435 | + mongoType: 'Number' as const, |
| 1436 | + fakerMethod: 'number.int', |
| 1437 | + fakerArgs: [{ json: '{"min": 18, "max": 65}' }], |
| 1438 | + probability: 1.0, |
| 1439 | + }, |
| 1440 | + }; |
| 1441 | + |
| 1442 | + const document = generateDocument(schema); |
| 1443 | + |
| 1444 | + expect(document).to.be.an('object'); |
| 1445 | + expect(document).to.have.property('name'); |
| 1446 | + expect(document.name).to.be.a('string').and.not.be.empty; |
| 1447 | + expect(document).to.have.property('age'); |
| 1448 | + expect(document.age).to.be.a('number'); |
| 1449 | + expect(document.age).to.be.at.least(18).and.at.most(65); |
| 1450 | + }); |
| 1451 | + |
| 1452 | + it('should generate document with arrays', () => { |
| 1453 | + const schema = { |
| 1454 | + 'tags[]': { |
| 1455 | + mongoType: 'String' as const, |
| 1456 | + fakerMethod: 'lorem.word', |
| 1457 | + fakerArgs: [], |
| 1458 | + probability: 1.0, |
| 1459 | + }, |
| 1460 | + }; |
| 1461 | + |
| 1462 | + const document = generateDocument(schema, { 'tags[]': 2 }); |
| 1463 | + |
| 1464 | + expect(document).to.be.an('object'); |
| 1465 | + expect(document).to.have.property('tags'); |
| 1466 | + expect(document.tags).to.be.an('array').with.length(2); |
| 1467 | + for (const tag of document.tags as string[]) { |
| 1468 | + expect(tag).to.be.a('string').and.not.be.empty; |
| 1469 | + } |
| 1470 | + }); |
| 1471 | + |
| 1472 | + it('should generate document with complex nested arrays and custom lengths', () => { |
| 1473 | + const schema = { |
| 1474 | + 'users[].posts[].tags[]': { |
| 1475 | + mongoType: 'String' as const, |
| 1476 | + fakerMethod: 'lorem.word', |
| 1477 | + fakerArgs: [], |
| 1478 | + probability: 1.0, |
| 1479 | + }, |
| 1480 | + 'matrix[][]': { |
| 1481 | + mongoType: 'Number' as const, |
| 1482 | + fakerMethod: 'number.int', |
| 1483 | + fakerArgs: [{ json: '{"min": 1, "max": 10}' }], |
| 1484 | + probability: 1.0, |
| 1485 | + }, |
| 1486 | + }; |
| 1487 | + |
| 1488 | + const arrayLengthMap = { |
| 1489 | + 'users[]': 2, |
| 1490 | + 'users[].posts[]': 3, |
| 1491 | + 'users[].posts[].tags[]': 4, |
| 1492 | + 'matrix[]': 2, |
| 1493 | + 'matrix[][]': 3, |
| 1494 | + }; |
| 1495 | + |
| 1496 | + const document = generateDocument(schema, arrayLengthMap); |
| 1497 | + |
| 1498 | + expect(document).to.be.an('object'); |
| 1499 | + |
| 1500 | + // Check users array structure |
| 1501 | + expect(document).to.have.property('users'); |
| 1502 | + expect(document.users).to.be.an('array').with.length(2); |
| 1503 | + |
| 1504 | + // Check nested structure with proper types |
| 1505 | + const users = document.users as Array<{ |
| 1506 | + posts: Array<{ tags: string[] }>; |
| 1507 | + }>; |
| 1508 | + |
| 1509 | + for (const user of users) { |
| 1510 | + expect(user).to.be.an('object'); |
| 1511 | + expect(user).to.have.property('posts'); |
| 1512 | + expect(user.posts).to.be.an('array').with.length(3); |
| 1513 | + |
| 1514 | + for (const post of user.posts) { |
| 1515 | + expect(post).to.be.an('object'); |
| 1516 | + expect(post).to.have.property('tags'); |
| 1517 | + expect(post.tags).to.be.an('array').with.length(4); |
| 1518 | + |
| 1519 | + for (const tag of post.tags) { |
| 1520 | + expect(tag).to.be.a('string').and.not.be.empty; |
| 1521 | + } |
| 1522 | + } |
| 1523 | + } |
| 1524 | + |
| 1525 | + // Check matrix (2D array) |
| 1526 | + expect(document).to.have.property('matrix'); |
| 1527 | + expect(document.matrix).to.be.an('array').with.length(2); |
| 1528 | + |
| 1529 | + const matrix = document.matrix as number[][]; |
| 1530 | + for (const row of matrix) { |
| 1531 | + expect(row).to.be.an('array').with.length(3); |
| 1532 | + for (const cell of row) { |
| 1533 | + expect(cell).to.be.a('number').and.be.at.least(1).and.at.most(10); |
| 1534 | + } |
| 1535 | + } |
| 1536 | + }); |
| 1537 | + |
| 1538 | + it('should handle probability fields correctly', () => { |
| 1539 | + const schema = { |
| 1540 | + name: { |
| 1541 | + mongoType: 'String' as const, |
| 1542 | + fakerMethod: 'person.fullName', |
| 1543 | + fakerArgs: [], |
| 1544 | + probability: 1.0, |
| 1545 | + }, |
| 1546 | + optionalField: { |
| 1547 | + mongoType: 'String' as const, |
| 1548 | + fakerMethod: 'lorem.word', |
| 1549 | + fakerArgs: [], |
| 1550 | + probability: 0.0, // Should never appear |
| 1551 | + }, |
| 1552 | + alwaysPresent: { |
| 1553 | + mongoType: 'Number' as const, |
| 1554 | + fakerMethod: 'number.int', |
| 1555 | + fakerArgs: [], |
| 1556 | + probability: 1.0, |
| 1557 | + }, |
| 1558 | + }; |
| 1559 | + |
| 1560 | + const document = generateDocument(schema); |
| 1561 | + |
| 1562 | + expect(document).to.be.an('object'); |
| 1563 | + expect(document).to.have.property('name'); |
| 1564 | + expect(document.name).to.be.a('string').and.not.be.empty; |
| 1565 | + expect(document).to.have.property('alwaysPresent'); |
| 1566 | + expect(document.alwaysPresent).to.be.a('number'); |
| 1567 | + // optionalField should not be present due to 0.0 probability |
| 1568 | + expect(document).to.not.have.property('optionalField'); |
| 1569 | + }); |
| 1570 | + }); |
1424 | 1571 | }); |
0 commit comments