import pytest
import os
from flattening_ocds import output, schema
import openpyxl


class MockParser(object):
    def __init__(self, main_sheet, sub_sheets):
        self.main_sheet = main_sheet
        self.sub_sheets = sub_sheets


def test_spreadsheetouput_base_fails():
    """The base class should fail as it is missing functionality that child
    classes must implement"""

    spreadsheet_output = output.SpreadsheetOutput(parser=MockParser([], {}))
    with pytest.raises(NotImplementedError):
        spreadsheet_output.write_sheets()


def test_blank_sheets(tmpdir):
    for format_name, spreadsheet_output_class in output.FORMATS.items():
        spreadsheet_output = spreadsheet_output_class(
            parser=MockParser([], {}),
            main_sheet_name='release',
            output_name=os.path.join(tmpdir.strpath, 'release'+output.FORMATS_SUFFIX[format_name]))
        spreadsheet_output.write_sheets()

    wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)
    assert wb.get_sheet_names() == ['release']

    # TODO Actually check the sheets are blank


def test_populated_sheets(tmpdir):
    for format_name, spreadsheet_output_class in output.FORMATS.items():
        subsheet = schema.SubSheet()
        subsheet.add_field('c')
        spreadsheet_output = spreadsheet_output_class(
            parser=MockParser(['a'], {'b': subsheet}),
            main_sheet_name='release',
            output_name=os.path.join(tmpdir.strpath, 'release'+output.FORMATS_SUFFIX[format_name]))
        spreadsheet_output.write_sheets()
    # TODO Actually check the sheets are populated

@@ -921,16 +921,42 @@
 eets()%0A%0A
+    # Check XLSX is empty%0A
     wb =
@@ -1067,55 +1067,338 @@
 e'%5D%0A
-%0A    # TODO Actually check the sheets are blank
+    assert len(wb%5B'release'%5D.rows) == 1%0A    assert len(wb%5B'release'%5D.rows%5B0%5D) == 1%0A    assert wb%5B'release'%5D.rows%5B0%5D%5B0%5D.value == None%0A    %0A    # Check CSV is Empty%0A    assert tmpdir.join('release').listdir() == %5B tmpdir.join('release').join('release.csv') %5D%0A    assert tmpdir.join('release', 'release.csv').read().strip('%5Cn') == ''
 %0A%0A%0Ad
@@ -1868,55 +1868,677 @@
 s()%0A
+%0A
     # 
-TODO Actually check the sheets are populated
+Check XLSX%0A    wb = openpyxl.load_workbook(tmpdir.join('release.xlsx').strpath)%0A    assert wb.get_sheet_names() == %5B'release', 'b'%5D%0A    assert len(wb%5B'release'%5D.rows) == 1%0A    assert %5B x.value for x in wb%5B'release'%5D.rows%5B0%5D %5D == %5B 'a' %5D%0A    assert len(wb%5B'b'%5D.rows) == 1%0A    assert %5B x.value for x in wb%5B'b'%5D.rows%5B0%5D %5D == %5B 'ocid', 'c' %5D%0A%0A    # Check CSV%0A    assert set(tmpdir.join('release').listdir()) == set(%5B%0A        tmpdir.join('release').join('release.csv'),%0A        tmpdir.join('release').join('b.csv')%0A    %5D)%0A    assert tmpdir.join('release', 'release.csv').read().strip('%5Cn') == 'a'%0A    assert tmpdir.join('release', 'b.csv').read().strip('%5Cn') == 'ocid,c'
 %0A

Add tests for existing xlsx and csv output (ie. from scheam)