Back to Repositories

Testing Git Adapter Database Operations in Insomnia

This test suite evaluates the Git adapter functionality in the Insomnia project, focusing on database seeding and filtering capabilities. The tests verify the adapter’s ability to handle Git repository data and validate the correct population of different data types like API specs, environments, and test suites.

Test Coverage Overview

The test suite provides comprehensive coverage of the Git adapter’s core functionality.

Key areas tested include:
  • Database seeding from Git repository directory
  • Filtered data loading capabilities
  • Invalid directory handling
  • Verification of multiple data type populations
Integration points focus on file system interactions and Git repository structure parsing.

Implementation Analysis

The testing approach utilizes Jest’s asynchronous testing capabilities with async/await patterns for handling Git operations. The implementation leverages path manipulation for cross-platform compatibility and employs expect assertions to validate database object structures and counts.

Technical patterns include:
  • Directory path resolution using path.join()
  • Async function testing
  • Null handling verification
  • Array length assertions for different data types

Technical Details

Testing tools and configuration:
  • Test Framework: Vitest
  • File System: Node.js path module
  • Fixture Setup: Git repository test data
  • Database Types: ApiSpec, Environment, Request, RequestGroup, Workspace, UnitTestSuite, UnitTest

Best Practices Demonstrated

The test suite demonstrates strong testing practices through organized and focused test cases. Each test has a clear purpose and validates specific functionality aspects.

Notable practices include:
  • Isolated test cases with specific assertions
  • Proper error case handling
  • Consistent testing patterns
  • Clear test descriptions
  • Effective use of test fixtures

kong/insomnia

packages/insomnia-inso/src/db/adapters/git-adapter.test.ts

            
import path from 'path';
import { describe, expect, it } from 'vitest';

import gitAdapter from './git-adapter';

describe('gitAdapter()', () => {
  const fixturesPath = path.join(__dirname, '../fixtures');

  it('should seed with git-repo directory', async () => {
    const workingDir = path.join(fixturesPath, 'git-repo');
    const db = await gitAdapter(workingDir);
    expect(db?.ApiSpec.length).toBe(1);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(2);
    expect(db?.RequestGroup.length).toBe(1);
    expect(db?.Workspace.length).toBe(1);
    expect(db?.UnitTestSuite.length).toBe(2);
    expect(db?.UnitTest.length).toBe(4);
  });

  it('should seed with git-repo directory with filter', async () => {
    const workingDir = path.join(fixturesPath, 'git-repo');
    const db = await gitAdapter(workingDir, ['Environment']);
    expect(db?.ApiSpec.length).toBe(0);
    expect(db?.Environment.length).toBe(2);
    expect(db?.Request.length).toBe(0);
    expect(db?.RequestGroup.length).toBe(0);
    expect(db?.Workspace.length).toBe(0);
    expect(db?.UnitTestSuite.length).toBe(0);
    expect(db?.UnitTest.length).toBe(0);
  });

  it('should return null if data directory is invalid', async () => {
    const workingDir = path.join(fixturesPath, 'nedb');
    const db = await gitAdapter(workingDir);
    expect(db).toBe(null);
  });
});