Course Week02 Day04&05

  • array.concat(array) is the same thing as adding two arrays together
  • moves to test jumping over in checkers
    • 23&32&56&45&25&34&45&23&12&34&65&56&32&41…
  • Rake
    • RSpec::Core::RakeTask.new(:spec) do |t|
      • symbol will be the name of the task, RSpec provides a configuration object t
      • can then specify pattern of files it should run (i.e. location of the files)
      • and you will also be able to specify the default task when you call rake without any args
  • RSpec
    • mock - double("obj_name") – blank state, a stub with an expectation that the method gets called
      • should_receive(:meth_to_be_run).with(specific_parameter)
        • can also add a and_return() 
        • should_receive(:bleh).exactly(3).times
    • stubcanned answers made during a test, and can record information about calls, returns a specified result. should be within a spec or a let statement
      • dog.stub(:meth_name).and_return(true)
      • if  is a stub, and you call x.as_null_object on it, from there on, no matter what method(s) are called on x, it will return self (Mock object)
        • use this feature in cases where object interaction is incidental
    • unit tests – mocks (behavior verification), specific and narrow
    • integration tests – real objects, large scope
    • let does not persist between different specs
    • stubbing:
      • name = stub(:name_given_to_stub, one: 1, two: 2)
        • will allow name.one to return 1 and so on
        • naming the sub is optional
      • let(:name) { double(“for_self_reference”) }
      • cat.encounter_dog(obj)
    • DRY tips
      • before's brother: after(:each)
      • shared examples:
        • can go at the top of the spec file: shared_examples_for 'ex_name' { code here }, call it later with:  describe Thingy { it_behaves_like 'something' }
        • or can go into the spec/support/ directory and create a shared_examples_for 'something' { code here on subject }
          • alternatively, the shared example can take an argument, which you can pipe as the subject in the spec/support/ and then call with an extra argument in the spec
      • can add tags to contexts, and specify tags when running the specs
        • can add a slow: true tag, and filter those out when running most of the time:
          • rspec --tag ~slow spec/lib/zombie_spec.rb
          • or change the spec/spec_helper.rb config file
      • have a context for each function inside of a class
  • Ruby Tests shortcuts again:
    • Command-Shift-R: run a single test
      • on either an it block or a describe block
    • Command-Shift-E: run the last test(s)
    • Command-Shift-T: run all the tests in current file
  • Postgres

SQL

  • foreign keys
    • storing links between elements when they are not attributes of a single thing (relating posts to the users that created them)
    • the foreign key in one table will reference the primary key in another table
    • usually name the column of the foreign key after the name of the other table plus an _id suffix
  • anatomomy of query execution:
    • database connection » query optimizer (chooses efficient execution plan) » result set (temporary table)
      • db connection is held until request app releases it, or server shuts down
  • SELECT DISTINCT this_column, 'new_value' nonreal_column FROM this_table
    • asking for a nonexistent column will show a nameless one filled with the nonexistent name you specified, alternatively you can precede it will a value to fill that column name
    • if you are not retrieving any data from tables, and are just executing built in functions, you don’t even need a FROM
    • DISTINCT removes duplicates (ALL is the automatic default), can be very slow for large datasets since data needs to be sorted
    • The tables that SELECT come in different flavors (permanent, temporary, virtual – from CREATE VIEW view_name AS)
      • FROM doesn’t have to take a permanent table
      • could take a table returned from a subquery
        • put the subquery in parentheses, and follow it with an alias after the parentheses
          • this could be a single letter e, that the main query then references like: e.column_name
      • could take a view/virtual table
        • a query stored in the data dictionary, which acts like a table but holds no data itself, just references
        • can be used to hide columns
    • can also handle multiple tables, in which case the link/joining technique needs to be specified
    • WHERE is for filtering rows in raw data, use AND OR NOT and parentheses
      • takes basic operators plus: <>, LIKE
        • column_name IN ('desired_row_value_1', 'desired_row_value_2')
          • also works with NOT
        • column_name IN ( subquery )
        • BETWEEN lower_inclusive AND upper_inclusive
        • LEFT(column_name, 1) = 'T' to find all the rows starting with the letter T
        • LIKE with wildcards: for one character, for zero or more characters
        • use for regex matching
        • IS NULL for nonexistent or empty values
    • GROUP BY will group data by column values, and in that case use HAVING to sort the data
    • ORDER BY can take the
      • DESC parameter (ASC is the default)
      • LIMIT with a number of rows
      • RIGHT(column_name, 3) to extract the last three characters of some column
      • can reference the columns already specified in the SELECT query: ORDER BY 2,  5 to sort by the 2nd and 5th columns specified earlier
  • Joining
    • select * from table_one o INNER JOIN table_two t USING (column_in_common)
      • same as: select * from table_one o INNER JOIN table_two t ON o.column_in_common = t.column_in_common
    • useful to join a table to itself when there is a self referenciung foreign key (it includes a column that points to the primary key within it)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s