Market Data Service is a high-performance financial data API that provides comprehensive Symbol prices of different markets through both RESTful endpoints and real-time WebSocket connections.

livePriceController.test.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const request = require('supertest');
  2. const app = require('../src/app');
  3. const { LivePrice, Symbol, sequelize } = require('../src/models');
  4. describe('LivePrice Controller Integration Tests', () => {
  5. beforeAll(async () => {
  6. // Verify database connection
  7. await sequelize.authenticate();
  8. // Sync models
  9. await sequelize.sync({ force: true });
  10. // Create test symbols
  11. await Symbol.bulkCreate([
  12. { symbol: 'TEST1', exchange: 'TEST', instrumentType: 'forex' },
  13. { symbol: 'TEST2', exchange: 'TEST', instrumentType: 'stock' }
  14. ]);
  15. // Get created symbols
  16. const symbols = await Symbol.findAll();
  17. // Create test live prices using actual symbol IDs
  18. await LivePrice.bulkCreate([
  19. {
  20. symbolId: symbols[0].id,
  21. price: 1.12348, // Added required price field
  22. bid: 1.12345,
  23. ask: 1.12350,
  24. lastUpdated: new Date()
  25. },
  26. {
  27. symbolId: symbols[1].id,
  28. price: 100.55, // Added required price field
  29. bid: 100.50,
  30. ask: 100.60,
  31. lastUpdated: new Date()
  32. }
  33. ]);
  34. });
  35. afterAll(async () => {
  36. await LivePrice.destroy({ where: {} });
  37. await Symbol.destroy({ where: {} });
  38. await sequelize.close();
  39. });
  40. describe('GET /api/live-prices', () => {
  41. it('should return all live prices with symbol associations', async () => {
  42. const response = await request(app)
  43. .get('/api/live-prices')
  44. .expect(200);
  45. expect(response.body.success).toBe(true);
  46. expect(response.body.data.length).toBe(2);
  47. // Verify association alias change
  48. expect(response.body.data[0].livePriceSymbol).toBeDefined();
  49. expect(response.body.data[0].livePriceSymbol.symbol).toBe('TEST1');
  50. expect(response.body.data[1].livePriceSymbol.symbol).toBe('TEST2');
  51. });
  52. });
  53. describe('GET /api/live-prices/:symbolId', () => {
  54. it('should return live price for specific symbol', async () => {
  55. const response = await request(app)
  56. .get('/api/live-prices/1')
  57. .expect(200);
  58. expect(response.body.success).toBe(true);
  59. expect(response.body.data.livePriceSymbol.symbol).toBe('TEST1');
  60. });
  61. it('should return 404 for invalid symbol ID', async () => {
  62. await request(app)
  63. .get('/api/live-prices/999')
  64. .expect(404);
  65. });
  66. });
  67. describe('GET /api/live-prices/exchange/:exchange', () => {
  68. it('should return live prices filtered by exchange', async () => {
  69. const response = await request(app)
  70. .get('/api/live-prices/exchange/TEST')
  71. .expect(200);
  72. expect(response.body.success).toBe(true);
  73. expect(response.body.data.length).toBe(2);
  74. response.body.data.forEach(price => {
  75. expect(price.livePriceSymbol.exchange).toBe('TEST');
  76. });
  77. });
  78. });
  79. describe('GET /api/live-prices/type/:type', () => {
  80. it('should return live prices filtered by instrument type', async () => {
  81. const response = await request(app)
  82. .get('/api/live-prices/type/forex')
  83. .expect(200);
  84. expect(response.body.success).toBe(true);
  85. expect(response.body.data.length).toBe(1);
  86. expect(response.body.data[0].livePriceSymbol.instrumentType).toBe('forex');
  87. });
  88. });
  89. });