Commit 67764478 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

csvfile stub, runtimeexception

parent 5c0e8ddf
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -7,7 +7,8 @@ project(fnordmetric)
enable_testing()

set(FNORDMETRIC_SOURCES
    src/util/format.cc)
    src/util/format.cc
    src/util/runtimeexception.cc)

set(FNORDMETRIC_UI_SOURCES
    ${FNORDMETRIC_SOURCES}
@@ -37,18 +38,26 @@ set(FNORDMETRIC_QUERY_SOURCES
    src/query/token.cc
    src/query/tokenize.cc)

set(FNORDMETRIC_CSV_BACKEND_SOURCES
    src/backends/csv/csvfile.cc)

include_directories(./src)
set(CMAKE_CXX_FLAGS "-std=c++0x -stdlib=libc++")

# add_library(fnordmetric SHARED ${FNORDMETRIC_SOURCES})

add_test(QueryTest test/query-test)
add_executable(test/query-test
add_test(QueryTest test/test-query)
add_executable(test/test-query
    ${FNORDMETRIC_QUERY_SOURCES}
    src/query/query_test.cc)


add_test(QueryTest test/ui-test)
add_executable(test/ui-test
add_test(UITest test/test-ui)
add_executable(test/test-ui
    ${FNORDMETRIC_UI_SOURCES}
    src/ui/ui_test.cc)

add_test(CSVBackendTest test/test-csv-backend)
add_executable(test/test-csv-backend
    ${FNORDMETRIC_UI_SOURCES}
    ${FNORDMETRIC_CSV_BACKEND_SOURCES}
    src/backends/csv/csvbackend_test.cc)
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@

      <div class="jumbotron">
        <h1>Create beautiful charts from SQL</h1>
        <p class="lead">Create beautiful charts and dashbaords directly from SQL with FnordMetric.</p>
        <p class="lead">Cut out the middleman. Build beautiful charts and realtime dashboards directly from SQL.</p>
      </div>

      <div style="height:430px; float: right; width: 670px;">
+51 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordStream" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/fcntl.h>
#include <unistd.h>
#include "csvfile.h"
//#include "csv_backend.h"
#include "../../util/runtimeexception.h"

namespace fnordmetric {
namespace csv_backend {

class CSVBackendTest {
public:
  CSVBackendTest() {}

  void run() {
    testOpenFile();
  }

  void testOpenFile() {
    auto csv_file = CSVFile::openFile("test/fixtures/gbp_per_country");
    assert(csv_file.get() != nullptr);
    assert(csv_file->fd_ > 0);
  }

};

}
}

int main() {
  fnordmetric::csv_backend::CSVBackendTest test;

  try {
    test.run();
  } catch (fnordmetric::util::RuntimeException e) {
    printf("test fail :(\n\n");
    e.debugPrint();
    exit(1);
  }

  printf("all tests passed! :)\n");
}
+33 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#include <assert.h>
#include <fcntl.h>
#include "csvfile.h"
#include "../../util/runtimeexception.h"

namespace fnordmetric {
namespace csv_backend {

std::unique_ptr<CSVFile> CSVFile::openFile(const std::string& file_path) {
  int fd = open(file_path.c_str(), O_RDONLY);

  if (fd < 1) {
    throw RUNTIME_EXCEPTION(
      "error opening file '%s':",
      file_path.c_str());
  }

  auto csv_file = new CSVFile(fd);
  return std::unique_ptr<CSVFile>(csv_file);
}

CSVFile::CSVFile(int fd) : fd_(fd) {
  assert(fd > 0);
}

}
}
+45 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * Licensed under the MIT license (see LICENSE).
 */
#ifndef _FNORDMETRIC_CSVFILE_H
#define _FNORDMETRIC_CSVFILE_H
#include <string>
#include <vector>
#include <memory>

namespace fnordmetric {
namespace csv_backend {

class CSVFile {
  friend class CSVBackendTest;
public:

  /**
   * Open a new csv file from the provided file path. Throws an exception if
   * the file cannot be opened.
   *
   * @param file_path the path to the csv file
   */
  static std::unique_ptr<CSVFile> openFile(const std::string& file_path);

  /**
   * Create a new CSVFile instance with the provided filedescriptor. The csv
   * file takes ownership of the fd and will close() it when destructed.
   *
   * @param fd a valid an opened fd, transfers ownership and closes on destruct
   */
  explicit CSVFile(int fd);

  CSVFile(const CSVFile& other) = delete;
  CSVFile& operator=(const CSVFile& other) = delete;

protected:
  int fd_;
};

}
}
#endif
Loading