title,body,link,tags,view_count,is_answered,score,answer_count
Instantiating and associating Ahoy::Visit for RSpec System Testing,"<h3>Env</h3>
<ul>
<li>Rails 7.0.4</li>
<li>Capybara 3.39.1</li>
<li>Rspec 3.12.2</li>
<li>Rspec Rails 6.0.2</li>
<li>AhoyMatey 4.2.1</li>
</ul>
<h3>General Context</h3>
<p>We have an app that uses <code>current_visit</code> as a stand-in user model when a user is not authenticated. We allow a user to &quot;trial&quot; the application, then request they register an account to continue after completing our wizard once. We then associate resources from <code>current_visit</code> with <code>current_user</code> using the Devise <code>after_sign_in</code> method. We also use Pundit, and have a <code>UserContext</code> object, but I don't think that's relevant for this particular question.</p>
<h3>Problem Context</h3>
<p>I am trying to write system tests and want to be able to instantiate a visit to a certain state. For example, we have two resources a user could optionally have and I'd like to be able to instantiate a visit into these states:</p>
<ul>
<li>Visitor is new user, has no associations</li>
<li>Visitor has an associated resource A, but no associated resource B</li>
<li>Visitor has both associated resources A and B</li>
</ul>
<p>Visitor could have associated resource B but not A, but this is not common, nor possible via the UI (which is the focus of this system test).</p>
<h3>Problem</h3>
<p>I have added an edit below about an additional approach using mocks.</p>
<p>I am able to instantiate a visitor in my system tests, and they are successfully created in the system test. Using the debugger, I can confirm that the visitor responds to the right methods as I expect, as I can call <code>expect(visitor.reload.resource_a.size).to eq(1)</code> and pass the spec</p>
<p>However, it seems that this visitor is not associated with the <code>current_visit</code> on the backend in the system test, as even though the instance of <code>visitor</code> has the traits I want, the frontend does not display this instantiated visit.</p>
<p>I have tried to initiate a tracker in my system test:</p>
<pre class=""lang-rb prettyprint-override""><code>let(:visitor) { create(:ahoyvisit, :with_resources) }
let(:tracker) { Ahoy::Tracker.new(visitor_token: visitor.token) }
</code></pre>
<p>But this doesn't seem to work to associate the created visitor to <code>current_visit</code> in the controller. I am generally unsure how I should properly bridge this gap in my system testing.</p>
<p>[edit]</p>
<h3>Mocks</h3>
<p>It was suggested to me to try using a mock, but that also didn't seem to work:
<code>allow_any_instance_of(Ahoy::Tracker).to receive(:visit).and_return(visitor)</code></p>
",https://stackoverflow.com/questions/76341769/instantiating-and-associating-ahoyvisit-for-rspec-system-testing,"['ruby-on-rails', 'testing', 'capybara', 'rspec-rails', 'ahoy']",40,True,1,2
Blackbox testing. Can we modify the code and is it automated or manual?,"<p>I would like to ask a question regarding <em>Blackbox testing</em></p>
<p>I cite <a href=""https://www.guru99.com/black-box-testing.html"" rel=""nofollow noreferrer"">this site</a></p>
<blockquote>
<p>Black Box Testing is a software testing method in which the
functionalities of software applications are tested without having
knowledge of internal code structure, implementation details and
internal paths. Black Box Testing mainly focuses on input and output
of software applications and it is entirely based on software
requirements and specifications. It is also known as Behavioral
Testing.</p>
</blockquote>
<p>Alright, I understand the concept. But my situation has the following characteristics:</p>
<ol>
<li>The application to test has harcoded in the code the input file it uses.</li>
<li>The output of the application are some plots and graphs</li>
</ol>
<p>I have to test this application so my questions are:</p>
<ul>
<li>It is ok for me to modify that one line of the code so as the input points to my test datafiles? (Because there is no other way to modify the input -other than creating a data file and naming it with the fake filename it uses)</li>
<li>Is blackbox testing manual or automatized? I don't see how can checking the output plots can be automatized.</li>
<li>Blackbox testing can be considere <em>system testing</em>? Does it use testing frameworks?</li>
</ul>
",https://stackoverflow.com/questions/75254468/blackbox-testing-can-we-modify-the-code-and-is-it-automated-or-manual,"['testing', 'automated-tests', 'black-box-testing']",22,False,0,0
How to remove error __init__() got an unexpected keyword argument &#39;estimator__n_neighbors&#39;,"<p>I have this task:</p>
<p>Implement knn data classification method in combination with PCA feature space dimension reduction technique and then test their effectiveness and select optimal parameters. Implementation should be done in python + jupyter notebook environment. Dedicated numerical libraries can be used. The classifiers are to use the database available at: <a href=""http://archive.ics.uci.edu/ml/datasets/Letter+Recognition"" rel=""nofollow noreferrer"">http://archive.ics.uci.edu/ml/datasets/Letter+Recognition</a>. From the database, 10 letters were selected for system testing: B, C, F, G, I, L, R, U, Z, W. The accuracy of the classifier's performance is to be verified and the result is to be estimated as a percentage for different parameter values: number k, distance function (metric), decision strategy (OvO, OvA/OvR). After determining the optimal values of the classifier's parameters, perform a dimension reduction operation of the feature space using the PCA method and estimate what is the smallest dimension of the feature space that achieves results no worse than the previously determined optimal classifier.
After testing, measure how much faster the classifier performs on the reduced feature space compared to the classifier on the full data.</p>
<p>This is the code I created:</p>
<pre><code>import pandas as pd
from sklearn.decomposition import PCA
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.multiclass import OneVsOneClassifier, OneVsRestClassifier
from sklearn.metrics import accuracy_score


url = &quot;https://archive.ics.uci.edu/ml/machine-learning-databases/letter-recognition/letter-recognition.data&quot;
data = pd.read_csv(url, header=None)

X = data.iloc[:, 1:]  
y = data.iloc[:, 0] 
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

knn = KNeighborsClassifier()
params = {
    &quot;estimator__n_neighbors&quot;: [3, 5, 7, 9], 
    &quot;estimator__metric&quot;: [&quot;euclidean&quot;, &quot;manhattan&quot;, &quot;minkowski&quot;], 
}

ovo = OneVsOneClassifier(knn)
grid_search = GridSearchCV(ovo, params, cv=5)
grid_search.fit(X_train_scaled, y_train)
print(f&quot;Best (OvO): {grid_search.best_params_}&quot;)
y_pred = grid_search.predict(X_test_scaled)
acc = accuracy_score(y_test, y_pred)
print(f&quot;accuracy_score(OvO): {acc:.2f}&quot;)

knn = KNeighborsClassifier()
params = {
    &quot;estimator__n_neighbors&quot;: [3, 5, 7, 9],
    &quot;estimator__metric&quot;: [&quot;euclidean&quot;, &quot;manhattan&quot;, &quot;minkowski&quot;],  
}
ovr = OneVsRestClassifier(knn)
grid_search_ovr = GridSearchCV(ovr, params, cv=5)
grid_search_ovr.fit(X_train_scaled, y_train)
print(f&quot;Best (OvR): {grid_search_ovr.best_params_}&quot;)
y_pred = grid_search_ovr.predict(X_test_scaled)
acc = accuracy_score(y_test, y_pred)
print(f&quot;accuracy_score(OvR): {acc:.2f}&quot;)

#PCA

best_params_ovo = grid_search_ovo.best_params_
best_params_ovr = grid_search_ovr.best_params_

knn_clf_ovo = KNeighborsClassifier(**best_params_ovo)
knn_clf_ovr = KNeighborsClassifier(**best_params_ovr)

accuracies = np.zeros((2, len(X_train[0])))


for n_components in range(1, len(X_train[0])+1):

    pca = PCA(n_components=n_components)

    X_train_reduced = pca.fit_transform(X_train)
    X_test_reduced = pca.transform(X_test)

    knn_clf_ovo.fit(X_train_reduced, y_train)
    accuracy_ovo = knn_clf_ovo.score(X_test_reduced, y_test)

    accuracies[0, n_components-1] = accuracy_ovo

    knn_clf_ovr.fit(X_train_reduced, y_train)
    accuracy_ovr = knn_clf_ovr.score(X_test_reduced, y_test)

    accuracies[1, n_components-1] = accuracy_ovr
    
    print(f&quot;Accuracy of KNN classifier for OvO strategy: {accuracy_ovo:.2f}&quot;)
    print(f&quot;Accuracy of KNN classifier for OvR strategy: {accuracy_ovr:.2f}&quot;)


for i in range(n_components):
  if accuracies[0, i] &gt;= best_accuracy_ovo and accuracies[1, i] &gt;= best_accuracy_ovr:
    min_dim = i+1
    break


print(f&quot;The smallest dimension to achieve results no worse than the optimal classifier:  {min_dim}&quot;)

#time:
X_train_full = X_train.values
X_test_full = X_test.values

%timeit knn_clf_ovo.predict(X_test_full)
%timeit knn_clf_ovo.predict(X_test_reduced)
%timeit knn_clf_ovr.predict(X_test_full)
%timeit knn_clf_ovr.predict(X_test_reduced)

</code></pre>
<p>In line: best_params_ovo = grid_search_ovo.best_params_ i have an error:
<strong>init</strong>() got an unexpected keyword argument 'estimator__n_neighbors'</p>
<p>In the code, I use the ovo object as an argument to GridSearchCV, and ovo is an instance of OneVsOneClassifier that has an estimator property. This means that in order to set the n_neighbors parameters for KNeighborsClassifier within OneVsOneClassifier, I need to use the estimator__n_neighbors notation. But later in this error requires me to use n_neighbors itself.</p>
<p>Is it possible to correct this in my code somehow? Is it at all well done and consistent with the content of the task, or am I doing it in all the wrong ways?</p>
",https://stackoverflow.com/questions/75033470/how-to-remove-error-init-got-an-unexpected-keyword-argument-estimator-n,"['python', 'machine-learning', 'scikit-learn', 'artificial-intelligence', 'knn']",200,False,0,0
Instantiating and associating Ahoy::Visit for RSpec System Testing,"<h3>Env</h3>
<ul>
<li>Rails 7.0.4</li>
<li>Capybara 3.39.1</li>
<li>Rspec 3.12.2</li>
<li>Rspec Rails 6.0.2</li>
<li>AhoyMatey 4.2.1</li>
</ul>
<h3>General Context</h3>
<p>We have an app that uses <code>current_visit</code> as a stand-in user model when a user is not authenticated. We allow a user to &quot;trial&quot; the application, then request they register an account to continue after completing our wizard once. We then associate resources from <code>current_visit</code> with <code>current_user</code> using the Devise <code>after_sign_in</code> method. We also use Pundit, and have a <code>UserContext</code> object, but I don't think that's relevant for this particular question.</p>
<h3>Problem Context</h3>
<p>I am trying to write system tests and want to be able to instantiate a visit to a certain state. For example, we have two resources a user could optionally have and I'd like to be able to instantiate a visit into these states:</p>
<ul>
<li>Visitor is new user, has no associations</li>
<li>Visitor has an associated resource A, but no associated resource B</li>
<li>Visitor has both associated resources A and B</li>
</ul>
<p>Visitor could have associated resource B but not A, but this is not common, nor possible via the UI (which is the focus of this system test).</p>
<h3>Problem</h3>
<p>I have added an edit below about an additional approach using mocks.</p>
<p>I am able to instantiate a visitor in my system tests, and they are successfully created in the system test. Using the debugger, I can confirm that the visitor responds to the right methods as I expect, as I can call <code>expect(visitor.reload.resource_a.size).to eq(1)</code> and pass the spec</p>
<p>However, it seems that this visitor is not associated with the <code>current_visit</code> on the backend in the system test, as even though the instance of <code>visitor</code> has the traits I want, the frontend does not display this instantiated visit.</p>
<p>I have tried to initiate a tracker in my system test:</p>
<pre class=""lang-rb prettyprint-override""><code>let(:visitor) { create(:ahoyvisit, :with_resources) }
let(:tracker) { Ahoy::Tracker.new(visitor_token: visitor.token) }
</code></pre>
<p>But this doesn't seem to work to associate the created visitor to <code>current_visit</code> in the controller. I am generally unsure how I should properly bridge this gap in my system testing.</p>
<p>[edit]</p>
<h3>Mocks</h3>
<p>It was suggested to me to try using a mock, but that also didn't seem to work:
<code>allow_any_instance_of(Ahoy::Tracker).to receive(:visit).and_return(visitor)</code></p>
",https://stackoverflow.com/questions/76341769/instantiating-and-associating-ahoyvisit-for-rspec-system-testing,"['ruby-on-rails', 'testing', 'capybara', 'rspec-rails', 'ahoy']",40,True,1,2
