Showing posts with label smoke-test. Show all posts
Showing posts with label smoke-test. Show all posts

Friday, November 21, 2014

No Idea How That Ever Worked


Again, things are broken in Patterns in Polymer. I would like to get it mostly in order by the end of the month, so having the latest Polymer expose yet more problems is less than ideal. Hopefully these are the easy kinds of problems (they never are).

Actually, there is some hope this time around. All of the book's tests are passing. Even though the tests could be better in places, that still counts for something. The actual problems occur in a handful of the JavaScript smoke tests. In addition to unit and/or acceptance tests, the code for each chapter includes an index.html page that I normally access with the aid of Python's simple HTTP server. Only they stopped working for some reason.

I am unsure if there is a common thread, but some of the pages are just blank. Looking through the code, I see that I am definitely using the correct life-cycle methods:
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="normal-changes">
  <template>
  </template>
  <script>
    Polymer('normal-changes', {
      // ...
      attached: function() {
        console.log('yo')
        // ...
    };
  </script>
</polymer-element>
Console debugging verifies that the Polymer element is being created properly. The page is just blank for some reason...

D'oh!

It is blank because the smoke test page contains nothing but a Polymer element wrapping a content-editable field:
  <body>
    <div class=container>
      <normal-changes>
        <div contenteditable>
          <!-- ... -->
        </div>
      </normal-changes>
    </div>
  </body>
Amazingly that worked at one point. The problem is not the page's HTML, but rather the Polymer element's <template>, which lacks a <content> element into which the page can project the wrapped content. The fix is to simply add the tag:
<link rel="import" href="../bower_components/polymer/polymer.html">
<polymer-element name="normal-changes">
  <template>
    <content></content>
  </template>
  <script>
    Polymer('normal-changes', {
      // ...
      attached: function() {
        console.log('yo')
        // ...
    };
  </script>
</polymer-element>
A reload now includes the Polymer wrapped content:



I have no idea how that ever worked, but Polymer must have been OK with it at one point.

Most of the other changes are along the same lines—silly things that I just missed or I cannot figure out how they ever worked. That said, the plain-old forms chapter was just broken. I definitely need to revisit the test code on that tomorrow. For now, I get it working through a combination of reflecting changes in the attribute and better mutation observing in the main page.

I am publishing the current pizza state in the state attribute. For changes to be reflected, I need to explicitly mark it as such:
Polymer('x-pizza', {
  // ...
  publish: {
    state: {
      value: '',
      reflect: true
    }
  },
  // ...
});
The chapter still recommends watching that value so that changes can be synchronized in the containing form. Mutation observing seems to have gotten a little easier thanks to whenPolymerReady():
Polymer.whenPolymerReady(function(){
  var input = document.querySelector('#pizza_value');
  var observer = new MutationObserver(function(mutations) {
    mutations.forEach(function(mutation) {
      if (mutation.attributeName == 'state') {
        input.value = mutation.target[mutation.attributeName];
      }
    });
  });
  observer.observe(document.querySelector('x-pizza'), {attributes: true});
});
The previous version of that code required three separate functions and some convoluted wait-for-polymer logic. This seems much nicer (and closer to the Dart solution).

At any rate, I have all of the smoke tests working. I will revisit the tests for the Forms chapter tomorrow.


Day #1

Friday, October 16, 2009

Smoke 'em If You Got 'em

‹prev | My Chain | next›

I did a quick push to the beta site this morning so that I could do some smoke testing. For the most part, everything looked good. Sadly, I did discover another oversight in my switch of the recipe URL delimiter from dashes to slashes. The recipe search results still had the dashes in them.

I am somewhat disappointed that my Cucumber scenarios did not catch this error. It would have been a simple matter of one of the 10 recipe search scenarios following a link from the search results to the actual recipe. A simple matter indeed, but I did not include such a step.

This is, of course, the point of smoke tests. Even if you have awesome tests, even if you do not make big changes the day before deploying, it is always a good idea to poke around. There may be a bit of smoke, there may be a raging inferno, but you won't know unless you exercise the code.

After fixing the link in the search results (a minor code change, but involving many spec changes), I am ready to deploy. But first...

I am not certain that I have identified the last of the dash/slash issues. Rather than delay the promotion to production, I do a bit of defensive Rack configuration with Rack::Rewrite. Specifically, a recipe URL that contains dashes should be redirected to the proper slash URL:
use Rack::Rewrite do
r301 %r{(.+)\.html}, '$1'
r301 %r{^/recipes/(\d{4})-(\d{2})-(\d{2})-(.+)}, '/recipes/$1/$2/$3/$4'
end
The curl command verifies that this works:
jaynestown% curl -I http://localhost:3000/recipes/2008-07-13-sausage   
HTTP/1.1 301 Moved Permanently
Location: /recipes/2008/07/13/sausage
Content-Length: 14
Connection: keep-alive
Server: thin 1.2.2 codename I Find Your Lack of Sauce Disturbing
With all of my RSpec tests passing, all of my Cucumber scenarios passing, and a bit of defensive Rack middleware, I am ready to deploy my Sinatra / CouchDB application:
jaynestown% rake vlad:stop_app
jaynestown% rake vlad:update vlad:migrate vlad:start_app
Finally, I update DNS so that http://legacy.eeecooks.com points to the legacy Rails site and http://www.eeecooks.com points to the new Sinatra / CouchDB site.

Thus endeth the chain.

Saturday, April 11, 2009

Some Recipe Detail Clean-Up

‹prev | My Chain | next›

At this point, I have the entire Recipe Details feature (5 scenarios and 20 steps) passing:
cstrom@jaynestown:~/repos/eee-code$ cucumber features/recipe_details.feature -n 
Feature: Recipe Details

So that I can accurately reproduce a recipe at home
As a web user
I want to be able to easily recognize important details
Scenario: Viewing a recipe with several ingredients
Given a recipe for Buttermilk Chocolate Chip Pancakes
When I view the recipe
Then I should see an ingredient of "1 cup all-purpose, unbleached flour"
And I should see an ingredient of "¼ teaspoons salt"
And I should see an ingredient of "chocolate chips (Nestle Tollhouse)"

Scenario: Viewing a recipe with non-active prep time
Given a recipe for Crockpot Lentil Andouille Soup
When I view the recipe
Then I should see 15 minutes of prep time
And I should see that it requires 5 hours of non-active cook time

Scenario: Viewing a list of tools used to prepare the recipe
Given a recipe for Chicken Noodle Soup
When I view the recipe
Then I should see that it requires a Bowl, a Colander, a Cutting Board, a Pot and a Skimmer to prepare

Scenario: Main site categories
Given a recipe for Mango and Tomato Salad
When I view the recipe
Then I should see the site-wide categories of Italian, Asian, Latin, Breakfast, Chicken, Fish, Meat, Salad, and Vegetarian
And the Salad and Vegetarian categories should be active

Scenario: Viewing summary and recipe instructions
Given a recipe for Curried Shrimp
When I view the recipe
Then I should a nice summary of the dish
And I should see detailed, easy-to-read instructions


5 scenarios
20 steps passed
Inside the application, I have 30 examples passing:
cstrom@jaynestown:~/repos/eee-code$ rake
(in /home/cstrom/repos/eee-code)
/usr/bin/ruby1.8 ./spec/eee_spec.rb
...

Finished in 0.073494 seconds

3 examples, 0 failures
/usr/bin/ruby1.8 ./spec/eee_helpers_spec.rb
......

Finished in 0.014949 seconds

6 examples, 0 failures
/usr/bin/ruby1.8 ./spec/views/recipe.haml_spec.rb
.......................

Finished in 0.191058 seconds

23 examples, 0 failures
(commit of the Rakefile).

With all that work done, I have yet to actually look at a recipe in a browser. Now is as good a time as any:



On first inspection, everything looks good, but I realize that I have not linked in the recipe images yet. Scrolling down, I see:



Oops, the parenthetical ingredient brands are showing even when there is no brand. I thought I had accounted for that case, but obviously not. No matter, it should not be difficult to fix. But first, the recipe photo...

Including a Recipe Photo

In the HAML spec, I am mostly interested in the location of the image in the document. The examples that describe this:
  context "a recipe with an image" do
it "should include an image in the recipe summary" do
self.stub!(:image_link).and_return("<img/>")
render("views/recipe.haml")
response.should have_selector("#eee-summary > img")
end

end

context "a recipe without an image" do
it "should not include an image" do
self.stub!(:image_link).and_return(nil)
render("views/recipe.haml")
response.should_not have_selector("#eee-summary > img")
end
end
I am stubbing out a helper method, so the actual implementation is trivial:
#eee-summary
= image_link @recipe
= wiki @recipe['summary']
When I started my red-green-refactor cycle, the responsibility for the image tag was in the HAML template. It got ugly as I started handling boundary conditions, which is how the image_link helper came into being. The URL for images was established towards the end of the recipe details feature. The examples:
describe "image_link" do
it "should return a link tag pointing to the document's image" do
doc = {
'_id' => "foo",
'_attachments' => { 'sample.jpg' => { } }
}

image_link(doc).
should have_selector("img",
:src => "/images/#{doc['_id']}/sample.jpg")
end

it "should return nil if no attachments" do
image_link({ }).should be_nil
end
it "should return nil if no image attachments" do
doc = { '_attachments' => { 'sample.txt' => { } } }
image_link(doc).should be_nil
end
end
And the code that actually implements it:
    def image_link(doc)
return nil unless doc['_attachments']

filename = doc['_attachments'].
keys.
detect{ |f| f =~ /jpg/ }

return nil unless filename

%Q|<img src="/images/#{doc['_id']}/#{filename}"/>|
end
Since I do not have a cucumber scenario, I will just check the output in the browser:

(commit)

Boundary Condition: No Brand

Investigating this, I find that I accounted for the case of when brands are nil, but not empty strings. The empty strings are an artifact of the import from the legacy Rails application. I hate to add code to account for an artifact, but this is fairly trivial. If this is the only workaround, I can live with it. A TODO can serve as a reminder in case more artifacts are found, at which point, a CouchDB migration can be used.

This would be trivial to address with Rails' blank?, but, even without it, it is not too difficult. First, the example (including the TODO):
  # TODO: this should not be necessary.  The blank brands are an
# artifact of the import-from-rails process
context "a recipe with a blank brand" do
it "should not include brand information" do
@recipe['preparations'] = []
@recipe['preparations'] << {
'quantity' => 1,
'unit' => '12 ounce bag',
'brand' => '',
'ingredient' => {
'name' => 'chocolate chips'
}
}

render("views/recipe.haml")

response.should_not have_selector(".ingredient > .brand")
end
end
To implement this example, a Regexp check for any non-space character will work:
        - if preparation['brand'] =~ /\S/
%span.brand
= "(" + preparation['brand'] + ")"
To prevent the nil brand case from breaking, this code relies on the fact that the NilClass does support Regexp matching. Specifically:
>> nil =~ /\S/
=> false
A quick check in the browser confirms that the issue has been resolved—no more blank brands:

(commit)

That should wrap up work on the recipe details. Next up, search, I think.