public void GetTallyEntriesByUnitCode()
        {
            var unit        = Units.First();
            var subpop      = Subpops[0];
            var stratum     = subpop.StratumCode;
            var sampleGroup = subpop.SampleGroupCode;
            var species     = subpop.SpeciesCode;
            var liveDead    = subpop.LiveDead;

            using (var database = CreateDatabase())
            {
                var datastore = new TallyDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));
                var tpds      = new TallyPopulationDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID);
                var cuds      = new CuttingUnitDatastore(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));

                var pop = tpds.GetTallyPopulation(unit, stratum, sampleGroup, species, liveDead);

                // insert entry using InsertTallyAction
                datastore.InsertTallyAction(new TallyAction(unit, pop));
                var tallyEntries = datastore.GetTallyEntriesByUnitCode(unit);
                tallyEntries.Should().HaveCount(1);

                // add another entry using insertTallyLedger
                datastore.InsertTallyLedger(new TallyLedger(unit, pop));
                tallyEntries = datastore.GetTallyEntriesByUnitCode(unit);
                tallyEntries.Should().HaveCount(2);

                // inset a tally ledger with plot number
                // and conferm that GetTallyEntriesByUnitCode doesn't return plot tally entries
                cuds.AddNewPlot(unit);
                datastore.InsertTallyAction(new TallyAction(unit, 1, pop));
                tallyEntries = datastore.GetTallyEntriesByUnitCode(unit);
                tallyEntries.Should().HaveCount(2);
            }
        }
        [InlineData("st3", "sg1", "sp1", "L", SampleResult.I)] // tally by subpop - insurance
        public void InsertTallyAction(string stratumCode, string sgCode, string species, string liveDead, SampleResult sampleResult)
        {
            var unitCode  = "u1";
            var treeCount = 50;

            using (var database = CreateDatabase())
            {
                var datastore = new TallyDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));
                var tpds      = new TallyPopulationDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID);
                var cuds      = new CuttingUnitDatastore(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));

                var tallyPops = database.QueryGeneric($"Select * from TallyPopulation WHERE StratumCode = '{stratumCode}';")
                                .ToArray();

                var pop = tpds.GetTallyPopulation(unitCode, stratumCode, sgCode, species, liveDead);

                pop.Should().NotBeNull();

                var tallyAction = new TallyAction(unitCode, pop)
                {
                    SampleResult = sampleResult,
                    TreeCount    = treeCount,
                };

                var entry = datastore.InsertTallyAction(tallyAction);

                entry.TallyLedgerID.Should().NotBeEmpty();

                ValidateTallyEntry(entry, sampleResult == SampleResult.M || sampleResult == SampleResult.I);

                var entryAgain = datastore.GetTallyEntry(entry.TallyLedgerID);

                ValidateTallyEntry(entryAgain, sampleResult == SampleResult.M || sampleResult == SampleResult.I);

                //var tree = database.From<Tree>().Where("TreeID = @p1").Query(entry.TreeID).FirstOrDefault();

                if (sampleResult == SampleResult.M || sampleResult == SampleResult.I)
                {
                    var tree = cuds.GetTree(entry.TreeID);

                    tree.Should().NotBeNull();

                    tree.TreeID.Should().Be(entry.TreeID);
                    tree.StratumCode.Should().Be(stratumCode);
                    tree.SampleGroupCode.Should().Be(sgCode);
                    tree.SpeciesCode.Should().Be(pop.SpeciesCode);
                    tree.LiveDead.Should().Be(pop.LiveDead);
                    tree.CountOrMeasure.Should().Be(sampleResult.ToString());
                }

                var tallyPopulate = tpds.GetTallyPopulationsByUnitCode(unitCode).Where(x => (x.SpeciesCode ?? "") == (species ?? "")).Single();

                tallyPopulate.TreeCount.Should().Be(treeCount);
            }
        }
        public void DeleteTallyEntry()
        {
            var unitCode      = "u1";
            var stratumCode   = "st3";
            var sgCode        = "sg1";
            var species       = "sp1";
            var liveDead      = "L";
            var tree_guid     = Guid.NewGuid().ToString();
            var tallyLedgerID = Guid.NewGuid().ToString();
            var treeCount     = 1;
            var cruiseID      = CruiseID;

            using (var database = CreateDatabase())
            {
                var datastore = new TallyDataservice(database, cruiseID, TestDeviceInfoService.TEST_DEVICEID, new SamplerInfoDataservice(database, CruiseID, TestDeviceInfoService.TEST_DEVICEID));
                var tpds      = new TallyPopulationDataservice(database, cruiseID, TestDeviceInfoService.TEST_DEVICEID);

                var tallyPop = tpds.GetTallyPopulation(unitCode, stratumCode, sgCode, species, liveDead);

                tallyPop.Should().NotBeNull("tallyPop");

                var tallyEntry = new TallyAction(unitCode, tallyPop)
                {
                    TreeCount = treeCount
                };

                var entry = datastore.InsertTallyAction(tallyEntry);

                datastore.DeleteTallyEntry(entry.TallyLedgerID);

                var tallyPopAgain = tpds.GetTallyPopulationsByUnitCode(unitCode)
                                    .Where(x => x.StratumCode == stratumCode &&
                                           x.SampleGroupCode == sgCode &&
                                           x.SpeciesCode == species).Single();

                tallyPopAgain.TreeCount.Should().Be(0, "TreeCount");
                tallyPopAgain.SumKPI.Should().Be(0, "SumKPI");

                database.ExecuteScalar <int>("SELECT count(*) FROM Tree WHERE TreeID = @p1", entry.TreeID).Should().Be(0, "tree should be deleted");
            }
        }