{"size": 3341, "ext": "js", "max_stars_repo_path": "test/backend/filesystem.js", "max_stars_repo_name": "datopian/metastore-lib-js", "max_stars_repo_stars_event_min_datetime": "2021-02-01T15:26:25.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-23T15:50:17.000Z", "max_issues_repo_path": "test/backend/filesystem.js", "max_issues_repo_name": "datopian/metastore-lib-js", "max_issues_repo_issues_event_min_datetime": "2021-01-04T10:46:42.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-20T08:27:48.000Z", "max_forks_repo_path": "test/backend/filesystem.js", "max_forks_repo_name": "datopian/metastore-lib-js", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 6, "max_issues_count": 12, "max_forks_count": null, "avg_line_length": 34.8020833333, "max_line_length": 76, "alphanum_fraction": 0.673151751}
import { expect } from 'chai'
import { FilesystemStorage } from '../../src/backend/filesystem'
import { _createTestDatapackage } from '../commonTestFunctions'
const fs = require('fs')
import path from 'path'
import { Author } from '../../src/types'

before(async () => {
  let basePath = path.join(process.cwd(), 'tmp')
  fs.mkdirSync(basePath)
})

after(async () => {
  let basePath = path.join(process.cwd(), 'tmp')
  fs.rmdirSync(basePath, { recursive: true })
})


describe('File System Backend', () => {
  it('Checks if a base directory tmp exists', () => {
    let storage = new FilesystemStorage()
    expect(storage._fs.path).to.eq(path.join(process.cwd(), 'tmp'))
  })

  it('Creates a new data package dir', () => {
    let storage = new FilesystemStorage()
    storage.create('France-bud-2020')
    let dirs = storage._fs.readSync()
    expect(dirs.name).to.eq('France-bud-2020')
  })

  it('Throws error on duplicate package creation', () => {
    let storage = new FilesystemStorage()
    storage.create('Japan-Budget-2000')
    expect(() => {
      storage.create('Japan-Budget-2000')
    }).to.throw(`EEXIST: file Japan-Budget-2000 already exists`)
  })

  it('Creates a new data package file in dir', () => {
    let storage = new FilesystemStorage()
    let name = 'Nigeria-Financial-2020'
    let metadata = _createTestDatapackage(name)
    let author = new Author('Rising Odegua', 'rising@datopian.com')
    let message = 'This is my financial budget file'

    let objectInfo = storage.create(name, metadata, author, message)
    let filePath = path.join(process.cwd(), 'tmp', name, 'datapackage.json')
    let datapackage = JSON.parse(fs.readFileSync(filePath))
    expect(datapackage.name).to.eq(name)
    expect(datapackage.resources[0].path).to.eq('data/myresource.csv')
  })

  it('Fetchs a data package file in dir', () => {
    let storage = new FilesystemStorage()
    let name = 'India-Financial-2020'
    let metadata = _createTestDatapackage(name)
    let author = new Author('Rising Odegua', 'rising@datopian.com')
    let message = 'This is my financial budget file'

    storage.create(name, metadata, author, message)

    let datapackage = storage.fetch(name)
    expect(datapackage.name).to.eq(name)
    expect(datapackage.resources[0].path).to.eq('data/myresource.csv')
  })

  it('Throws error on fetch object not exist', () => {
    let storage = new FilesystemStorage()
    expect(() => {
      storage.fetch('Some-file')
    }).to.throw('ENOENT: no such file or directory')
  })

  it('Update an existing data package', () => {
    let storage = new FilesystemStorage()
    let name = 'India-Finance-2020'
    let metadata = _createTestDatapackage(name)
    let author = new Author('Rising Odegua', 'rising@datopian.com')
    let message = 'This is my financial budget file'

    storage.create(name, metadata, author, message)

    let newMetadata = _createTestDatapackage('India-New-2021')
    let newAuthor = new Author('Rising Odegua', 'rising@datopian.com')
    let newMessage = 'This is an updated file'

    storage.update(name, newMetadata, newAuthor, false, newMessage)

    let datapackage = storage.fetch(name)
    expect(datapackage.name).to.eq('India-New-2021')
    expect(datapackage.resources[0].path).to.eq('data/myresource.csv')
    // expect(datapackage.revision).to.eq(1)
  })
})
