<commit_before>#include "stdafx.h"
#include "CppUnitTest.h"

#include "ThreadIds.h"
#include "ReadyEvent.h"

#include "AppFactory.h"
#include "App.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace threadily {
	namespace test {
		TEST_CLASS(AppExampleUnitTest)
		{
		public:


			/**
			* Basically tries to create everything in the system and any linkages in the system
			*/
			TEST_METHOD(ReadProductsAsync_bug_2017_03_18)
			{
				auto app = AppFactory::getInstance().create();

				// first see if the business exists
				waitForAsync(
					app->isBusinessesPending,
					[&app]() {
					app->readBusinessesAsync(0, 10, "Wit");
				});
				Assert::AreEqual(0, (int)app->businesses->size(), L"Should be 0 businesses in the system");

				// Since it doesn't exist, we should make it! First the address
				waitForAsync(
					app->isCreateBusinessPending,
					[&app]() {
					app->createBusinessAsync("Witness");
				});
				auto witness = app->createdBusiness->get();
				Assert::AreEqual(std::string("Witness"), witness->name->get());

				auto similarToThreadilyBridge = witness->products->subscribe([](std::shared_ptr<Product> newValue, size_t index, threadily::ObservableActionType action) {
				});

				// Even though we have the business let's check it's product listings anyway
				waitForAsync(
					witness->isProductsPending,
					[&witness]() {
					witness->readProductsAsync(0, 20, "");
				});
				Assert::AreEqual(0, (int)witness->products->size(), L"Should be 0 products for the business in the system");

				// create a product
				waitForAsync(
					witness->isCreateProductPending,
					[&witness]() {
					witness->createProductAsync("Name");
				});
				Assert::IsTrue(witness->createdProduct->get() != nullptr, L"Product should not be null after creating it");

				// Make sure the product exists
				waitForAsync(
					witness->isProductsPending,
					[&witness]() {
					witness->readProductsAsync(0, 20, "");
				});
				Assert::AreEqual(1, (int)witness->products->size(), L"Should be 1 products for the business in the system");
			}

			TEST_METHOD(BusinessVectorIncreasesRefCount)
			{
				auto app = AppFactory::getInstance().create();

				// Create a business
				waitForAsync(
					app->isCreateBusinessPending,
					[&app]() {
					app->createBusinessAsync("Witness");
				});
				auto witness = app->createdBusiness->get();
				Assert::AreEqual(std::string("Witness"), witness->name->get());

				auto oldUseCount = witness.use_count();

				// Now query the business populating app->businesses
				waitForAsync(
					app->isBusinessesPending,
					[&app]() {
					app->readBusinessesAsync(0, 10, "Wit");
				});
				Assert::AreEqual(1, (int)app->businesses->size(), L"Should be 1 businesses in the system");

				Assert::AreEqual(oldUseCount + 1, witness.use_count(), L"Expected the use count to increase by one as it should now be stored in the businesses listing");
			}

			void waitForAsync(std::shared_ptr<threadily::Observable<bool>> isPending, std::function<void()> asyncMethod)
			{
				threadily::ReadyEvent ready;<commit_msg>Added unit test to check ref counts of vectors<commit_after>#include "stdafx.h"
#include "CppUnitTest.h"

#include "ThreadIds.h"
#include "ReadyEvent.h"

#include "AppFactory.h"
#include "App.h"

using namespace Microsoft::VisualStudio::CppUnitTestFramework;

namespace threadily {
	namespace test {
		TEST_CLASS(AppExampleUnitTest)
		{
		public:


			/**
			* Basically tries to create everything in the system and any linkages in the system
			*/
			TEST_METHOD(ReadProductsAsync_bug_2017_03_18)
			{
				auto app = AppFactory::getInstance().create();

				// first see if the business exists
				waitForAsync(
					app->isBusinessesPending,
					[&app]() {
					app->readBusinessesAsync(0, 10, "Wit");
				});
				Assert::AreEqual(0, (int)app->businesses->size(), L"Should be 0 businesses in the system");

				// Since it doesn't exist, we should make it! First the address
				waitForAsync(
					app->isCreateBusinessPending,
					[&app]() {
					app->createBusinessAsync("Witness");
				});
				auto witness = app->createdBusiness->get();
				Assert::AreEqual(std::string("Witness"), witness->name->get());

				auto similarToThreadilyBridge = witness->products->subscribe([](std::shared_ptr<Product> newValue, size_t index, threadily::ObservableActionType action) {
				});

				// Even though we have the business let's check it's product listings anyway
				waitForAsync(
					witness->isProductsPending,
					[&witness]() {
					witness->readProductsAsync(0, 20, "");
				});
				Assert::AreEqual(0, (int)witness->products->size(), L"Should be 0 products for the business in the system");

				// create a product
				waitForAsync(
					witness->isCreateProductPending,
					[&witness]() {
					witness->createProductAsync("Name");
				});
				Assert::IsTrue(witness->createdProduct->get() != nullptr, L"Product should not be null after creating it");

				// Make sure the product exists
				waitForAsync(
					witness->isProductsPending,
					[&witness]() {
					witness->readProductsAsync(0, 20, "");
				});
				Assert::AreEqual(1, (int)witness->products->size(), L"Should be 1 products for the business in the system");
			}

			// https://github.com/k2snowman69/Threadily/issues/7
			TEST_METHOD(BusinessVectorIncreasesRefCount_issue_7)
			{
				auto app = AppFactory::getInstance().create();

				// Create a business
				waitForAsync(
					app->isCreateBusinessPending,
					[&app]() {
					app->createBusinessAsync("Witness");
				});
				auto witness = app->createdBusiness->get();
				Assert::AreEqual(std::string("Witness"), witness->name->get());

				auto oldUseCount = witness.use_count();

				// Now query the business populating app->businesses
				waitForAsync(
					app->isBusinessesPending,
					[&app]() {
					app->readBusinessesAsync(0, 10, "Wit");
				});
				Assert::AreEqual(1, (int)app->businesses->size(), L"Should be 1 businesses in the system");

				Assert::AreEqual(oldUseCount + 1, witness.use_count(), L"Expected the use count to increase by one as it should now be stored in the businesses listing");
			}

			void waitForAsync(std::shared_ptr<threadily::Observable<bool>> isPending, std::function<void()> asyncMethod)
			{
				threadily::ReadyEvent ready;
{'fraction_non_alphanumeric': 0.13984461709211987, 'fraction_numerical': 0.009988901220865706, 'mean_word_length': 3.848578016910069, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 81, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6950993', 'n_tokens_mistral': 2418, 'n_tokens_neox': 1863, 'n_words': 456}