Files
tilbudgivern/test_materials_sync.js
T
alex fe8d73e8d1 Add integration tests for Bygma import services
- Implement test script for Bygma ESG data import (test_bygma_import.js)
- Create test script for Bygma Prisbog import functionality (test_bygma_prisbog_import.js)
- Develop full integration test for Bygma materials system (test_full_bygma_integration.js)
- Add materials synchronization test script (test_materials_sync.js)
- Ensure database connection and import statistics are logged
- Validate API visibility and price updates in integration tests
- Include sample data checks and cleanup procedures in tests
2025-09-18 13:24:50 +02:00

94 lines
4.0 KiB
JavaScript

#!/usr/bin/env node
const BygmaImportService = require('./backend/src/services/bygmaImportService');
const databaseService = require('./backend/src/services/databaseService');
async function testMaterialsSync() {
console.log('🧪 Testing Bygma materials sync...\n');
try {
// Initialize database service
await databaseService.initialize();
console.log('✅ Database connection established');
// Check current materials count
const beforeMaterials = await databaseService.query(
'SELECT COUNT(*) as count FROM materials'
);
const beforePrices = await databaseService.query(
'SELECT COUNT(*) as count FROM material_prices WHERE supplier_name = "Bygma A/S"'
);
console.log(`📊 Before import:`);
console.log(` - Materials: ${beforeMaterials[0].count}`);
console.log(` - Bygma prices: ${beforePrices[0].count}`);
// Test import with a small sample
const csvPath = '/home/alex/git/tilbudgivern/frontend/firmadata/subvendor/bygma/bygma_esg_data.csv';
// Create a small test file with just a few rows for testing
const testCsvPath = '/tmp/bygma_test_sample.csv';
// Read first 10 lines of the original file
const fs = require('fs');
const originalContent = fs.readFileSync(csvPath, 'utf-8');
const lines = originalContent.split('\n');
const testContent = lines.slice(0, 11).join('\n'); // Header + 10 data rows
fs.writeFileSync(testCsvPath, testContent);
console.log(`📂 Created test file with 10 sample rows`);
// Import the test data
const bygmaImporter = new BygmaImportService(databaseService);
const result = await bygmaImporter.importESGData(testCsvPath, 'bygma_test_sample.csv');
console.log('\n🎉 Import completed!');
console.log(`📊 Import Statistics:`);
console.log(` - Processed: ${result.importStats.processed} records`);
console.log(` - Imported: ${result.importStats.imported} records`);
console.log(` - Failed: ${result.importStats.failed} records`);
// Check results in materials system
const afterMaterials = await databaseService.query(
'SELECT COUNT(*) as count FROM materials'
);
const afterPrices = await databaseService.query(
'SELECT COUNT(*) as count FROM material_prices WHERE supplier_name = "Bygma A/S"'
);
console.log(`\n📊 After import:`);
console.log(` - Materials: ${afterMaterials[0].count} (+${afterMaterials[0].count - beforeMaterials[0].count})`);
console.log(` - Bygma prices: ${afterPrices[0].count} (+${afterPrices[0].count - beforePrices[0].count})`);
// Show some sample materials
const sampleMaterials = await databaseService.query(`
SELECT m.sku, m.name, m.unit, m.category, m.brand,
mp.price, mp.valid_from
FROM materials m
LEFT JOIN material_prices mp ON m.id = mp.material_id AND mp.is_active = 1
WHERE m.brand = 'Bygma A/S'
ORDER BY m.created_at DESC
LIMIT 5
`);
console.log('\n📦 Sample materials in system:');
sampleMaterials.forEach((material, index) => {
console.log(` ${index + 1}. ${material.sku}: ${material.name.substring(0, 50)}...`);
console.log(` Category: ${material.category}, Unit: ${material.unit}`);
console.log(` Price: ${material.price ? material.price + ' DKK' : 'N/A'} (from ${material.valid_from || 'N/A'})`);
});
// Clean up test file
fs.unlinkSync(testCsvPath);
console.log('\n🧹 Test file cleaned up');
} catch (error) {
console.error('❌ Test failed:', error.message);
console.error('Stack:', error.stack);
}
console.log('\n🏁 Test completed');
}
testMaterialsSync();