Skip to content

Commit

Permalink
Enabling and fix stream unit test for modem code
Browse files Browse the repository at this point in the history
  • Loading branch information
kotl committed May 7, 2018
1 parent 4c5a98c commit a7bf160
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 30 deletions.
3 changes: 1 addition & 2 deletions contrib/src/modem/db/stream-command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ bool StreamCommand::execute(const String& command,
out->println(fbase().error().c_str());
return false;
}

bool running = true;
DynamicJsonBuffer buffer;
while(running) {
if (fbase().available()) {
FirebaseObject event = fbase().readEvent();
out->print("+");
out->print(event.getString("event").c_str());
out->print(event.getString("type").c_str());
out->print(" ");
String data = event.getString("data");
out->println(event.getString("path"));
Expand Down
1 change: 1 addition & 0 deletions contrib/test/mock-firebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class MockFirebase : public FirebaseArduino {
MOCK_METHOD0(available, bool ());
MOCK_METHOD0(readEvent, FirebaseObject ());
MOCK_METHOD2(begin, void (const String& host, const String& auth));
MOCK_METHOD1(stream, void (const String& path));
};

} // modem
Expand Down
19 changes: 9 additions & 10 deletions contrib/test/modem/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ CXXFLAGS += -g -Wall -Wextra -pthread -std=c++11
# created to the list.
TESTS = get-command_test set-command_test remove-command_test \
push-command_test begin-command_test \
serial-transceiver_test
# stream-command_test
serial-transceiver_test stream-command_test

# All Google Test headers. Usually you shouldn't change this
# definition.
Expand Down Expand Up @@ -200,15 +199,15 @@ begin-command_test : begin-command.o begin-command_test.o FirebaseArduino.o Fire
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@


#stream-command.o : $(SRC_ROOT)/modem/db/stream-command.cpp
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC_ROOT)/modem/db/stream-command.cpp
#
#stream-command_test.o : $(TEST_DIR)/stream-command_test.cpp $(GMOCK_HEADERS)
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/stream-command_test.cpp
#
#stream-command_test : stream-command.o stream-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
stream-command.o : $(SRC_ROOT)/modem/db/stream-command.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(SRC_ROOT)/modem/db/stream-command.cpp

stream-command_test.o : $(TEST_DIR)/stream-command_test.cpp $(GMOCK_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(TEST_DIR)/stream-command_test.cpp

stream-command_test : stream-command.o stream-command_test.o FirebaseArduino.o Firebase.o FirebaseObject.o FirebaseHttpClient_dummy.o gmock_main.a \
arduino_mock_all.a
# $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@


SerialTransceiver.o : $(SRC_ROOT)/modem/SerialTransceiver.cpp
Expand Down
36 changes: 19 additions & 17 deletions contrib/test/modem/stream-command_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,41 +17,41 @@ using ::testing::_;
class StreamCommandTest : public ::testing::Test {
protected:
void SetUp() override {
stream_.reset(new FirebaseStream());
}

bool RunCommand(const FirebaseError& error) {
bool RunCommand() {
StreamCommand cmd(&fbase_);
return cmd.execute("BEGIN_STREAM", &in_, &out_);
}

MockInputStream in_;
MockOutputStream out_;
MockFirebase fbase_;
std::unique_ptr<FirebaseStream> stream_;
};

TEST_F(StreamCommandTest, streams) {
const String path("/test/path");

const String no_error = "";
EXPECT_CALL(fbase_, error()).WillRepeatedly(ReturnRef(no_error));

const String data = "TestValue";
const String value(String("{\"path\":\"/test/path\",\"data\":\"") + data + "\",\"type\":\"PUT\"}");

const FirebaseObject fo = FirebaseObject(value.c_str());
EXPECT_CALL(fbase_, readEvent()).WillRepeatedly(Return(fo));

EXPECT_CALL(in_, available())
.WillRepeatedly(Return(true));

EXPECT_CALL(in_, readLine())
.WillOnce(Return(path))
.WillOnce(Return("END_STREAM"));

const String data = "Test Value";
const String value(String("{\"path\" : \"/test/path\", \"data\" : \"") + data + "\"}");
EXPECT_CALL(fbase_, available())
EXPECT_CALL(fbase_, available())
.WillOnce(Return(true))
.WillRepeatedly(Return(false));

EXPECT_CALL(fbase_, startStreaming());
EXPECT_CALL(*stream_, read(_))
.WillOnce(Invoke([&value](std::string& json) {
json = value.c_str();
return FirebaseStream::PUT;
}));
EXPECT_CALL(fbase_, stream(path));

EXPECT_CALL(out_, print(String("+")))
.WillOnce(Return(1));
Expand All @@ -70,21 +70,23 @@ TEST_F(StreamCommandTest, streams) {
EXPECT_CALL(out_, println(String("+OK")))
.WillOnce(Return(1));

ASSERT_TRUE(RunCommand(FirebaseError()));
ASSERT_TRUE(RunCommand());
}

TEST_F(StreamCommandTest, handlesError) {
FirebaseError error(-200, "Test Error.");
const String error("Test Error.");
const String path("/test/path");
EXPECT_CALL(in_, readLine())
.WillOnce(Return(path));

EXPECT_CALL(fbase_, error()).WillRepeatedly(ReturnRef(error));

EXPECT_CALL(out_, print(String("-FAIL ")))
.WillOnce(Return(1));

EXPECT_CALL(out_, println(String(error.message().c_str())))
EXPECT_CALL(out_, println(String(error.c_str())))
.WillOnce(Return(1));
ASSERT_FALSE(RunCommand(error));
ASSERT_FALSE(RunCommand());
}

} // modem
Expand Down
2 changes: 1 addition & 1 deletion src/FirebaseArduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ class FirebaseArduino {
* monitoring available() and calling readEvent() to get new events.
* \param path The path inside of your db to the node you wish to monitor.
*/
void stream(const String& path);
virtual void stream(const String& path);

/**
* Checks if there are new events available. This is only meaningful once
Expand Down

0 comments on commit a7bf160

Please sign in to comment.