Skip to content

Commit

Permalink
Add tests for the Geojson writer (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
franziska-wegner committed Dec 23, 2023
1 parent f892c45 commit 5e69055
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
65 changes: 65 additions & 0 deletions tests/IO/TestGeojsonWriter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* TestGeojsonWriter.cpp
*
* Created on: Nov 04, 2019
* Author: Franziska Wegner
*/

#include "TestGeojsonWriter.hpp"
#include "IO/Writer/GeojsonWriter.hpp"

#include "Auxiliary/Types.hpp"
#include "Helper/TestHelper.hpp"

#include <fstream>
#include <sstream>

namespace egoa::test {

TEST_F ( TestGeojsonExample, FilenameWriteTest )
{
if ( ! egoa::PowerGridIO<TGraph>::write ( network_
, TestCaseSmallExampleOutputFile_
, TPowerGridIO::WriteGeoJson ) )
{
ASSERT_TRUE(false);
}
}

TEST_F ( TestGeojsonExample, OutputStreamWriteTest )
{
std::ofstream file;
file.open(TestCaseSmallExampleOutputFile_, std::ofstream::trunc);
if (!file.is_open()) return;
file.seekp(0, std::ios::end); // file is empty

if ( ! egoa::PowerGridIO<TGraph>::write ( network_
, file
, TPowerGridIO::WriteGeoJson ) )
{
ASSERT_TRUE(false);
}
}

TEST_F ( TestGeojsonExample, CompareGeneratedStringWithFile )
{
// Comparison string
std::stringstream genfileStream;
if ( ! egoa::PowerGridIO<TGraph>::write ( network_
, genfileStream
, TPowerGridIO::WriteGeoJson ) )
{
EXPECT_TRUE(false);
}
Types::string genfile = genfileStream.str();

// Expected output converted to string
std::ifstream inFile; inFile.open(TestCaseSmallExampleExpectedOutput_);
std::stringstream expectedOutputStream;
expectedOutputStream << inFile.rdbuf();
Types::string expectedOutput = expectedOutputStream.str();

EXPECT_EQ(expectedOutput,genfile);
}

} //namespace egoa::test
87 changes: 87 additions & 0 deletions tests/IO/TestGeojsonWriter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* TestGeojsonWriter.hpp
*
* Created on: Nov 04, 2019
* Author: Franziska Wegner
*/

#ifndef EGOA___TESTS___IO___TEST_GEO_JSON_WRITER_HPP
#define EGOA___TESTS___IO___TEST_GEO_JSON_WRITER_HPP

#include "gtest/gtest.h"
#include "gmock/gmock.h"

#include "IO/PowerGridIO.hpp"

#include "DataStructures/Networks/PowerGrid.hpp"

#include "DataStructures/Graphs/StaticGraph.hpp"
#include "DataStructures/Graphs/DynamicGraph.hpp"

namespace egoa::test {

class TestGeojsonWriter : public ::testing::Test {
protected:
// Vertices
using TVertex = Vertices::Vertex<Vertices::ElectricalProperties<>>;
using TVertexType = typename TVertex::TProperties::TVertexType;
using TVertexProperties = typename TVertex::TProperties;
// Edges
using TEdge = Edges::Edge<Edges::ElectricalProperties>;
using TEdgeProperties = typename TEdge::TProperties;
// Network specific types
using TGraph = StaticGraph< Vertices::ElectricalProperties<>
, Edges::ElectricalProperties>;
// Network specific types
using TGeneratorProperties = Vertices::GeneratorProperties<>;
using TLoadProperties = Vertices::LoadProperties<Vertices::IeeeBusType>;
using TNetwork = PowerGrid<TGraph>;
// IO
using TPowerGridIO = PowerGridIO<TGraph>;

protected:
TestGeojsonWriter()
: network_(){}

TestGeojsonWriter( TNetwork & network )
: network_(){}

// You can do clean-up work that doesn't throw exceptions here.
virtual ~TestGeojsonWriter(){}

protected:
TGraph graph_;
TGraph const & graphConst_ = graph_;
TNetwork network_;
TNetwork const & networkConst_ = network_;
};

class TestGeojsonExample : public TestGeojsonWriter {
protected:
TestGeojsonExample ()
: TestGeojsonWriter ()
{}

virtual void SetUp () override
{
if ( ! egoa::PowerGridIO<TGraph>::read( network_
, graph_
, TestCaseSmallExampleInput_
, TPowerGridIO::ReadPyPsa ) )
{
std::cerr << "Expected file "
<< TestCaseSmallExampleInput_
<< " does not exist!";
ASSERT_TRUE(false);
}
}

Types::string const TestCaseSmallExampleInput_ = "../../framework/tests/Data/PowerGrids/PyPsaExampleGeoJsonWriter";
Types::string const TestCaseSmallExampleExpectedOutput_ = "../../framework/tests/Data/Output/PyPsaExampleJsonWriterExpectedOutput.json";
Types::string const TestCaseSmallExampleOutputFile_ = "../../framework/tests/Data/Output/PyPsaExampleJsonWriter.json";
};


} // namespace egoa::test

#endif // EGOA___TESTS___IO___TEST_GEO_JSON_WRITER_HPP

0 comments on commit 5e69055

Please sign in to comment.