def test_deserialize_not_empty_but_dead():
    session = MagicMock()
    session.is_final_status.return_value = True
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = """{
  "clients": [
    {
      "name": "py",
      "id": "1",
      "sqlcontext": true,
      "kind": "pyspark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    },
    {
      "name": "sc",
      "id": "2",
      "sqlcontext": false,
      "kind": "spark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    }
  ]
}
"""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(return_value=session)
    serializer._create_livy_client = MagicMock()

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 0
    serializer._create_livy_session.assert_no_called()
    serializer._create_livy_client.assert_no_called()
def test_deserialize_not_empty_but_dead():
    session = MagicMock()
    session.is_final_status.return_value = True
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = """{
  "clients": [
    {
      "name": "py",
      "id": "1",
      "sqlcontext": true,
      "kind": "pyspark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    },
    {
      "name": "sc",
      "id": "2",
      "sqlcontext": false,
      "kind": "spark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    }
  ]
}
"""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(return_value=session)
    serializer._create_livy_client = MagicMock()

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 0
    serializer._create_livy_session.assert_no_called()
    serializer._create_livy_client.assert_no_called()
def test_deserialize_empty():
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = ""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(side_effect=ValueError)
    serializer._create_livy_client = MagicMock(side_effect=ValueError)

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 0
def test_deserialize_empty():
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = ""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(side_effect=ValueError)
    serializer._create_livy_client = MagicMock(side_effect=ValueError)

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 0
def test_deserialize_not_empty():
    session = MagicMock()
    session.is_final_status.return_value = False
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = """{
  "clients": [
    {
      "name": "py",
      "id": "1",
      "sqlcontext": true,
      "kind": "pyspark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    },
    {
      "name": "sc",
      "id": "2",
      "sqlcontext": false,
      "kind": "spark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    }
  ]
}
"""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(return_value=session)
    serializer._create_livy_client = MagicMock()

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 2

    (name, client) = deserialized[0]
    assert name == "py"
    serializer._create_livy_session.assert_any_call(
        "url=https://mysite.com/livy;username=user;password=pass",
        {"kind": "pyspark"}, serializer._ipython_display, "1", True)
    serializer._create_livy_client.assert_any_call(session)

    (name, client) = deserialized[1]
    assert name == "sc"
    serializer._create_livy_session.assert_any_call(
        "url=https://mysite.com/livy;username=user;password=pass",
        {"kind": "spark"}, serializer._ipython_display, "2", False)
    serializer._create_livy_client.assert_any_call(session)
def test_deserialize_not_empty():
    session = MagicMock()
    session.is_final_status.return_value = False
    reader_writer = MagicMock()
    reader_writer.read_lines.return_value = """{
  "clients": [
    {
      "name": "py",
      "id": "1",
      "sqlcontext": true,
      "kind": "pyspark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    },
    {
      "name": "sc",
      "id": "2",
      "sqlcontext": false,
      "kind": "spark",
      "connectionstring": "url=https://mysite.com/livy;username=user;password=pass",
      "version": "0.0.0"
    }
  ]
}
"""
    serializer = ClientManagerStateSerializer(reader_writer)
    serializer._create_livy_session = MagicMock(return_value=session)
    serializer._create_livy_client = MagicMock()

    deserialized = serializer.deserialize_state()

    assert len(deserialized) == 2

    (name, client) = deserialized[0]
    assert name == "py"
    serializer._create_livy_session.assert_any_call("url=https://mysite.com/livy;username=user;password=pass",
                                                    {"kind":"pyspark"}, serializer._ipython_display, "1", True)
    serializer._create_livy_client.assert_any_call(session)

    (name, client) = deserialized[1]
    assert name == "sc"
    serializer._create_livy_session.assert_any_call("url=https://mysite.com/livy;username=user;password=pass",
                                                     {"kind":"spark"}, serializer._ipython_display, "2", False)
    serializer._create_livy_client.assert_any_call(session)